use of org.neo4j.server.rest.repr.IndexedEntityRepresentation in project neo4j by neo4j.
the class DatabaseActionsTest method shouldIndexNodeOnlyOnce.
@Test
public void shouldIndexNodeOnlyOnce() throws Exception {
long nodeId = graphdbHelper.createNode();
graphdbHelper.createRelationshipIndex("myIndex");
try (Transaction tx = graph.beginTx()) {
Pair<IndexedEntityRepresentation, Boolean> result = actions.getOrCreateIndexedNode("myIndex", "foo", "bar", nodeId, null);
assertThat(result.other(), is(true));
assertThat(serialize(actions.getIndexedNodes("myIndex", "foo", "bar")).size(), is(1));
assertThat(actions.nodeIsIndexed("myIndex", "foo", "bar", nodeId), is(true));
tx.success();
}
try (Transaction tx = graph.beginTx()) {
Pair<IndexedEntityRepresentation, Boolean> result = actions.getOrCreateIndexedNode("myIndex", "foo", "bar", nodeId, null);
assertThat(result.other(), is(false));
assertThat(serialize(actions.getIndexedNodes("myIndex", "foo", "bar")).size(), is(1));
assertThat(actions.nodeIsIndexed("myIndex", "foo", "bar", nodeId), is(true));
tx.success();
}
}
use of org.neo4j.server.rest.repr.IndexedEntityRepresentation in project neo4j by neo4j.
the class DatabaseActionsTest method shouldNotIndexNodeWhenAnotherNodeAlreadyIndexed.
@Test
public void shouldNotIndexNodeWhenAnotherNodeAlreadyIndexed() throws Exception {
graphdbHelper.createRelationshipIndex("myIndex");
try (Transaction tx = graph.beginTx()) {
long nodeId = graphdbHelper.createNode();
Pair<IndexedEntityRepresentation, Boolean> result = actions.getOrCreateIndexedNode("myIndex", "foo", "bar", nodeId, null);
assertThat(result.other(), is(true));
assertThat(serialize(actions.getIndexedNodes("myIndex", "foo", "bar")).size(), is(1));
assertThat(actions.nodeIsIndexed("myIndex", "foo", "bar", nodeId), is(true));
tx.success();
}
try (Transaction tx = graph.beginTx()) {
long nodeId = graphdbHelper.createNode();
Pair<IndexedEntityRepresentation, Boolean> result = actions.getOrCreateIndexedNode("myIndex", "foo", "bar", nodeId, null);
assertThat(result.other(), is(false));
assertThat(serialize(actions.getIndexedNodes("myIndex", "foo", "bar")).size(), is(1));
assertThat(actions.nodeIsIndexed("myIndex", "foo", "bar", nodeId), is(false));
tx.success();
}
}
use of org.neo4j.server.rest.repr.IndexedEntityRepresentation in project neo4j by neo4j.
the class DatabaseActions method addToNodeIndex.
public IndexedEntityRepresentation addToNodeIndex(String indexName, String key, String value, long nodeId) {
Node node = graphDb.getNodeById(nodeId);
Index<Node> index = graphDb.index().forNodes(indexName);
index.add(node, key, value);
return new IndexedEntityRepresentation(node, key, value, new NodeIndexRepresentation(indexName, Collections.<String, String>emptyMap()));
}
use of org.neo4j.server.rest.repr.IndexedEntityRepresentation in project neo4j by neo4j.
the class DatabaseActions method getOrCreateIndexedNode.
public Pair<IndexedEntityRepresentation, Boolean> getOrCreateIndexedNode(String indexName, String key, String value, Long nodeOrNull, Map<String, Object> properties) throws BadInputException, NodeNotFoundException {
assertIsLegalIndexName(indexName);
Node result;
boolean created;
if (nodeOrNull != null) {
if (properties != null) {
throw new InvalidArgumentsException("Cannot specify properties for a new node, " + "when a node to index is specified.");
}
Node node = node(nodeOrNull);
result = graphDb.index().forNodes(indexName).putIfAbsent(node, key, value);
created = result == null;
if (created) {
UniqueNodeFactory factory = new UniqueNodeFactory(indexName, properties);
UniqueEntity<Node> entity = factory.getOrCreateWithOutcome(key, value);
// when given a node id, return as created if that node was newly added to the index
created = entity.entity().getId() == node.getId() || entity.wasCreated();
result = entity.entity();
}
} else {
if (properties != null) {
for (Map.Entry<String, Object> entry : properties.entrySet()) {
entry.setValue(propertySetter.convert(entry.getValue()));
}
}
UniqueNodeFactory factory = new UniqueNodeFactory(indexName, properties);
UniqueEntity<Node> entity = factory.getOrCreateWithOutcome(key, value);
result = entity.entity();
created = entity.wasCreated();
}
return Pair.of(new IndexedEntityRepresentation(result, key, value, new NodeIndexRepresentation(indexName, Collections.<String, String>emptyMap())), created);
}
use of org.neo4j.server.rest.repr.IndexedEntityRepresentation in project neo4j by neo4j.
the class DatabaseActions method addToRelationshipIndex.
public IndexedEntityRepresentation addToRelationshipIndex(String indexName, String key, String value, long relationshipId) {
Relationship relationship = graphDb.getRelationshipById(relationshipId);
Index<Relationship> index = graphDb.index().forRelationships(indexName);
index.add(relationship, key, value);
return new IndexedEntityRepresentation(relationship, key, value, new RelationshipIndexRepresentation(indexName, Collections.<String, String>emptyMap()));
}
Aggregations