persistence pointers

1 view
Skip to first unread message

behram farrokh thomas mistree

unread,
Aug 24, 2011, 9:13:18 PM8/24/11
to emerso...@googlegroups.com
There were a couple questions about persistence. I'll write a tutorial
as well, but thought I'd send out a quick email for those curious.

You'll need to import/require the file
std/shim/restore/simpleStorage.em . What you're probably going to use
most frequently are the setField, readField, and eraseField methods.

setField takes in a (string) name you provide named fieldName, an object
you provide to be persisted fieldVal, and a callback to execute after
storage. The callback takes a single bool argument, which is false if
the call to store failed and true if it succeeded.

To read an object back from storage, use readField. readField takes in
a string name, a callback function, and a default value. The name
should be the same string that you saved the object with using setField.
The callback takes a single argument. This argument is either: 1) the
object that was read back in from storage, or 2) if the read failed
(object did not exist, etc.), the default value that you provided as the
third argument.

eraseField should make sense on its own.


One other thing that you should probably know: setScript. The setScript
command states, when you reboot, run this first. It takes in a single
non-closure-capturing function.

Let's take an example of a bank that wants to save some object stored in
mAccountInfo and restart itself whenever an oh crashes:

bank.em
----
system.require('std/shim/restore/simpleStorage.em');

var ACCT_INFO_FIELDNAME = 'acctInfo';
//tries to read mAccountInfo back in from storage
std.simpleStorage.readField(ACCT_INFO_FIELDNAME, readAcctInfoCB, {});
//tells the system to execute toExecOnReload whenever oh crashes and
//booted back up.
std.simpleStorage.setScript(toExecOnReload,false);


function readAcctInfoCB(readAcctInfo)
{
mAccountInfo = readAcctInfo;
doRegularBankStuff();
}

function toExecOnReload()
{
system.import('bank.em');
}


function doRegularBankStuff()
{
//do all the rest of your bank code in here.
//probably have a timer that every 20 minutes, saves mAccountInfo

}

-----------
To test if persistence works, use the following process with the
avatar.db scene attached.

0) rm storage.db
1) Start a world regularly (./cppoh)
2) Move presences around in it for a bit
3) Wait at least a minute
4) Kill/quit the world
5) Copy your storage.db file to be named restore.db
6) Run
./cppoh --object-factory=sqlite --object-factory-opts=--db=restore.db
--oh.storage.opts=--db=restore.db
--oh.persistentset.opts=--db=restore.db

to reload all the information about persisted objects.

7) Run a separate version of cppoh that just contains your avatar:
./cppoh --object-factory-opts=--db=avatarScene.db

Hopefully, everything that you saved will still be there. If you have
any questions, email me back or come by.

-Behram

avatarScene.db
Reply all
Reply to author
Forward
0 new messages