index = mgmt.getGraphIndex(indexName); } else { indexRelationType = mgmt.getRelationType(indexRelationTypeName); Preconditions.checkArgument(indexRelationType!=null,"Could not find relation type: %s", indexRelationTypeName); index = mgmt.getRelationIndex(indexRelationType,indexName); writeTx = (StandardTitanTx)txb.start(); } catch (final Exception e) { if (null != mgmt && mgmt.isOpen()) mgmt.rollback(); if (writeTx!=null && writeTx.isOpen()) writeTx.rollback();
private TitanGraphIndex createCompositeIndex(String indexName, ElementCategory elementCategory, boolean unique, TitanSchemaType constraint, PropertyKey... keys) { checkIndexName(indexName); Preconditions.checkArgument(keys != null && keys.length > 0, "Need to provide keys to index [%s]", indexName); Preconditions.checkArgument(!unique || elementCategory == ElementCategory.VERTEX, "Unique indexes can only be created on vertices [%s]", indexName); for (int i = 0; i < keys.length; i++) { Parameter[] paras = {ParameterType.INDEX_POSITION.getParameter(i)}; addSchemaEdge(indexVertex, keys[i], TypeDefinitionCategory.INDEX_FIELD, paras); addSchemaEdge(indexVertex, (TitanSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null); updateSchemaVertex(indexVertex); TitanGraphIndexWrapper index = new TitanGraphIndexWrapper(indexVertex.asIndexType()); if (!oneNewKey) updateIndex(index, SchemaAction.REGISTER_INDEX); return index;
private TitanIndex retrieve(ManagementSystem mgmt) { if (relationTypeName == null) return mgmt.getGraphIndex(indexName); else return mgmt.getRelationIndex(mgmt.getRelationType(relationTypeName), indexName); }
public void workerIterationEnd(ScanMetrics metrics) { try { if (null != mgmt && mgmt.isOpen()) mgmt.commit(); if (writeTx!=null && writeTx.isOpen()) writeTx.commit(); metrics.incrementCustom(SUCCESS_TX); } catch (RuntimeException e) { log.error("Transaction commit threw runtime exception:", e); metrics.incrementCustom(FAILED_TX); throw e; } }
public Consumer<ScanMetrics> getIndexJobFinisher(final TitanGraph graph, final SchemaAction action) { Preconditions.checkArgument((graph != null && action != null) || (graph == null && action == null)); return metrics -> { try { if (metrics.get(ScanMetrics.Metric.FAILURE) == 0) { if (action != null) { ManagementSystem mgmt = (ManagementSystem) graph.openManagement(); try { TitanIndex index = retrieve(mgmt); mgmt.updateIndex(index, action); } finally { mgmt.commit(); } } LOGGER.info("Index update job successful for [{}]", IndexIdentifier.this.toString()); } else { LOGGER.error("Index update job unsuccessful for [{}]. Check logs", IndexIdentifier.this.toString()); } } catch (Throwable e) { LOGGER.error("Error encountered when updating index after job finished [" + IndexIdentifier.this.toString() + "]: ", e); } }; } }
private void createIndexes() { ManagementSystem manager = (ManagementSystem) getTitanGraph().openManagement(); if (!manager.containsGraphIndex("vertices") && !manager.containsGraphIndex("allIndex")) { PropertyKey vertexValue = manager.getPropertyKey(VERTEX_VALUE); PropertyKey kindValue = manager.getPropertyKey(KIND); PropertyKey typeValue = manager.getPropertyKey(TYPE); PropertyKey langValue = manager.getPropertyKey(LANG); PropertyKey graphKey = manager.getPropertyKey(EDGE_G); PropertyKey subjectKey = manager.getPropertyKey(EDGE_S); PropertyKey predicateKey = manager.getPropertyKey(EDGE_P); PropertyKey objectKey = manager.getPropertyKey(EDGE_O); TitanGraphIndex vIndex = manager. buildIndex("vertices", Vertex.class). addKey(vertexValue). addKey(kindValue). buildCompositeIndex(); manager. buildIndex("vertices2", Vertex.class). addKey(vertexValue). addKey(kindValue). manager.commit(); String[] indexNames = { "vertices", for (String indexName : indexNames) { try { GraphIndexStatusReport indexStatus = ManagementSystem.awaitGraphIndexStatus(getTitanGraph(), indexName).status(SchemaStatus.REGISTERED).call(); logger.log(Level.INFO, "status for {0} {1}", new Object[]{indexName, indexStatus});
extendedParas[arrPosition++] = ParameterType.STATUS.getParameter(key.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED); addSchemaEdge(indexVertex, key, TypeDefinitionCategory.INDEX_FIELD, extendedParas); updateSchemaVertex(indexVertex); indexType.resetCache(); if (!key.isNew()) updateIndex(index, SchemaAction.REGISTER_INDEX);
"The relation type [%s] has a multiplicity or cardinality constraint in direction [%s] and can therefore not be indexed", type, direction); String composedName = composeRelationTypeIndexName(type, name); StandardRelationTypeMaker maker; if (type.isEdgeLabel()) { addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null); RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType) typeIndex); if (!type.isNew()) updateIndex(index, SchemaAction.REGISTER_INDEX); return index;
/** * Create in the db model a new property and the class it uses. * * @param propertyName Entry name in the model. * @param c Class for the entry. */ private void makeIfNotExistProperty(String propertyName, Class<?> c) { ManagementSystem manager = (ManagementSystem) getTitanGraph().openManagement(); if (!manager.containsPropertyKey(propertyName)) { manager.makePropertyKey(propertyName).dataType(c).cardinality(Cardinality.SINGLE).make(); System.out.println("adding key " + propertyName); manager.commit(); } else { manager.rollback(); } }
private TitanGraphIndex createMixedIndex(String indexName, ElementCategory elementCategory, TitanSchemaType constraint, String backingIndex) { Preconditions.checkArgument(graph.getIndexSerializer().containsIndex(backingIndex), "Unknown external index backend: %s", backingIndex); checkIndexName(indexName); TypeDefinitionMap def = new TypeDefinitionMap(); def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, false); def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory); def.setValue(TypeDefinitionCategory.BACKING_INDEX, backingIndex); def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName); def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, Cardinality.LIST); def.setValue(TypeDefinitionCategory.STATUS, SchemaStatus.ENABLED); TitanSchemaVertex indexVertex = transaction.makeSchemaVertex(TitanSchemaCategory.GRAPHINDEX, indexName, def); Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof TitanSchemaVertex)); if (constraint != null) { addSchemaEdge(indexVertex, (TitanSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null); } updateSchemaVertex(indexVertex); return new TitanGraphIndexWrapper(indexVertex.asIndexType()); }
assertTrue(ManagementSystem.awaitGraphIndexStatus(graph, "theIndex").status(SchemaStatus.REGISTERED) .timeout(TestGraphConfigs.getSchemaConvergenceTime(ChronoUnit.SECONDS), ChronoUnit.SECONDS) .call().getSucceeded());
@Override public boolean containsGraphIndex(String name) { return getGraphIndex(name) != null; }
metrics.incrementCustom(ADDED_RECORDS_COUNT, additions.size()); } else if (index instanceof TitanGraphIndex) { IndexType indexType = mgmt.getSchemaVertex(index).asIndexType(); assert indexType!=null; IndexSerializer indexSerializer = graph.getIndexSerializer(); mgmt.rollback(); writeTx.rollback(); metrics.incrementCustom(FAILED_TX);
def.setValue(cat, value); TitanSchemaVertex cVertex = transaction.makeSchemaVertex(TitanSchemaCategory.TYPE_MODIFIER, null, def); addSchemaEdge(typeVertex, cVertex, TypeDefinitionCategory.TYPE_MODIFIER, null); updateSchemaVertex(typeVertex); updatedTypes.add(typeVertex);
@Override public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, PropertyKey... sortKeys) { return buildRelationTypeIndex(label, name, direction, Order.ASC, sortKeys); }
@Override public void process(StaticBuffer key, Map<SliceQuery, EntryList> entries, ScanMetrics metrics) { //The queries are already tailored enough => everything should be removed try { BackendTransaction mutator = writeTx.getTxHandle(); final List<Entry> deletions; if (entries.size()==1) deletions = Iterables.getOnlyElement(entries.values()); else { int size = IteratorUtils.stream(entries.values().iterator()).map( e -> e.size()).reduce(0, (x,y) -> x+y); deletions = new ArrayList<>(size); entries.values().forEach(e -> deletions.addAll(e)); } metrics.incrementCustom(DELETED_RECORDS_COUNT,deletions.size()); if (isRelationTypeIndex()) { mutator.mutateEdges(key, KCVSCache.NO_ADDITIONS, deletions); } else { mutator.mutateIndex(key, KCVSCache.NO_ADDITIONS, deletions); } } catch (final Exception e) { mgmt.rollback(); writeTx.rollback(); metrics.incrementCustom(FAILED_TX); throw new TitanException(e.getMessage(), e); } }
@Override public TitanGraphIndex buildMixedIndex(String backingIndex) { Preconditions.checkArgument(StringUtils.isNotBlank(backingIndex), "Need to specify backing index name"); Preconditions.checkArgument(!unique, "An external index cannot be unique"); TitanGraphIndex index = createMixedIndex(indexName, elementCategory, constraint, backingIndex); for (Map.Entry<PropertyKey, Parameter[]> entry : keys.entrySet()) { addIndexKey(index, entry.getKey(), entry.getValue()); } return index; } }
@Override public TitanManagement openManagement() { return new ManagementSystem(this,backend.getGlobalSystemConfig(),backend.getSystemMgmtLog(), mgmtLogger, schemaCache); }
private void setStatusEdges(TitanSchemaVertex vertex, SchemaStatus status, Set<PropertyKeyVertex> keys) { Preconditions.checkArgument(vertex.asIndexType().isMixedIndex()); for (TitanEdge edge : vertex.getEdges(TypeDefinitionCategory.INDEX_FIELD, Direction.OUT)) { if (!keys.contains(edge.vertex(Direction.IN))) continue; //Only address edges with matching keys TypeDefinitionDescription desc = edge.valueOrNull(BaseKey.SchemaDefinitionDesc); assert desc.getCategory() == TypeDefinitionCategory.INDEX_FIELD; Parameter[] parameters = (Parameter[]) desc.getModifier(); assert parameters[parameters.length - 1].key().equals(ParameterType.STATUS.getName()); if (parameters[parameters.length - 1].value().equals(status)) continue; Parameter[] paraCopy = Arrays.copyOf(parameters, parameters.length); paraCopy[parameters.length - 1] = ParameterType.STATUS.getParameter(status); edge.remove(); addSchemaEdge(vertex, edge.vertex(Direction.IN), TypeDefinitionCategory.INDEX_FIELD, paraCopy); } for (PropertyKeyVertex prop : keys) prop.resetCache(); }