Skip to content

Persistence Service_136675804

nxi edited this page Apr 9, 2015 · 1 revision
Created by Tony Lam, last modified on Dec 22, 2009

Introduction

The GumTree local persistence (ILocalPersistenceManager) service provides a managed way to store object and state locally for application recovery. The database used for persistence is called db4o (managed by service IObjectContainerManager), which is an small embedded object database for Java and OSGi.  To use the persistence service, you will need to import plug-in org.gumtree.core.persistence, and have plug-in db4o_osgi available.

Local Persistence Service

The local persistence service provides a simple way to store object using key-value pairs, backed by the default db4o object container offered by the GumTree platform.  The following test case illustrates how this service can be used:
// Should initially be empty
assertNull(manager.retrieve(key, String.class));
assertFalse(manager.contains(key));

// Persist
manager.persist(key, data);
assertTrue(manager.contains(key));
assertEquals(data, manager.retrieve(key, String.class));

// Update
manager.persist(key, newData);
assertTrue(manager.contains(key));
assertEquals(newData, manager.retrieve(key, String.class));

// Remove
manager.remove(key);
assertNull(manager.retrieve(key, String.class));
assertFalse(manager.contains(key));
Developer note: Due to the constraint of OSGi class loading, the stored data MUST be located in exported package (ie, cannot persist object from internal packages).

Object Container Service

This service provides a default object database, as well as keeping track of other object database based on user creation. Those databases are stored under <workspace>/.metadata/.plugins/org.gumtree.core.persistence/. Developer note: We use the OSGi version db4o (Db4oService) and not the generic helper class (Db4o) to create new object container instance, since it solves the class loading problem.

Using Default Persistence Database

To use this service for storage:
ILocalPersistenceManager manager = GTPlatform.getService(ILocalPersistenceManager.class);
ObjectContainer db = manager.getDefaultObjectContainer();
db.store(object);
To retrieve object, use Native Query directly on the object container:
manager.getDefaultObjectContainer().query(...)

Using Your Own Persistence Database

You can create a new object container with the local persistence manager:
// Create a new one called &quot;mydatabase&quot; and do not overwrite
ObjectContainer db = manager.createObjectContainer(&quot;mydatabase&quot;, false);
db.store(object, &quot;mydatabase&quot;);
If the database has already opened, it will return the database straight away.
Document generated by Confluence on Apr 01, 2015 00:11
Clone this wiki locally