Skip to content
gwendal edited this page May 24, 2017 · 6 revisions

Initialize NeoEMF

Add a Persistence Backend Factory

In order to use NeoEMF, you first need to register a persistence backend factory (concrete subclass of [PersistenceBackendFactory][persistenceBackendFactory-link]). This factory is responsible of the creation of the persistence backend (concrete subclass of [PersistenceBackend][persistenceBackend-link]) that is in charge of the model storage.

Persistence backend factories bundled with NeoEMF are:

  • Neo4j under the Blueprints API (dedicated page [here][neo4j-link])
  • MapDB (dedicated page [here][mapdb-link])
  • HBase (dedicated page [here][hbase-link])

Neo4j under Blueprints API

PersistenceBackendFactoryRegistry.register(BlueprintsURI.SCHEME, BlueprintsPersistenceBackendFactory.getInstance());

MapDB

PersistenceBackendFactoryRegistry.register(MapURI.SCHEME, MapPersistenceBackendFactory.getInstance());

HBase

PersistenceBackendFactoryRegistry.register(HBaseURI.SCHEME, HBasePersistenceBackendFactory.getInstance());

Register the Persistent Resource Factory

As regular EMF initialization, you need to register the [PersistentResourceFactory][persistentResourceFactory-link] implementation in the resource set specifying the URI protocol. Each backend implementation provides a subclass of [PersistenceURI][neoUri-link] to ease protocol definition. Note that the associated [PersistentResourceFactory][persistentResourceFactory-link] and the created [PersistentResource][persistentResource-link] do not depend on the selected backend.

Neo4j under Blueprints API

ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(BlueprintsURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(BlueprintsURI.createFileURI(new File("path_to_neodb")));

MapDB

ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(MapURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(MapURI.createFileURI(new File("path_to_mapdb")));

HBase

ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(HBaseURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(HBaseURI.createURI("hbaseHost", "hbasePort", "modelURI"));

Once this two initialization steps has been performed, the resulting resource can be used as a regular EMF [Resource][resource-link].

Save a Resource

/* Saving depending on the backend */
resource.save(AbstractPersistenceOptionsBuilder.noOption());

Load an existing Resource

/* Loading depending on the backend */
resource.load(AbstractPersistenceOptionsBuilder.noOption());

Modify an existing Resource

Neo4j under Blueprints API

PersistenceBackendFactoryRegistry.register(BlueprintsURI.SCHEME, BlueprintsPersistenceBackendFactory.getInstance());
ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(BlueprintsURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(BlueprintsURI.createFileURI(new File("path_to_neodb")));

// Load the resource
resource.load(BlueprintsOptionsBuilder.noOption());

// Perform modifications
MyClass myClass = (MyClass) resource.getContents().get(0);
myClass.setName("NewName");

// Save the modifications
resource.save(BlueprintsOptionsBuilder.noOption());

// Unload the resource and shutdown the database engine
if (resource instanceof PersistentResource) ((PersistentResource) resource).close();
else resource.unload();

MapDB

PersistenceBackendFactoryRegistry.register(MapURI.SCHEME, MapPersistenceBackendFactory.getInstance());
ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(MapURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(MapURI.createFileURI(new File("path_to_neodb")));

// Load the resource
resource.load(MapOptionsBuilder.noOption());

// Perform modifications
MyClass myClass = (MyClass) resource.getContents().get(0);
myClass.setName("NewName");

// Save the modifications
resource.save(MapOptionsBuilder.noOption());

// Unload the resource and shutdown the database engine
if (resource instanceof PersistentResource) ((PersistentResource) resource).close();
else resource.unload();