Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
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 SqlExtendedDataTable extendedDataTable;
final SqlStreamingPropertyTable streamingPropertyTable;
ConfigHolder(SqlGraphConfiguration configuration) {
this.configuration = configuration;
this.vertexMap = configuration.newVertexMap();
this.edgeMap = configuration.newEdgeMap();
this.extendedDataTable = configuration.newExtendedDataTable();
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),
configHolder.extendedDataTable
);
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) {
if (config.isCreateTables()) {
SqlGraphDDL.create(config.getDataSource(), 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, endTime, authorizations);
}
}
@Override
public Iterable getVerticesWithPrefix(
final String vertexIdPrefix, final EnumSet fetchHints,
final Long endTime, final Authorizations authorizations
) {
validateAuthorizations(authorizations);
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, fetchHints, 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, 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(
Iterable ids,
EnumSet fetchHints,
Long endTime,
Authorizations authorizations
) {
return (Iterable) getElements(ids, fetchHints, endTime, authorizations, edgeMap);
}
private Iterable> getElements(
Iterable ids,
EnumSet fetchHints,
Long endTime,
Authorizations authorizations,
SqlMap> sqlMap
) {
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, fetchHints, 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(
String vertexId,
EnumSet fetchHints,
Long endTime,
Authorizations authorizations
) {
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, fetchHints, 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);
}
}