private Node setLabels(Node node, Collection<String> labels) {
     if (labels==null || labels.isEmpty()) return node;
     for (String label : labels) {
         node.addLabel(DynamicLabel.label(label));
     }
     return node;
 }
public class FriendOfAFriendDepth4 {
  public static final Label USER = DynamicLabel.label("User");

  private final TraversalDescription traversalDescription;

  private final GraphDatabaseService db;

  public FriendOfAFriendDepth4(GraphDatabaseService db) {
    this.db = db;
    traversalDescription = traversalDescription(db);
  }

  private TraversalDescription traversalDescription(GraphDatabaseService db) {
    return db.traversalDescription()
        .breadthFirst()
        .uniqueness(NODE_GLOBAL)
        .relationships(withName("FRIEND"))
        .evaluator(
            new Evaluator() {
              @Override
              public Evaluation evaluate(Path path) {
                if (path.length() == 4) {
                  return Evaluation.INCLUDE_AND_PRUNE;
                }
                return Evaluation.EXCLUDE_AND_CONTINUE;
              }
            });
  }

  public Iterable<Node> getFriends(String name) {
    ResourceIterable<Node> users = db.findNodesByLabelAndProperty(USER, "name", name);
    Node startNode = IteratorUtil.single(users);
    return traversalDescription.traverse(startNode).nodes();
  }
}
示例#3
0
  private static List<Double> getWeightVectorForClass(
      Map<String, List<LinkedHashMap<String, Object>>> documents,
      String key,
      List<Integer> featureIndexList,
      GraphDatabaseService db) {
    List<Double> weightVector;

    Transaction tx = db.beginTx();
    // Get class id
    Long classId =
        db.findNodesByLabelAndProperty(DynamicLabel.label("Class"), "name", key)
            .iterator()
            .next()
            .getId();

    // Get weight vector for class
    List<Long> longs =
        documents
            .get(key)
            .stream()
            .map(a -> ((Integer) a.get("feature")).longValue())
            .collect(Collectors.toList());

    weightVector =
        featureIndexList
            .stream()
            .map(i -> longs.contains(i.longValue()) ? tfidf(db, i.longValue(), classId) : 0.0)
            .collect(Collectors.toList());
    tx.success();
    tx.close();
    return weightVector;
  }
  @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);
  }
 @Override
 protected ConstraintDefinition obtainEntityInTransaction(
     GraphDatabaseService graphDatabaseService) {
   return graphDatabaseService
       .schema()
       .constraintFor(DynamicLabel.label("Label"))
       .assertPropertyIsUnique("property")
       .create();
 }
 private Label[] toLabels(Collection<String> labels) {
     if (labels==null || labels.isEmpty()) return NO_LABELS;
     Label[] labelArray = new Label[labels.size()];
     int i=0;
     for (String label : labels) {
         labelArray[i++]= DynamicLabel.label(label);
     }
     return labelArray;
 }
 @Test
 public void listing3_10_node_labels() {
   usersAndMovies.addLabelToMovies();
   try (Transaction tx = graphDb.beginTx()) {
     ResourceIterable<Node> movies =
         GlobalGraphOperations.at(graphDb).getAllNodesWithLabel(DynamicLabel.label("MOVIES"));
     assertEquals(3, IteratorUtil.count(movies));
     tx.success();
   }
 }
示例#8
0
  private static List<Node> getAllClasses(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> finalClasses = new ArrayList<>();
    GlobalGraphOperations.at(db)
        .getAllNodesWithLabel(DynamicLabel.label("Class"))
        .forEach(a -> finalClasses.add(a));
    tx.success();
    tx.close();

    return finalClasses.stream().map(a -> a).collect(Collectors.toList());
  }
示例#9
0
 public static int getDocumentSize(GraphDatabaseService db) {
   int documentSize;
   String cacheKey = "GLOBAL_DOCUMENT_SIZE";
   if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) {
     documentSize =
         IteratorUtil.count(
             GlobalGraphOperations.at(db).getAllNodesWithLabel(DynamicLabel.label("Class")));
     vectorSpaceModelCache.put(cacheKey, documentSize);
   } else {
     documentSize = (Integer) vectorSpaceModelCache.getIfPresent(cacheKey);
   }
   return documentSize;
 }
 @Test
 public void listing3_11_node_labels_and_property() {
   try {
     usersAndMovies.addLabelToMovies();
   } catch (Exception e) {
     // ignore if index already exist
   }
   try (Transaction tx = graphDb.beginTx()) {
     ResourceIterable<Node> movies =
         graphDb.findNodesByLabelAndProperty(DynamicLabel.label("MOVIES"), "name", "Fargo");
     assertEquals(1, IteratorUtil.count(movies));
     tx.success();
   }
 }
示例#11
0
  private static List<Integer> getFeatureIndexList(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> patterns = new ArrayList<>();
    GlobalGraphOperations.at(db)
        .getAllNodesWithLabel(DynamicLabel.label("Pattern"))
        .forEach(a -> patterns.add(a));

    Collections.sort(
        patterns,
        (a, b) ->
            ((Integer) b.getProperty("threshold"))
                .compareTo(((Integer) a.getProperty("threshold"))));

    List<Integer> patternIds =
        patterns.stream().map(a -> ((Long) a.getId()).intValue()).collect(Collectors.toList());

    tx.success();
    tx.close();

    return patternIds;
  }
 @Override
 public boolean include(Node node) {
   return !(node.hasLabel(DynamicLabel.label("ChangeSet")));
 }
 @Override
 public boolean include(Node node) {
   return node.hasLabel(DynamicLabel.label("Blue"));
 }
示例#14
0
public class IndexingAcceptanceTest {
  /* This test is a bit interesting. It tests a case where we've got a property that sits in one
   * property block and the value is of a long type. So given that plus that there's an index for that
   * label/property, do an update that changes the long value into a value that requires two property blocks.
   * This is interesting because the transaction logic compares before/after views per property record and
   * not per node as a whole.
   *
   * In this case this change will be converted into one "add" and one "remove" property updates instead of
   * a single "change" property update. At the very basic level it's nice to test for this corner-case so
   * that the externally observed behavior is correct, even if this test doesn't assert anything about
   * the underlying add/remove vs. change internal details.
   */
  @Test
  public void shouldInterpretPropertyAsChangedEvenIfPropertyMovesFromOneRecordToAnother()
      throws Exception {
    // GIVEN
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI();
    long smallValue = 10L, bigValue = 1L << 62;
    Node myNode;
    {
      try (Transaction tx = beansAPI.beginTx()) {
        myNode = beansAPI.createNode(LABEL1);
        myNode.setProperty("pad0", true);
        myNode.setProperty("pad1", true);
        myNode.setProperty("pad2", true);
        // Use a small long here which will only occupy one property block
        myNode.setProperty("key", smallValue);

        tx.success();
      }
    }
    {
      IndexDefinition indexDefinition;
      try (Transaction tx = beansAPI.beginTx()) {
        indexDefinition = beansAPI.schema().indexFor(LABEL1).on("key").create();

        tx.success();
      }
      waitForIndex(beansAPI, indexDefinition);
    }

    // WHEN
    try (Transaction tx = beansAPI.beginTx()) {
      // A big long value which will occupy two property blocks
      myNode.setProperty("key", bigValue);
      tx.success();
    }

    // THEN
    assertThat(
        findNodesByLabelAndProperty(LABEL1, "key", bigValue, beansAPI), containsOnly(myNode));
    assertThat(findNodesByLabelAndProperty(LABEL1, "key", smallValue, beansAPI), isEmpty());
  }

  @Test
  public void
      shouldUseDynamicPropertiesToIndexANodeWhenAddedAlongsideExistingPropertiesInASeparateTransaction()
          throws Exception {
    // Given
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI();

    // When
    long id;
    {
      try (Transaction tx = beansAPI.beginTx()) {
        Node myNode = beansAPI.createNode();
        id = myNode.getId();
        myNode.setProperty("key0", true);
        myNode.setProperty("key1", true);

        tx.success();
      }
    }
    {
      IndexDefinition indexDefinition;
      try (Transaction tx = beansAPI.beginTx()) {
        indexDefinition = beansAPI.schema().indexFor(LABEL1).on("key2").create();

        tx.success();
      }
      waitForIndex(beansAPI, indexDefinition);
    }
    Node myNode;
    {
      try (Transaction tx = beansAPI.beginTx()) {
        myNode = beansAPI.getNodeById(id);
        myNode.addLabel(LABEL1);
        myNode.setProperty("key2", LONG_STRING);
        myNode.setProperty("key3", LONG_STRING);

        tx.success();
      }
    }

    // Then
    assertThat(myNode, inTx(beansAPI, hasProperty("key2").withValue(LONG_STRING)));
    assertThat(myNode, inTx(beansAPI, hasProperty("key3").withValue(LONG_STRING)));
    assertThat(
        findNodesByLabelAndProperty(LABEL1, "key2", LONG_STRING, beansAPI), containsOnly(myNode));
  }

  @Test
  public void searchingForNodeByPropertyShouldWorkWithoutIndex() throws Exception {
    // Given
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
    Node myNode = createNode(beansAPI, map("name", "Hawking"), LABEL1);

    // When
    assertThat(
        findNodesByLabelAndProperty(LABEL1, "name", "Hawking", beansAPI), containsOnly(myNode));
  }

  @Test
  public void searchingUsesIndexWhenItExists() throws Exception {
    // Given
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
    Node myNode = createNode(beansAPI, map("name", "Hawking"), LABEL1);
    createIndex(beansAPI, LABEL1, "name");

    // When
    assertThat(
        findNodesByLabelAndProperty(LABEL1, "name", "Hawking", beansAPI), containsOnly(myNode));
  }

  @Test
  public void shouldCorrectlyUpdateIndexesWhenChangingLabelsAndPropertyAtTheSameTime()
      throws Exception {
    // Given
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
    Node myNode = createNode(beansAPI, map("name", "Hawking"), LABEL1, LABEL2);
    createIndex(beansAPI, LABEL1, "name");
    createIndex(beansAPI, LABEL2, "name");
    createIndex(beansAPI, LABEL3, "name");

    // When
    try (Transaction tx = beansAPI.beginTx()) {
      myNode.removeLabel(LABEL1);
      myNode.addLabel(LABEL3);
      myNode.setProperty("name", "Einstein");
      tx.success();
    }

    // Then
    assertThat(myNode, inTx(beansAPI, hasProperty("name").withValue("Einstein")));
    assertThat(labels(myNode), containsOnly(LABEL2, LABEL3));

    assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Hawking", beansAPI), isEmpty());
    assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Einstein", beansAPI), isEmpty());

    assertThat(findNodesByLabelAndProperty(LABEL2, "name", "Hawking", beansAPI), isEmpty());
    assertThat(
        findNodesByLabelAndProperty(LABEL2, "name", "Einstein", beansAPI), containsOnly(myNode));

    assertThat(findNodesByLabelAndProperty(LABEL3, "name", "Hawking", beansAPI), isEmpty());
    assertThat(
        findNodesByLabelAndProperty(LABEL3, "name", "Einstein", beansAPI), containsOnly(myNode));
  }

  @Test
  public void shouldCorrectlyUpdateIndexesWhenChangingLabelsAndPropertyMultipleTimesAllAtOnce()
      throws Exception {
    // Given
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
    Node myNode = createNode(beansAPI, map("name", "Hawking"), LABEL1, LABEL2);
    createIndex(beansAPI, LABEL1, "name");
    createIndex(beansAPI, LABEL2, "name");
    createIndex(beansAPI, LABEL3, "name");

    // When
    try (Transaction tx = beansAPI.beginTx()) {
      myNode.addLabel(LABEL3);
      myNode.setProperty("name", "Einstein");
      myNode.removeLabel(LABEL1);
      myNode.setProperty("name", "Feynman");
      tx.success();
    }

    // Then
    assertThat(myNode, inTx(beansAPI, hasProperty("name").withValue("Feynman")));
    assertThat(labels(myNode), containsOnly(LABEL2, LABEL3));

    assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Hawking", beansAPI), isEmpty());
    assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Einstein", beansAPI), isEmpty());
    assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Feynman", beansAPI), isEmpty());

    assertThat(findNodesByLabelAndProperty(LABEL2, "name", "Hawking", beansAPI), isEmpty());
    assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Einstein", beansAPI), isEmpty());
    assertThat(
        findNodesByLabelAndProperty(LABEL2, "name", "Feynman", beansAPI), containsOnly(myNode));

    assertThat(findNodesByLabelAndProperty(LABEL3, "name", "Hawking", beansAPI), isEmpty());
    assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Einstein", beansAPI), isEmpty());
    assertThat(
        findNodesByLabelAndProperty(LABEL3, "name", "Feynman", beansAPI), containsOnly(myNode));
  }

  @Test
  public void searchingByLabelAndPropertyReturnsEmptyWhenMissingLabelOrProperty() throws Exception {
    // Given
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();

    // When/Then
    assertThat(findNodesByLabelAndProperty(LABEL1, "name", "Hawking", beansAPI), isEmpty());
  }

  @Test
  public void shouldSeeIndexUpdatesWhenQueryingOutsideTransaction() throws Exception {
    // GIVEN
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
    createIndex(beansAPI, LABEL1, "name");
    Node firstNode = createNode(beansAPI, map("name", "Mattias"), LABEL1);

    // WHEN THEN
    assertThat(
        findNodesByLabelAndProperty(LABEL1, "name", "Mattias", beansAPI), containsOnly(firstNode));
    Node secondNode = createNode(beansAPI, map("name", "Taylor"), LABEL1);
    assertThat(
        findNodesByLabelAndProperty(LABEL1, "name", "Taylor", beansAPI), containsOnly(secondNode));
  }

  @Test
  public void createdNodeShouldShowUpWithinTransaction() throws Exception {
    // GIVEN
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
    createIndex(beansAPI, LABEL1, "name");

    // WHEN
    Transaction tx = beansAPI.beginTx();

    Node firstNode = createNode(beansAPI, map("name", "Mattias"), LABEL1);
    long sizeBeforeDelete = count(beansAPI.findNodes(LABEL1, "name", "Mattias"));
    firstNode.delete();
    long sizeAfterDelete = count(beansAPI.findNodes(LABEL1, "name", "Mattias"));

    tx.close();

    // THEN
    assertThat(sizeBeforeDelete, equalTo(1l));
    assertThat(sizeAfterDelete, equalTo(0l));
  }

  @Test
  public void deletedNodeShouldShowUpWithinTransaction() throws Exception {
    // GIVEN
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
    createIndex(beansAPI, LABEL1, "name");
    Node firstNode = createNode(beansAPI, map("name", "Mattias"), LABEL1);

    // WHEN
    Transaction tx = beansAPI.beginTx();

    long sizeBeforeDelete = count(beansAPI.findNodes(LABEL1, "name", "Mattias"));
    firstNode.delete();
    long sizeAfterDelete = count(beansAPI.findNodes(LABEL1, "name", "Mattias"));

    tx.close();

    // THEN
    assertThat(sizeBeforeDelete, equalTo(1l));
    assertThat(sizeAfterDelete, equalTo(0l));
  }

  @Test
  public void createdNodeShouldShowUpInIndexQuery() throws Exception {
    // GIVEN
    GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
    createIndex(beansAPI, LABEL1, "name");
    createNode(beansAPI, map("name", "Mattias"), LABEL1);

    // WHEN
    Transaction tx = beansAPI.beginTx();

    long sizeBeforeDelete = count(beansAPI.findNodes(LABEL1, "name", "Mattias"));
    createNode(beansAPI, map("name", "Mattias"), LABEL1);
    long sizeAfterDelete = count(beansAPI.findNodes(LABEL1, "name", "Mattias"));

    tx.close();

    // THEN
    assertThat(sizeBeforeDelete, equalTo(1l));
    assertThat(sizeAfterDelete, equalTo(2l));
  }

  @Test
  public void shouldBeAbleToQuerySupportedPropertyTypes() throws Exception {
    // GIVEN
    String property = "name";
    GraphDatabaseService db = dbRule.getGraphDatabaseService();
    createIndex(db, LABEL1, property);

    // WHEN & THEN
    assertCanCreateAndFind(db, LABEL1, property, "A String");
    assertCanCreateAndFind(db, LABEL1, property, true);
    assertCanCreateAndFind(db, LABEL1, property, false);
    assertCanCreateAndFind(db, LABEL1, property, (byte) 56);
    assertCanCreateAndFind(db, LABEL1, property, 'z');
    assertCanCreateAndFind(db, LABEL1, property, (short) 12);
    assertCanCreateAndFind(db, LABEL1, property, 12);
    assertCanCreateAndFind(db, LABEL1, property, 12l);
    assertCanCreateAndFind(db, LABEL1, property, (float) 12.);
    assertCanCreateAndFind(db, LABEL1, property, 12.);

    assertCanCreateAndFind(db, LABEL1, property, new String[] {"A String"});
    assertCanCreateAndFind(db, LABEL1, property, new boolean[] {true});
    assertCanCreateAndFind(db, LABEL1, property, new Boolean[] {false});
    assertCanCreateAndFind(db, LABEL1, property, new byte[] {56});
    assertCanCreateAndFind(db, LABEL1, property, new Byte[] {57});
    assertCanCreateAndFind(db, LABEL1, property, new char[] {'a'});
    assertCanCreateAndFind(db, LABEL1, property, new Character[] {'b'});
    assertCanCreateAndFind(db, LABEL1, property, new short[] {12});
    assertCanCreateAndFind(db, LABEL1, property, new Short[] {13});
    assertCanCreateAndFind(db, LABEL1, property, new int[] {14});
    assertCanCreateAndFind(db, LABEL1, property, new Integer[] {15});
    assertCanCreateAndFind(db, LABEL1, property, new long[] {16l});
    assertCanCreateAndFind(db, LABEL1, property, new Long[] {17l});
    assertCanCreateAndFind(db, LABEL1, property, new float[] {(float) 18.});
    assertCanCreateAndFind(db, LABEL1, property, new Float[] {(float) 19.});
    assertCanCreateAndFind(db, LABEL1, property, new double[] {20.});
    assertCanCreateAndFind(db, LABEL1, property, new Double[] {21.});
  }

  @Test
  public void shouldRetrieveMultipleNodesWithSameValueFromIndex() throws Exception {
    // this test was included here for now as a precondition for the following test

    // given
    GraphDatabaseService graph = dbRule.getGraphDatabaseService();
    createIndex(graph, LABEL1, "name");

    Node node1, node2;
    try (Transaction tx = graph.beginTx()) {
      node1 = graph.createNode(LABEL1);
      node1.setProperty("name", "Stefan");

      node2 = graph.createNode(LABEL1);
      node2.setProperty("name", "Stefan");
      tx.success();
    }

    try (Transaction tx = graph.beginTx()) {
      ResourceIterator<Node> result = graph.findNodes(LABEL1, "name", "Stefan");
      assertEquals(asSet(node1, node2), asSet(result));

      tx.success();
    }
  }

  @Test(expected = MultipleFoundException.class)
  public void shouldThrowWhenMulitpleResultsForSingleNode() throws Exception {
    // given
    GraphDatabaseService graph = dbRule.getGraphDatabaseService();
    createIndex(graph, LABEL1, "name");

    Node node1, node2;
    try (Transaction tx = graph.beginTx()) {
      node1 = graph.createNode(LABEL1);
      node1.setProperty("name", "Stefan");

      node2 = graph.createNode(LABEL1);
      node2.setProperty("name", "Stefan");
      tx.success();
    }

    try (Transaction tx = graph.beginTx()) {
      graph.findNode(LABEL1, "name", "Stefan");
    }
  }

  private void assertCanCreateAndFind(
      GraphDatabaseService db, Label label, String propertyKey, Object value) {
    Node created = createNode(db, map(propertyKey, value), label);

    try (Transaction tx = db.beginTx()) {
      Node found = db.findNode(label, propertyKey, value);
      assertThat(found, equalTo(created));
      found.delete();
      tx.success();
    }
  }

  public static final String LONG_STRING = "a long string that has to be stored in dynamic records";

  public @Rule ImpermanentDatabaseRule dbRule = new ImpermanentDatabaseRule();

  private Label LABEL1 = DynamicLabel.label("LABEL1");
  private Label LABEL2 = DynamicLabel.label("LABEL2");
  private Label LABEL3 = DynamicLabel.label("LABEL3");

  private Node createNode(
      GraphDatabaseService beansAPI, Map<String, Object> properties, Label... labels) {
    try (Transaction tx = beansAPI.beginTx()) {
      Node node = beansAPI.createNode(labels);
      for (Map.Entry<String, Object> property : properties.entrySet())
        node.setProperty(property.getKey(), property.getValue());
      tx.success();
      return node;
    }
  }

  private Neo4jMatchers.Deferred<Label> labels(final Node myNode) {
    return new Neo4jMatchers.Deferred<Label>(dbRule.getGraphDatabaseService()) {
      @Override
      protected Iterable<Label> manifest() {
        return myNode.getLabels();
      }
    };
  }
}
 @Override
 public void call(GlobalGraphOperations self) {
   self.getAllNodesWithLabel(DynamicLabel.label("Label"));
 }