JSON Files

(BDB Replacement)

JSON files replace the much despised BDB database mechanism for storing metadata in EMAN2. These files offer a number of advantages over BDB, but there are also a few tradeoffs.

Advantages:

Tradeoffs:

Basic usage

The basic methods for accessing JSON files (which should be fairly familiar to those with BDB experience):

The main object is the JSDict class. An instance of this class represents a single file on disk with a '.js' extension.

The JSDict class acts much like a standard python dictionary, once opened. Default behavior is to sync with the file on disk (only if necessary) on each read or write, giving it a high level of persistence and making it feasible to use in multi-process and shared-filesystem environments. However, this scheme can be extremely inefficient, so mechanisms exist for deferred writing of changes and reading without checking the file for changes (though this second task is fairly inexpensive anyway. In addition to all of the standard dictionary methods:

Examples

Convert a BDB to a JSON file

Pretty trivial:

a = db_open_dict("bdb:refine_01#register")
b = js_open_dict("refine01/register.js)

b.update(a)

Make a large JSON file efficiently

Consider this:

from EMAN2 import *

a=range(1000)
d=js_open_dict("tst.js")

for i in range(500): 
        d[i]=a
        print i

now consider this:

from EMAN2 import *

a=range(1000)
d=js_open_dict("tst.js")

for i in range(500): 
        d.setval(i,a,deferupdate=True)
        print i

Both produce exactly the same tst.js file in the end, but the first version takes almost 2 minutes to run as compared to 0.5 seconds for the second version. Of course, if another program were to try and access the file during that 0.5 seconds, it wouldn't see any of the changes...