use of org.janusgraph.core.JanusGraphVertexProperty in project janusgraph by JanusGraph.
the class JanusGraphSchemaVertex method getDefinition.
@Override
public TypeDefinitionMap getDefinition() {
TypeDefinitionMap def = definition;
if (def == null) {
def = new TypeDefinitionMap();
Iterable<JanusGraphVertexProperty> ps;
if (isLoaded()) {
StandardJanusGraphTx tx = tx();
ps = (Iterable) RelationConstructor.readRelation(this, tx.getGraph().getSchemaCache().getSchemaRelations(longId(), BaseKey.SchemaDefinitionProperty, Direction.OUT), tx);
} else {
ps = query().type(BaseKey.SchemaDefinitionProperty).properties();
}
for (JanusGraphVertexProperty property : ps) {
TypeDefinitionDescription desc = property.valueOrNull(BaseKey.SchemaDefinitionDesc);
Preconditions.checkArgument(desc != null && desc.getCategory().isProperty());
def.setValue(desc.getCategory(), property.value());
}
assert def.size() > 0;
definition = def;
}
assert def != null;
return def;
}
use of org.janusgraph.core.JanusGraphVertexProperty in project janusgraph by JanusGraph.
the class AbstractVertex method property.
@Override
public <V> JanusGraphVertexProperty<V> property(@Nullable final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) {
PropertyKey propertyKey = tx().getOrCreatePropertyKey(key, value, cardinality);
if (propertyKey == null) {
return JanusGraphVertexProperty.empty();
}
VertexProperty.Cardinality vCardinality = cardinality == null ? propertyKey.cardinality().convert() : cardinality;
if (value == null) {
if (vCardinality.equals(VertexProperty.Cardinality.single)) {
// putting null value with SINGLE cardinality is equivalent to removing existing value
properties(key).forEachRemaining(Property::remove);
} else {
// simply ignore this mutation
assert vCardinality.equals(VertexProperty.Cardinality.list) || vCardinality.equals(VertexProperty.Cardinality.set);
}
return JanusGraphVertexProperty.empty();
}
JanusGraphVertexProperty<V> p = tx().addProperty(vCardinality, it(), propertyKey, value);
ElementHelper.attachProperties(p, keyValues);
return p;
}
use of org.janusgraph.core.JanusGraphVertexProperty in project janusgraph by JanusGraph.
the class StandardJanusGraphTx method removeRelation.
public void removeRelation(InternalRelation relation) {
Preconditions.checkArgument(!relation.isRemoved());
relation = relation.it();
for (int i = 0; i < relation.getLen(); i++) verifyWriteAccess(relation.getVertex(i));
// Delete from Vertex
for (int i = 0; i < relation.getLen(); i++) {
InternalVertex vertex = relation.getVertex(i);
vertex.removeRelation(relation);
if (!vertex.isNew()) {
vertexCache.add(vertex, vertex.longId());
}
}
// Update transaction data structures
if (relation.isNew()) {
addedRelations.remove(relation);
if (TypeUtil.hasSimpleInternalVertexKeyIndex(relation))
newVertexIndexEntries.remove((JanusGraphVertexProperty) relation);
} else {
Preconditions.checkArgument(relation.isLoaded());
Map<Long, InternalRelation> result = deletedRelations;
if (result == EMPTY_DELETED_RELATIONS) {
if (config.isSingleThreaded()) {
deletedRelations = result = new HashMap<>();
} else {
synchronized (this) {
result = deletedRelations;
if (result == EMPTY_DELETED_RELATIONS)
deletedRelations = result = new ConcurrentHashMap<>();
}
}
}
result.put(relation.longId(), relation);
}
}
use of org.janusgraph.core.JanusGraphVertexProperty in project janusgraph by JanusGraph.
the class StandardJanusGraphTx method makeSchemaVertex.
/*
* ------------------------------------ Schema Handling ------------------------------------
*/
public final JanusGraphSchemaVertex makeSchemaVertex(JanusGraphSchemaCategory schemaCategory, String name, TypeDefinitionMap definition) {
verifyOpen();
Preconditions.checkArgument(!schemaCategory.hasName() || StringUtils.isNotBlank(name), "Need to provide a valid name for type [%s]", schemaCategory);
schemaCategory.verifyValidDefinition(definition);
JanusGraphSchemaVertex schemaVertex;
if (schemaCategory.isRelationType()) {
if (schemaCategory == JanusGraphSchemaCategory.PROPERTYKEY) {
schemaVertex = new PropertyKeyVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.UserPropertyKey, temporaryIds.nextID()), ElementLifeCycle.New);
} else {
assert schemaCategory == JanusGraphSchemaCategory.EDGELABEL;
schemaVertex = new EdgeLabelVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.UserEdgeLabel, temporaryIds.nextID()), ElementLifeCycle.New);
}
} else if (schemaCategory == JanusGraphSchemaCategory.VERTEXLABEL) {
schemaVertex = new VertexLabelVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.GenericSchemaType, temporaryIds.nextID()), ElementLifeCycle.New);
} else {
schemaVertex = new JanusGraphSchemaVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.GenericSchemaType, temporaryIds.nextID()), ElementLifeCycle.New);
}
graph.assignID(schemaVertex, BaseVertexLabel.DEFAULT_VERTEXLABEL);
Preconditions.checkArgument(schemaVertex.longId() > 0);
if (schemaCategory.hasName())
addProperty(schemaVertex, BaseKey.SchemaName, schemaCategory.getSchemaName(name));
addProperty(schemaVertex, BaseKey.VertexExists, Boolean.TRUE);
addProperty(schemaVertex, BaseKey.SchemaCategory, schemaCategory);
updateSchemaVertex(schemaVertex);
for (Map.Entry<TypeDefinitionCategory, Object> def : definition.entrySet()) {
JanusGraphVertexProperty p = addProperty(schemaVertex, BaseKey.SchemaDefinitionProperty, def.getValue());
p.property(BaseKey.SchemaDefinitionDesc.name(), TypeDefinitionDescription.of(def.getKey()));
}
vertexCache.add(schemaVertex, schemaVertex.longId());
if (schemaCategory.hasName())
newTypeCache.put(schemaCategory.getSchemaName(name), schemaVertex.longId());
return schemaVertex;
}
use of org.janusgraph.core.JanusGraphVertexProperty 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();
}
}
Aggregations