コード例 #1
0
 /** {@inheritDoc} */
 @Override
 protected Relationship create(GraphDatabaseService database) {
   return database
       .getNodeById(startNodeGraphId)
       .createRelationshipTo(
           database.getNodeById(endNodeGraphId), DynamicRelationshipType.withName(type));
 }
  @Test
  public void iterableAcquiredInTransactionShouldBeProcessed() {
    try (Transaction tx = database.beginTx()) {
      for (int i = 0; i < 100; i++) {
        database.createNode();
      }
      tx.success();
    }

    BatchTransactionExecutor executor =
        new IterableInputBatchTransactionExecutor<>(
            database,
            10,
            new AllNodes(database, 10),
            new UnitOfWork<Node>() {
              @Override
              public void execute(
                  GraphDatabaseService database, Node node, int batchNumber, int stepNumber) {
                node.setProperty("name", "Name" + batchNumber + stepNumber);
              }
            });

    executor.execute();

    try (Transaction tx = database.beginTx()) {
      assertEquals("Name11", database.getNodeById(0).getProperty("name"));
      assertEquals("Name12", database.getNodeById(1).getProperty("name"));
      assertEquals("Name13", database.getNodeById(2).getProperty("name"));
      assertEquals("Name108", database.getNodeById(97).getProperty("name"));
      assertEquals("Name109", database.getNodeById(98).getProperty("name"));
      assertEquals("Name1010", database.getNodeById(99).getProperty("name"));
    }
  }
コード例 #3
0
  @RequestMapping(value = "/{rootNodeId}/single/{time}", method = RequestMethod.GET)
  @ResponseBody
  public JsonNode getInstantWithCustomRoot(
      @PathVariable long rootNodeId,
      @PathVariable long time,
      @RequestParam(required = false) String resolution,
      @RequestParam(required = false) String timezone) {

    TimeInstant timeInstant =
        TimeInstant.fromValueObject(new TimeInstantVO(time, resolution, timezone));

    long id;
    try (Transaction tx = database.beginTx()) {
      id =
          new CustomRootTimeTree(database.getNodeById(rootNodeId))
              .getOrCreateInstant(timeInstant)
              .getId();
      tx.success();
    }

    JsonNode result;
    try (Transaction tx = database.beginTx()) {
      result = new JsonNode(database.getNodeById(id));
      tx.success();
    }

    return result;
  }
  @Test
  public void nodesShouldBeCreatedFromListOfNames() {
    List<String> nodeNames = Arrays.asList("Name1", "Name2", "Name3");

    BatchTransactionExecutor executor =
        new IterableInputBatchTransactionExecutor<>(
            database,
            2,
            nodeNames,
            new UnitOfWork<String>() {
              @Override
              public void execute(
                  GraphDatabaseService database, String nodeName, int batchNumber, int stepNumber) {
                Node node = database.createNode();
                node.setProperty("name", nodeName + batchNumber + stepNumber);
              }
            });

    executor.execute();

    try (Transaction tx = database.beginTx()) {
      assertEquals(3, countNodes(database));
      assertEquals("Name111", database.getNodeById(0).getProperty("name"));
      assertEquals("Name212", database.getNodeById(1).getProperty("name"));
      assertEquals("Name321", database.getNodeById(2).getProperty("name"));
    }
  }
コード例 #5
0
 @Test
 public void listing3_7_create_movies() {
   try (Transaction tx = graphDb.beginTx()) {
     assertNotNull(graphDb.getNodeById(3));
     assertNotNull(graphDb.getNodeById(4));
     assertNotNull(graphDb.getNodeById(5));
     tx.success();
   }
 }
コード例 #6
0
  @RequestMapping(value = "/{rootNodeId}/range/{startTime}/{endTime}", method = RequestMethod.GET)
  @ResponseBody
  public JsonNode[] getInstantsWithCustomRoot(
      @PathVariable long rootNodeId,
      @PathVariable long startTime,
      @PathVariable long endTime,
      @RequestParam(required = false) String resolution,
      @RequestParam(required = false) String timezone) {

    TimeInstant startTimeInstant =
        TimeInstant.fromValueObject(new TimeInstantVO(startTime, resolution, timezone));
    TimeInstant endTimeInstant =
        TimeInstant.fromValueObject(new TimeInstantVO(endTime, resolution, timezone));

    List<Node> nodes;
    try (Transaction tx = database.beginTx()) {
      nodes =
          new CustomRootTimeTree(database.getNodeById(rootNodeId))
              .getOrCreateInstants(startTimeInstant, endTimeInstant);
      tx.success();
    }

    JsonNode[] result;
    try (Transaction tx = database.beginTx()) {
      result = jsonNodes(nodes);
      tx.success();
    }

    return result;
  }
コード例 #7
0
  public static Map<Long, Integer> getTermFrequencyMapForDocument(
      GraphDatabaseService db, Long classId) {
    Map<Long, Integer> termDocumentMatrix;

    String cacheKey = "TERM_DOCUMENT_FREQUENCY_" + classId;

    if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) {
      Node classNode = db.getNodeById(classId);

      termDocumentMatrix = new HashMap<>();

      IteratorUtil.asCollection(
              db.traversalDescription()
                  .depthFirst()
                  .relationships(withName("HAS_CLASS"), Direction.INCOMING)
                  .evaluator(Evaluators.fromDepth(1))
                  .evaluator(Evaluators.toDepth(1))
                  .traverse(classNode))
          .stream()
          .forEach(
              p ->
                  termDocumentMatrix.put(
                      p.endNode().getId(), (Integer) p.lastRelationship().getProperty("matches")));

      vectorSpaceModelCache.put(cacheKey, termDocumentMatrix);
    } else {
      termDocumentMatrix = (Map<Long, Integer>) vectorSpaceModelCache.getIfPresent(cacheKey);
    }

    return termDocumentMatrix;
  }
コード例 #8
0
  /**
   * Removes all nodes with non-valid resource classes from the graph.
   *
   * @param docGraph
   * @param validResourceClasses
   */
  private void preProcessGraph(DocGraph docGraph, Set<String> validResourceClasses) {
    log.info(String.format("Preprocessing DocGraph[%d]", docGraph.getId()));
    Node n;
    int cnt = 0;
    try (Transaction tx = graphDB.beginTx()) {
      for (Long nodeId : docGraph.getNodes()) {
        n = graphDB.getNodeById(nodeId);
        // node's class is resource class and it's not in the valid set
        if (n.hasProperty(Constants.NODE_SUPER_CLASS_KEY)
            && n.getProperty(Constants.NODE_SUPER_CLASS_KEY)
                .equals(Constants.NODE_SUPER_CLASS_RESOURCE_VALUE)
            && !validResourceClasses.contains(getNodeClass(n))) {
          try (Transaction innerTx = graphDB.beginTx()) {

            log.info("Deleting " + n);
            for (Relationship e : n.getRelationships()) {
              e.delete();
            }
            n.delete();
            innerTx.success();
            cnt++;
          }
        }
      }
      tx.success();
    }

    log.info(
        String.format("Preprocessing removed %d nodes from DocGraph[%d]", cnt, docGraph.getId()));
  }
コード例 #9
0
  public static int getDocumentSizeForFeature(GraphDatabaseService db, Long id) {
    int documentSize;

    String cacheKey = "DOCUMENT_SIZE_FEATURE_" + id;

    if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) {
      Node startNode = db.getNodeById(id);

      Iterator<Node> classes =
          db.traversalDescription()
              .depthFirst()
              .relationships(withName("HAS_CLASS"), Direction.OUTGOING)
              .evaluator(Evaluators.fromDepth(1))
              .evaluator(Evaluators.toDepth(1))
              .traverse(startNode)
              .nodes()
              .iterator();

      documentSize = IteratorUtil.count(classes);

      vectorSpaceModelCache.put(cacheKey, documentSize);
    } else {
      documentSize = (Integer) vectorSpaceModelCache.getIfPresent(cacheKey);
    }

    return documentSize;
  }
コード例 #10
0
  @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();
  }
コード例 #11
0
 @Test
 public void listing3_2_create_single_user() {
   usersAndMovies.createSingleUser();
   try (Transaction tx = graphDb.beginTx()) {
     assertNotNull(graphDb.getNodeById(0));
     tx.success();
   }
 }
コード例 #12
0
 @Test
 public void listing3_3_create_multiple_users() {
   usersAndMovies.createMultipleUsersInSingleTransaction();
   try (Transaction tx = graphDb.beginTx()) {
     assertNotNull(graphDb.getNodeById(1));
     assertNotNull(graphDb.getNodeById(2));
     tx.success();
   }
 }
コード例 #13
0
ファイル: NodesWithIds.java プロジェクト: BhavinGitHub/wonder
  private void calculateNext() {
    while (nodeIdsIt.hasNext()) {
      long id = nodeIdsIt.next().longValue();

      try {
        next = db.getNodeById(id);
        return;
      } catch (NotFoundException e) {
        // ignore
      }
    }
  }
コード例 #14
0
 /**
  * Utility method for instantiating a new node wrapper instance, using the class' constructor
  * which takes a {@link Node}.
  *
  * @param <T> the resulting instance's class type.
  * @param instanceClass the resulting instance's class type.
  * @param graphDb the {@link GraphDatabaseService} used with the node.
  * @param nodeId the id of the node to wrap, the node returned from {@link #getUnderlyingNode()}.
  * @return the new instance wrapping the node (with the given id).
  */
 public static <T extends NodeWrapper> T newInstance(
     Class<T> instanceClass, GraphDatabaseService graphDb, long nodeId) {
   Transaction tx = graphDb.beginTx();
   try {
     Node node = graphDb.getNodeById(nodeId);
     T result = newInstance(instanceClass, graphDb, node);
     tx.success();
     return result;
   } finally {
     tx.finish();
   }
 }
コード例 #15
0
  @Test
  public void deletedNewPropsShouldNotInfluenceEquality() { // bug test
    try (Transaction tx = database.beginTx()) {
      database.createNode().setProperty("accident", "dummy");
      database.createNode();
      tx.success();
    }

    try (Transaction tx = database.beginTx()) {
      database.getNodeById(0).delete();
      tx.success();
    }

    String cypher = "CREATE (n)";

    assertSameGraph(database, cypher);
  }
コード例 #16
0
  @Test
  public void deletedNewLabelShouldNotInfluenceEquality() { // bug test
    try (Transaction tx = database.beginTx()) {
      database.createNode(DynamicLabel.label("Accident"));
      database.createNode(DynamicLabel.label("RealLabel"));
      tx.success();
    }

    try (Transaction tx = database.beginTx()) {
      database.getNodeById(0).delete();
      tx.success();
    }

    String cypher = "CREATE (n:RealLabel)";

    assertSameGraph(database, cypher);
  }
コード例 #17
0
  private void deleteRandomNode() {
    if (nodes.size() < 3) return;
    int delIndex = r.nextInt(nodes.size());
    Node toDelete;

    for (int i = 0; i < delIndex; i++) {
      nodes.offer(nodes.poll());
    }
    toDelete = nodes.poll();
    for (Relationship rel : toDelete.getRelationships(Direction.BOTH)) {
      rel.delete();
      writes += 1; // The relationship delete
      reads += 1;
    }
    graphDb.getNodeById(toDelete.getId()).delete();
    reads += 1; // The node read in
    writes += 1; // The node delete
  }
コード例 #18
0
ファイル: Neo4j.java プロジェクト: jmdb-spikes/neo4j
  @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();
    }
  }
コード例 #19
0
  private Set<Node> filterNodesByClass(Set<Long> nodes, Set<String> validNodeClasses) {

    Node n = null;
    Set<Node> result = new HashSet<Node>();
    try (Transaction tx = graphDB.beginTx()) {
      for (Long nodeId : nodes) {
        try {
          n = graphDB.getNodeById(nodeId);
          // valid node class?
          if (n.hasProperty(Constants.CLASS_KEY)
              && validNodeClasses.contains((String) n.getProperty(Constants.CLASS_KEY))) {
            result.add(n);
          }
        } catch (NotFoundException e) {
          log.error("Node not found in database: " + nodeId);
          continue;
        }
      }
      tx.success();
    }
    return result;
  }
コード例 #20
0
  protected void verifyData(int nodeCount, GraphDatabaseService db) {
    // Verify that all the labels are in place
    Set<Label> expectedLabels = new HashSet<>();
    for (String label : LABELS) {
      expectedLabels.add(DynamicLabel.label(label));
    }
    GlobalGraphOperations globalOps = GlobalGraphOperations.at(db);
    Set<Label> allLabels = Iterables.toSet(globalOps.getAllLabels());
    assertThat(allLabels, is(expectedLabels));

    // Sample some nodes for deeper inspection of their contents
    Random random = new Random();
    for (int i = 0; i < nodeCount / 10; i++) {
      Node node = db.getNodeById(random.nextInt(nodeCount));
      int count = count(node.getRelationships());
      assertEquals("For node " + node, count, node.getDegree());
      for (String key : node.getPropertyKeys()) {
        node.getProperty(key);
        Set<Label> actualLabels = Iterables.toSet(node.getLabels());
        assertThat(actualLabels, is(expectedLabels));
      }
    }
  }
コード例 #21
0
ファイル: UuidProcedure.java プロジェクト: CoericK/neo4j-uuid
 protected Node findNodeByUuid(String moduleId, String uuid) {
   return database.getNodeById(reader(moduleId).getNodeIdByUuid(uuid));
 }
コード例 #22
0
 public boolean nodeExistsInDatabase(Node node) {
   return db.getNodeById(node.getId()) != null;
 }
コード例 #23
0
ファイル: REST.java プロジェクト: nereuschen/fast-http
 private Node node() {
   return gds.getNodeById(id());
 }
コード例 #24
0
ファイル: REST.java プロジェクト: nereuschen/fast-http
 public REST checkNodeProperty(long id, String prop, Object value) {
   checkProperty(gds.getNodeById(id), prop, value);
   return this;
 }
コード例 #25
0
ファイル: Neo4jDatabase.java プロジェクト: aaryani/Inference
 @Deprecated
 public Node _getNodeById(long id) {
   return graphDb.getNodeById(id);
 }
コード例 #26
0
 @Override
 public Node getNodeById(long id) {
     return delegate.getNodeById(id);
 }