@Test
  public void testAddProperty() {
    String key3 = "key3";

    Node node1 = getGraphDb().getNodeById(node1Id);
    Node node2 = getGraphDb().getNodeById(node2Id);
    Relationship rel = node1.getSingleRelationship(MyRelTypes.TEST, Direction.BOTH);
    // add new property
    node2.setProperty(key3, int1);
    rel.setProperty(key3, int2);
    assertTrue(node1.hasProperty(key1));
    assertTrue(node2.hasProperty(key1));
    assertTrue(node1.hasProperty(key2));
    assertTrue(node2.hasProperty(key2));
    assertTrue(node1.hasProperty(arrayKey));
    assertTrue(node2.hasProperty(arrayKey));
    assertTrue(rel.hasProperty(arrayKey));
    assertTrue(!node1.hasProperty(key3));
    assertTrue(node2.hasProperty(key3));
    assertEquals(int1, node1.getProperty(key1));
    assertEquals(int2, node2.getProperty(key1));
    assertEquals(string1, node1.getProperty(key2));
    assertEquals(string2, node2.getProperty(key2));
    assertEquals(int1, rel.getProperty(key1));
    assertEquals(string1, rel.getProperty(key2));
    assertEquals(int2, rel.getProperty(key3));
  }
  @Override
  public NodePointer getImmediateParentPointer() {
    Node parentNode =
        node.getSingleRelationship(RelType.HAS_CHILD, Direction.INCOMING).getStartNode();

    return (new Neo4jNodePointer(this, parentNode));
  }
Example #3
0
  protected Node findLeafContainingGeometryNode(Node geomNode, boolean throwExceptionIfNotFound) {
    if (!geomNode.hasRelationship(RTreeRelationshipTypes.RTREE_REFERENCE, Direction.INCOMING)) {
      if (throwExceptionIfNotFound) {
        throw new RuntimeException("GeometryNode not indexed with an RTree: " + geomNode.getId());
      } else {
        return null;
      }
    }

    Node indexNodeLeaf =
        geomNode
            .getSingleRelationship(RTreeRelationshipTypes.RTREE_REFERENCE, Direction.INCOMING)
            .getStartNode();

    Node root = null;
    Node child = indexNodeLeaf;
    while (root == null) {
      Node parent = getIndexNodeParent(child);
      if (parent == null) {
        root = child;
      } else {
        child = parent;
      }
    }

    if (root.getId() != getIndexRoot().getId()) {
      if (throwExceptionIfNotFound) {
        throw new RuntimeException("GeometryNode not indexed in this RTree: " + geomNode.getId());
      } else {
        return null;
      }
    } else {
      return indexNodeLeaf;
    }
  }
 @After
 public void deleteTestingGraph() {
   Node node1 = getGraphDb().getNodeById(node1Id);
   Node node2 = getGraphDb().getNodeById(node2Id);
   node1.getSingleRelationship(MyRelTypes.TEST, Direction.BOTH).delete();
   node1.delete();
   node2.delete();
 }
Example #5
0
 private Node getIndexNodeParent(Node indexNode) {
   Relationship relationship =
       indexNode.getSingleRelationship(RTreeRelationshipTypes.RTREE_CHILD, Direction.INCOMING);
   if (relationship == null) {
     return null;
   } else {
     return relationship.getStartNode();
   }
 }
Example #6
0
  private void createNewRoot(Node oldRoot, Node newIndexNode) {
    Node newRoot = database.createNode();
    addChild(newRoot, RTreeRelationshipTypes.RTREE_CHILD, oldRoot);
    addChild(newRoot, RTreeRelationshipTypes.RTREE_CHILD, newIndexNode);

    Node layerNode = getRootNode();
    layerNode.getSingleRelationship(RTreeRelationshipTypes.RTREE_ROOT, Direction.OUTGOING).delete();
    layerNode.createRelationshipTo(newRoot, RTreeRelationshipTypes.RTREE_ROOT);
  }
 /**
  * Wraps a {@link Node#getSingleRelationship(RelationshipType, Direction)} in a transaction.
  *
  * @param node the {@link Node}.
  * @param type the {@link RelationshipType}
  * @param direction the {@link Direction}.
  * @return the result from {@link Node#getSingleRelationship(RelationshipType, Direction)}.
  */
 public Relationship getSingleRelationship(Node node, RelationshipType type, Direction direction) {
   Transaction tx = graphDb().beginTx();
   try {
     Relationship singleRelationship = node.getSingleRelationship(type, direction);
     tx.success();
     return singleRelationship;
   } finally {
     tx.finish();
   }
 }
Example #8
0
  @Override
  public void removeAll(final boolean deleteGeomNodes, final Listener monitor) {
    Node indexRoot = getIndexRoot();

    monitor.begin(count());
    try {
      // delete all geometry nodes
      visitInTx(
          new SpatialIndexVisitor() {
            public boolean needsToVisit(Envelope indexNodeEnvelope) {
              return true;
            }

            public void onIndexReference(Node geomNode) {
              geomNode
                  .getSingleRelationship(RTreeRelationshipTypes.RTREE_REFERENCE, Direction.INCOMING)
                  .delete();
              if (deleteGeomNodes) {
                deleteNode(geomNode);
              }

              monitor.worked(1);
            }
          },
          indexRoot.getId());
    } finally {
      monitor.done();
    }

    try (Transaction tx = database.beginTx()) {
      // delete index root relationship
      indexRoot
          .getSingleRelationship(RTreeRelationshipTypes.RTREE_ROOT, Direction.INCOMING)
          .delete();

      // delete tree
      deleteRecursivelySubtree(indexRoot);

      // delete tree metadata
      Relationship metadataNodeRelationship =
          getRootNode()
              .getSingleRelationship(RTreeRelationshipTypes.RTREE_METADATA, Direction.OUTGOING);
      Node metadataNode = metadataNodeRelationship.getEndNode();
      metadataNodeRelationship.delete();
      metadataNode.delete();

      tx.success();
    }

    countSaved = false;
    totalGeometryCount = 0;
  }
Example #9
0
  public void remove(long geomNodeId, boolean deleteGeomNode, boolean throwExceptionIfNotFound) {

    Node geomNode = null;
    // getNodeById throws NotFoundException if node is already removed
    try {
      geomNode = database.getNodeById(geomNodeId);

    } catch (NotFoundException nfe) {

      // propagate exception only if flag is set
      if (throwExceptionIfNotFound) {
        throw nfe;
      }
    }

    if (geomNode == null && !throwExceptionIfNotFound) {
      // fail silently
      return;
    }

    // be sure geomNode is inside this RTree
    Node indexNode = findLeafContainingGeometryNode(geomNode, throwExceptionIfNotFound);
    if (indexNode == null) {
      return;
    }

    // remove the entry
    final Relationship geometryRtreeReference =
        geomNode.getSingleRelationship(RTreeRelationshipTypes.RTREE_REFERENCE, Direction.INCOMING);
    if (geometryRtreeReference != null) {
      geometryRtreeReference.delete();
    }
    if (deleteGeomNode) {
      deleteNode(geomNode);
    }

    // reorganize the tree if needed
    if (countChildren(indexNode, RTreeRelationshipTypes.RTREE_REFERENCE) == 0) {
      indexNode = deleteEmptyTreeNodes(indexNode, RTreeRelationshipTypes.RTREE_REFERENCE);
      adjustParentBoundingBox(indexNode, RTreeRelationshipTypes.RTREE_CHILD);
    } else {
      adjustParentBoundingBox(indexNode, RTreeRelationshipTypes.RTREE_REFERENCE);
    }

    adjustPathBoundingBox(indexNode);

    countSaved = false;
    totalGeometryCount--;
  }
  @Test
  public void testNodeChangeProperty() {
    Node node1 = getGraphDb().getNodeById(node1Id);
    Node node2 = getGraphDb().getNodeById(node2Id);
    Relationship rel = node1.getSingleRelationship(MyRelTypes.TEST, Direction.BOTH);

    // test change property
    node1.setProperty(key1, int2);
    node2.setProperty(key1, int1);
    rel.setProperty(key1, int2);
    int[] newIntArray = new int[] {3, 2, 1};
    node1.setProperty(arrayKey, newIntArray);
    node2.setProperty(arrayKey, newIntArray);
    rel.setProperty(arrayKey, newIntArray);
  }
Example #11
0
  private void deleteRecursivelySubtree(Node indexNode) {
    for (Relationship relationship :
        indexNode.getRelationships(RTreeRelationshipTypes.RTREE_CHILD, Direction.OUTGOING)) {
      deleteRecursivelySubtree(relationship.getEndNode());
    }

    Relationship relationshipWithFather =
        indexNode.getSingleRelationship(RTreeRelationshipTypes.RTREE_CHILD, Direction.INCOMING);
    // the following check is needed because rootNode doesn't have this relationship
    if (relationshipWithFather != null) {
      relationshipWithFather.delete();
    }

    indexNode.delete();
  }
Example #12
0
  @Test
  public void insert() throws InterruptedException {
    // START SNIPPET: insert
    BatchInserter inserter = null;
    try {
      inserter =
          BatchInserters.inserter(
              new File("target/batchinserter-example").getAbsolutePath(), fileSystem);

      Label personLabel = DynamicLabel.label("Person");
      inserter.createDeferredSchemaIndex(personLabel).on("name").create();

      Map<String, Object> properties = new HashMap<>();

      properties.put("name", "Mattias");
      long mattiasNode = inserter.createNode(properties, personLabel);

      properties.put("name", "Chris");
      long chrisNode = inserter.createNode(properties, personLabel);

      RelationshipType knows = DynamicRelationshipType.withName("KNOWS");
      inserter.createRelationship(mattiasNode, chrisNode, knows, null);
    } finally {
      if (inserter != null) {
        inserter.shutdown();
      }
    }
    // END SNIPPET: insert

    // try it out from a normal db
    GraphDatabaseService db =
        new TestGraphDatabaseFactory()
            .setFileSystem(fileSystem)
            .newImpermanentDatabase(new File("target/batchinserter-example").getAbsolutePath());
    try (Transaction tx = db.beginTx()) {
      Label personLabelForTesting = DynamicLabel.label("Person");
      Node mNode = db.findNode(personLabelForTesting, "name", "Mattias");
      Node cNode =
          mNode
              .getSingleRelationship(DynamicRelationshipType.withName("KNOWS"), Direction.OUTGOING)
              .getEndNode();
      assertThat((String) cNode.getProperty("name"), is("Chris"));
      assertThat(db.schema().getIndexes(personLabelForTesting).iterator().hasNext(), is(true));
    } finally {
      db.shutdown();
    }
  }
Example #13
0
 private Node deleteEmptyTreeNodes(Node indexNode, RelationshipType relType) {
   if (countChildren(indexNode, relType) == 0) {
     Node parent = getIndexNodeParent(indexNode);
     if (parent != null) {
       indexNode
           .getSingleRelationship(RTreeRelationshipTypes.RTREE_CHILD, Direction.INCOMING)
           .delete();
       indexNode.delete();
       return deleteEmptyTreeNodes(parent, RTreeRelationshipTypes.RTREE_CHILD);
     } else {
       // root
       return indexNode;
     }
   } else {
     return indexNode;
   }
 }
  @Test
  public void testNodeRemoveProperty() {
    Node node1 = getGraphDb().getNodeById(node1Id);
    Node node2 = getGraphDb().getNodeById(node2Id);
    Relationship rel = node1.getSingleRelationship(MyRelTypes.TEST, Direction.BOTH);

    // test remove property
    assertEquals(1, node1.removeProperty(key1));
    assertEquals(2, node2.removeProperty(key1));
    assertEquals(1, rel.removeProperty(key1));
    assertEquals(string1, node1.removeProperty(key2));
    assertEquals(string2, node2.removeProperty(key2));
    assertEquals(string1, rel.removeProperty(key2));
    assertTrue(node1.removeProperty(arrayKey) != null);
    assertTrue(node2.removeProperty(arrayKey) != null);
    assertTrue(rel.removeProperty(arrayKey) != null);
  }
  /**
   * Tries to get a sub reference node with relationship type <code>type</code>. If it doesn't
   * exist, it is created. There can be only one of any given type.
   *
   * <p>[NodeSpaceReferenceNode] -- type --> [SubReferenceNode]
   *
   * @param type the relationship type.
   * @param direction the direction of the relationship.
   * @return the sub-reference node.
   */
  public Node getOrCreateSubReferenceNode(RelationshipType type, Direction direction) {
    Transaction tx = graphDb().beginTx();
    try {
      Node referenceNode = getReferenceNode();
      Node node = null;
      Relationship singleRelationship = referenceNode.getSingleRelationship(type, direction);
      if (singleRelationship != null) {
        node = singleRelationship.getOtherNode(referenceNode);
      } else {
        node = graphDb().createNode();
        referenceNode.createRelationshipTo(node, type);
      }

      tx.success();
      return node;
    } finally {
      tx.finish();
    }
  }
  @Test
  public void testDirectedRelationship1() {
    Node node1 = getGraphDb().getNodeById(node1Id);
    Relationship rel = node1.getSingleRelationship(MyRelTypes.TEST, Direction.BOTH);
    Node nodes[] = rel.getNodes();
    assertEquals(2, nodes.length);

    Node node2 = getGraphDb().getNodeById(node2Id);
    assertTrue(nodes[0].equals(node1) && nodes[1].equals(node2));
    assertEquals(node1, rel.getStartNode());
    assertEquals(node2, rel.getEndNode());

    Relationship relArray[] =
        getRelationshipArray(node1.getRelationships(MyRelTypes.TEST, Direction.OUTGOING));
    assertEquals(1, relArray.length);
    assertEquals(rel, relArray[0]);
    relArray = getRelationshipArray(node2.getRelationships(MyRelTypes.TEST, Direction.INCOMING));
    assertEquals(1, relArray.length);
    assertEquals(rel, relArray[0]);
  }
Example #17
0
  private void initIndexMetadata() {
    Node layerNode = getRootNode();
    if (layerNode.hasRelationship(RTreeRelationshipTypes.RTREE_METADATA, Direction.OUTGOING)) {
      // metadata already present
      metadataNode =
          layerNode
              .getSingleRelationship(RTreeRelationshipTypes.RTREE_METADATA, Direction.OUTGOING)
              .getEndNode();

      maxNodeReferences = (Integer) metadataNode.getProperty("maxNodeReferences");
    } else {
      // metadata initialization
      metadataNode = database.createNode();
      layerNode.createRelationshipTo(metadataNode, RTreeRelationshipTypes.RTREE_METADATA);

      metadataNode.setProperty("maxNodeReferences", maxNodeReferences);
    }

    saveCount();
  }
Example #18
0
  @Test
  public void simple_persistent_storage() throws Exception {
    long firstNodeId = createDb();

    GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);

    try {
      Node firstNode = graphDb.getNodeById(firstNodeId);

      Relationship relationship = firstNode.getSingleRelationship(KNOWS, Direction.OUTGOING);
      Node secondNode = relationship.getEndNode();

      out.println("I just loaded this from the data store:");
      out.println(firstNode.getProperty("message"));
      out.println(firstNode.getProperty("someJson"));
      out.println(relationship.getProperty("message"));
      out.println(secondNode.getProperty("message"));
    } finally {
      graphDb.shutdown();
    }
  }
 public void deletePerson(Person person) {
   Transaction tx = graphDb.beginTx();
   try {
     Node personNode = person.getUnderlyingNode();
     index.remove(personNode, Person.NAME, person.getName());
     for (Person friend : person.getFriends()) {
       person.removeFriend(friend);
     }
     personNode.getSingleRelationship(RelTypes.A_PERSON, Direction.INCOMING).delete();
     for (StatusUpdate status : person.getStatus()) {
       Node statusNode = status.getUnderlyingNode();
       for (Relationship r : statusNode.getRelationships()) {
         r.delete();
       }
       statusNode.delete();
     }
     personNode.delete();
     tx.success();
   } finally {
     tx.finish();
   }
 }
 @Test
 public void testGetDirectedRelationship() {
   Node node1 = getGraphDb().getNodeById(node1Id);
   Relationship rel = node1.getSingleRelationship(MyRelTypes.TEST, Direction.OUTGOING);
   assertEquals(int1, rel.getProperty(key1));
 }
 @Override
 public Relationship getRelationTo() {
   return node.getSingleRelationship(getMainRelationshipType(), Direction.INCOMING);
 }