Search in sources :

Example 21 with EdgeLabel

use of com.thinkaurelius.titan.core.EdgeLabel in project titan by thinkaurelius.

the class GraphOfTheGodsFactory method load.

public static void load(final TitanGraph graph, String mixedIndexName, boolean uniqueNameCompositeIndex) {
    //Create Schema
    TitanManagement mgmt = graph.openManagement();
    final PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
    TitanManagement.IndexBuilder nameIndexBuilder = mgmt.buildIndex("name", Vertex.class).addKey(name);
    if (uniqueNameCompositeIndex)
        nameIndexBuilder.unique();
    TitanGraphIndex namei = nameIndexBuilder.buildCompositeIndex();
    mgmt.setConsistency(namei, ConsistencyModifier.LOCK);
    final PropertyKey age = mgmt.makePropertyKey("age").dataType(Integer.class).make();
    if (null != mixedIndexName)
        mgmt.buildIndex("vertices", Vertex.class).addKey(age).buildMixedIndex(mixedIndexName);
    final PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).make();
    final PropertyKey reason = mgmt.makePropertyKey("reason").dataType(String.class).make();
    final PropertyKey place = mgmt.makePropertyKey("place").dataType(Geoshape.class).make();
    if (null != mixedIndexName)
        mgmt.buildIndex("edges", Edge.class).addKey(reason).addKey(place).buildMixedIndex(mixedIndexName);
    mgmt.makeEdgeLabel("father").multiplicity(Multiplicity.MANY2ONE).make();
    mgmt.makeEdgeLabel("mother").multiplicity(Multiplicity.MANY2ONE).make();
    EdgeLabel battled = mgmt.makeEdgeLabel("battled").signature(time).make();
    mgmt.buildEdgeIndex(battled, "battlesByTime", Direction.BOTH, Order.decr, time);
    mgmt.makeEdgeLabel("lives").signature(reason).make();
    mgmt.makeEdgeLabel("pet").make();
    mgmt.makeEdgeLabel("brother").make();
    mgmt.makeVertexLabel("titan").make();
    mgmt.makeVertexLabel("location").make();
    mgmt.makeVertexLabel("god").make();
    mgmt.makeVertexLabel("demigod").make();
    mgmt.makeVertexLabel("human").make();
    mgmt.makeVertexLabel("monster").make();
    mgmt.commit();
    TitanTransaction tx = graph.newTransaction();
    // vertices
    Vertex saturn = tx.addVertex(T.label, "titan", "name", "saturn", "age", 10000);
    Vertex sky = tx.addVertex(T.label, "location", "name", "sky");
    Vertex sea = tx.addVertex(T.label, "location", "name", "sea");
    Vertex jupiter = tx.addVertex(T.label, "god", "name", "jupiter", "age", 5000);
    Vertex neptune = tx.addVertex(T.label, "god", "name", "neptune", "age", 4500);
    Vertex hercules = tx.addVertex(T.label, "demigod", "name", "hercules", "age", 30);
    Vertex alcmene = tx.addVertex(T.label, "human", "name", "alcmene", "age", 45);
    Vertex pluto = tx.addVertex(T.label, "god", "name", "pluto", "age", 4000);
    Vertex nemean = tx.addVertex(T.label, "monster", "name", "nemean");
    Vertex hydra = tx.addVertex(T.label, "monster", "name", "hydra");
    Vertex cerberus = tx.addVertex(T.label, "monster", "name", "cerberus");
    Vertex tartarus = tx.addVertex(T.label, "location", "name", "tartarus");
    // edges
    jupiter.addEdge("father", saturn);
    jupiter.addEdge("lives", sky, "reason", "loves fresh breezes");
    jupiter.addEdge("brother", neptune);
    jupiter.addEdge("brother", pluto);
    neptune.addEdge("lives", sea).property("reason", "loves waves");
    neptune.addEdge("brother", jupiter);
    neptune.addEdge("brother", pluto);
    hercules.addEdge("father", jupiter);
    hercules.addEdge("mother", alcmene);
    hercules.addEdge("battled", nemean, "time", 1, "place", Geoshape.point(38.1f, 23.7f));
    hercules.addEdge("battled", hydra, "time", 2, "place", Geoshape.point(37.7f, 23.9f));
    hercules.addEdge("battled", cerberus, "time", 12, "place", Geoshape.point(39f, 22f));
    pluto.addEdge("brother", jupiter);
    pluto.addEdge("brother", neptune);
    pluto.addEdge("lives", tartarus, "reason", "no fear of death");
    pluto.addEdge("pet", cerberus);
    cerberus.addEdge("lives", tartarus);
    // commit the transaction to disk
    tx.commit();
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Geoshape(com.thinkaurelius.titan.core.attribute.Geoshape) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) TitanManagement(com.thinkaurelius.titan.core.schema.TitanManagement) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) TitanTransaction(com.thinkaurelius.titan.core.TitanTransaction) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Example 22 with EdgeLabel

use of com.thinkaurelius.titan.core.EdgeLabel in project titan by thinkaurelius.

the class ManagementSystem method buildRelationTypeIndex.

private RelationTypeIndex buildRelationTypeIndex(RelationType type, String name, Direction direction, Order sortOrder, PropertyKey... sortKeys) {
    Preconditions.checkArgument(type != null && direction != null && sortOrder != null && sortKeys != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name cannot be blank: %s", name);
    Token.verifyName(name);
    Preconditions.checkArgument(sortKeys.length > 0, "Need to specify sort keys");
    for (RelationType key : sortKeys) Preconditions.checkArgument(key != null, "Keys cannot be null");
    Preconditions.checkArgument(!(type instanceof EdgeLabel) || !((EdgeLabel) type).isUnidirected() || direction == Direction.OUT, "Can only index uni-directed labels in the out-direction: %s", type);
    Preconditions.checkArgument(!((InternalRelationType) type).multiplicity().isConstrained(direction), "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()) {
        StandardEdgeLabelMaker lm = (StandardEdgeLabelMaker) transaction.makeEdgeLabel(composedName);
        lm.unidirected(direction);
        maker = lm;
    } else {
        assert type.isPropertyKey();
        assert direction == Direction.OUT;
        StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker) transaction.makePropertyKey(composedName);
        lm.dataType(((PropertyKey) type).dataType());
        maker = lm;
    }
    maker.status(type.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    maker.invisible();
    maker.multiplicity(Multiplicity.MULTI);
    maker.sortKey(sortKeys);
    maker.sortOrder(sortOrder);
    //Compose signature
    long[] typeSig = ((InternalRelationType) type).getSignature();
    Set<PropertyKey> signature = Sets.newHashSet();
    for (long typeId : typeSig) signature.add(transaction.getExistingPropertyKey(typeId));
    for (RelationType sortType : sortKeys) signature.remove(sortType);
    if (!signature.isEmpty()) {
        PropertyKey[] sig = signature.toArray(new PropertyKey[signature.size()]);
        maker.signature(sig);
    }
    RelationType typeIndex = maker.make();
    addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null);
    RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType) typeIndex);
    if (!type.isNew())
        updateIndex(index, SchemaAction.REGISTER_INDEX);
    return index;
}
Also used : StandardRelationTypeMaker(com.thinkaurelius.titan.graphdb.types.StandardRelationTypeMaker) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) StandardEdgeLabelMaker(com.thinkaurelius.titan.graphdb.types.StandardEdgeLabelMaker) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) RelationType(com.thinkaurelius.titan.core.RelationType) StandardPropertyKeyMaker(com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Example 23 with EdgeLabel

use of com.thinkaurelius.titan.core.EdgeLabel in project titan by thinkaurelius.

the class ModificationDeserializer method parseRelation.

public static InternalRelation parseRelation(TransactionLogHeader.Modification modification, StandardTitanTx 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;
}
Also used : EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) Change(com.thinkaurelius.titan.core.log.Change) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) Entry(com.thinkaurelius.titan.diskstorage.Entry) InternalVertex(com.thinkaurelius.titan.graphdb.internal.InternalVertex) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Aggregations

EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)23 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)18 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)18 Test (org.junit.Test)18 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)8 Edge (org.apache.tinkerpop.gremlin.structure.Edge)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)5 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)5 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)5 VertexLabel (com.thinkaurelius.titan.core.VertexLabel)4 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)4 BaseVertexLabel (com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)4 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)3 StandardEdgeLabelMaker (com.thinkaurelius.titan.graphdb.types.StandardEdgeLabelMaker)3 SchemaViolationException (com.thinkaurelius.titan.core.SchemaViolationException)2 VertexList (com.thinkaurelius.titan.core.VertexList)2 Change (com.thinkaurelius.titan.core.log.Change)2