use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.
the class LegacyIndexProxy method putIfAbsent.
@Override
public T putIfAbsent(T entity, String key, Object value) {
try (Statement statement = statementContextBridge.get()) {
// Does it already exist?
long existing = single(internalGet(key, value, statement), -1L);
if (existing != -1) {
return entityOf(existing);
}
// No, OK so Grab lock
statement.readOperations().acquireExclusive(LEGACY_INDEX, legacyIndexResourceId(name, key));
// and check again -- now holding an exclusive lock
existing = single(internalGet(key, value, statement), -1L);
if (existing != -1) {
// Someone else created this entry before us just before we got the lock,
// release the lock as we won't be needing it
statement.readOperations().releaseExclusive(LEGACY_INDEX, legacyIndexResourceId(name, key));
return entityOf(existing);
}
internalAdd(entity, key, value, statement);
return null;
} catch (EntityNotFoundException e) {
throw new NotFoundException(format("%s %d not found", type, type.id(entity)), e);
} catch (InvalidTransactionTypeKernelException e) {
throw new ConstraintViolationException(e.getMessage(), e);
} catch (LegacyIndexNotFoundKernelException e) {
throw new RuntimeException(e);
}
}
use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.
the class NodeProxy method getAllProperties.
@Override
public Map<String, Object> getAllProperties() {
try (Statement statement = actions.statement()) {
try (Cursor<NodeItem> node = statement.readOperations().nodeCursorById(nodeId)) {
try (Cursor<PropertyItem> propertyCursor = statement.readOperations().nodeGetProperties(node.get())) {
Map<String, Object> properties = new HashMap<>();
// Get all properties
while (propertyCursor.next()) {
String name = statement.readOperations().propertyKeyGetName(propertyCursor.get().propertyKeyId());
properties.put(name, propertyCursor.get().value());
}
return properties;
}
} catch (EntityNotFoundException e) {
throw new NotFoundException("Node not found", e);
}
} catch (PropertyKeyIdNotFoundKernelException e) {
throw new IllegalStateException("Property key retrieved through kernel API should exist.", e);
}
}
use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.
the class NodeProxy method getProperty.
@Override
public Object getProperty(String key) throws NotFoundException {
if (null == key) {
throw new IllegalArgumentException("(null) property key is not allowed");
}
try (Statement statement = actions.statement()) {
try {
int propertyKeyId = statement.readOperations().propertyKeyGetForName(key);
if (propertyKeyId == KeyReadOperations.NO_SUCH_PROPERTY_KEY) {
throw new NotFoundException(format("No such property, '%s'.", key));
}
Object value = statement.readOperations().nodeGetProperty(nodeId, propertyKeyId);
if (value == null) {
throw new PropertyNotFoundException(propertyKeyId, EntityType.NODE, nodeId);
}
return value;
} catch (EntityNotFoundException | PropertyNotFoundException e) {
throw new NotFoundException(e.getUserMessage(new StatementTokenNameLookup(statement.readOperations())), e);
}
}
}
use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.
the class NodeProxy method getDegree.
@Override
public int getDegree(RelationshipType type) {
try (Statement statement = actions.statement()) {
ReadOperations ops = statement.readOperations();
int typeId = ops.relationshipTypeGetForName(type.name());
if (typeId == NO_ID) {
// This type doesn't even exist. Return 0
return 0;
}
return ops.nodeGetDegree(nodeId, Direction.BOTH, typeId);
} catch (EntityNotFoundException e) {
throw new NotFoundException("Node not found.", e);
}
}
use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.
the class RelationshipCreateDeleteLockOrderingIT method shouldNotDeadlockWhenConcurrentCreateAndDeleteRelationships.
@Test
public void shouldNotDeadlockWhenConcurrentCreateAndDeleteRelationships() throws Throwable {
// GIVEN (A) -[R]-> (B)
final Node a;
final Node b;
try (Transaction tx = db.beginTx()) {
(a = db.createNode()).createRelationshipTo(b = db.createNode(), MyRelTypes.TEST);
tx.success();
}
// WHEN
Race race = new Race();
// a bunch of deleters
for (int i = 0; i < 30; i++) {
race.addContestant(new Runnable() {
@Override
public void run() {
try (Transaction tx = db.beginTx()) {
Node node = random.nextBoolean() ? a : b;
for (Relationship relationship : node.getRelationships()) {
try {
relationship.delete();
} catch (NotFoundException e) {
// This is OK and expected since there are multiple threads deleting
assertTrue(e.getMessage().contains("already deleted"));
}
}
tx.success();
}
}
});
}
// a bunch of creators
for (int i = 0; i < 30; i++) {
race.addContestant(new Runnable() {
@Override
public void run() {
try (Transaction tx = db.beginTx()) {
boolean order = random.nextBoolean();
Node start = order ? a : b;
Node end = order ? b : a;
start.createRelationshipTo(end, MyRelTypes.TEST);
tx.success();
}
}
});
}
// THEN there should be no thread throwing exception, especially DeadlockDetectedException
race.go();
}
Aggregations