Search in sources :

Example 11 with PropertyContainer

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

the class TransactionProvidingApp method printPath.

protected void printPath(Path path, boolean quietPrint, Session session, Output out) throws RemoteException, ShellException {
    StringBuilder builder = new StringBuilder();
    Node currentNode = null;
    for (PropertyContainer entity : path) {
        String display;
        if (entity instanceof Relationship) {
            display = quietPrint ? "" : getDisplayName(getServer(), session, (Relationship) entity, false, true);
            display = withArrows((Relationship) entity, display, currentNode);
        } else {
            currentNode = (Node) entity;
            display = getDisplayName(getServer(), session, currentNode, true);
        }
        builder.append(display);
    }
    out.println(builder.toString());
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) ShellException.stackTraceAsString(org.neo4j.shell.ShellException.stackTraceAsString)

Example 12 with PropertyContainer

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

the class Atomic method subtract.

/**
 * decrement a property's value
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.atomic.subtract(node/relatonship,propertyName,number) Subtracts the 'number' value to the property's value")
public Stream<AtomicResults> subtract(@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.sub((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 13 with PropertyContainer

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

the class Atomic method insert.

/**
 * insert a value into an array property value
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.atomic.insert(node/relatonship,propertyName,position,value) insert a value into the property's array value at 'position'")
public Stream<AtomicResults> insert(@Name("container") Object container, @Name("propertyName") String property, @Name("position") Long position, @Name("value") Object value, @Name(value = "times", defaultValue = "5") Long times) throws ClassNotFoundException {
    checkIsPropertyContainer(container);
    PropertyContainer propertyContainer;
    final Object[] oldValue = new Object[1];
    final Object[] newValue = new Object[1];
    propertyContainer = (PropertyContainer) container;
    final ExecutionContext executionContext = new ExecutionContext(db, propertyContainer, property);
    retry(executionContext, (context) -> {
        oldValue[0] = propertyContainer.getProperty(property);
        List<Object> values = insertValueIntoArray(propertyContainer.getProperty(property), position, value);
        Class clazz;
        try {
            clazz = Class.forName(values.toArray()[0].getClass().getName());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        newValue[0] = Array.newInstance(clazz, values.size());
        try {
            System.arraycopy(values.toArray(), 0, newValue[0], 0, values.size());
        } catch (Exception e) {
            String message = "Property's array value has type: " + values.toArray()[0].getClass().getName() + ", and your value to insert has type: " + value.getClass().getName();
            throw new ArrayStoreException(message);
        }
        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 14 with PropertyContainer

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

the class Atomic method remove.

/**
 * remove a value into an array property value
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.atomic.remove(node/relatonship,propertyName,position) remove the element at position 'position'")
public Stream<AtomicResults> remove(@Name("container") Object container, @Name("propertyName") String property, @Name("position") Long position, @Name(value = "times", defaultValue = "5") Long times) throws ClassNotFoundException {
    checkIsPropertyContainer(container);
    PropertyContainer propertyContainer;
    final Object[] oldValue = new Object[1];
    final Object[] newValue = new Object[1];
    propertyContainer = (PropertyContainer) container;
    final ExecutionContext executionContext = new ExecutionContext(db, propertyContainer, property);
    retry(executionContext, (context) -> {
        Object[] arrayBackedList = new ArrayBackedList(propertyContainer.getProperty(property)).toArray();
        oldValue[0] = arrayBackedList;
        if (position > arrayBackedList.length || position < 0) {
            throw new RuntimeException("Attention your position out of range or higher than array length, that is " + arrayBackedList.length);
        }
        Object[] newArray = ArrayUtils.addAll(Arrays.copyOfRange(arrayBackedList, 0, position.intValue()), Arrays.copyOfRange(arrayBackedList, position.intValue() + 1, arrayBackedList.length));
        Class clazz;
        try {
            clazz = Class.forName(arrayBackedList[0].getClass().getName());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        /*it's not possible to return directly the newArray, we have to create a new array with the specific class*/
        newValue[0] = Array.newInstance(clazz, newArray.length);
        System.arraycopy(newArray, 0, newValue[0], 0, newArray.length);
        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) ArrayBackedList(apoc.util.ArrayBackedList)

Example 15 with PropertyContainer

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

the class Atomic method update.

/**
 * update the property's value
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.atomic.update(node/relatonship,propertyName,updateOperation) update a property's value with a cypher operation (ex. \"n.prop1+n.prop2\")")
public Stream<AtomicResults> update(@Name("container") Object container, @Name("propertyName") String property, @Name("operation") String operation, @Name(value = "times", defaultValue = "5") Long times) throws InterruptedException {
    checkIsPropertyContainer(container);
    PropertyContainer propertyContainer = (PropertyContainer) container;
    final Object[] oldValue = new Object[1];
    final ExecutionContext executionContext = new ExecutionContext(db, propertyContainer, property);
    retry(executionContext, (context) -> {
        oldValue[0] = propertyContainer.getProperty(property);
        String statement = "WITH {container} as n with n set n." + property + "=" + operation + ";";
        Map<String, Object> properties = MapUtil.map("container", propertyContainer);
        return context.db.execute(statement, properties);
    }, times);
    return Stream.of(new AtomicResults(propertyContainer, property, oldValue[0], propertyContainer.getProperty(property)));
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer)

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