Search in sources :

Example 6 with ArrayBackedList

use of apoc.util.ArrayBackedList in project neo4j-apoc-procedures by neo4j-contrib.

the class AtomicTest method testRemoveLastItemArray.

@Test
public void testRemoveLastItemArray() {
    db.execute("CREATE (p:Person {name:'Tom',age: [40]})");
    Node node = (Node) db.execute("MATCH (n:Person {name:'Tom'}) return n;").next().get("n");
    testCall(db, "CALL apoc.atomic.remove({node},{property},{position},{times})", map("node", node, "property", "age", "position", 0, "times", 5), (r) -> {
    });
    assertEquals(Arrays.asList().toArray(), new ArrayBackedList(db.execute("MATCH (n:Person {name:'Tom'}) RETURN n.age as age;").next().get("age")).toArray());
}
Also used : Node(org.neo4j.graphdb.Node) ArrayBackedList(apoc.util.ArrayBackedList)

Example 7 with ArrayBackedList

use of apoc.util.ArrayBackedList in project neo4j-apoc-procedures by neo4j-contrib.

the class AtomicTest method testRemoveEmptyArray.

@Test(expected = RuntimeException.class)
public void testRemoveEmptyArray() {
    db.execute("CREATE (p:Person {name:'Tom',age: []})");
    Node node = (Node) db.execute("MATCH (n:Person {name:'Tom'}) return n;").next().get("n");
    testCall(db, "CALL apoc.atomic.remove({node},{property},{position},{times})", map("node", node, "property", "age", "position", 5, "times", 5), (r) -> {
    });
    assertEquals(Arrays.asList().toArray(), new ArrayBackedList(db.execute("MATCH (n:Person {name:'Tom'}) RETURN n.age as age;").next().get("age")).toArray());
}
Also used : Node(org.neo4j.graphdb.Node) ArrayBackedList(apoc.util.ArrayBackedList)

Example 8 with ArrayBackedList

use of apoc.util.ArrayBackedList 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 9 with ArrayBackedList

use of apoc.util.ArrayBackedList in project neo4j-apoc-procedures by neo4j-contrib.

the class AtomicTest method testRemoveFirstElementArrayValueLong.

@Test
public void testRemoveFirstElementArrayValueLong() {
    db.execute("CREATE (p:Person {name:'Tom',age: [40,50,60]})");
    Node node = (Node) db.execute("MATCH (n:Person {name:'Tom'}) return n;").next().get("n");
    testCall(db, "CALL apoc.atomic.remove({node},{property},{position},{times})", map("node", node, "property", "age", "position", 0, "times", 5), (r) -> {
    });
    assertEquals(Arrays.asList(50L, 60L).toArray(), new ArrayBackedList(db.execute("MATCH (n:Person {name:'Tom'}) RETURN n.age as age;").next().get("age")).toArray());
}
Also used : Node(org.neo4j.graphdb.Node) ArrayBackedList(apoc.util.ArrayBackedList)

Example 10 with ArrayBackedList

use of apoc.util.ArrayBackedList in project neo4j-apoc-procedures by neo4j-contrib.

the class AtomicTest method testRemoveArrayValueLong.

@Test
public void testRemoveArrayValueLong() {
    db.execute("CREATE (p:Person {name:'Tom',age: [40,50,60]})");
    Node node = (Node) db.execute("MATCH (n:Person {name:'Tom'}) return n;").next().get("n");
    testCall(db, "CALL apoc.atomic.remove({node},{property},{position},{times})", map("node", node, "property", "age", "position", 1, "times", 5), (r) -> {
    });
    assertEquals(Arrays.asList(40L, 60L).toArray(), new ArrayBackedList(db.execute("MATCH (n:Person {name:'Tom'}) RETURN n.age as age;").next().get("age")).toArray());
}
Also used : Node(org.neo4j.graphdb.Node) ArrayBackedList(apoc.util.ArrayBackedList)

Aggregations

ArrayBackedList (apoc.util.ArrayBackedList)11 Node (org.neo4j.graphdb.Node)9 ExecutorService (java.util.concurrent.ExecutorService)2 PropertyContainer (org.neo4j.graphdb.PropertyContainer)1 Relationship (org.neo4j.graphdb.Relationship)1