@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)); }
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; }
private void addLabels(Node node, Label... labels) { try (Transaction tx = dbRule.getGraphDatabaseService().beginTx()) { for (Label label : labels) { node.addLabel(label); } tx.success(); } }
@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 shouldHandleLargeAmountsOfNodesAddedAndRemovedInSameTx() throws Exception { // Given GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI(); int labelsToAdd = 80; int labelsToRemove = 40; // When Node node; try (Transaction tx = db.beginTx()) { node = db.createNode(); // I create a lot of labels, enough to push the store to use two dynamic records for (int l = 0; l < labelsToAdd; l++) { node.addLabel(label("Label-" + l)); } // and I delete some of them, enough to bring the number of dynamic records needed down to 1 for (int l = 0; l < labelsToRemove; l++) { node.removeLabel(label("Label-" + l)); } tx.success(); } // Then try (Transaction ignore = db.beginTx()) { // All the labels remaining should be in the label scan store for (int l = labelsToAdd - 1; l >= labelsToRemove; l--) { Label label = label("Label-" + l); assertThat( "Should have founnd node when looking for label " + label, single(GlobalGraphOperations.at(db).getAllNodesWithLabel(label)), equalTo(node)); } } }