User:LepreKhaun Resident/Json Database 2 Chat

From Second Life Wiki
Jump to navigation Jump to search

[NOTE: Pages within my Name Space are a WIP and constantly changing. As my understanding of the problems I attempt to address and the grasp of the subject matter itself deepens, I regularly review what I have written and update the content as better algorithms occur to me.

However, for this process of refinement, improvement and tweaking to result in something that might (hopefully!) benefit the community at large, I ask that comments, suggested improvements, corrections of fact or your own personal style preferences be made ONLY on the Discussion Pages within my Name Space. Thank you!]

You may find at some point that you've written an application with a Json text database within it that is 10k or so in size and now you need to update the code, perhaps to add some new record fields you realize you require. Ohoh!

Let's hope you have the following code in place to retrieve the database, so you can update and debug your code and then reset everything properly. Otherwise, you'll have lost your entire database. Ouch!

Can also (now! lol) be used just to obtain a back up of your database at times.

// Where myDataBase is a global Json text string
// function jsonDb2Chat ();
// Version 1.5 (8/29/2013 - added a necessary conversion of DB back to original!)
// Version 1.0
// By LepreKhaun 8/28/2013

jsonDb2Chat () {
	integer count = 0;
	// Needed to keep internal quotes and escape characters intact
	// Be *certain* you have the available memory needed for this inflation!
	myDataBase = llEscapeURL(myDataBase);
	// Keeping in mind chat can only output 1024 single-byte characters at a time:
	integer totalChunks = (integer)(llStringLength (myDataBase)/1022);
	llOwnerSay ("Chatting your database.\n\n");
	while (count < totalChunks) {
		llOwnerSay ("\n" + llGetSubString (myDataBase, count*1022, ((++count)*1022)-1));
	llOwnerSay ("\n" +  llGetSubString(myDataBase, count*1022, -1));
	// convert DB back so this can be used to simply retrieve a back up at times
	myDataBase = llUnescapeURL(myDataBase); 


After calling this function (possibly as a response to a button within your admin menu- "ChatDB", or a chatted command that's listened for on some obscure channel that is masked for the owner's key), the object will chat out the database for you.

All that remains now is to copy&paste this output to a text document, remove the inserted new lines and '[time stamp] Object:' bits, and then put double quotes around it all. Finally, have the following initialization line in your rewritten code's default state_entry() event for your global 'string myDataBase;':

myDataBase = llUnescapeURL('your cleaned and double quote enclosed chat output');

NOTE: When using Json text for a database, you should check free memory after each insertion of new data into it. Only by doing so will you be able to realize that things may be getting a wee bit too tight for comfort within your program.

At that point, you need to consider a way to either shorten the length of your database, possible by going with (shudder) one character "Key"s (making a note in comments of what Key "Z" stands for) deleting older or no longer necessary records and/or transferring the entire database into a stand alone script with minimal functionality and having a link message protocol worked out for retrieval, insertion, deletion and updating the records.

== More Json Tips, Tricks and Coding Examples ==