Skip to content

Instantly share code, notes, and snippets.

@ahmed-bacha
Forked from riahichedy/GraphPersistance.java
Created February 7, 2015 22:31
Show Gist options
  • Save ahmed-bacha/213771bda779eb21722f to your computer and use it in GitHub Desktop.
Save ahmed-bacha/213771bda779eb21722f to your computer and use it in GitHub Desktop.
package org.pri.controller;
import java.io.File;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.pri.entities.Domain;
import org.pri.entities.Page;
public class GraphPersistance {
private static final String DB_PATH = "src/test/neo4j-hello-db";
private static enum RelTypes implements RelationshipType {
A_POUR_PAGE, A_POUR_LIEN, A_POUR_EVENT
}
// START SNIPPET: vars
GraphDatabaseService graphDb;
Node domaine;
Node page, page2;
Node link1, link2;
Relationship relationship;
public static void main(final String[] args) {
GraphPersistance hello = new GraphPersistance();
hello.createDb();
//hello.removeData();
//hello.shutDown();
}
public void createDb(Domain dom) {
//deleteFileOrDirectory(new File(DB_PATH));
// START SNIPPET: startDb
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
registerShutdownHook(graphDb);
// END SNIPPET: startDb
// START SNIPPET: transaction
try (Transaction tx = graphDb.beginTx()) {
// Database operations go here
// END SNIPPET: transaction
// START SNIPPET: addData
domaine = graphDb.createNode();
domaine.setProperty("type", dom.getVertexType());
domaine.setProperty("domaine_URL", dom.getUrl());
domaine.setProperty("created_on", dom.getCreatedOn());
for(Page p:dom.getPages()){
page = graphDb.createNode();
page.setProperty("type", p.getVertexType());
}
page = graphDb.createNode();
page.setProperty("type", "page");
page.setProperty("page_url", "lignes-photoshop.html");
page.setProperty("page_mediaimageLinks_nb", 50);
page.setProperty("created_on", "07022015");
page2 = graphDb.createNode();
page2.setProperty("type", "page");
page2.setProperty("page_url", "lignes.html");
page2.setProperty("page_mediaimageLinks_nb", 35);
page2.setProperty("created_on", "07022015");
link1 = graphDb.createNode();
link1.setProperty("type", "link");
link1.setProperty("link_attribute", "id");
link1.setProperty("link_position", 16);
link1.setProperty("created_on", "07022015");
link2 = graphDb.createNode();
link2.setProperty("type", "link");
link2.setProperty("link_attribute", "class");
link2.setProperty("link_position", 5);
link2.setProperty("created_on", "07022015");
relationship = domaine.createRelationshipTo(page,
RelTypes.A_POUR_PAGE);
relationship = domaine.createRelationshipTo(page2,
RelTypes.A_POUR_PAGE);
relationship = page2.createRelationshipTo(link1,
RelTypes.A_POUR_LIEN);
relationship = page2.createRelationshipTo(link2,
RelTypes.A_POUR_LIEN);
// END SNIPPET: addData
// START SNIPPET: readData
System.out.println(domaine.getProperty("type") + " created on "
+ domaine.getProperty("created_on"));
System.out.println(page.getProperty("type") + " created on "
+ page.getProperty("created_on"));
System.out.println(page2.getProperty("type") + " created on "
+ page2.getProperty("created_on"));
// END SNIPPET: readData
// START SNIPPET: transaction
tx.success();
}
// END SNIPPET: transaction
}
void removeData() {
try (Transaction tx = graphDb.beginTx()) {
// START SNIPPET: removingData
// let's remove the data
domaine.getSingleRelationship(RelTypes.A_POUR_PAGE,
Direction.OUTGOING).delete();
domaine.delete();
page.delete();
link1.delete();
// END SNIPPET: removingData
tx.success();
}
}
void shutDown() {
System.out.println();
System.out.println("Shutting down database ...");
// START SNIPPET: shutdownServer
graphDb.shutdown();
// END SNIPPET: shutdownServer
}
// START SNIPPET: shutdownHook
private static void registerShutdownHook(final GraphDatabaseService graphDb) {
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running application).
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
graphDb.shutdown();
}
});
}
// END SNIPPET: shutdownHook
private static void deleteFileOrDirectory(File file) {
if (file.exists()) {
if (file.isDirectory()) {
for (File child : file.listFiles()) {
deleteFileOrDirectory(child);
}
}
file.delete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment