use of org.janusgraph.graphdb.relations.StandardVertexProperty 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.relations.StandardVertexProperty in project janusgraph by JanusGraph.
the class StandardJanusGraphTx method addProperty.
public JanusGraphVertexProperty addProperty(VertexProperty.Cardinality cardinality, JanusGraphVertex vertex, PropertyKey key, Object value) {
if (key.cardinality().convert() != cardinality && cardinality != VertexProperty.Cardinality.single)
throw new SchemaViolationException(String.format("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);
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 {
// Delete properties if the cardinality is restricted
if (cardinality == VertexProperty.Cardinality.single || cardinality == VertexProperty.Cardinality.set) {
Consumer<JanusGraphRelation> propertyRemover;
if (cardinality == VertexProperty.Cardinality.single)
propertyRemover = JanusGraphElement::remove;
else
propertyRemover = p -> {
if (((JanusGraphVertexProperty) p).value().equals(normalizedValue))
p.remove();
};
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
((InternalVertex) vertex).getAddedRelations(p -> p.getType().equals(key)).forEach(propertyRemover);
} 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());
}
}
StandardVertexProperty prop = new StandardVertexProperty(IDManager.getTemporaryRelationID(temporaryIds.nextID()), key, (InternalVertex) vertex, normalizedValue, ElementLifeCycle.New);
if (config.hasAssignIDsImmediately())
graph.assignID(prop);
connectRelation(prop);
return prop;
} finally {
uniqueLock.unlock();
}
}
use of org.janusgraph.graphdb.relations.StandardVertexProperty in project janusgraph by JanusGraph.
the class ModificationDeserializer method parseRelation.
public static InternalRelation parseRelation(TransactionLogHeader.Modification modification, StandardJanusGraphTx tx) {
Change state = modification.state;
assert state.isProper();
long outVertexId = modification.outVertexId;
Entry relEntry = modification.relationEntry;
InternalVertex outVertex = tx.getInternalVertex(outVertexId);
// Special relation parsing, compare to {@link RelationConstructor}
RelationCache relCache = tx.getEdgeSerializer().readRelation(relEntry, false, tx);
assert relCache.direction == Direction.OUT;
InternalRelationType type = (InternalRelationType) tx.getExistingRelationType(relCache.typeId);
assert type.getBaseType() == null;
InternalRelation rel;
if (type.isPropertyKey()) {
if (state == Change.REMOVED) {
rel = new StandardVertexProperty(relCache.relationId, (PropertyKey) type, outVertex, relCache.getValue(), ElementLifeCycle.Removed);
} else {
rel = new CacheVertexProperty(relCache.relationId, (PropertyKey) type, outVertex, relCache.getValue(), relEntry);
}
} else {
assert type.isEdgeLabel();
InternalVertex otherVertex = tx.getInternalVertex(relCache.getOtherVertexId());
if (state == Change.REMOVED) {
rel = new StandardEdge(relCache.relationId, (EdgeLabel) type, outVertex, otherVertex, ElementLifeCycle.Removed);
} else {
rel = new CacheEdge(relCache.relationId, (EdgeLabel) type, outVertex, otherVertex, relEntry);
}
}
if (state == Change.REMOVED && relCache.hasProperties()) {
// copy over properties
for (LongObjectCursor<Object> entry : relCache) {
rel.setPropertyDirect(tx.getExistingPropertyKey(entry.key), entry.value);
}
}
return rel;
}
Aggregations