use of org.janusgraph.graphdb.types.IndexType in project janusgraph by JanusGraph.
the class ManagementSystem method setTypeModifier.
private void setTypeModifier(final JanusGraphSchemaElement element, final ModifierType modifierType, final Object value) {
Preconditions.checkArgument(element != null, "null schema element");
TypeDefinitionCategory cat = modifierType.getCategory();
if (cat.hasDataType() && null != value) {
Preconditions.checkArgument(cat.getDataType().equals(value.getClass()), "modifier value is not of expected type " + cat.getDataType());
}
JanusGraphSchemaVertex typeVertex;
if (element instanceof JanusGraphSchemaVertex) {
typeVertex = (JanusGraphSchemaVertex) element;
} else if (element instanceof JanusGraphIndex) {
IndexType index = ((JanusGraphIndexWrapper) element).getBaseIndex();
assert index instanceof IndexTypeWrapper;
SchemaSource base = ((IndexTypeWrapper) index).getSchemaBase();
typeVertex = (JanusGraphSchemaVertex) base;
} else
throw new IllegalArgumentException("Invalid schema element: " + element);
// remove any pre-existing value for the modifier, or return if an identical value has already been set
for (JanusGraphEdge e : typeVertex.getEdges(TypeDefinitionCategory.TYPE_MODIFIER, Direction.OUT)) {
JanusGraphSchemaVertex v = (JanusGraphSchemaVertex) e.vertex(Direction.IN);
TypeDefinitionMap def = v.getDefinition();
Object existingValue = def.getValue(modifierType.getCategory());
if (null != existingValue) {
if (existingValue.equals(value)) {
// Already has the right value, don't need to do anything
return;
} else {
e.remove();
v.remove();
}
}
}
if (null != value) {
TypeDefinitionMap def = new TypeDefinitionMap();
def.setValue(cat, value);
JanusGraphSchemaVertex cVertex = transaction.makeSchemaVertex(JanusGraphSchemaCategory.TYPE_MODIFIER, null, def);
addSchemaEdge(typeVertex, cVertex, TypeDefinitionCategory.TYPE_MODIFIER, null);
}
updateSchemaVertex(typeVertex);
updatedTypes.add(typeVertex);
}
use of org.janusgraph.graphdb.types.IndexType in project janusgraph by JanusGraph.
the class ManagementSystem method addIndexKey.
@Override
public void addIndexKey(final JanusGraphIndex index, final PropertyKey key, Parameter... parameters) {
Preconditions.checkArgument(index != null && key != null && index instanceof JanusGraphIndexWrapper && !(key instanceof BaseKey), "Need to provide valid index and key");
if (parameters == null)
parameters = new Parameter[0];
IndexType indexType = ((JanusGraphIndexWrapper) index).getBaseIndex();
Preconditions.checkArgument(indexType instanceof MixedIndexType, "Can only add keys to an external index, not %s", index.name());
Preconditions.checkArgument(indexType instanceof IndexTypeWrapper && key instanceof JanusGraphSchemaVertex && ((IndexTypeWrapper) indexType).getSchemaBase() instanceof JanusGraphSchemaVertex);
JanusGraphSchemaVertex indexVertex = (JanusGraphSchemaVertex) ((IndexTypeWrapper) indexType).getSchemaBase();
for (IndexField field : indexType.getFieldKeys()) Preconditions.checkArgument(!field.getFieldKey().equals(key), "Key [%s] has already been added to index %s", key.name(), index.name());
// Assemble parameters
boolean addMappingParameter = !ParameterType.MAPPED_NAME.hasParameter(parameters);
Parameter[] extendedParas = new Parameter[parameters.length + 1 + (addMappingParameter ? 1 : 0)];
System.arraycopy(parameters, 0, extendedParas, 0, parameters.length);
int arrPosition = parameters.length;
if (addMappingParameter)
extendedParas[arrPosition++] = ParameterType.MAPPED_NAME.getParameter(graph.getIndexSerializer().getDefaultFieldName(key, parameters, indexType.getBackingIndexName()));
extendedParas[arrPosition] = ParameterType.STATUS.getParameter(key.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
addSchemaEdge(indexVertex, key, TypeDefinitionCategory.INDEX_FIELD, extendedParas);
updateSchemaVertex(indexVertex);
indexType.resetCache();
// Check to see if the index supports this
if (!graph.getIndexSerializer().supports((MixedIndexType) indexType, ParameterIndexField.of(key, parameters))) {
throw new JanusGraphException("Could not register new index field '" + key.name() + "' with index backend as the data type, cardinality or parameter combination is not supported.");
}
try {
IndexSerializer.register((MixedIndexType) indexType, key, transaction.getTxHandle());
} catch (BackendException e) {
throw new JanusGraphException("Could not register new index field with index backend", e);
}
if (!indexVertex.isNew())
updatedTypes.add(indexVertex);
if (!key.isNew())
updateIndex(index, SchemaAction.REGISTER_INDEX);
}
use of org.janusgraph.graphdb.types.IndexType in project janusgraph by JanusGraph.
the class IndexRepairJob method process.
@Override
public void process(JanusGraphVertex vertex, ScanMetrics metrics) {
try {
BackendTransaction mutator = writeTx.getTxHandle();
if (index instanceof RelationTypeIndex) {
RelationTypeIndexWrapper wrapper = (RelationTypeIndexWrapper) index;
InternalRelationType wrappedType = wrapper.getWrappedType();
EdgeSerializer edgeSerializer = writeTx.getEdgeSerializer();
List<Entry> additions = new ArrayList<>();
for (Object relation : vertex.query().types(indexRelationTypeName).direction(Direction.OUT).relations()) {
InternalRelation janusgraphRelation = (InternalRelation) relation;
for (int pos = 0; pos < janusgraphRelation.getArity(); pos++) {
if (!wrappedType.isUnidirected(Direction.BOTH) && !wrappedType.isUnidirected(EdgeDirection.fromPosition(pos)))
// Directionality is not covered
continue;
Entry entry = edgeSerializer.writeRelation(janusgraphRelation, wrappedType, pos, writeTx);
additions.add(entry);
}
}
StaticBuffer vertexKey = writeTx.getIdInspector().getKey(vertex.longId());
mutator.mutateEdges(vertexKey, additions, KCVSCache.NO_DELETIONS);
metrics.incrementCustom(ADDED_RECORDS_COUNT, additions.size());
} else if (index instanceof JanusGraphIndex) {
IndexType indexType = managementSystem.getSchemaVertex(index).asIndexType();
assert indexType != null;
IndexSerializer indexSerializer = graph.getIndexSerializer();
// Gather elements to index
List<JanusGraphElement> elements;
switch(indexType.getElement()) {
case VERTEX:
elements = ImmutableList.of(vertex);
break;
case PROPERTY:
elements = Lists.newArrayList();
for (JanusGraphVertexProperty p : addIndexSchemaConstraint(vertex.query(), indexType).properties()) {
elements.add(p);
}
break;
case EDGE:
elements = Lists.newArrayList();
for (Object e : addIndexSchemaConstraint(vertex.query().direction(Direction.OUT), indexType).edges()) {
elements.add((JanusGraphEdge) e);
}
break;
default:
throw new AssertionError("Unexpected category: " + indexType.getElement());
}
if (indexType.isCompositeIndex()) {
for (JanusGraphElement element : elements) {
Set<IndexSerializer.IndexUpdate<StaticBuffer, Entry>> updates = indexSerializer.reindexElement(element, (CompositeIndexType) indexType);
for (IndexSerializer.IndexUpdate<StaticBuffer, Entry> update : updates) {
log.debug("Mutating index {}: {}", indexType, update.getEntry());
mutator.mutateIndex(update.getKey(), Lists.newArrayList(update.getEntry()), KCVSCache.NO_DELETIONS);
metrics.incrementCustom(ADDED_RECORDS_COUNT);
}
}
} else {
assert indexType.isMixedIndex();
Map<String, Map<String, List<IndexEntry>>> documentsPerStore = new HashMap<>();
for (JanusGraphElement element : elements) {
indexSerializer.reindexElement(element, (MixedIndexType) indexType, documentsPerStore);
metrics.incrementCustom(DOCUMENT_UPDATES_COUNT);
}
mutator.getIndexTransaction(indexType.getBackingIndexName()).restore(documentsPerStore);
}
} else
throw new UnsupportedOperationException("Unsupported index found: " + index);
} catch (final Exception e) {
managementSystem.rollback();
writeTx.rollback();
metrics.incrementCustom(FAILED_TX);
throw new JanusGraphException(e.getMessage(), e);
}
}
use of org.janusgraph.graphdb.types.IndexType in project janusgraph by JanusGraph.
the class JanusGraphOperationCountingTest method testReadOperations.
@SuppressWarnings("unchecked")
public void testReadOperations(boolean cache) {
metricsPrefix = "testReadOperations" + cache;
resetEdgeCacheCounts();
makeVertexIndexedUniqueKey("uid", Integer.class);
mgmt.setConsistency(mgmt.getGraphIndex("uid"), ConsistencyModifier.LOCK);
finishSchema();
if (cache)
clopen(option(DB_CACHE), true, option(DB_CACHE_CLEAN_WAIT), 0, option(DB_CACHE_TIME), 0);
else
clopen();
JanusGraphTransaction tx = graph.buildTransaction().groupName(metricsPrefix).start();
tx.makePropertyKey("name").dataType(String.class).make();
tx.makeEdgeLabel("knows").make();
tx.makeVertexLabel("person").make();
tx.commit();
verifyStoreMetrics(EDGESTORE_NAME);
verifyLockingOverwrite(3);
verifyStoreMetrics(METRICS_STOREMANAGER_NAME, ImmutableMap.of(M_MUTATE, 1L));
resetMetrics();
metricsPrefix = GraphDatabaseConfiguration.METRICS_SCHEMA_PREFIX_DEFAULT;
resetMetrics();
// Test schema caching
for (int t = 0; t < 10; t++) {
tx = graph.buildTransaction().groupName(metricsPrefix).start();
// Retrieve name by index (one backend call each)
assertTrue(tx.containsRelationType("name"));
assertTrue(tx.containsRelationType("knows"));
assertTrue(tx.containsVertexLabel("person"));
PropertyKey name = tx.getPropertyKey("name");
EdgeLabel knows = tx.getEdgeLabel("knows");
VertexLabel person = tx.getVertexLabel("person");
PropertyKey uid = tx.getPropertyKey("uid");
// Retrieve name as property (one backend call each)
assertEquals("name", name.name());
assertEquals("knows", knows.name());
assertEquals("person", person.name());
assertEquals("uid", uid.name());
// Looking up the definition (one backend call each)
assertEquals(Cardinality.SINGLE, name.cardinality());
assertEquals(Multiplicity.MULTI, knows.multiplicity());
assertFalse(person.isPartitioned());
assertEquals(Integer.class, uid.dataType());
// Retrieving in and out relations for the relation types...
InternalRelationType nameInternal = (InternalRelationType) name;
InternalRelationType knowsInternal = (InternalRelationType) knows;
InternalRelationType uidInternal = (InternalRelationType) uid;
assertNull(nameInternal.getBaseType());
assertNull(knowsInternal.getBaseType());
IndexType index = Iterables.getOnlyElement(uidInternal.getKeyIndexes());
assertEquals(1, index.getFieldKeys().length);
assertEquals(ElementCategory.VERTEX, index.getElement());
assertEquals(ConsistencyModifier.LOCK, ((CompositeIndexType) index).getConsistencyModifier());
assertEquals(1, Iterables.size(uidInternal.getRelationIndexes()));
assertEquals(1, Iterables.size(nameInternal.getRelationIndexes()));
assertEquals(nameInternal, Iterables.getOnlyElement(nameInternal.getRelationIndexes()));
assertEquals(knowsInternal, Iterables.getOnlyElement(knowsInternal.getRelationIndexes()));
// .. and vertex labels
assertEquals(0, ((InternalVertexLabel) person).getTTL());
tx.commit();
// Needs to read on first iteration, after that it doesn't change anymore
verifyStoreMetrics(EDGESTORE_NAME, ImmutableMap.of(M_GET_SLICE, 19L));
verifyStoreMetrics(INDEXSTORE_NAME, ImmutableMap.of(M_GET_SLICE, 4L, /* name, knows, person, uid */
M_ACQUIRE_LOCK, 0L));
}
// Create some graph data
metricsPrefix = "add" + cache;
tx = graph.buildTransaction().groupName(metricsPrefix).start();
JanusGraphVertex v = tx.addVertex(), u = tx.addVertex("person");
v.property(VertexProperty.Cardinality.single, "uid", 1);
u.property(VertexProperty.Cardinality.single, "name", "juju");
Edge e = v.addEdge("knows", u);
e.property("name", "edge");
tx.commit();
verifyStoreMetrics(EDGESTORE_NAME);
verifyLockingOverwrite(1);
for (int i = 1; i <= 30; i++) {
metricsPrefix = "op" + i + cache;
tx = graph.buildTransaction().groupName(metricsPrefix).start();
v = getOnlyElement(tx.query().has("uid", 1).vertices());
assertEquals(1, v.<Integer>value("uid").intValue());
u = getOnlyElement(v.query().direction(Direction.BOTH).labels("knows").vertices());
e = getOnlyElement(u.query().direction(Direction.IN).labels("knows").edges());
assertEquals("juju", u.value("name"));
assertEquals("edge", e.value("name"));
tx.commit();
if (!cache) {
verifyStoreMetrics(EDGESTORE_NAME, ImmutableMap.of(M_GET_SLICE, 4L));
verifyStoreMetrics(INDEXSTORE_NAME, ImmutableMap.of(M_GET_SLICE, 1L));
} else if (i > 20) {
// Needs a couple of iterations for cache to be cleaned
verifyStoreMetrics(EDGESTORE_NAME);
verifyStoreMetrics(INDEXSTORE_NAME);
}
}
}
use of org.janusgraph.graphdb.types.IndexType in project janusgraph by JanusGraph.
the class RelationTypeVertex method getKeyIndexes.
public Iterable<IndexType> getKeyIndexes() {
List<IndexType> result = indexes;
if (result == null) {
ImmutableList.Builder<IndexType> b = ImmutableList.builder();
for (Entry entry : getRelated(TypeDefinitionCategory.INDEX_FIELD, Direction.IN)) {
SchemaSource index = entry.getSchemaType();
b.add(index.asIndexType());
}
result = b.build();
indexes = result;
}
assert result != null;
return result;
}
Aggregations