Search in sources :

Example 6 with Node

use of org.neo4j.graphdb.Node in project neo4j-clean-remote-db-addon by jexp.

the class DeleteDatabaseTest method createData.

private void createData(GraphDatabaseAPI db, int max) {
    Transaction tx = db.beginTx();
    try {
        final IndexManager indexManager = db.index();
        Node[] nodes = new Node[max];
        for (int i = 0; i < max; i++) {
            nodes[i] = db.createNode();
            final Index<Node> index = indexManager.forNodes("node_index_" + String.valueOf(i % 5));
            index.add(nodes[i], "ID", i);
        }
        Random random = new Random();
        for (int i = 0; i < max * 2; i++) {
            int from = random.nextInt(max);
            final int to = (from + 1 + random.nextInt(max - 1)) % max;
            final Relationship relationship = nodes[from].createRelationshipTo(nodes[to], DynamicRelationshipType.withName("TEST_" + i));
            final Index<Relationship> index = indexManager.forRelationships("rel_index_" + String.valueOf(i % 5));
            index.add(relationship, "ID", i);
        }
        tx.success();
    } finally {
        tx.finish();
    }
}
Also used : IndexManager(org.neo4j.graphdb.index.IndexManager) Transaction(org.neo4j.graphdb.Transaction) Random(java.util.Random) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 7 with Node

use of org.neo4j.graphdb.Node in project neo4j by neo4j.

the class TransactionIT method shouldReadYourOwnWrites.

@Test
public void shouldReadYourOwnWrites() throws Exception {
    try (Transaction tx = env.graph().beginTx()) {
        Node node = env.graph().createNode(Label.label("A"));
        node.setProperty("prop", "one");
        tx.success();
    }
    BinaryLatch latch = new BinaryLatch();
    long dbVersion = env.lastClosedTxId();
    Thread thread = new Thread() {

        @Override
        public void run() {
            try (BoltStateMachine machine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<writeServer>", 7468)))) {
                machine.init(USER_AGENT, emptyMap(), null);
                latch.await();
                machine.run("MATCH (n:A) SET n.prop = 'two'", emptyMap(), nullResponseHandler());
                machine.pullAll(nullResponseHandler());
            } catch (BoltConnectionFatality connectionFatality) {
                throw new RuntimeException(connectionFatality);
            }
        }
    };
    thread.start();
    long dbVersionAfterWrite = dbVersion + 1;
    try (BoltStateMachine machine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<readServer>", 7468)))) {
        BoltResponseRecorder recorder = new BoltResponseRecorder();
        machine.init(USER_AGENT, emptyMap(), null);
        latch.release();
        final String bookmark = "neo4j:bookmark:v1:tx" + Long.toString(dbVersionAfterWrite);
        machine.run("BEGIN", singletonMap("bookmark", bookmark), nullResponseHandler());
        machine.pullAll(recorder);
        machine.run("MATCH (n:A) RETURN n.prop", emptyMap(), nullResponseHandler());
        machine.pullAll(recorder);
        machine.run("COMMIT", emptyMap(), nullResponseHandler());
        machine.pullAll(recorder);
        assertThat(recorder.nextResponse(), succeededWithMetadata("bookmark", BOOKMARK_PATTERN));
        assertThat(recorder.nextResponse(), succeededWithRecord("two"));
        assertThat(recorder.nextResponse(), succeededWithMetadata("bookmark", BOOKMARK_PATTERN));
    }
    thread.join();
}
Also used : BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) Transaction(org.neo4j.graphdb.Transaction) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) InetSocketAddress(java.net.InetSocketAddress) Node(org.neo4j.graphdb.Node) BoltResponseRecorder(org.neo4j.bolt.testing.BoltResponseRecorder) BoltConnectionFatality(org.neo4j.bolt.v1.runtime.BoltConnectionFatality) BinaryLatch(org.neo4j.concurrent.BinaryLatch) Test(org.junit.Test)

Example 8 with Node

use of org.neo4j.graphdb.Node in project neo4j by neo4j.

the class ImportToolTest method shouldNotTrimStringsByDefault.

@Test
public void shouldNotTrimStringsByDefault() throws Exception {
    // GIVEN
    String name = "  This is a line with leading and trailing whitespaces   ";
    File data = data(":ID,name", "1,\"" + name + "\"");
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--nodes", data.getAbsolutePath());
    // THEN
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        ResourceIterator<Node> allNodes = db.getAllNodes().iterator();
        Node node = Iterators.single(allNodes);
        allNodes.close();
        assertEquals(name, node.getProperty("name"));
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 9 with Node

use of org.neo4j.graphdb.Node in project neo4j by neo4j.

the class ImportToolTest method import4097Labels.

@Test
public void import4097Labels() throws Exception {
    // GIVEN
    File header = file(fileName("4097labels-header.csv"));
    try (PrintStream writer = new PrintStream(header)) {
        writer.println(":LABEL");
    }
    File data = file(fileName("4097labels.csv"));
    try (PrintStream writer = new PrintStream(data)) {
        // Need to have unique names in order to get unique ids for labels. Want 4096 unique label ids present.
        for (int i = 0; i < 4096; i++) {
            writer.println("SIMPLE" + i);
        }
        // Then insert one with 3 array entries which will get ids greater than 4096. These cannot be inlined
        // due 36 bits being divided into 3 parts of 12 bits each and 4097 > 2^12, thus these labels will be
        // need to be dynamic records.
        writer.println("FIRST 4096|SECOND 4096|");
    }
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--delimiter", "TAB", "--array-delimiter", "|", "--nodes", header.getAbsolutePath() + MULTI_FILE_DELIMITER + data.getAbsolutePath());
    // THEN
    try (Transaction tx = dbRule.beginTx()) {
        long nodeCount = Iterables.count(dbRule.getAllNodes());
        assertEquals(4097, nodeCount);
        tx.success();
        ResourceIterator<Node> nodes = dbRule.findNodes(DynamicLabel.label("FIRST 4096"));
        assertEquals(1, Iterators.asList(nodes).size());
        nodes = dbRule.findNodes(DynamicLabel.label("SECOND 4096"));
        assertEquals(1, Iterators.asList(nodes).size());
    }
}
Also used : PrintStream(java.io.PrintStream) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) File(java.io.File) Test(org.junit.Test)

Example 10 with Node

use of org.neo4j.graphdb.Node in project neo4j by neo4j.

the class ImportToolTest method shouldAcceptRawAsciiCharacterCodeAsQuoteConfiguration.

@Test
public void shouldAcceptRawAsciiCharacterCodeAsQuoteConfiguration() throws Exception {
    // GIVEN
    // not '1', just the character represented with code 1, which seems to be SOH
    char weirdDelimiter = 1;
    String name1 = weirdDelimiter + "Weird" + weirdDelimiter;
    String name2 = "Start " + weirdDelimiter + "middle thing" + weirdDelimiter + " end!";
    File data = data(":ID,name", "1," + name1, "2," + name2);
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--nodes", data.getAbsolutePath(), "--quote", String.valueOf(weirdDelimiter));
    // THEN
    Set<String> names = asSet("Weird", name2);
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        for (Node node : db.getAllNodes()) {
            String name = (String) node.getProperty("name");
            assertTrue("Didn't expect node with name '" + name + "'", names.remove(name));
        }
        assertTrue(names.isEmpty());
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Aggregations

Node (org.neo4j.graphdb.Node)1271 Test (org.junit.Test)781 Transaction (org.neo4j.graphdb.Transaction)540 Relationship (org.neo4j.graphdb.Relationship)372 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)146 NotFoundException (org.neo4j.graphdb.NotFoundException)78 File (java.io.File)65 HashMap (java.util.HashMap)57 Label (org.neo4j.graphdb.Label)57 RelationshipType (org.neo4j.graphdb.RelationshipType)57 LinkedList (java.util.LinkedList)56 Path (org.neo4j.graphdb.Path)52 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)46 HashSet (java.util.HashSet)45 Map (java.util.Map)44 WeightedPath (org.neo4j.graphalgo.WeightedPath)37 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)35 ArrayList (java.util.ArrayList)30 List (java.util.List)27 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)26