@Override public Collection<Injectable<?>> start(GraphDatabaseService graphDatabaseService, Configuration config) { EmbeddedDriver embeddedDriver = new EmbeddedDriver(graphDatabaseService); sessionFactory = createSessionFactory(embeddedDriver); return Arrays.asList(new OgmInjectable<>(sessionFactory, SessionFactory.class)); }
@Override public synchronized void configure(DriverConfiguration config) { super.configure(config); close(); // force any existing graph database to shutdown try { String fileStoreUri = config.getURI(); // if no URI is set, create a temporary folder for the graph db // that will persist only for the duration of the JVM // This is effectively what the ImpermanentDatabase does. if (fileStoreUri == null) { fileStoreUri = createTemporaryEphemeralFileStore(); } File file = new File(new URI(fileStoreUri)); graphDatabaseService = new GraphDatabaseFactory().newEmbeddedDatabase(file); registerShutdownHook(); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Configure a new embedded driver according to the supplied driver configuration * @param driverConfiguration the {@link DriverConfiguration} to use */ public EmbeddedDriver(DriverConfiguration driverConfiguration) { configure(driverConfiguration); }
@Override public Request request() { return new EmbeddedRequest(graphDatabaseService, transactionManager, getParameterConversion(), getCypherModification()); }
private String createTemporaryFileStore() { try { Path path = Files.createTempDirectory("neo4jTmpEmbedded.db"); Path databasePath = Paths.get(path.toFile().getAbsolutePath() + "/database"); Files.createDirectories(databasePath); final File f = databasePath.toFile(); URI uri = f.toURI(); final String databaseUriValue = uri.toString(); logger.warn("Creating temporary file store: " + databaseUriValue); Runtime.getRuntime().addShutdownHook(new Thread(() -> { close(); try { logger.warn("Deleting temporary file store: " + databaseUriValue); deleteDirectory(path); } catch (IOException e) { throw new RuntimeException("Failed to delete temporary files in " + databaseUriValue, e); } })); return databaseUriValue; } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void shouldBeAbleToHaveMultipleInstances() { Configuration configuration = new Configuration.Builder().build(); try ( EmbeddedDriver driver1 = new EmbeddedDriver(); EmbeddedDriver driver2 = new EmbeddedDriver()) { driver1.configure(configuration); driver2.configure(configuration); GraphDatabaseService service1 = driver1.unwrap(GraphDatabaseService.class); GraphDatabaseService service2 = driver2.unwrap(GraphDatabaseService.class); assertThat(service1).isNotNull(); assertThat(service2).isNotNull(); // instances should be different assertThat(service1 == service2).isFalse(); // underlying file stores should be different assertThat( service1.toString().equals(service2.toString())) .isFalse(); } }
@Override public synchronized void configure(Configuration config) { super.configure(config); try { String fileStoreUri = config.getURI(); // if no URI is set, create a temporary folder for the graph db // that will persist only for the duration of the JVM // This is effectively what the ImpermanentDatabase does. if (fileStoreUri == null) { fileStoreUri = createTemporaryFileStore(); } else { createPermanentFileStore(fileStoreUri); } File file = new File(new URI(fileStoreUri)); if (!file.exists()) { throw new RuntimeException("Could not create/open filestore: " + fileStoreUri); } GraphDatabaseBuilder graphDatabaseBuilder = getGraphDatabaseFactory(configuration) .newEmbeddedDatabaseBuilder(file); String neo4jConfLocation = config.getNeo4jConfLocation(); if(neo4jConfLocation != null) { URL neo4ConfUrl = ResourceUtils.getResourceUrl(neo4jConfLocation); graphDatabaseBuilder = graphDatabaseBuilder.loadPropertiesFromURL(neo4ConfUrl); } this.graphDatabaseService = graphDatabaseBuilder.newGraphDatabase(); } catch (Exception e) { throw new ConnectionException("Error connecting to embedded graph", e); } }
@Override public Transaction newTransaction(Transaction.Type type, Iterable<String> bookmarks) { if (bookmarks != null && bookmarks.iterator().hasNext()) { logger.warn("Passing bookmarks {} to EmbeddedDriver. This is not currently supported.", bookmarks); } return new EmbeddedTransaction(transactionManager, nativeTransaction(), type); }
@Test public void shouldWriteAndReadFromProvidedDatabase() throws Exception { GraphDatabaseService impermanentDatabase = new TestGraphDatabaseFactory().newImpermanentDatabase(); try (EmbeddedDriver driver = new EmbeddedDriver(impermanentDatabase, null)) { GraphDatabaseService databaseService = driver.unwrap(GraphDatabaseService.class); try (Transaction tx = databaseService.beginTx()) { databaseService.execute("CREATE (n: Node {name: 'node'})"); Result r = databaseService.execute("MATCH (n) RETURN n"); assertThat(r.hasNext()).isTrue(); tx.success(); } } }
@Override public void run() { close(); } });
@Override public Request request() { return new EmbeddedRequest(graphDatabaseService, transactionManager, parameterConversion, new EmbeddedEntityAdapter(typeSystem), getCypherModification()); }
@Test public void shouldCreateImpermanentInstanceWhenNoURI() { Configuration configuration = new Configuration.Builder().build(); try (EmbeddedDriver driver = new EmbeddedDriver()) { driver.configure(configuration); assertThat(configuration.getURI()).isNull(); assertThat(driver.unwrap(GraphDatabaseService.class)).isNotNull(); } }
@Override public synchronized void configure(Configuration newConfiguration) { super.configure(newConfiguration); try { String fileStoreUri = newConfiguration.getURI(); // if no URI is set, create a temporary folder for the graph db // that will persist only for the duration of the JVM // This is effectively what the ImpermanentDatabase does. if (fileStoreUri == null) { fileStoreUri = createTemporaryFileStore(); } else { createPermanentFileStore(fileStoreUri); } File file = new File(new URI(fileStoreUri)); if (!file.exists()) { throw new RuntimeException("Could not create/open filestore: " + fileStoreUri); } GraphDatabaseBuilder graphDatabaseBuilder = getGraphDatabaseFactory(newConfiguration) .newEmbeddedDatabaseBuilder(file); String neo4jConfLocation = newConfiguration.getNeo4jConfLocation(); if (neo4jConfLocation != null) { URL neo4ConfUrl = ResourceUtils.getResourceUrl(neo4jConfLocation); graphDatabaseBuilder = graphDatabaseBuilder.loadPropertiesFromURL(neo4ConfUrl); } this.graphDatabaseService = graphDatabaseBuilder.newGraphDatabase(); } catch (Exception e) { throw new ConnectionException("Error connecting to embedded graph", e); } }
@Override public Transaction newTransaction(Transaction.Type type, Iterable<String> bookmarks) { if (bookmarks != null && bookmarks.iterator().hasNext()) { logger.warn("Passing bookmarks {} to EmbeddedDriver. This is not currently supported.", bookmarks); } return new EmbeddedTransaction(transactionManager, nativeTransaction(), type); }
private String createTemporaryFileStore() { try { Path path = Files.createTempDirectory("neo4jTmpEmbedded.db"); Path databasePath = Paths.get(path.toFile().getAbsolutePath() + "/database"); Files.createDirectories(databasePath); final File f = databasePath.toFile(); URI uri = f.toURI(); final String databaseUriValue = uri.toString(); logger.warn("Creating temporary file store: " + databaseUriValue); Runtime.getRuntime().addShutdownHook(new Thread(() -> { close(); try { logger.warn("Deleting temporary file store: " + databaseUriValue); deleteDirectory(path); } catch (IOException e) { throw new RuntimeException("Failed to delete temporary files in " + databaseUriValue, e); } })); return databaseUriValue; } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void shouldHandleCustomConfFiles() { try (EmbeddedDriver driver = new EmbeddedDriver()) { driver.configure(new Configuration.Builder().neo4jConfLocation("classpath:custom-neo4j.conf").build()); GraphDatabaseService databaseService = driver.unwrap(GraphDatabaseService.class); assertReadOnly(databaseService); } }
@Override public Collection<Injectable<?>> start(GraphDatabaseService graphDatabaseService, Configuration config) { EmbeddedDriver embeddedDriver = new EmbeddedDriver(graphDatabaseService, null); sessionFactory = createSessionFactory(embeddedDriver); return Arrays.asList(new OgmInjectable<>(sessionFactory, SessionFactory.class)); }
@Override public Transaction newTransaction() { // return a new, or join an existing transaction return new EmbeddedTransaction(transactionManager, nativeTransaction()); }
@Test public void shouldLoadHaBasedOnNeo4ConfFile() { assumeTrue(canRunHATests()); try (EmbeddedDriver driver = new EmbeddedDriver()) { driver.configure(new Configuration.Builder().neo4jConfLocation("classpath:custom-neo4j-ha.conf").build()); GraphDatabaseService databaseService = driver.unwrap(GraphDatabaseService.class); assertThat(databaseService.getClass().getSimpleName()).isEqualTo(NAME_OF_HA_DATABASE_CLASS); } }
@BeforeClass public static void initSessionFactory() { Driver driver = new EmbeddedDriver(serverControls.graph(), new Configuration.Builder().build()); sessionFactory = new SessionFactory(driver, DOMAIN_PACKAGE); }