Exemple #1
0
    @SuppressWarnings("unchecked")
    private void expand() {
      Iterable<Relationship> expand = expander.expand(this, BranchState.NO_STATE);
      for (Relationship rel : expand) {
        lastMetadata.rels++;
        Node node = rel.getOtherNode(lastNode);
        Visit visit = visitData.get(node.getId());
        if (visit != null && visit.visited) {
          continue;
        }

        Visit lastVisit = visitData.get(lastNode.getId());
        double tentativeGScore =
            lastVisit.wayLength + lengthEvaluator.getCost(rel, Direction.OUTGOING);
        double estimate = estimateEvaluator.getCost(node, end);

        if (visit == null || !visit.next || tentativeGScore < visit.wayLength) {
          if (visit == null) {
            visit = new Visit(rel.getId(), tentativeGScore, estimate);
            visitData.put(node.getId(), visit);
          } else {
            visit.update(rel.getId(), tentativeGScore, estimate);
          }
          addNext(node, estimate + tentativeGScore, visit);
        }
      }
    }
Exemple #2
0
  @Test
  public void shouldNotBeAbleToCommitIfFailedTransactionContext() throws Exception {
    Transaction tx = db.beginTx();
    Statement statement = statementContextProvider.instance();

    // WHEN
    Node node = null;
    int labelId = -1;
    try {
      node = db.createNode();
      labelId = statement.dataWriteOperations().labelGetOrCreateForName("labello");
      statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId);
      statement.close();
      tx.failure();
      tx.success();
      tx.finish();
      fail("Should have failed");
    } catch (TransactionFailureException e) { // Expected
    }

    // THEN
    tx = db.beginTx();
    statement = statementContextProvider.instance();
    try {
      statement.readOperations().nodeHasLabel(node.getId(), labelId);
      fail("should have thrown exception");
    } catch (EntityNotFoundException e) {
      // Yay!
    }
    tx.finish();
  }
Exemple #3
0
  @Test
  public void transactionStateShouldReflectRemovingLabelImmediately() throws Exception {
    // GIVEN
    Transaction tx = db.beginTx();
    Statement statement = statementContextProvider.instance();
    Node node = db.createNode();
    int labelId1 = statement.dataWriteOperations().labelGetOrCreateForName("labello1");
    int labelId2 = statement.dataWriteOperations().labelGetOrCreateForName("labello2");
    statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId1);
    statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId2);
    statement.close();
    tx.success();
    tx.finish();
    tx = db.beginTx();
    statement = statementContextProvider.instance();

    // WHEN
    statement.dataWriteOperations().nodeRemoveLabel(node.getId(), labelId2);

    // THEN
    PrimitiveIntIterator labelsIterator = statement.readOperations().nodeGetLabels(node.getId());
    Set<Integer> labels = asSet(labelsIterator);
    assertFalse(statement.readOperations().nodeHasLabel(node.getId(), labelId2));
    assertEquals(asSet(labelId1), labels);
    statement.close();
    tx.success();
    tx.finish();
  }
  @Test
  public void shouldRecoverTransactionWhereNodeIsDeletedInTheFuture() throws Exception {
    // GIVEN
    Node node;
    try (Transaction tx = db.beginTx()) {
      node = db.createNode(label);
      node.setProperty("key", "value");
      tx.success();
    }
    rotateLog();
    try (Transaction tx = db.beginTx()) {
      node.setProperty("other-key", 1);
      tx.success();
    }
    try (Transaction tx = db.beginTx()) {
      node.delete();
      tx.success();
    }
    flushAll();

    // WHEN
    FileSystemAbstraction uncleanFs = fsRule.snapshot(shutdownDb(db));
    db =
        (GraphDatabaseAPI)
            new TestGraphDatabaseFactory().setFileSystem(uncleanFs).newImpermanentDatabase();

    // THEN
    // -- really the problem was that recovery threw exception, so mostly assert that.
    try (Transaction tx = db.beginTx()) {
      node = db.getNodeById(node.getId());
      fail("Should not exist");
    } catch (NotFoundException e) {
      assertEquals("Node " + node.getId() + " not found", e.getMessage());
    }
  }
Exemple #5
0
  @Test
  public void labelShouldBeRemovedAfterCommit() throws Exception {
    // GIVEN
    Transaction tx = db.beginTx();
    Statement statement = statementContextProvider.instance();
    Node node = db.createNode();
    int labelId1 = statement.dataWriteOperations().labelGetOrCreateForName("labello1");
    statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId1);
    statement.close();
    tx.success();
    tx.finish();

    // WHEN
    tx = db.beginTx();
    statement = statementContextProvider.instance();
    statement.dataWriteOperations().nodeRemoveLabel(node.getId(), labelId1);
    statement.close();
    tx.success();
    tx.finish();

    // THEN
    tx = db.beginTx();
    statement = statementContextProvider.instance();
    PrimitiveIntIterator labels = statement.readOperations().nodeGetLabels(node.getId());
    statement.close();
    tx.success();
    tx.finish();

    assertThat(asSet(labels), equalTo(Collections.<Integer>emptySet()));
  }
Exemple #6
0
  @Test
  public void deletingNodeWithLabelsShouldHaveThoseLabelRemovalsReflectedInTransaction()
      throws Exception {
    // GIVEN
    Transaction tx = db.beginTx();
    Label label = label("labello");
    Node node = db.createNode(label);
    tx.success();
    tx.finish();

    tx = db.beginTx();
    Statement statement = statementContextProvider.instance();

    // WHEN
    statement.dataWriteOperations().nodeDelete(node.getId());

    // Then
    int labelId = statement.readOperations().labelGetForName(label.name());
    Set<Integer> labels = asSet(statement.readOperations().nodeGetLabels(node.getId()));
    boolean labelIsSet = statement.readOperations().nodeHasLabel(node.getId(), labelId);
    Set<Long> nodes = asSet(statement.readOperations().nodesGetForLabel(labelId));

    statement.close();

    tx.success();
    tx.finish();

    assertEquals(emptySetOf(Long.class), nodes);
    assertEquals(emptySetOf(Integer.class), labels);
    assertFalse("Label should not be set on node here", labelIsSet);
  }
 @Before
 public void createTestingGraph() {
   Node node1 = getGraphDb().createNode();
   Node node2 = getGraphDb().createNode();
   Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST);
   node1Id = (int) node1.getId();
   node2Id = (int) node2.getId();
   node1.setProperty(key1, int1);
   node1.setProperty(key2, string1);
   node2.setProperty(key1, int2);
   node2.setProperty(key2, string2);
   rel.setProperty(key1, int1);
   rel.setProperty(key2, string1);
   node1.setProperty(arrayKey, array);
   node2.setProperty(arrayKey, array);
   rel.setProperty(arrayKey, array);
   Transaction tx = getTransaction();
   tx.success();
   tx.finish();
   NodeManager nodeManager =
       ((EmbeddedGraphDatabase) getGraphDb()).getConfig().getGraphDbModule().getNodeManager();
   nodeManager.clearCache();
   tx = getGraphDb().beginTx();
   setTransaction(tx);
 }
  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;
    }
  }
Exemple #9
0
 @Override
 public WeightedPath findSinglePath(Node start, Node end) {
   lastMetadata = new Metadata();
   AStarIterator iterator = new AStarIterator(start, end);
   while (iterator.hasNext()) {
     Node node = iterator.next();
     GraphDatabaseService graphDb = node.getGraphDatabase();
     if (node.equals(end)) {
       // Hit, return path
       double weight = iterator.visitData.get(node.getId()).wayLength;
       LinkedList<Relationship> rels = new LinkedList<Relationship>();
       Relationship rel =
           graphDb.getRelationshipById(iterator.visitData.get(node.getId()).cameFromRelationship);
       while (rel != null) {
         rels.addFirst(rel);
         node = rel.getOtherNode(node);
         long nextRelId = iterator.visitData.get(node.getId()).cameFromRelationship;
         rel = nextRelId == -1 ? null : graphDb.getRelationshipById(nextRelId);
       }
       Path path = toPath(start, rels);
       lastMetadata.paths++;
       return new WeightedPathImpl(weight, path);
     }
   }
   return null;
 }
 public Node getOtherNode(Node node) {
   if (startNodeId == (int) node.getId()) {
     return new NodeProxy(endNodeId, nodeManager);
   }
   if (endNodeId == (int) node.getId()) {
     return new NodeProxy(startNodeId, nodeManager);
   }
   throw new NotFoundException(
       "Node[" + node.getId() + "] not connected to this relationship[" + getId() + "]");
 }
 public Node getOtherNode(NodeManager nodeManager, Node node) {
   if (getStartNodeId() == node.getId()) {
     return new NodeProxy(getEndNodeId(), nodeManager);
   }
   if (getEndNodeId() == node.getId()) {
     return new NodeProxy(getStartNodeId(), nodeManager);
   }
   throw new NotFoundException(
       "Node[" + node.getId() + "] not connected to this relationship[" + getId() + "]");
 }
 public long setupCountryNode() {
   Transaction tx = graphDb.beginTx();
   try {
     Node countryNode = graphDb.createNode();
     countryNode.setProperty("name", "Countries");
     template.findOne(countryNode.getId(), CountryEntity.class);
     tx.success();
     return countryNode.getId();
   } finally {
     tx.finish();
   }
 }
 static Vertex addNode(Graph graph, Node node) {
   Vertex vertex = graph.getVertex(node.getId());
   if (null == vertex) {
     vertex = graph.addVertex(node.getId());
     copyProperties(node, vertex);
     Set<String> labels = new HashSet<>();
     for (Label label : node.getLabels()) {
       labels.add(label.name());
     }
     vertex.setProperty("types", labels);
   }
   return vertex;
 }
  /**
   * Default constructor
   *
   * @param relationship the relation
   */
  public DotEdge(Relationship relationship) {
    Node startNode = relationship.getStartNode();
    this.startId = ShapeIdPrefix.fromNode(startNode) + startNode.getId();
    Node endNode = relationship.getEndNode();
    this.endId = ShapeIdPrefix.fromNode(endNode) + endNode.getId();

    if (NodeUtils.isScoperelation(relationship.getType())) {
      this.edgeStyle = EdgeStyle.dotted;
    } else {
      this.edgeStyle = EdgeStyle.solid;
    }

    this.label = relationship.getType().toString();
  }
 @Test
 public void testQueryEndpointMultipleResults() throws Exception {
   Node andres = createNode("name", "Andres");
   Node peter = createNode("name", "Peter");
   cypherRsPath.put(ClientResponse.class, "match n where id(n) in {ids} return n");
   ClientResponse response =
       cypherRsPath
           .queryParam("ids", String.valueOf(andres.getId()))
           .queryParam("ids", String.valueOf(peter.getId()))
           .get(ClientResponse.class);
   String result = response.getEntity(String.class);
   assertEquals(result, 200, response.getStatus());
   assertEquals("[{\"name\":\"Andres\"},{\"name\":\"Peter\"}]", result);
 }
 @Test
 public void testQueryEndpointMultipleRowsMultipleColumns() throws Exception {
   Node andres = createNode("name", "Andres");
   Node peter = createNode("name", "Peter");
   cypherRsPath.put(ClientResponse.class, MULTI_COLUMN_QUERY);
   ClientResponse response =
       cypherRsPath
           .queryParam("ids", String.valueOf(andres.getId()))
           .queryParam("ids", String.valueOf(peter.getId()))
           .get(ClientResponse.class);
   String result = response.getEntity(String.class);
   assertEquals(result, 200, response.getStatus());
   assertEquals("[{\"l\":6,\"name\":\"Andres\"},{\"l\":5,\"name\":\"Peter\"}]", result);
 }
  /** {@inheritDoc} */
  @Override
  public Set<Label> removedLabels(Node node) {
    initializeChanged();

    if (!hasBeenChanged(node)) {
      LOG.warn(node + " has not been changed but the caller thinks it should have removed labels.");
      return Collections.emptySet();
    }

    if (!removedLabels.containsKey(node.getId())) {
      return Collections.emptySet();
    }

    return Collections.unmodifiableSet(removedLabels.get(node.getId()));
  }
  /** {@inheritDoc} */
  @Override
  public boolean hasLabelBeenRemoved(Node node, Label label) {
    initializeChanged();

    if (!hasBeenChanged(node)) {
      LOG.warn(node + " has not been changed but the caller thinks it should have removed labels.");
      return false;
    }

    if (!removedLabels.containsKey(node.getId())) {
      return false;
    }

    return removedLabels.get(node.getId()).contains(label);
  }
Exemple #19
0
  /**
   * While we transition ownership from the Beans API to the Kernel API for core database
   * interactions, there will be a bit of a mess. Our first goal is an architecture like this:
   *
   * <p>Users / \ Beans API Cypher \ / Kernel API | Kernel Implementation
   *
   * <p>But our current intermediate architecture looks like this:
   *
   * <p>Users / \ Beans API <--- Cypher | \ / | Kernel API | | Kernel Implementation
   *
   * <p>Meaning Kernel API and Beans API both manipulate the underlying kernel, causing lots of
   * corner cases. Most notably, those corner cases are related to Transactions, and the interplay
   * between three transaction APIs: - The Beans API - The JTA Transaction Manager API - The Kernel
   * TransactionContext API
   *
   * <p>In the long term, the goal is for JTA compliant stuff to live outside of the kernel, as an
   * addon. The Kernel API will rule supreme over the land of transactions. We are a long way away
   * from there, however, so as a first intermediary step, the JTA transaction manager rules
   * supreme, and the Kernel API piggybacks on it.
   *
   * <p>This test shows us how to use both the Kernel API and the Beans API together in the same
   * transaction, during the transition phase.
   */
  @Test
  public void mixingBeansApiWithKernelAPI() throws Exception {
    // 1: Start your transactions through the Beans API
    Transaction beansAPITx = db.beginTx();

    // 2: Get a hold of a KernelAPI statement context for the *current* transaction this way:
    Statement statement = statementContextProvider.instance();

    // 3: Now you can interact through both the statement context and the kernel API to manipulate
    // the
    //    same transaction.
    Node node = db.createNode();

    int labelId = statement.dataWriteOperations().labelGetOrCreateForName("labello");
    statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId);

    // 4: Close the StatementContext
    statement.close();

    // 5: Commit through the beans API
    beansAPITx.success();
    beansAPITx.finish();

    // NOTE: Transactions are still thread-bound right now, because we use JTA to "own"
    // transactions,
    // meaning if you use
    // both the Kernel API to create transactions while a Beans API transaction is running in the
    // same
    // thread, the results are undefined.

    // When the Kernel API implementation is done, the Kernel API transaction implementation is not
    // meant
    // to be bound to threads.
  }
Exemple #20
0
 @Override
 public List<String> completionCandidates(String partOfLine, Session session) {
   String lastWord = TextUtil.lastWordOrQuoteOf(partOfLine, false);
   if (lastWord.startsWith("-")) {
     return super.completionCandidates(partOfLine, session);
   }
   try {
     TreeSet<String> result = new TreeSet<String>();
     NodeOrRelationship current = getCurrent(session);
     if (current.isNode()) {
       // TODO Check if -r is supplied
       Node node = current.asNode();
       for (Node otherNode : RelationshipToNodeIterable.wrap(node.getRelationships(), node)) {
         long otherNodeId = otherNode.getId();
         String title = findTitle(getServer(), session, otherNode);
         if (title != null) {
           if (!result.contains(title)) {
             maybeAddCompletionCandidate(result, title + "," + otherNodeId, lastWord);
           }
         }
         maybeAddCompletionCandidate(result, "" + otherNodeId, lastWord);
       }
     } else {
       maybeAddCompletionCandidate(result, START_ALIAS, lastWord);
       maybeAddCompletionCandidate(result, END_ALIAS, lastWord);
       Relationship rel = current.asRelationship();
       maybeAddCompletionCandidate(result, "" + rel.getStartNode().getId(), lastWord);
       maybeAddCompletionCandidate(result, "" + rel.getEndNode().getId(), lastWord);
     }
     return new ArrayList<String>(result);
   } catch (ShellException e) {
     e.printStackTrace();
     return super.completionCandidates(partOfLine, session);
   }
 }
Exemple #21
0
 @Test
 @Graph(value = {"I know you"})
 public void runSimpleQuery() throws Exception {
   Node i = data.get().get("I");
   Representation result = testQuery("start n=node(" + i.getId() + ") return n");
   assertTrue(json.assemble(result).contains("I"));
 }
 private Node addNode(EmbeddedGraphDatabase graphDb) {
   Node referenceNode = graphDb.getReferenceNode();
   Node node = graphDb.createNode();
   node.setProperty("theId", node.getId());
   referenceNode.createRelationshipTo(node, MyRels.TEST);
   return node;
 }
 private Object toJsonCompatible(Object value) {
   if (value instanceof Node) {
     final Node node = (Node) value;
     final Map<String, Object> result = SubGraph.toMap(node);
     result.put("_id", node.getId());
     return result;
   }
   if (value instanceof Relationship) {
     final Relationship relationship = (Relationship) value;
     final Map<String, Object> result = SubGraph.toMap(relationship);
     result.put("_id", relationship.getId());
     result.put("_start", relationship.getStartNode().getId());
     result.put("_end", relationship.getEndNode().getId());
     result.put("_type", relationship.getType().name());
     return result;
   }
   if (value instanceof Iterable) {
     final List<Object> result = new ArrayList<Object>();
     for (Object inner : (Iterable) value) {
       result.add(toJsonCompatible(inner));
     }
     return result;
   }
   return value;
 }
  @Test
  public void shouldOverwriteExistingProperties()
      throws PropertyValueException, NodeNotFoundException {

    long nodeId;
    Transaction tx = database.getGraph().beginTx();
    try {
      Node node = database.getGraph().createNode();
      node.setProperty("remove me", "trash");
      nodeId = node.getId();
      tx.success();
    } finally {
      tx.finish();
    }
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("foo", "bar");
    properties.put("baz", 17);
    actions.setAllNodeProperties(nodeId, properties);
    tx = database.getGraph().beginTx();
    try {
      Node node = database.getGraph().getNodeById(nodeId);
      assertHasProperties(node, properties);
      assertNull(node.getProperty("remove me", null));
    } finally {
      tx.finish();
    }
  }
  public Concept getNextConceptToUpdate() {
    synchronized (lock) {
      IndexHits<Node> hits =
          conceptsForUpdate.query(
              new QueryContext(
                      NumericRangeQuery.newLongRange(
                          NamespaceConstants.DATE_UPDATED,
                          Long.MIN_VALUE,
                          Long.MAX_VALUE,
                          true,
                          true))
                  .sort(
                      new Sort(
                          new SortField(NamespaceConstants.DATE_UPDATED, SortField.LONG, false))));

      if (hits.hasNext()) {
        Node job = hits.next();
        System.out.println(job.getId());
        Concept concept = getByNode(job);
        removeFromCU(concept);
        return concept;
      }
    }
    return null;
  }
Exemple #26
0
  private long createDb() {
    GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);

    long firstNodeId = -1;

    Transaction tx = graphDb.beginTx();
    try {
      Node firstNode = graphDb.createNode();
      firstNodeId = firstNode.getId();
      firstNode.setProperty("message", "Hello, ");
      firstNode.setProperty(
          "someJson", "{\n  \"is\" : \"vcard\",\n  \"name\" : \"Jim Barritt\"\n}");

      Node secondNode = graphDb.createNode();
      secondNode.setProperty("message", "World!");

      Relationship relationship = firstNode.createRelationshipTo(secondNode, KNOWS);
      relationship.setProperty("message", "brave Neo4j ");

      tx.success();

      return firstNodeId;
    } finally {
      tx.finish();
      graphDb.shutdown();
    }
  }
  /** {@inheritDoc} */
  @Override
  public Set<Label> labelsOfDeletedNode(Node node) {
    initializeChanged();

    if (!hasBeenDeleted(node)) {
      LOG.error(node + " has not been deleted but the caller thinks it has! This is a bug.");
      throw new IllegalStateException(
          node + " has not been deleted but the caller thinks it has! This is a bug.");
    }

    if (!deletedNodeLabels.containsKey(node.getId())) {
      return Collections.emptySet();
    }

    return Collections.unmodifiableSet(deletedNodeLabels.get(node.getId()));
  }
  @Test
  public void
      makeSureLazyLoadingRelationshipsWorksEvenIfOtherIteratorAlsoLoadsInTheSameIteration() {
    int numEdges = 100;

    /* create 256 nodes */
    GraphDatabaseService graphDB = getGraphDb();
    Node[] nodes = new Node[256];
    for (int num_nodes = 0; num_nodes < nodes.length; num_nodes += 1) {
      nodes[num_nodes] = graphDB.createNode();
    }
    newTransaction();

    /* create random outgoing relationships from node 5 */
    Node hub = nodes[4];
    int nextID = 7;

    RelationshipType outtie = withName("outtie");
    RelationshipType innie = withName("innie");
    for (int k = 0; k < numEdges; k++) {
      Node neighbor = nodes[nextID];
      nextID += 7;
      nextID &= 255;
      if (nextID == 0) {
        nextID = 1;
      }
      hub.createRelationshipTo(neighbor, outtie);
    }
    newTransaction();

    /* create random incoming relationships to node 5 */
    for (int k = 0; k < numEdges; k += 1) {
      Node neighbor = nodes[nextID];
      nextID += 7;
      nextID &= 255;
      if (nextID == 0) {
        nextID = 1;
      }
      neighbor.createRelationshipTo(hub, innie);
    }
    commit();
    clearCache();

    newTransaction();
    hub = graphDB.getNodeById(hub.getId());

    int count = 0;
    for (@SuppressWarnings("unused") Relationship r1 : hub.getRelationships()) {
      count += count(hub.getRelationships());
    }
    assertEquals(40000, count);

    count = 0;
    for (@SuppressWarnings("unused") Relationship r1 : hub.getRelationships()) {
      count += count(hub.getRelationships());
    }
    assertEquals(40000, count);
    commit();
  }
Exemple #29
0
 long createNode() {
   GraphDatabaseAPI graphdb = server().getDatabase().getGraph();
   try (Transaction tx = graphdb.beginTx()) {
     Node node = graphdb.createNode();
     tx.success();
     return node.getId();
   }
 }
Exemple #30
0
    AStarIterator(Node start, Node end) {
      this.start = start;
      this.end = end;

      Visit visit = new Visit(-1, 0, estimateEvaluator.getCost(start, end));
      addNext(start, visit.getFscore(), visit);
      this.visitData.put(start.getId(), visit);
    }