use of org.janusgraph.graphdb.transaction.lock.TransactionLock in project janusgraph by JanusGraph.
the class StandardJanusGraphTx method getLock.
private TransactionLock getLock(final LockTuple la) {
if (config.isSingleThreaded())
return FakeLock.INSTANCE;
Map<LockTuple, TransactionLock> result = uniqueLocks;
if (result == UNINITIALIZED_LOCKS) {
Preconditions.checkArgument(!config.isSingleThreaded());
synchronized (this) {
result = uniqueLocks;
if (result == UNINITIALIZED_LOCKS)
uniqueLocks = result = new ConcurrentHashMap<>();
}
}
// TODO: clean out no longer used locks from uniqueLocks when it grows to large (use ReadWriteLock to protect against race conditions)
TransactionLock lock = new ReentrantTransactionLock();
TransactionLock existingLock = result.putIfAbsent(la, lock);
return (existingLock == null) ? lock : existingLock;
}
use of org.janusgraph.graphdb.transaction.lock.TransactionLock in project janusgraph by JanusGraph.
the class StandardJanusGraphTx method addEdge.
public JanusGraphEdge addEdge(Long id, JanusGraphVertex outVertex, JanusGraphVertex inVertex, EdgeLabel label) {
verifyWriteAccess(outVertex, inVertex);
outVertex = ((InternalVertex) outVertex).it();
inVertex = ((InternalVertex) inVertex).it();
Preconditions.checkNotNull(label);
checkConnectionConstraintOrCreateConnectionConstraint(outVertex, inVertex, label);
Multiplicity multiplicity = label.multiplicity();
TransactionLock uniqueLock = getUniquenessLock(outVertex, (InternalRelationType) label, inVertex);
uniqueLock.lock(LOCK_TIMEOUT);
try {
// Check uniqueness
if (config.hasVerifyUniqueness()) {
if (multiplicity == Multiplicity.SIMPLE) {
if (!Iterables.isEmpty(query(outVertex).type(label).direction(Direction.OUT).adjacent(inVertex).edges()))
throw new SchemaViolationException("An edge with the given label already exists between the pair of vertices and the label [%s] is simple", label.name());
}
if (multiplicity.isUnique(Direction.OUT)) {
if (!Iterables.isEmpty(query(outVertex).type(label).direction(Direction.OUT).edges()))
throw new SchemaViolationException("An edge with the given label already exists on the out-vertex and the label [%s] is out-unique", label.name());
}
if (multiplicity.isUnique(Direction.IN)) {
if (!Iterables.isEmpty(query(inVertex).type(label).direction(Direction.IN).edges()))
throw new SchemaViolationException("An edge with the given label already exists on the in-vertex and the label [%s] is in-unique", label.name());
}
}
long edgeId = id == null ? IDManager.getTemporaryRelationID(temporaryIds.nextID()) : id;
StandardEdge edge = new StandardEdge(edgeId, label, (InternalVertex) outVertex, (InternalVertex) inVertex, ElementLifeCycle.New);
if (config.hasAssignIDsImmediately() && id == null)
graph.assignID(edge);
connectRelation(edge);
return edge;
} finally {
uniqueLock.unlock();
}
}
use of org.janusgraph.graphdb.transaction.lock.TransactionLock in project janusgraph by JanusGraph.
the class StandardJanusGraphTx method addProperty.
public JanusGraphVertexProperty addProperty(VertexProperty.Cardinality cardinality, JanusGraphVertex vertex, PropertyKey key, Object value, Long id) {
if (key.cardinality().convert() != cardinality && cardinality != VertexProperty.Cardinality.single)
throw new SchemaViolationException("Key is defined for %s cardinality which conflicts with specified: %s", key.cardinality(), cardinality);
verifyWriteAccess(vertex);
Preconditions.checkArgument(!(key instanceof ImplicitKey), "Cannot create a property of implicit type: %s", key.name());
vertex = ((InternalVertex) vertex).it();
Preconditions.checkNotNull(key);
checkPropertyConstraintForVertexOrCreatePropertyConstraint(vertex, key);
final Object normalizedValue = verifyAttribute(key, value);
Cardinality keyCardinality = key.cardinality();
// Determine unique indexes
final List<IndexLockTuple> uniqueIndexTuples = new ArrayList<>();
for (CompositeIndexType index : TypeUtil.getUniqueIndexes(key)) {
IndexSerializer.IndexRecords matches = IndexSerializer.indexMatches(vertex, index, key, normalizedValue);
for (Object[] match : matches.getRecordValues()) uniqueIndexTuples.add(new IndexLockTuple(index, match));
}
TransactionLock uniqueLock = getUniquenessLock(vertex, (InternalRelationType) key, normalizedValue);
// Add locks for unique indexes
for (IndexLockTuple lockTuple : uniqueIndexTuples) uniqueLock = new CombinerLock(uniqueLock, getLock(lockTuple), times);
uniqueLock.lock(LOCK_TIMEOUT);
try {
// //Check vertex-centric uniqueness -> this doesn't really make sense to check
// if (config.hasVerifyUniqueness()) {
// if (cardinality == Cardinality.SINGLE) {
// if (!Iterables.isEmpty(query(vertex).type(key).properties()))
// throw new SchemaViolationException("A property with the given key [%s] already exists on the vertex [%s] and the property key is defined as single-valued", key.name(), vertex);
// }
// if (cardinality == Cardinality.SET) {
// if (!Iterables.isEmpty(Iterables.filter(query(vertex).type(key).properties(), new Predicate<JanusGraphVertexProperty>() {
// @Override
// public boolean apply(@Nullable JanusGraphVertexProperty janusgraphProperty) {
// return normalizedValue.equals(janusgraphProperty.value());
// }
// })))
// throw new SchemaViolationException("A property with the given key [%s] and value [%s] already exists on the vertex and the property key is defined as set-valued", key.name(), normalizedValue);
// }
// }
long propId = id == null ? IDManager.getTemporaryRelationID(temporaryIds.nextID()) : id;
StandardVertexProperty prop = new StandardVertexProperty(propId, key, (InternalVertex) vertex, normalizedValue, ElementLifeCycle.New);
if (config.hasAssignIDsImmediately() && id == null)
graph.assignID(prop);
// Delete properties if the cardinality is restricted
if (cardinality == VertexProperty.Cardinality.single || cardinality == VertexProperty.Cardinality.set) {
Consumer<JanusGraphVertexProperty> propertyRemover = JanusGraphVertexProperty.getRemover(cardinality, normalizedValue);
if ((!config.hasVerifyUniqueness() || ((InternalRelationType) key).getConsistencyModifier() != ConsistencyModifier.LOCK) && !TypeUtil.hasAnyIndex(key) && cardinality == keyCardinality.convert()) {
// Only delete in-memory so as to not trigger a read from the database which isn't necessary because we will overwrite blindly
// We need to label the new property as "upsert", so that in case property deletion happens, we not only delete this new
// in-memory property, but also read from database to delete the old value (if exists)
((InternalVertex) vertex).getAddedRelations(p -> p.getType().equals(key)).forEach(p -> propertyRemover.accept((JanusGraphVertexProperty) p));
prop.setUpsert(true);
} else {
((InternalVertex) vertex).query().types(key).properties().forEach(propertyRemover);
}
}
// Check index uniqueness
if (config.hasVerifyUniqueness()) {
// Check all unique indexes
for (IndexLockTuple lockTuple : uniqueIndexTuples) {
if (!Iterables.isEmpty(IndexHelper.getQueryResults(lockTuple.getIndex(), lockTuple.getAll(), this)))
throw new SchemaViolationException("Adding this property for key [%s] and value [%s] violates a uniqueness constraint [%s]", key.name(), normalizedValue, lockTuple.getIndex());
}
}
connectRelation(prop);
return prop;
} finally {
uniqueLock.unlock();
}
}
use of org.janusgraph.graphdb.transaction.lock.TransactionLock in project janusgraph by JanusGraph.
the class StandardJanusGraphTx method getUniquenessLock.
private TransactionLock getUniquenessLock(final JanusGraphVertex out, final InternalRelationType type, final Object in) {
Multiplicity multiplicity = type.multiplicity();
TransactionLock uniqueLock = FakeLock.INSTANCE;
if (config.hasVerifyUniqueness() && multiplicity.isConstrained()) {
uniqueLock = null;
if (multiplicity == Multiplicity.SIMPLE) {
uniqueLock = getLock(out, type, in);
} else {
for (Direction dir : Direction.proper) {
if (multiplicity.isUnique(dir)) {
TransactionLock lock = getLock(dir == Direction.OUT ? out : in, type, dir);
if (uniqueLock == null)
uniqueLock = lock;
else
uniqueLock = new CombinerLock(uniqueLock, lock, times);
}
}
}
}
assert uniqueLock != null;
return uniqueLock;
}
Aggregations