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());
}
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]));
}
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]));
}
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]));
}
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)));
}
Aggregations