Skip to content

Commit

Permalink
Merge branch 'titan09' of github.com:thinkaurelius/titan into titan09
Browse files Browse the repository at this point in the history
  • Loading branch information
dalaro committed May 8, 2015
2 parents 4670988 + b019dda commit b26a9b4
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class StandardStoreFeatures implements StoreFeatures {
private final boolean cellLevelTTL;
private final boolean storeLevelTTL;
private final boolean visibility;
private final boolean supportsPersist;
private final Configuration keyConsistentTxConfig;
private final Configuration localKeyConsistentTxConfig;
private final Configuration scanTxConfig;
Expand Down Expand Up @@ -102,6 +103,9 @@ public boolean hasVisibility() {
return visibility;
}

@Override
public boolean supportsPersistence() { return supportsPersist; }

@Override
public Configuration getKeyConsistentTxConfig() {
return keyConsistentTxConfig;
Expand Down Expand Up @@ -141,6 +145,7 @@ public static class Builder {
private boolean cellLevelTTL;
private boolean storeLevelTTL;
private boolean visibility;
private boolean supportsPersist = true;
private boolean keyConsistent;
private Configuration keyConsistentTxConfig;
private Configuration localKeyConsistentTxConfig;
Expand Down Expand Up @@ -170,6 +175,7 @@ public Builder(StoreFeatures template) {
cellTTL(template.hasCellTTL());
storeTTL(template.hasStoreTTL());
visibility(template.hasVisibility());
persists(template.supportsPersistence());
if (template.isKeyConsistent()) {
keyConsistent(template.getKeyConsistentTxConfig(), template.getLocalKeyConsistentTxConfig());
}
Expand Down Expand Up @@ -247,6 +253,11 @@ public Builder visibility(boolean b) {
return this;
}

public Builder persists(boolean b) {
supportsPersist = b;
return this;
}

public Builder keyConsistent(Configuration c) {
keyConsistent = true;
keyConsistentTxConfig = c;
Expand Down Expand Up @@ -275,7 +286,8 @@ public StandardStoreFeatures build() {
multiQuery, locking, batchMutation, localKeyPartition,
keyOrdered, distributed, transactional, keyConsistent,
timestamps, preferredTimestamps, cellLevelTTL,
storeLevelTTL, visibility, keyConsistentTxConfig,
storeLevelTTL, visibility, supportsPersist,
keyConsistentTxConfig,
localKeyConsistentTxConfig, scanTxConfig);
}
}
Expand All @@ -286,7 +298,7 @@ private StandardStoreFeatures(boolean unorderedScan, boolean orderedScan,
boolean transactional, boolean keyConsistent,
boolean timestamps, TimestampProviders preferredTimestamps,
boolean cellLevelTTL, boolean storeLevelTTL,
boolean visibility,
boolean visibility, boolean supportsPersist,
Configuration keyConsistentTxConfig,
Configuration localKeyConsistentTxConfig,
Configuration scanTxConfig) {
Expand All @@ -305,6 +317,7 @@ private StandardStoreFeatures(boolean unorderedScan, boolean orderedScan,
this.cellLevelTTL = cellLevelTTL;
this.storeLevelTTL = storeLevelTTL;
this.visibility = visibility;
this.supportsPersist = supportsPersist;
this.keyConsistentTxConfig = keyConsistentTxConfig;
this.localKeyConsistentTxConfig = localKeyConsistentTxConfig;
this.scanTxConfig = scanTxConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public interface StoreFeatures {
*/
public boolean hasVisibility();

/**
* Whether the backend supports data persistence. Return false if the backend is in-memory only.
* @return
*/
public boolean supportsPersistence();

/**
* Get a transaction configuration that enforces key consistency. This
* method has undefined behavior when {@link #isKeyConsistent()} is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public InMemoryStoreManager(final Configuration configuration) {
.orderedScan(true)
.unorderedScan(true)
.keyOrdered(true)
.persists(false)
.keyConsistent(GraphDatabaseConfiguration.buildGraphConfiguration())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import com.google.common.primitives.Longs;
import com.thinkaurelius.titan.core.*;
import com.thinkaurelius.titan.graphdb.idmanagement.IDManager;
import com.thinkaurelius.titan.graphdb.relations.RelationIdentifier;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;

/**
Expand Down Expand Up @@ -30,12 +35,30 @@ public static boolean isTemporaryId(long elementId) {

@Override
public int hashCode() {
return ElementHelper.hashCode(this);
return Long.valueOf(getCompareId()).hashCode();
}

@Override
public boolean equals(Object other) {
return ElementHelper.areEqual(this, other);
if (other==null)
return false;

if (this==other)
return true;
if (!((this instanceof Vertex && other instanceof Vertex) ||
(this instanceof Edge && other instanceof Edge) ||
(this instanceof VertexProperty && other instanceof VertexProperty)))
return false;
//Same type => they are the same if they have identical ids.
if (other instanceof AbstractElement) {
return getCompareId()==((AbstractElement)other).getCompareId();
} else if (other instanceof TitanElement) {
return ((TitanElement) other).hasId() && getCompareId()==((TitanElement)other).longId();
} else if (other instanceof Element) {
Object otherId = ((Element)other).id();
if (otherId instanceof RelationIdentifier) return ((RelationIdentifier) otherId).getRelationId()==getCompareId();
else return otherId.equals(getCompareId());
} else return false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public QueryProfiler getProfiler() {
@Override
public void observeWith(QueryProfiler parentProfiler) {
Preconditions.checkArgument(parentProfiler!=null);
this.profiler = parentProfiler.addNested();
this.profiler = parentProfiler.addNested("OR-query");
profiler.setAnnotation(QueryProfiler.FITTED_ANNOTATION,isFitted);
profiler.setAnnotation(QueryProfiler.ORDERED_ANNOTATION,isSorted);
profiler.setAnnotation(QueryProfiler.QUERY_ANNOTATION,backendQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private Subquery(IndexType index, BackendQuery query) {
}

public void observeWith(QueryProfiler prof) {
this.profiler = prof.addNested();
this.profiler = prof.addNested("AND-query");
profiler.setAnnotation(QueryProfiler.QUERY_ANNOTATION,query);
profiler.setAnnotation(QueryProfiler.INDEX_ANNOTATION,index.getName());
if (index.isMixedIndex()) profiler.setAnnotation(QueryProfiler.INDEX_ANNOTATION+"_impl",index.getBackingIndexName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<EntryList> execute(final BackendTransaction tx) {

@Override
public int hashCode() {
return new HashCodeBuilder().append(queries).appendSuper(super.hashCode()).toHashCode();
return new HashCodeBuilder().append(queries).append(getLimit()).toHashCode();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface QueryProfiler {

public static final QueryProfiler NO_OP = new QueryProfiler() {
@Override
public QueryProfiler addNested() {
public QueryProfiler addNested(String groupName) {
return this;
}

Expand All @@ -50,7 +50,7 @@ public void setResultSize(long size) {
};


public QueryProfiler addNested();
public QueryProfiler addNested(String groupName);

public QueryProfiler setAnnotation(String key, Object value);

Expand All @@ -64,23 +64,32 @@ public static<Q extends Query,R extends Collection> R profile(QueryProfiler prof
return profile(profiler,query,false,queryExecutor);
}

public static<Q extends Query,R extends Collection> R profile(String groupName, QueryProfiler profiler, Q query, Function<Q,R> queryExecutor) {
return profile(groupName,profiler,query,false,queryExecutor);
}

public static<Q extends Query,R extends Collection> R profile(QueryProfiler profiler, Q query, boolean multiQuery, Function<Q,R> queryExecutor) {
QueryProfiler sub = profiler.addNested();
return profile("backend-query",profiler,query,multiQuery,queryExecutor);
}

public static<Q extends Query,R extends Collection> R profile(String groupName, QueryProfiler profiler, Q query, boolean multiQuery, Function<Q,R> queryExecutor) {
QueryProfiler sub = profiler.addNested(groupName);
sub.setAnnotation(QUERY_ANNOTATION, query);
if (query.hasLimit()) sub.setAnnotation(LIMIT_ANNOTATION,query.getLimit());
sub.startTimer();
R result = queryExecutor.apply(query);
sub.stopTimer();
long resultSize = 0;
if (multiQuery && profiler!=QueryProfiler.NO_OP) {
//The result set is a collection of collections, but don't do this computation if profiling is disabled
long resultSize = 0;
for (Object r : result) {
if (r instanceof Collection) resultSize+=((Collection)r).size();
else resultSize++;
}
} else {
sub.setResultSize(result.size());
resultSize = result.size();
}
sub.setResultSize(resultSize);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ public class SimpleQueryProfiler implements QueryProfiler, Iterable<SimpleQueryP
private final List<SimpleQueryProfiler> nestedProfilers = new ArrayList<>();
private final Map<String,Object> annotations = new HashMap<>();

private final String groupName;
private long resultSize = 0;

private long startTimeNs = 0;
private boolean runningTimer = false;
private long measuredTimeNs = 0;

public SimpleQueryProfiler() {
this("__root");
}

public SimpleQueryProfiler(final String groupName) {
Preconditions.checkArgument(StringUtils.isNotBlank(groupName));
this.groupName=groupName;
}

@Override
public QueryProfiler addNested() {
SimpleQueryProfiler nested = new SimpleQueryProfiler();
public QueryProfiler addNested(String groupName) {
SimpleQueryProfiler nested = new SimpleQueryProfiler(groupName);
nestedProfilers.add(nested);
return nested;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ private Q addConstraint(String type, TitanPredicate rel, Object value) {
//Treat special cases
if (type.equals(ImplicitKey.ADJACENT_ID.name())) {
Preconditions.checkArgument(rel == Cmp.EQUAL,"Only equality constraints are supported for %s",type);
Preconditions.checkArgument(value instanceof Number,"Expected valid vertex id: %s",value);
return adjacent(getVertex(((Number)value).longValue()));
long vertexId = ElementUtils.getVertexId(value);
Preconditions.checkArgument(vertexId>0,"Expected valid vertex id: %s",value);
return adjacent(getVertex(vertexId));
} else if (type.equals(ImplicitKey.ID.name())) {
value = ElementUtils.getEdgeId(value);
return addConstraint(ImplicitKey.TITANID.name(),rel,((RelationIdentifier)value).getRelationId());
RelationIdentifier rid = ElementUtils.getEdgeId(value);
Preconditions.checkArgument(rid!=null,"Expected valid relation id: %s",value);
return addConstraint(ImplicitKey.TITANID.name(),rel,rid.getRelationId());
} else {
Preconditions.checkArgument(rel.isValidCondition(value),"Invalid condition provided: " + value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thinkaurelius.titan.graphdb.relations;

import com.google.common.base.Preconditions;
import com.thinkaurelius.titan.core.*;
import com.thinkaurelius.titan.graphdb.internal.InternalRelation;
import com.thinkaurelius.titan.graphdb.query.vertex.VertexCentricQueryBuilder;
Expand Down Expand Up @@ -49,6 +50,19 @@ public long getRelationId() {
return relationId;
}

public long getTypeId() {
return typeId;
}

public long getOutVertexId() {
return outVertexId;
}

public long getInVertexId() {
Preconditions.checkState(inVertexId!=0);
return inVertexId;
}

public static final RelationIdentifier get(long[] ids) {
if (ids.length != 3 && ids.length!=4) throw new IllegalArgumentException("Not a valid relation identifier: " + Arrays.toString(ids));
for (int i = 0; i < 3; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;

/**
* Blueprint's features of a TitanGraph.
*
Expand All @@ -26,8 +24,8 @@ public class TitanFeatures implements Graph.Features {
private final EdgeFeatures edgeFeatures;


private TitanFeatures() {
graphFeatures = new TitanGraphFeatures();
private TitanFeatures(boolean persists) {
graphFeatures = new TitanGraphFeatures(persists);
vertexFeatures = new TitanVertexFeatures();
edgeFeatures = new TitanEdgeFeatures();
}
Expand All @@ -53,11 +51,13 @@ public String toString() {
}


private static final TitanFeatures INSTANCE = new TitanFeatures();
private static final TitanFeatures PERSISTS_INSTANCE = new TitanFeatures(true);
private static final TitanFeatures INMEMORY_INSTANCE = new TitanFeatures(false);


public static TitanFeatures getFeatures(GraphDatabaseConfiguration config, StoreFeatures storageFeatures) {
return INSTANCE;
if (storageFeatures.supportsPersistence()) return PERSISTS_INSTANCE;
else return INMEMORY_INSTANCE;
}

private static class TitanDataTypeFeatures implements DataTypeFeatures {
Expand Down Expand Up @@ -87,6 +87,12 @@ private static class TitanVariableFeatures extends TitanDataTypeFeatures impleme

private static class TitanGraphFeatures extends TitanDataTypeFeatures implements GraphFeatures {

private final boolean persists;

private TitanGraphFeatures(boolean persists) {
this.persists = persists;
}

@Override
public VariableFeatures variables() {
return new TitanVariableFeatures();
Expand All @@ -99,7 +105,7 @@ public boolean supportsComputer() {

@Override
public boolean supportsPersistence() {
return true;
return persists;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TitanIoRegistry extends AbstractIoRegistry {

public static TitanIoRegistry INSTANCE = new TitanIoRegistry();

public TitanIoRegistry() {
private TitanIoRegistry() {
register(GraphSONIo.class, null, TitanGraphSONModule.getInstance());
register(GryoIo.class, RelationIdentifier.class, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.thinkaurelius.titan.core.attribute.Geoshape;
import com.thinkaurelius.titan.graphdb.relations.RelationIdentifier;
import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONTokens;

import java.io.IOException;

Expand Down Expand Up @@ -37,18 +38,22 @@ public RelationIdentifierSerializer() {
@Override
public void serialize(final RelationIdentifier relationIdentifier, final JsonGenerator jsonGenerator,
final SerializerProvider serializerProvider) throws IOException, JsonGenerationException {
ser(relationIdentifier, jsonGenerator);
jsonGenerator.writeString(relationIdentifier.toString());
}

@Override
public void serializeWithType(final RelationIdentifier relationIdentifier, final JsonGenerator jsonGenerator,
final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException, JsonProcessingException {
ser(relationIdentifier, jsonGenerator);
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(GraphSONTokens.CLASS, RelationIdentifier.class.getName());
long[] asLong = relationIdentifier.getLongRepresentation();
// 0=relationId, 1=outVertexId, 2=typeId, 3=inVertexId (optional)
jsonGenerator.writeNumberField("relationId",asLong[0]);
jsonGenerator.writeNumberField("outVertexId", asLong[1]);
jsonGenerator.writeNumberField("typeId", asLong[2]);
if (asLong.length>3) jsonGenerator.writeNumberField("inVertexId",asLong[3]);
jsonGenerator.writeEndObject();
}

private void ser(final RelationIdentifier relationIdentifier, final JsonGenerator jsonGenerator)
throws IOException {
jsonGenerator.writeString(relationIdentifier.toString());
}
}
}
Loading

0 comments on commit b26a9b4

Please sign in to comment.