Search in sources :

Example 21 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project neo4j by neo4j.

the class Neo4jJsonCodecTest method testPathWriting.

@Test
public void testPathWriting() throws IOException {
    //Given
    Path path = mock(Path.class);
    PropertyContainer propertyContainer = mock(PropertyContainer.class);
    when(propertyContainer.getAllProperties()).thenThrow(RuntimeException.class);
    when(path.iterator()).thenReturn(Arrays.asList(propertyContainer).listIterator());
    //When
    try {
        jsonCodec.writeValue(jsonGenerator, path);
    } catch (Exception ignored) {
    }
    //Then
    verify(jsonGenerator, times(1)).writeEndArray();
}
Also used : Path(org.neo4j.graphdb.Path) PropertyContainer(org.neo4j.graphdb.PropertyContainer) IOException(java.io.IOException) Test(org.junit.Test)

Example 22 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project neo4j by neo4j.

the class PropertyContainerProxyTest method shouldListAllProperties.

@Test
public void shouldListAllProperties() {
    // Given
    Map<String, Object> properties = new HashMap<>();
    properties.put("boolean", true);
    properties.put("short_string", "abc");
    properties.put("string", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW" + "XYZabcdefghijklmnopqrstuvwxyz");
    properties.put("long", Long.MAX_VALUE);
    properties.put("short_array", new long[] { 1, 2, 3, 4 });
    properties.put("array", new long[] { Long.MAX_VALUE - 1, Long.MAX_VALUE - 2, Long.MAX_VALUE - 3, Long.MAX_VALUE - 4, Long.MAX_VALUE - 5, Long.MAX_VALUE - 6, Long.MAX_VALUE - 7, Long.MAX_VALUE - 8, Long.MAX_VALUE - 9, Long.MAX_VALUE - 10, Long.MAX_VALUE - 11 });
    long containerId;
    try (Transaction tx = db.beginTx()) {
        containerId = createPropertyContainer();
        PropertyContainer container = lookupPropertyContainer(containerId);
        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            container.setProperty(entry.getKey(), entry.getValue());
        }
        tx.success();
    }
    // When
    Map<String, Object> listedProperties;
    try (Transaction tx = db.beginTx()) {
        listedProperties = lookupPropertyContainer(containerId).getAllProperties();
        tx.success();
    }
    // Then
    assertEquals(properties.size(), listedProperties.size());
    for (String key : properties.keySet()) {
        assertObjectOrArrayEquals(properties.get(key), listedProperties.get(key));
    }
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Transaction(org.neo4j.graphdb.Transaction) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 23 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project neo4j-apoc-procedures by neo4j-contrib.

the class Atomic method concat.

/**
 * concat a property's value
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.atomic.concat(node/relatonship,propertyName,string) Concats the property's value with the 'string' value")
public Stream<AtomicResults> concat(@Name("container") Object container, @Name("propertyName") String property, @Name("string") String string, @Name(value = "times", defaultValue = "5") Long times) {
    checkIsPropertyContainer(container);
    PropertyContainer propertyContainer;
    final String[] newValue = new String[1];
    final String[] oldValue = new String[1];
    propertyContainer = (PropertyContainer) container;
    final ExecutionContext executionContext = new ExecutionContext(db, propertyContainer, property);
    retry(executionContext, (context) -> {
        oldValue[0] = propertyContainer.getProperty(property).toString();
        newValue[0] = oldValue[0].toString().concat(string);
        propertyContainer.setProperty(property, newValue[0]);
        return context.propertyContainer.getProperty(property);
    }, times);
    return Stream.of(new AtomicResults(propertyContainer, property, oldValue[0], newValue[0]));
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer)

Example 24 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project neo4j-apoc-procedures by neo4j-contrib.

the class Atomic method add.

/**
 * increment a property's value
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.atomic.add(node/relatonship,propertyName,number) Sums the property's value with the 'number' value ")
public Stream<AtomicResults> add(@Name("container") Object container, @Name("propertyName") String property, @Name("number") Number number, @Name(value = "times", defaultValue = "5") Long times) {
    checkIsPropertyContainer(container);
    PropertyContainer propertyContainer;
    final Number[] newValue = new Number[1];
    final Number[] oldValue = new Number[1];
    propertyContainer = (PropertyContainer) container;
    final ExecutionContext executionContext = new ExecutionContext(db, propertyContainer, property);
    retry(executionContext, (context) -> {
        oldValue[0] = (Number) propertyContainer.getProperty(property);
        newValue[0] = AtomicUtils.sum((Number) propertyContainer.getProperty(property), number);
        propertyContainer.setProperty(property, newValue[0]);
        return context.propertyContainer.getProperty(property);
    }, times);
    return Stream.of(new AtomicResults(propertyContainer, property, oldValue[0], newValue[0]));
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer)

Example 25 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project graphdb by neo4j-attic.

the class Dijkstra method getPath.

/**
 * @return One of the shortest paths found or null.
 */
public List<PropertyContainer> getPath() {
    if (startNode == null || endNode == null) {
        throw new RuntimeException("Start or end node undefined.");
    }
    calculate();
    if (foundPathsMiddleNodes == null || foundPathsMiddleNodes.size() == 0) {
        return null;
    }
    Node middleNode = foundPathsMiddleNodes.iterator().next();
    LinkedList<PropertyContainer> path = new LinkedList<PropertyContainer>();
    path.addAll(Util.constructSinglePathToNode(middleNode, predecessors1, true, false));
    path.addAll(Util.constructSinglePathToNode(middleNode, predecessors2, false, true));
    return path;
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Node(org.neo4j.graphdb.Node) LinkedList(java.util.LinkedList)

Aggregations

PropertyContainer (org.neo4j.graphdb.PropertyContainer)36 Test (org.junit.Test)11 Node (org.neo4j.graphdb.Node)11 LinkedList (java.util.LinkedList)8 Relationship (org.neo4j.graphdb.Relationship)7 Transaction (org.neo4j.graphdb.Transaction)6 HashSet (java.util.HashSet)4 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 GraphDatabaseAPI (org.neo4j.kernel.GraphDatabaseAPI)3 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)3 HashMap (java.util.HashMap)2 SystemException (javax.transaction.SystemException)2 NotFoundException (org.neo4j.graphdb.NotFoundException)2 Path (org.neo4j.graphdb.Path)2 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)2 ArrayBackedList (apoc.util.ArrayBackedList)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1