Skip to content

Commit

Permalink
Fixes #860 which entailed a lot of refactoring of existing core and i…
Browse files Browse the repository at this point in the history
…nternal interfaces with changes throughout the codebase.
  • Loading branch information
mbroecheler committed Apr 15, 2015
1 parent 8f6404f commit 38130d3
Show file tree
Hide file tree
Showing 30 changed files with 253 additions and 283 deletions.
Expand Up @@ -93,15 +93,15 @@ public default Object id() {
public<V> Property<V> property(String key, V value); public<V> Property<V> property(String key, V value);


/** /**
* Retrieves the value associated with the given key on this vertex and casts it to the specified type. * Retrieves the value associated with the given key on this element and casts it to the specified type.
* If the key has cardinality SINGLE, then there can be at most one value and this value is returned (or null). * If the key has cardinality SINGLE, then there can be at most one value and this value is returned (or null).
* Otherwise a list of all associated values is returned, or an empty list if non exist. * Otherwise a list of all associated values is returned, or an empty list if non exist.
* <p/> * <p/>
* *
* @param key key * @param key key
* @return value or list of values associated with key * @return value or list of values associated with key
*/ */
public <V> V valueOrNull(RelationType key); public <V> V valueOrNull(PropertyKey key);


//########### LifeCycle Status ########## //########### LifeCycle Status ##########


Expand Down
@@ -1,18 +1,17 @@
package com.thinkaurelius.titan.core; package com.thinkaurelius.titan.core;


import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Property; import org.apache.tinkerpop.gremlin.structure.Property;


/** /**
* @author Matthias Broecheler (me@matthiasb.com) * @author Matthias Broecheler (me@matthiasb.com)
*/ */
public interface TitanProperty<V> extends Property<V> { public interface TitanProperty<V> extends Property<V> {


public RelationType getType(); public PropertyKey propertyKey();


@Override @Override
public default String key() { public default String key() {
return getType().name(); return propertyKey().name();
} }


} }
Expand Up @@ -26,11 +26,7 @@ public default TitanTransaction graph() {
return element().graph(); return element().graph();
} }


/** @Override
* Returns the {@link PropertyKey} for this property
*
* @return
*/
public default PropertyKey propertyKey() { public default PropertyKey propertyKey() {
return (PropertyKey)getType(); return (PropertyKey)getType();
} }
Expand Down
Expand Up @@ -2,6 +2,7 @@


import com.thinkaurelius.titan.core.EdgeLabel; import com.thinkaurelius.titan.core.EdgeLabel;
import com.thinkaurelius.titan.core.Multiplicity; import com.thinkaurelius.titan.core.Multiplicity;
import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.RelationType; import com.thinkaurelius.titan.core.RelationType;


/** /**
Expand Down Expand Up @@ -42,7 +43,7 @@ public interface EdgeLabelMaker extends RelationTypeMaker {




@Override @Override
public EdgeLabelMaker signature(RelationType... types); public EdgeLabelMaker signature(PropertyKey... types);




/** /**
Expand Down
Expand Up @@ -38,7 +38,7 @@ public interface PropertyKeyMaker extends RelationTypeMaker {
public PropertyKeyMaker dataType(Class<?> clazz); public PropertyKeyMaker dataType(Class<?> clazz);


@Override @Override
public PropertyKeyMaker signature(RelationType... types); public PropertyKeyMaker signature(PropertyKey... types);




/** /**
Expand Down
@@ -1,5 +1,6 @@
package com.thinkaurelius.titan.core.schema; package com.thinkaurelius.titan.core.schema;


import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.RelationType; import com.thinkaurelius.titan.core.RelationType;


/** /**
Expand Down Expand Up @@ -39,10 +40,10 @@ public interface RelationTypeMaker {
* <br /> * <br />
* The signature is empty by default. * The signature is empty by default.
* *
* @param types RelationTypes composing the signature for the configured relation type. The order is irrelevant. * @param keys PropertyKey composing the signature for the configured relation type. The order is irrelevant.
* @return this RelationTypeMaker * @return this RelationTypeMaker
*/ */
public RelationTypeMaker signature(RelationType... types); public RelationTypeMaker signature(PropertyKey... keys);


/** /**
* Builds the configured relation type * Builds the configured relation type
Expand Down
Expand Up @@ -37,7 +37,7 @@ public interface TitanManagement extends TitanConfiguration, SchemaManager {
*/ */


/** /**
* Identical to {@link #buildEdgeIndex(com.thinkaurelius.titan.core.EdgeLabel, String, com.tinkerpop.gremlin.structure.Direction, com.tinkerpop.gremlin.structure.Order, com.thinkaurelius.titan.core.RelationType...)} * Identical to {@link #buildEdgeIndex(com.thinkaurelius.titan.core.EdgeLabel, String, com.tinkerpop.gremlin.structure.Direction, com.tinkerpop.gremlin.structure.Order, com.thinkaurelius.titan.core.PropertyKey...)}
* with default sort order {@link com.tinkerpop.gremlin.structure.Order#incr}. * with default sort order {@link com.tinkerpop.gremlin.structure.Order#incr}.
* *
* @param label * @param label
Expand All @@ -46,7 +46,7 @@ public interface TitanManagement extends TitanConfiguration, SchemaManager {
* @param sortKeys * @param sortKeys
* @return the created {@link RelationTypeIndex} * @return the created {@link RelationTypeIndex}
*/ */
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, RelationType... sortKeys); public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, PropertyKey... sortKeys);


/** /**
* Creates a {@link RelationTypeIndex} for the provided edge label. That means, that all edges of that label will be * Creates a {@link RelationTypeIndex} for the provided edge label. That means, that all edges of that label will be
Expand All @@ -62,18 +62,18 @@ public interface TitanManagement extends TitanConfiguration, SchemaManager {
* @param sortKeys * @param sortKeys
* @return the created {@link RelationTypeIndex} * @return the created {@link RelationTypeIndex}
*/ */
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, Order sortOrder, RelationType... sortKeys); public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, Order sortOrder, PropertyKey... sortKeys);


/** /**
* Identical to {@link #buildPropertyIndex(com.thinkaurelius.titan.core.PropertyKey, String, com.tinkerpop.gremlin.structure.Order, com.thinkaurelius.titan.core.RelationType...)} * Identical to {@link #buildPropertyIndex(com.thinkaurelius.titan.core.PropertyKey, String, com.tinkerpop.gremlin.structure.Order, com.thinkaurelius.titan.core.PropertyKey...)}
* with default sort order {@link com.tinkerpop.gremlin.structure.Order#incr}. * with default sort order {@link com.tinkerpop.gremlin.structure.Order#incr}.
* *
* @param key * @param key
* @param name * @param name
* @param sortKeys * @param sortKeys
* @return the created {@link RelationTypeIndex} * @return the created {@link RelationTypeIndex}
*/ */
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, RelationType... sortKeys); public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, PropertyKey... sortKeys);


/** /**
* Creates a {@link RelationTypeIndex} for the provided property key. That means, that all properties of that key will be * Creates a {@link RelationTypeIndex} for the provided property key. That means, that all properties of that key will be
Expand All @@ -87,7 +87,7 @@ public interface TitanManagement extends TitanConfiguration, SchemaManager {
* @param sortKeys * @param sortKeys
* @return the created {@link RelationTypeIndex} * @return the created {@link RelationTypeIndex}
*/ */
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, Order sortOrder, RelationType... sortKeys); public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, Order sortOrder, PropertyKey... sortKeys);


/** /**
* Whether a {@link RelationTypeIndex} with the given name has been defined for the provided {@link RelationType} * Whether a {@link RelationTypeIndex} with the given name has been defined for the provided {@link RelationType}
Expand Down
Expand Up @@ -33,7 +33,7 @@
import java.util.Map; import java.util.Map;


import static com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.DirectionID; import static com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.DirectionID;
import static com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.EdgeTypeParse; import static com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.RelationTypeParse;
import static com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.getBounds; import static com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.getBounds;


/** /**
Expand Down Expand Up @@ -66,15 +66,15 @@ public RelationCache readRelation(Entry data, boolean parseHeaderOnly, TypeInspe
public Direction parseDirection(Entry data) { public Direction parseDirection(Entry data) {
RelationCache map = data.getCache(); RelationCache map = data.getCache();
if (map != null) return map.direction; if (map != null) return map.direction;
return IDHandler.readEdgeType(data.asReadBuffer()).dirID.getDirection(); return IDHandler.readRelationType(data.asReadBuffer()).dirID.getDirection();
} }


@Override @Override
public RelationCache parseRelation(Entry data, boolean excludeProperties, TypeInspector tx) { public RelationCache parseRelation(Entry data, boolean excludeProperties, TypeInspector tx) {
ReadBuffer in = data.asReadBuffer(); ReadBuffer in = data.asReadBuffer();


LongObjectOpenHashMap properties = excludeProperties ? null : new LongObjectOpenHashMap(4); LongObjectOpenHashMap properties = excludeProperties ? null : new LongObjectOpenHashMap(4);
EdgeTypeParse typeAndDir = IDHandler.readEdgeType(in); RelationTypeParse typeAndDir = IDHandler.readRelationType(in);


long typeId = typeAndDir.typeId; long typeId = typeAndDir.typeId;
Direction dir = typeAndDir.dirID.getDirection(); Direction dir = typeAndDir.dirID.getDirection();
Expand Down Expand Up @@ -145,7 +145,7 @@ public RelationCache parseRelation(Entry data, boolean excludeProperties, TypeIn


//Third: read rest //Third: read rest
while (in.hasRemaining()) { while (in.hasRemaining()) {
RelationType type = tx.getExistingRelationType(IDHandler.readInlineEdgeType(in)); PropertyKey type = tx.getExistingPropertyKey(IDHandler.readInlineRelationType(in));
Object pvalue = readInline(in, type, InlineType.NORMAL); Object pvalue = readInline(in, type, InlineType.NORMAL);
assert pvalue != null; assert pvalue != null;
properties.put(type.longId(), pvalue); properties.put(type.longId(), pvalue);
Expand All @@ -165,27 +165,16 @@ public RelationCache parseRelation(Entry data, boolean excludeProperties, TypeIn
return new RelationCache(dir, typeId, relationId, other, properties); return new RelationCache(dir, typeId, relationId, other, properties);
} }


private void readInlineTypes(long[] typeids, LongObjectOpenHashMap properties, ReadBuffer in, TypeInspector tx, InlineType inlineType) { private void readInlineTypes(long[] keyIds, LongObjectOpenHashMap properties, ReadBuffer in, TypeInspector tx, InlineType inlineType) {
for (long typeid : typeids) { for (long keyId : keyIds) {
RelationType keyType = tx.getExistingRelationType(typeid); PropertyKey keyType = tx.getExistingPropertyKey(keyId);
Object value = readInline(in, keyType, inlineType); Object value = readInline(in, keyType, inlineType);
if (value != null) properties.put(typeid, value); if (value != null) properties.put(keyId, value);
} }
} }


private Object readInline(ReadBuffer read, RelationType type, InlineType inlineType) { private Object readInline(ReadBuffer read, PropertyKey key, InlineType inlineType) {
if (type.isPropertyKey()) { return readPropertyValue(read, key, inlineType);
PropertyKey key = ((PropertyKey) type);
return readPropertyValue(read,key, inlineType);
} else {
assert type.isEdgeLabel();
long id;
if (inlineType.writeByteOrdered())
id = LongSerializer.INSTANCE.readByteOrder(read);
else
id = VariableLong.readPositive(read);
return id == 0 ? null : id;
}
} }


private Object readPropertyValue(ReadBuffer read, PropertyKey key) { private Object readPropertyValue(ReadBuffer read, PropertyKey key) {
Expand Down Expand Up @@ -239,7 +228,7 @@ public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelatio


DataOutput out = serializer.getDataOutput(DEFAULT_CAPACITY); DataOutput out = serializer.getDataOutput(DEFAULT_CAPACITY);
int valuePosition; int valuePosition;
IDHandler.writeEdgeType(out, typeid, dirID, type.isInvisibleType()); IDHandler.writeRelationType(out, typeid, dirID, type.isInvisibleType());
Multiplicity multiplicity = type.multiplicity(); Multiplicity multiplicity = type.multiplicity();


long[] sortKey = type.getSortKey(); long[] sortKey = type.getSortKey();
Expand Down Expand Up @@ -305,7 +294,7 @@ public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelatio
for (long id : signature) writtenTypes.add(id); for (long id : signature) writtenTypes.add(id);
} }
LongArrayList remainingTypes = new LongArrayList(8); LongArrayList remainingTypes = new LongArrayList(8);
for (RelationType t : relation.getPropertyKeysDirect()) { for (PropertyKey t : relation.getPropertyKeysDirect()) {
if (!(t instanceof ImplicitKey) && !writtenTypes.contains(t.longId())) { if (!(t instanceof ImplicitKey) && !writtenTypes.contains(t.longId())) {
remainingTypes.add(t.longId()); remainingTypes.add(t.longId());
} }
Expand All @@ -314,7 +303,7 @@ public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelatio
long[] remaining = remainingTypes.toArray(); long[] remaining = remainingTypes.toArray();
Arrays.sort(remaining); Arrays.sort(remaining);
for (long tid : remaining) { for (long tid : remaining) {
RelationType t = tx.getExistingRelationType(tid); PropertyKey t = tx.getExistingPropertyKey(tid);
writeInline(out, t, relation.getValueDirect(t), InlineType.NORMAL); writeInline(out, t, relation.getValueDirect(t), InlineType.NORMAL);
} }
assert valuePosition>0; assert valuePosition>0;
Expand All @@ -329,7 +318,7 @@ private enum InlineType {


KEY, SIGNATURE, NORMAL; KEY, SIGNATURE, NORMAL;


public boolean writeEdgeType() { public boolean writeInlineKey() {
return this==NORMAL; return this==NORMAL;
} }


Expand All @@ -339,28 +328,21 @@ public boolean writeByteOrdered() {


} }


private void writeInlineTypes(long[] typeids, InternalRelation relation, DataOutput out, TypeInspector tx, InlineType inlineType) { private void writeInlineTypes(long[] keyIds, InternalRelation relation, DataOutput out, TypeInspector tx, InlineType inlineType) {
for (long typeid : typeids) { for (long keyId : keyIds) {
RelationType t = tx.getExistingRelationType(typeid); PropertyKey t = tx.getExistingPropertyKey(keyId);
writeInline(out, t, relation.getValueDirect(t), inlineType); writeInline(out, t, relation.getValueDirect(t), inlineType);
} }
} }


private void writeInline(DataOutput out, RelationType type, Object value, InlineType inlineType) { private void writeInline(DataOutput out, PropertyKey inlineKey, Object value, InlineType inlineType) {
assert !(type.isPropertyKey() && !inlineType.writeEdgeType()) || !AttributeUtil.hasGenericDataType((PropertyKey) type); assert inlineType.writeInlineKey() || !AttributeUtil.hasGenericDataType(inlineKey);


if (inlineType.writeEdgeType()) { if (inlineType.writeInlineKey()) {
IDHandler.writeInlineEdgeType(out, type.longId()); IDHandler.writeInlineRelationType(out, inlineKey.longId());
} }


if (type.isPropertyKey()) { writePropertyValue(out,inlineKey,value, inlineType);
writePropertyValue(out,(PropertyKey)type,value, inlineType);
} else {
assert type.isEdgeLabel() && ((EdgeLabel) type).isUnidirected();
long id = (value==null?0:((InternalVertex) value).longId());
if (inlineType.writeByteOrdered()) LongSerializer.INSTANCE.writeByteOrder(out,id);
else VariableLong.writePositive(out,id);
}
} }


private void writePropertyValue(DataOutput out, PropertyKey key, Object value) { private void writePropertyValue(DataOutput out, PropertyKey key, Object value) {
Expand Down Expand Up @@ -394,59 +376,59 @@ public SliceQuery getQuery(InternalRelationType type, Direction dir, TypedInterv
RelationCategory rt = type.isPropertyKey() ? RelationCategory.PROPERTY : RelationCategory.EDGE; RelationCategory rt = type.isPropertyKey() ? RelationCategory.PROPERTY : RelationCategory.EDGE;
if (dir == Direction.BOTH) { if (dir == Direction.BOTH) {
assert type.isEdgeLabel(); assert type.isEdgeLabel();
sliceStart = IDHandler.getEdgeType(type.longId(), getDirID(Direction.OUT, rt),type.isInvisibleType()); sliceStart = IDHandler.getRelationType(type.longId(), getDirID(Direction.OUT, rt), type.isInvisibleType());
sliceEnd = IDHandler.getEdgeType(type.longId(), getDirID(Direction.IN, rt),type.isInvisibleType()); sliceEnd = IDHandler.getRelationType(type.longId(), getDirID(Direction.IN, rt), type.isInvisibleType());
assert sliceStart.compareTo(sliceEnd)<0; assert sliceStart.compareTo(sliceEnd)<0;
sliceEnd = BufferUtil.nextBiggerBuffer(sliceEnd); sliceEnd = BufferUtil.nextBiggerBuffer(sliceEnd);
} else { } else {
DirectionID dirID = getDirID(dir, rt); DirectionID dirID = getDirID(dir, rt);


DataOutput colStart = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY); DataOutput colStart = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY);
DataOutput colEnd = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY); DataOutput colEnd = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY);
IDHandler.writeEdgeType(colStart, type.longId(), dirID, type.isInvisibleType()); IDHandler.writeRelationType(colStart, type.longId(), dirID, type.isInvisibleType());
IDHandler.writeEdgeType(colEnd, type.longId(), dirID, type.isInvisibleType()); IDHandler.writeRelationType(colEnd, type.longId(), dirID, type.isInvisibleType());


long[] sortKeyIDs = type.getSortKey(); long[] sortKeyIDs = type.getSortKey();
Preconditions.checkArgument(sortKey.length >= sortKeyIDs.length); Preconditions.checkArgument(sortKey.length >= sortKeyIDs.length);
assert colStart.getPosition() == colEnd.getPosition(); assert colStart.getPosition() == colEnd.getPosition();
int keyStartPos = colStart.getPosition(); int keyStartPos = colStart.getPosition();
int keyEndPos = -1; int keyEndPos = -1;
for (int i = 0; i < sortKey.length && sortKey[i] != null; i++) { for (int i = 0; i < sortKey.length && sortKey[i] != null; i++) {
RelationType t = sortKey[i].type; PropertyKey skey = sortKey[i].key;
Interval interval = sortKey[i].interval; Interval interval = sortKey[i].interval;


if (i>=sortKeyIDs.length) { if (i>=sortKeyIDs.length) {
assert !type.multiplicity().isUnique(dir); assert !type.multiplicity().isUnique(dir);
assert (t instanceof ImplicitKey) && (t==ImplicitKey.TITANID || t==ImplicitKey.ADJACENT_ID); assert (skey instanceof ImplicitKey) && (skey==ImplicitKey.TITANID || skey==ImplicitKey.ADJACENT_ID);
assert t!=ImplicitKey.ADJACENT_ID || (i==sortKeyIDs.length); assert skey!=ImplicitKey.ADJACENT_ID || (i==sortKeyIDs.length);
assert t!=ImplicitKey.TITANID || (!type.multiplicity().isConstrained() && assert skey!=ImplicitKey.TITANID || (!type.multiplicity().isConstrained() &&
(i==sortKeyIDs.length && t.isPropertyKey() || i==sortKeyIDs.length+1 && t.isEdgeLabel() )); (i==sortKeyIDs.length && skey.isPropertyKey() || i==sortKeyIDs.length+1 && skey.isEdgeLabel() ));
assert colStart.getPosition()==colEnd.getPosition(); assert colStart.getPosition()==colEnd.getPosition();
assert interval==null || interval.isPoints(); assert interval==null || interval.isPoints();
keyEndPos = colStart.getPosition(); keyEndPos = colStart.getPosition();


} else { } else {
assert !type.multiplicity().isConstrained(); assert !type.multiplicity().isConstrained();
assert t.longId() == sortKeyIDs[i]; assert skey.longId() == sortKeyIDs[i];
} }


if (interval == null || interval.isEmpty()) { if (interval == null || interval.isEmpty()) {
break; break;
} }
if (interval.isPoints()) { if (interval.isPoints()) {
if (t==ImplicitKey.TITANID || t==ImplicitKey.ADJACENT_ID) { if (skey==ImplicitKey.TITANID || skey==ImplicitKey.ADJACENT_ID) {
assert !type.multiplicity().isUnique(dir); assert !type.multiplicity().isUnique(dir);
VariableLong.writePositiveBackward(colStart, (Long)interval.getStart()); VariableLong.writePositiveBackward(colStart, (Long)interval.getStart());
VariableLong.writePositiveBackward(colEnd, (Long)interval.getEnd()); VariableLong.writePositiveBackward(colEnd, (Long)interval.getEnd());
} else { } else {
writeInline(colStart, t, interval.getStart(), InlineType.KEY); writeInline(colStart, skey, interval.getStart(), InlineType.KEY);
writeInline(colEnd, t, interval.getEnd(), InlineType.KEY); writeInline(colEnd, skey, interval.getEnd(), InlineType.KEY);
} }
} else { } else {
if (interval.getStart() != null) if (interval.getStart() != null)
writeInline(colStart, t, interval.getStart(), InlineType.KEY); writeInline(colStart, skey, interval.getStart(), InlineType.KEY);
if (interval.getEnd() != null) if (interval.getEnd() != null)
writeInline(colEnd, t, interval.getEnd(), InlineType.KEY); writeInline(colEnd, skey, interval.getEnd(), InlineType.KEY);


switch (type.getSortOrder()) { switch (type.getSortOrder()) {
case ASC: case ASC:
Expand Down Expand Up @@ -493,12 +475,12 @@ public SliceQuery getQuery(InternalRelationType type, Direction dir, TypedInterv
} }


public static class TypedInterval { public static class TypedInterval {
public final InternalRelationType type; public final PropertyKey key;
public final Interval interval; public final Interval interval;




public TypedInterval(InternalRelationType type, Interval interval) { public TypedInterval(PropertyKey key, Interval interval) {
this.type = type; this.key = key;
this.interval = interval; this.interval = interval;
} }
} }
Expand Down

0 comments on commit 38130d3

Please sign in to comment.