Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Looking to do a simple List based database

yevka Laws
Commander Eva "Yevka" Law
Join date: 19 Jul 2006
Posts: 32
03-03-2009 02:59
Flat files nothing fancy.

Just want to store some information in lists.

I'm told I can do this by writting data out to a script file.

A script creating a script.

I could use a template or a startup for this process.

I looked at this link:
http://wiki.secondlife.com/wiki/List

I have a hard time with a blank page.

I'd be very appreciative if I saw a few examples including the following functions:
1) Script writes list data to another script and then renames it.
2) Read list into memoery.

I know how to build in a listener to handle read and write requests.

Thanks.
ElQ Homewood
Sleeps Professionally
Join date: 25 Apr 2007
Posts: 280
03-03-2009 04:20
You might try out Very's system if it's going to build to any size I'd imagine. If nothing else you'll want to look at it to see how he did it. /54/4a/290413/1.html
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
03-03-2009 05:22
From: yevka Laws

A script creating a script.

That can not be done, it can copy, drop, stop, start and delete another script in the same prim's inventory, that is all.
It might make a source code, but not a compiled and/or running script.
For your other questions read the wikis and the libraries:)
_____________________
From Studio Dora
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
03-03-2009 06:37
Well first you have to decide what you are planning to store, what format the data is and how you plan to retrieve it.
Even a flat file database can become quite complex, depending on how and how often you need to access it.
Also the different types of variables in LSL, have different overheads in terms of memory used per value stored, and whilst the additional memory available to MONO compiled scripts is sufficient to offset this in small applications, if you are wanting to optimise your memory use and therefor storage capacity, you have to carefully consider the best method for the problem at hand.
see:
The second issue if where do you wish to store the data? the simple solution is to store the data in the same script that is useing the data, however if you want to store the data in one or more scripts other than the script reading from or writing to the database complexity again increases. If the Storage scripts are in the same object then you will have to communicate with them using LinkMessages, as an object cannot hear itself in chat see:

Lets look at some simple implementations using lists to see if it points you in the right direction.

Probably the simplest for of data storage is the key:value pair method, and can be implemented in LSL in at least 2 forms, namely parallel lists and strided lists. lets say you want to store a list of user names and keys. in the first method you would create 2 lists:

list Key;
list Value;

to add data we would use

Key += [llDetectedName(0)];
Value += [llDetectedKey(0)];

to retrieve a key we could say:

string name = "yevka Laws"
integer Index = llListFindList(Key, Name);
key result = llList2Key(Value, Index);

but that assumes we have a key for that person, what if we don't?

the easiest way is to test the Index as lists in LSL are indexed from 0, if no match is found the index will be -1.
so we can modify the above to read:

string name = "yevka Laws"
integer Index = llListFindList(Key, Name);
if(Index >= 0)
{
key result = llList2Key(Value, Index);
}
else
{
key result = NULL_KEY;
}

second method is a strided list

List Data;

Data += [llDetectedName(0), llDetectedKey(0)];

this time to retrieve we say:

string name = "yevka Laws"
integer Index = llListFindList(Data, Name);
if(Index >= 0)
{
key result = llList2Key(Data, Index + 1);
}
else
{
key result = NULL_KEY;
}

That should give you something to think about and get you started in the right direction.
If I get a chance later, I will try and show you some basic functions that I use to manipulate lists as data.
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
03-03-2009 08:36
Let's have a look building a script that can act as a simple, but flexable, database.:
CODE

//Very Keynes - 2006 - 2009
//_______________________________________________________________
//
// very's Very Simple Database
//_______________________________________________________________
//
integer Fields; // number of Fields in the database
integer Records; // Number of Records in the Database
integer Index; // Current record Number
list Database; // The actual Data
//
// I prefer to create scripts as a set of user defined functions
// and in this case it will be easer to show what is happening too.
//
// First thing we need is a function to create the database, and
// to keep it simple we can assume only one database per script.
// For a multi table database see:
// VK-DBMS-VM .
//
Create(list Headers)
{
Database = Headers;
Fields = llGetListLength(Headers);
Records = 0;
Index = 0;
}
//
//now lets add some data:
//
Insert(list Data)
{
// for the sake of database integrity we need to ensure that sufficient data has been passed
// i.e we have a value for each Field, and nothing more
if(llGetListLength(Data) != Fields)
{
llOwnerSay("Error, Insert: Number of Fields do not match");
}
else
{
Database += Data;
Records += 1;
}
}
//
// To return a record we need the Record Number and we return up to the length of the rcord
//
list Return(integer Record)
{
return llList2List(Database, Record * Fields, (Record * Fields) + Fields - 1);
}
//
// To replace a record we need 2 values the record number and the data
//
Replace(integer Record, list Data)
{
// for the sake of database integrity we need to ensure that sufficient data has been passed
// and that the Record is within the bounds of the Database
if(Record > 0 && Record <= Records)
{
if(llGetListLength(Data) != Fields)
{
llOwnerSay("Error, Replace: Number of Fields do not match");
}
else
{
Database = llListReplaceList(Database, Data, Record * Fields, (Record * Fields) + Fields - 1);
}
}
else llOwnerSay("Error, Replace: Index out of range");
}
//
// we also need a way to remove old records
//
Delete(integer Record)
{
if(Record > 0 && Record <= Records)
{
Database = llDeleteSubList(Database, Record * Fields, (Record * Fields) + Fields - 1);
Records -= 1;
}
else llOwnerSay("Error, Replace: Index out of range");
}
//
// Posably the most important function is the ability to find our data
// Take a look at the dbTest function in my VK-DBMS code for an advanced
// Search function, for this example we will return any record that has
// val in any field, just to keep it simple
//
integer Find(string val)
{
integer x = llGetListLength(Database);
while( x-- > Fields )
{
if(llList2String(Database, x) == val)
{
return (integer)(x / Fields);
}
}
return -1;
}
//
// Finaly a utility function to dump our database to the owners chat History
//
List()
{
for(Index = 1 ; Index <= Records ; Index++)
{
llOwnerSay(llList2CSV(Return(Index)));
}
llOwnerSay("___________________________\n");
}

default
{
// lets run a simple test to try the above functions
touch_start(integer total_number)
{
// Create the database
Create(["Col1", "Col2", "Col3"]);
// Addsome test data
Insert(["A", 1, "Test 1"]);
Insert(["b", 2, "Test 2"]);
Insert(["C", 3, "Test 3"]);
Insert(["C", 3, "Test 3"]);
Insert(["D", 4, "Test 4"]);
Insert(["E", 5, "Test 5"]);
// List the contents
List();
// Find the incorrect record
Index = Find("b");
// and Correct it
if(Index != -1)
{
list Temp = Return(Index);
Replace(Index, ["B"] + llList2List(Temp, 1, -1));
}
// List it
List();
// Find the duplicate record
Index = Find("C");
// and delete it
if(Index != -1)
{
Delete(Index);
}
// list the result
List();
}
}
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
03-03-2009 09:59
Hmm. It might actually be interesting to design a system with multiple scripts per database. For example, a script per table.
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
03-03-2009 10:20
Now I'm imagining sending SQL requests in link messages to use multiple scripts in parallel to perform the queries.
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
British Knight
Registered User
Join date: 15 Apr 2006
Posts: 4
03-03-2009 11:42
Hewee, Argent, I have done them both. Basically one instance of VK_DBMS is running as a system table and tracks which table is contained by which script. The open request sets that table and any continuation scripts to a running state. A subset of the VK-Dbms called a DASD_Slice (published over a year ago but I cant remember the post) can be configured to hold one or more columns of data and a script called SQL-Exec receives the SQL command in linkmessage form, and then runs the select statement, with each component script of the data table working in parallel. and passing the results back to a temporary system table to form the final dataset. The response is then a link to that system table which the calling script can use other SQL or VK-Dbms calls to manipulate. Needles to say it is a very incestues system and the initial system table is hard coded and the rest fully dynamic in terms of spawning and deleting scripts etc. (what I would give for a working llGetFreMemory).
The Published version of VK-DBMS is effectively an embedded version of the SQL-Slice combined with the SQL-Exec, with some of the more memory intensive SQL Functions, most notably Select, omitted.
I have been threatening to Publish VK-SQL as a follow up to VK-DBMS, as they compliment each other. But I doubt I will ever Publish the VK-SQL-Server code (described above) as it underpins the club/venue management system I have been developing and so don't want to give away my possible competitive edge :)
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
03-03-2009 11:44
:eek:

And what happens to Script Time in the sim when you do a JOIN or CREATE VIEW?
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
03-03-2009 12:08
It is not actually too bad, though I have not fully implemented a join or union, the SQL is a very small subset and hampered by being interpretive, but by turning off idle scripts and only having 4 or 5 running at a time it is generally less resource hungry than my scanner and some AOs. I did once calculate that it could hold several Gigs of data, but I would imagine the sim itself would crash way before getting there :) and with the unreliability issues inherent to SL anyone storing more than a few Megs is asking for trouble.
The only really complex join/union, that I use in my beta test system, is when it compiles the hourly report for email, where it combines data from the greater, tracker/scanner, tip jar system, calendar/group admin and vendors, effectivly massaging 5 tables to produce the output data set and I still receive the report on the hour in email even with 60+ people present for a gig.

P.s. the above post was me, I didn't realise that BK had logged in to my Computer :)