Database Memory Device Specification in Java

For Java applications using in-memory databases, a single conventional memory device required for all database data, indexes and metadata is automatically defined and initialized in the Database.open() method when no device array is specified. For example, the typical sequence of API calls that uses the default device initialization could look like the following:

 
    class Obj
    {
        @Indexable(Type=Database.IndexType.BTree, Unique=false) // Declare non-unique tree index by "value" field
        int value;
    }
     
    public static void main(String[] args)
    {
        static final int PAGE_SIZE = 128;
        static final int DATABASE_SIZE = 16*1024*1024;
        ...
        Database db = new Database();
        Database.Parameters params = new Database.Parameters();
        ...
        params.memPageSize = PAGE_SIZE;
        params.classes = new Class[]{Obj.class};
        ...
        db.open("tree-db", params, DATABASE_SIZE);
        ...
    }
 

 

However, Java applications can override the default device initialization by specifying an array of memory devices to pass to the Database.open() method. For example, to define a shared memory device, the application code could look like the following:

 
    class Obj
    {
        @Indexable(Type=Database.IndexType.BTree, Unique=false) // Declare non-unique tree index by "value" field
        public int value;
    }
     
    public static void main(String[] args)
    {
        const int PAGE_SIZE = 128;
        const int DATABASE_SIZE = 16*1024*1024;
        ...
        Database db = new Database();
        Database.Device[] devs;
        char * dbName = "openDb";
        char * shareName = "openDb-db";
        long hint = 0;
        Database.Parameters params = new Database.Parameters();
        ...
        parames.memPageSize = PAGE_SIZE;
        parames.classes = new Class[]{Obj.class};
        ...
        devs = new Database.Device[1];
        devs[0] = new Database.SharedMemoryDevice(Database.Device.Kind.Data, shareName, hint, DATABASE_SIZE);
        ...
        db.open(dbName, params, devs);
        ...
    }
 

Note that a shared memory device must specify the shared memory name, a "hint" address for the shared memory segment and the total database size.

Persistent Databases

Applications using persistent databases must define an array of at least four memory devices to manage the following distinct memory regions:

Database.Device.Kind.Data For metadata and database objects, indexes and other database structures
Database.Device.Kind.DiskCache For the disk manager cache (page pool)
Database.Device.Kind.Data

A persistent storage device (can be file, multi-file or RAID). Note that if a persistent device is defined and the application does not explicitly assign DiskPageSize the runtime will assign it the default value of 4096 bytes. (If multiple applications or tasks attempt to assign different disk page sizes for the same database the runtime returns an error code.)

Database.Device.Kind.TransactionLog A persistent storage device that contains the database log file

For a typical persistent database application the memory devices could be defined as follows:

     
    static final int   N_DEVICES             4;
    static final int PAGE_SIZE = 128;
    static final int DISK_PAGE_SIZE = 4096;
    static final int DISK_CACHE_SIZE = 8*1024*1024;
    static final int DATABASE_SIZE = 16*1024*1024;
     
    Database.Device[] devs = new Database.Device[N_DEVICES];
     
    devs[0] = new Database.PrivateMemoryDevice(Database.Device.Kind.Data, DATABASE_SIZE);
    devs[1] = new Database.PrivateMemoryDevice(Database.Device.Kind.DiskCache, DISK_CACHE_SIZE);
    devs[2] = new Database.FileDevice(Database.Device.Kind.Data, "opendb.dbs");
    devs[3] = new Database.FileDevice(Database.Device.Kind.TransactionLog, "opendb.log");