org.vertexium.sql.SqlGraph Maven / Gradle / Ivy
package org.vertexium.sql;
import com.google.common.collect.Iterables;
import org.vertexium.*;
import org.vertexium.inmemory.*;
import org.vertexium.mutation.SetPropertyMetadata;
import org.vertexium.property.StreamingPropertyValue;
import org.vertexium.property.StreamingPropertyValueRef;
import org.vertexium.sql.collections.SqlMap;
import org.vertexium.sql.collections.Storable;
import org.vertexium.util.ConvertingIterable;
import org.vertexium.util.LookAheadIterable;
import java.util.*;
public class SqlGraph extends InMemoryGraph {
private final SqlMap> vertexMap;
private final SqlMap> edgeMap;
private final SqlStreamingPropertyTable streamingPropertyTable;
private static class ConfigHolder {
final SqlGraphConfiguration configuration;
final SqlMap> vertexMap;
final SqlMap> edgeMap;
final SqlStreamingPropertyTable streamingPropertyTable;
ConfigHolder(SqlGraphConfiguration configuration) {
this.configuration = configuration;
this.vertexMap = configuration.newVertexMap();
this.edgeMap = configuration.newEdgeMap();
this.streamingPropertyTable = configuration.newStreamingPropertyTable();
}
}
public SqlGraph(SqlGraphConfiguration configuration) {
this(new ConfigHolder(configuration));
}
private SqlGraph(ConfigHolder configHolder) {
super(configHolder.configuration,
new SqlVertexTable(configHolder.vertexMap), new SqlEdgeTable(configHolder.edgeMap));
this.vertexMap = configHolder.vertexMap;
this.edgeMap = configHolder.edgeMap;
this.streamingPropertyTable = configHolder.streamingPropertyTable;
this.vertexMap.setStorableContext(this);
this.edgeMap.setStorableContext(this);
}
@Override
protected GraphMetadataStore newGraphMetadataStore(GraphConfiguration configuration) {
return new SqlGraphMetadataStore(((SqlGraphConfiguration) configuration).newMetadataMap());
}
public static SqlGraph create(SqlGraphConfiguration config) {
SqlGraph graph = new SqlGraph(config);
graph.setup();
return graph;
}
@SuppressWarnings("unused")
public static SqlGraph create(Map config) {
return create(new SqlGraphConfiguration(config));
}
@Override
public Vertex getVertex(String vertexId, EnumSet fetchHints, Long endTime,
Authorizations authorizations) {
validateAuthorizations(authorizations);
InMemoryTableElement element = vertexMap.get(vertexId);
if (element == null || !isIncludedInTimeSpan(element, fetchHints, endTime, authorizations)) {
return null;
} else {
return element.createElement(this, fetchHints.contains(FetchHint.INCLUDE_HIDDEN), endTime, authorizations);
}
}
@Override
public Iterable getVerticesWithPrefix(final String vertexIdPrefix, final EnumSet fetchHints,
final Long endTime, final Authorizations authorizations) {
validateAuthorizations(authorizations);
final boolean includeHidden = fetchHints.contains(FetchHint.INCLUDE_HIDDEN);
return new LookAheadIterable() {
@Override
protected boolean isIncluded(InMemoryTableVertex element, Vertex vertex) {
return vertex != null && SqlGraph.this.isIncluded(element, fetchHints, authorizations);
}
@Override
protected Vertex convert(InMemoryTableVertex element) {
return element.createElement(SqlGraph.this, includeHidden, endTime, authorizations);
}
@Override
protected Iterator createIterator() {
Iterator> elements = vertexMap.query("id like ?",
vertexIdPrefix + "%");
return new ConvertingIterable, InMemoryTableVertex>(elements) {
@Override
protected InMemoryTableVertex convert(InMemoryTableElement element) {
return ((SqlTableVertex) element).asInMemoryTableElement();
}
}.iterator();
}
};
}
@Override
public Edge getEdge(String edgeId, EnumSet fetchHints, Long endTime, Authorizations authorizations) {
InMemoryTableElement element = edgeMap.get(edgeId);
if (element == null || !isIncluded(element, fetchHints, authorizations)) {
return null;
} else {
return element.createElement(this, fetchHints.contains(FetchHint.INCLUDE_HIDDEN), endTime, authorizations);
}
}
@SuppressWarnings("unchecked")
@Override
public Iterable getVertices(final Iterable ids, final EnumSet fetchHints,
final Long endTime, final Authorizations authorizations) {
return (Iterable) getElements(ids, fetchHints, endTime, authorizations, vertexMap);
}
@SuppressWarnings("unchecked")
@Override
public Iterable getEdges(final Iterable ids, final EnumSet fetchHints, final Long endTime,
final Authorizations authorizations) {
return (Iterable) getElements(ids, fetchHints, endTime, authorizations, edgeMap);
}
private Iterable> getElements(final Iterable ids,
final EnumSet fetchHints, final Long endTime,
final Authorizations authorizations,
final SqlMap> sqlMap) {
final boolean includeHidden = fetchHints.contains(FetchHint.INCLUDE_HIDDEN);
return new LookAheadIterable() {
@Override
protected boolean isIncluded(InMemoryTableElement srcElement, T destElement) {
return destElement != null && SqlGraph.this.isIncluded(srcElement, fetchHints, authorizations);
}
@SuppressWarnings("unchecked")
@Override
protected T convert(InMemoryTableElement element) {
return (T) element.createElement(SqlGraph.this, includeHidden, endTime, authorizations);
}
@SuppressWarnings("unused")
@Override
protected Iterator createIterator() {
StringBuilder idWhere = new StringBuilder();
boolean first = true;
for (String id : ids) {
if (first) {
idWhere.append("id = ?");
first = false;
} else {
idWhere.append(" or id = ?");
}
}
if (first) {
return Collections.emptyIterator();
} else {
Iterator> elements = sqlMap.query(idWhere.toString(),
Iterables.toArray(ids, Object.class));
return new ConvertingIterable(elements) {
@Override
protected InMemoryTableElement convert(InMemoryTableElement element) {
return ((SqlTableElement) element).asInMemoryTableElement();
}
}.iterator();
}
}
};
}
@Override
public Iterable getEdgesFromVertex(final String vertexId, final EnumSet fetchHints,
final Long endTime, final Authorizations authorizations) {
final boolean includeHidden = fetchHints.contains(FetchHint.INCLUDE_HIDDEN);
return new LookAheadIterable() {
@Override
protected boolean isIncluded(InMemoryTableEdge element, Edge edge) {
return edge != null && SqlGraph.this.isIncluded(element, fetchHints, authorizations);
}
@Override
protected Edge convert(InMemoryTableEdge element) {
return element.createElement(SqlGraph.this, includeHidden, endTime, authorizations);
}
@Override
protected Iterator createIterator() {
Iterator> elements =
edgeMap.query("in_vertex_id = ? or out_vertex_id = ?", vertexId, vertexId);
return new ConvertingIterable, InMemoryTableEdge>(elements) {
@Override
protected InMemoryTableEdge convert(InMemoryTableElement element) {
return ((SqlTableEdge) element).asInMemoryTableElement();
}
}.iterator();
}
};
}
@Override
protected void alterElementPropertyMetadata(InMemoryTableElement inMemoryTableElement,
List setPropertyMetadatas,
Authorizations authorizations) {
super.alterElementPropertyMetadata(inMemoryTableElement, setPropertyMetadatas, authorizations);
((Storable) inMemoryTableElement).store();
}
protected SqlStreamingPropertyTable getStreamingPropertyTable() {
return streamingPropertyTable;
}
@Override
protected StreamingPropertyValueRef saveStreamingPropertyValue(String elementId, String key, String name,
Visibility visibility, long timestamp,
StreamingPropertyValue value) {
return streamingPropertyTable.put(elementId, key, name, visibility, timestamp, value);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy