Search in sources :

Example 6 with IndexedEntityRepresentation

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();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) IndexedEntityRepresentation(org.neo4j.server.rest.repr.IndexedEntityRepresentation) NodeRepresentationTest(org.neo4j.server.rest.repr.NodeRepresentationTest) RelationshipRepresentationTest(org.neo4j.server.rest.repr.RelationshipRepresentationTest) Test(org.junit.Test)

Example 7 with IndexedEntityRepresentation

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();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) IndexedEntityRepresentation(org.neo4j.server.rest.repr.IndexedEntityRepresentation) NodeRepresentationTest(org.neo4j.server.rest.repr.NodeRepresentationTest) RelationshipRepresentationTest(org.neo4j.server.rest.repr.RelationshipRepresentationTest) Test(org.junit.Test)

Example 8 with IndexedEntityRepresentation

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()));
}
Also used : NodeIndexRepresentation(org.neo4j.server.rest.repr.NodeIndexRepresentation) Node(org.neo4j.graphdb.Node) IndexedEntityRepresentation(org.neo4j.server.rest.repr.IndexedEntityRepresentation)

Example 9 with IndexedEntityRepresentation

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);
}
Also used : NodeIndexRepresentation(org.neo4j.server.rest.repr.NodeIndexRepresentation) Node(org.neo4j.graphdb.Node) IndexedEntityRepresentation(org.neo4j.server.rest.repr.IndexedEntityRepresentation) InvalidArgumentsException(org.neo4j.server.rest.repr.InvalidArgumentsException) Map(java.util.Map)

Example 10 with IndexedEntityRepresentation

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()));
}
Also used : RelationshipIndexRepresentation(org.neo4j.server.rest.repr.RelationshipIndexRepresentation) Relationship(org.neo4j.graphdb.Relationship) IndexedEntityRepresentation(org.neo4j.server.rest.repr.IndexedEntityRepresentation)

Aggregations

IndexedEntityRepresentation (org.neo4j.server.rest.repr.IndexedEntityRepresentation)11 Transaction (org.neo4j.graphdb.Transaction)5 Test (org.junit.Test)4 NodeIndexRepresentation (org.neo4j.server.rest.repr.NodeIndexRepresentation)4 NodeRepresentationTest (org.neo4j.server.rest.repr.NodeRepresentationTest)4 RelationshipIndexRepresentation (org.neo4j.server.rest.repr.RelationshipIndexRepresentation)4 RelationshipRepresentationTest (org.neo4j.server.rest.repr.RelationshipRepresentationTest)4 Node (org.neo4j.graphdb.Node)3 Relationship (org.neo4j.graphdb.Relationship)3 NotFoundException (org.neo4j.graphdb.NotFoundException)2 IterableWrapper (org.neo4j.helpers.collection.IterableWrapper)2 EndNodeNotFoundException (org.neo4j.server.rest.domain.EndNodeNotFoundException)2 StartNodeNotFoundException (org.neo4j.server.rest.domain.StartNodeNotFoundException)2 ConstraintDefinitionRepresentation (org.neo4j.server.rest.repr.ConstraintDefinitionRepresentation)2 DatabaseRepresentation (org.neo4j.server.rest.repr.DatabaseRepresentation)2 IndexDefinitionRepresentation (org.neo4j.server.rest.repr.IndexDefinitionRepresentation)2 IndexRepresentation (org.neo4j.server.rest.repr.IndexRepresentation)2 InvalidArgumentsException (org.neo4j.server.rest.repr.InvalidArgumentsException)2 ListRepresentation (org.neo4j.server.rest.repr.ListRepresentation)2 NodeIndexRootRepresentation (org.neo4j.server.rest.repr.NodeIndexRootRepresentation)2