Search in sources :

Example 66 with EmbeddedGraphDatabase

use of org.neo4j.kernel.EmbeddedGraphDatabase in project neo4j-mobile-android by neo4j-contrib.

the class Neo4jServiceImpl method doOpenOrCreateDatabase.

private IGraphDatabase doOpenOrCreateDatabase(String name) {
    mDatabaseLock.lock();
    try {
        // TODO: [eRiC] what happens if another application tries to
        // open the same database?
        WeakReference<EmbeddedGraphDatabase> dbRef = mDatabases.get(name);
        // check if the DB is already loaded, or if has been
        // previously loaded but unloaded by the GC
        EmbeddedGraphDatabase db = null;
        if (dbRef != null) {
            db = dbRef.get();
            if (db == null) {
                Log.d(TAG, "database '" + name + "' was previously loaded, and garbage collected.");
                // delete the mapping, we will reload and remap it
                // later
                mDatabases.remove(name);
            }
        }
        // been unloaded by the GC
        if (db == null) {
            File dbDir = new File(getApplicationContext().getFilesDir(), name);
            db = new EmbeddedGraphDatabase(getApplicationContext(), dbDir.getAbsolutePath());
            mDatabases.put(name, new WeakReference<EmbeddedGraphDatabase>(db));
            Log.d(TAG, "database '" + name + "' loaded.");
        }
        DbWrapper dbWrapper = new DbWrapper(db, mTrxManager, getApplicationContext());
        Log.i(TAG, "Returning database '" + name + "' (binder hashcode '" + dbWrapper.asBinder().hashCode() + "'): " + db);
        return dbWrapper;
    } finally {
        mDatabaseLock.unlock();
    }
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) File(java.io.File)

Example 67 with EmbeddedGraphDatabase

use of org.neo4j.kernel.EmbeddedGraphDatabase in project graphdb by neo4j-attic.

the class UdcExtensionImplTest method shouldLoadWhenNormalGraphdbIsCreated.

/**
     * Expect the counts to be initialized.
     */
@Test
public void shouldLoadWhenNormalGraphdbIsCreated() throws Exception {
    EmbeddedGraphDatabase graphdb = createTempDatabase(null);
    // when the UDC extension successfully loads, it initializes the attempts count to 0
    assertGotSuccessWithRetry(IS_ZERO);
    destroy(graphdb);
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) Test(org.junit.Test)

Example 68 with EmbeddedGraphDatabase

use of org.neo4j.kernel.EmbeddedGraphDatabase in project graphdb by neo4j-attic.

the class UdcExtensionImplTest method shouldLoadForEachCreatedGraphdb.

/**
     * Expect separate counts for each graphdb.
     */
@Test
public void shouldLoadForEachCreatedGraphdb() throws IOException {
    EmbeddedGraphDatabase graphdb1 = createTempDatabase(null);
    EmbeddedGraphDatabase graphdb2 = createTempDatabase(null);
    Set<String> successCountValues = UdcTimerTask.successCounts.keySet();
    assertThat(successCountValues.size(), equalTo(2));
    assertThat("this", is(not("that")));
    destroy(graphdb1);
    destroy(graphdb2);
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) Test(org.junit.Test)

Example 69 with EmbeddedGraphDatabase

use of org.neo4j.kernel.EmbeddedGraphDatabase in project graphdb by neo4j-attic.

the class UdcExtensionImplTest method shouldBeAbleToSpecifySourceWithConfig.

@Test
public void shouldBeAbleToSpecifySourceWithConfig() throws Exception {
    // first, set up the test server
    LocalTestServer server = new LocalTestServer(null, null);
    PingerHandler handler = new PingerHandler();
    server.register("/*", handler);
    server.start();
    final String hostname = server.getServiceHostName();
    final String serverAddress = hostname + ":" + server.getServicePort();
    Map<String, String> config = new HashMap<String, String>();
    config.put(UdcExtensionImpl.FIRST_DELAY_CONFIG_KEY, "100");
    config.put(UdcExtensionImpl.UDC_HOST_ADDRESS_KEY, serverAddress);
    config.put(UdcExtensionImpl.UDC_SOURCE_KEY, "test");
    EmbeddedGraphDatabase graphdb = createTempDatabase(config);
    assertGotSuccessWithRetry(IS_GREATER_THAN_ZERO);
    assertEquals("test", handler.getQueryMap().get("source"));
    destroy(graphdb);
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) HashMap(java.util.HashMap) LocalTestServer(org.apache.http.localserver.LocalTestServer) Test(org.junit.Test)

Example 70 with EmbeddedGraphDatabase

use of org.neo4j.kernel.EmbeddedGraphDatabase in project graphdb by neo4j-attic.

the class UdcExtensionImplTest method createTempDatabase.

private EmbeddedGraphDatabase createTempDatabase(Map<String, String> config) throws IOException {
    EmbeddedGraphDatabase tempdb = null;
    String randomDbName = "tmpdb-" + rnd.nextInt();
    File possibleDirectory = new File("target" + File.separator + randomDbName);
    if (possibleDirectory.exists()) {
        FileUtils.deleteDirectory(possibleDirectory);
    }
    if (config == null) {
        tempdb = new EmbeddedGraphDatabase(randomDbName);
    } else {
        tempdb = new EmbeddedGraphDatabase(randomDbName, config);
    }
    return tempdb;
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) File(java.io.File)

Aggregations

EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)84 Test (org.junit.Test)50 File (java.io.File)35 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)32 Node (org.neo4j.graphdb.Node)26 Transaction (org.neo4j.graphdb.Transaction)17 Relationship (org.neo4j.graphdb.Relationship)14 BeforeClass (org.junit.BeforeClass)13 HashMap (java.util.HashMap)7 BatchInserterImpl (org.neo4j.kernel.impl.batchinsert.BatchInserterImpl)6 RandomAccessFile (java.io.RandomAccessFile)5 BatchInserter (org.neo4j.kernel.impl.batchinsert.BatchInserter)5 BatchInserterIndex (org.neo4j.graphdb.index.BatchInserterIndex)4 TransactionManager (javax.transaction.TransactionManager)3 Before (org.junit.Before)3 DynamicRelationshipType (org.neo4j.graphdb.DynamicRelationshipType)3 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)3 BatchInserterIndexProvider (org.neo4j.graphdb.index.BatchInserterIndexProvider)3 IndexManager (org.neo4j.graphdb.index.IndexManager)3 Transaction (javax.transaction.Transaction)2