use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.
the class SchemaTableTree method hasOnlyOneInOutEdgeLabel.
private boolean hasOnlyOneInOutEdgeLabel(SchemaTable schemaTable) {
Optional<Schema> schemaOptional = sqlgGraph.getTopology().getSchema(schemaTable.getSchema());
Preconditions.checkState(schemaOptional.isPresent(), "BUG: %s not found in the topology.", schemaTable.getSchema());
Schema schema = schemaOptional.get();
boolean result = true;
if (schemaTable.isVertexTable()) {
// Need to delete any in/out edges.
Optional<VertexLabel> vertexLabelOptional = schema.getVertexLabel(schemaTable.withOutPrefix().getTable());
Preconditions.checkState(vertexLabelOptional.isPresent(), "BUG: %s not found in the topology.", schemaTable.withOutPrefix().getTable());
VertexLabel vertexLabel = vertexLabelOptional.get();
Collection<EdgeLabel> outEdgeLabels = vertexLabel.getOutEdgeLabels().values();
for (EdgeLabel edgeLabel : outEdgeLabels) {
result = edgeLabel.getOutVertexLabels().size() == 1;
if (!result) {
break;
}
}
if (result) {
Collection<EdgeLabel> inEdgeLabels = vertexLabel.getInEdgeLabels().values();
for (EdgeLabel edgeLabel : inEdgeLabels) {
result = edgeLabel.getInVertexLabels().size() == 1;
if (!result) {
break;
}
}
}
}
return result;
}
use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.
the class SqlgGraph method createVertexLabeledIndex.
/**
* @deprecated Please use {@link Topology#ensureVertexLabelExist(String, Map)} and {@link VertexLabel#ensureIndexExists(IndexType, List)}.
*/
@Deprecated
public void createVertexLabeledIndex(String label, Object... dummykeyValues) {
Map<String, PropertyType> columns = SqlgUtil.transformToColumnDefinitionMap(dummykeyValues);
SchemaTable schemaTablePair = SchemaTable.from(this, label);
VertexLabel vertexLabel = this.getTopology().ensureVertexLabelExist(schemaTablePair.getSchema(), schemaTablePair.getTable(), columns);
List<PropertyColumn> properties = new ArrayList<>();
List<String> keys = SqlgUtil.transformToKeyList(dummykeyValues);
for (String key : keys) {
properties.add(vertexLabel.getProperty(key).get());
}
vertexLabel.ensureIndexExists(IndexType.NON_UNIQUE, properties);
}
use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.
the class TestMultipleThreadMultipleJvm method testMultiThreadedVertexLabelCreation.
@Test
public void testMultiThreadedVertexLabelCreation() throws Exception {
// number graphs, pretending its a separate jvm
int NUMBER_OF_GRAPHS = 5;
int NUMBER_OF_SCHEMAS = 100;
// Pre-create all the graphs
List<SqlgGraph> graphs = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_GRAPHS; i++) {
graphs.add(SqlgGraph.open(configuration));
}
logger.info(String.format("Done firing up %d graphs", NUMBER_OF_GRAPHS));
ExecutorService poolPerGraph = Executors.newFixedThreadPool(NUMBER_OF_GRAPHS);
CompletionService<SqlgGraph> poolPerGraphsExecutorCompletionService = new ExecutorCompletionService<>(poolPerGraph);
try {
Map<String, PropertyType> properties = new HashMap<>();
properties.put("name", PropertyType.STRING);
properties.put("age", PropertyType.INTEGER);
List<Future<SqlgGraph>> results = new ArrayList<>();
for (final SqlgGraph sqlgGraphAsync : graphs) {
for (int i = 0; i < NUMBER_OF_SCHEMAS; i++) {
final int count = i;
results.add(poolPerGraphsExecutorCompletionService.submit(() -> {
// noinspection Duplicates
try {
VertexLabel outVertexLabel = sqlgGraphAsync.getTopology().ensureVertexLabelExist("schema_" + count, "tableOut_" + count, properties);
VertexLabel inVertexLabel = sqlgGraphAsync.getTopology().ensureVertexLabelExist("schema_" + count, "tableIn_" + count, properties);
sqlgGraphAsync.getTopology().ensureEdgeLabelExist("edge_" + count, outVertexLabel, inVertexLabel, properties);
final Random random = new Random();
if (random.nextBoolean()) {
sqlgGraphAsync.tx().commit();
} else {
sqlgGraphAsync.tx().rollback();
}
} catch (Exception e) {
sqlgGraphAsync.tx().rollback();
throw new RuntimeException(e);
}
return sqlgGraphAsync;
}));
}
}
for (Future<SqlgGraph> result : results) {
result.get(5, TimeUnit.MINUTES);
}
Thread.sleep(1000);
for (SqlgGraph graph : graphs) {
assertEquals(this.sqlgGraph.getTopology(), graph.getTopology());
assertEquals(this.sqlgGraph.getTopology().toJson(), graph.getTopology().toJson());
}
logger.info("starting inserting data");
for (final SqlgGraph sqlgGraphAsync : graphs) {
for (int i = 0; i < NUMBER_OF_SCHEMAS; i++) {
final int count = i;
results.add(poolPerGraphsExecutorCompletionService.submit(() -> {
// noinspection Duplicates
try {
Vertex v1 = sqlgGraphAsync.addVertex(T.label, "schema_" + count + "." + "tableOut_" + count, "name", "asdasd", "age", 1);
Vertex v2 = sqlgGraphAsync.addVertex(T.label, "schema_" + count + "." + "tableIn_" + count, "name", "asdasd", "age", 1);
v1.addEdge("edge_" + count, v2, "name", "asdasd", "age", 1);
final Random random = new Random();
if (random.nextBoolean()) {
sqlgGraphAsync.tx().rollback();
} else {
sqlgGraphAsync.tx().commit();
}
} catch (Exception e) {
sqlgGraphAsync.tx().rollback();
throw new RuntimeException(e);
}
return sqlgGraphAsync;
}));
}
}
poolPerGraph.shutdown();
for (Future<SqlgGraph> result : results) {
result.get(30, TimeUnit.SECONDS);
}
// Because of the rollBack logic the insert code may also create topology elements, so sleep a bit for notify to do its thing.
Thread.sleep(1000);
logger.info("starting querying data");
Set<Vertex> vertices = this.sqlgGraph.traversal().V().out().toSet();
this.sqlgGraph.tx().rollback();
for (SqlgGraph graph : graphs) {
logger.info("assert querying data");
Set<Vertex> actual = graph.traversal().V().out().toSet();
logger.info("vertices.size = " + vertices.size() + " actual.size = " + actual.size());
assertEquals(vertices, actual);
graph.tx().rollback();
}
} finally {
for (SqlgGraph graph : graphs) {
graph.close();
}
}
}
use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.
the class TestOrStep method testOrFullText.
@Test
public void testOrFullText() {
Assume.assumeTrue(isPostgres());
Vertex v0 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "a fat cat sat on a mat and ate a fat rat");
Vertex v1 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "fatal error");
Vertex v2 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "error is not fatal");
VertexLabel vl = this.sqlgGraph.getTopology().getVertexLabel("public", "Sentence").get();
vl.ensureIndexExists(IndexType.getFullTextGIN("english"), Collections.singletonList(vl.getProperty("name").get()));
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("Sentence").or(__.has("name", FullText.fullTextMatch("english", "fat & rat")), __.has("name", "fatal error"));
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(1, traversal.getSteps().size());
Assert.assertEquals(2, vertices.size());
Assert.assertTrue(vertices.containsAll(Arrays.asList(v0, v1)));
traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("Sentence").or(__.has("name", FullText.fullTextMatch("english", "fat & cow")), __.has("name", "fatal error"));
vertices = traversal.toList();
Assert.assertEquals(1, traversal.getSteps().size());
Assert.assertEquals(1, vertices.size());
Assert.assertTrue(vertices.containsAll(Arrays.asList(v1)));
}
use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.
the class TestGremlinCompileFullTextPredicate method testDocExamplesWhere.
@Test
public void testDocExamplesWhere() throws SQLException {
assumeTrue(isPostgres());
Vertex v0 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "a fat cat sat on a mat and ate a fat rat");
Vertex v1 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "fatal error");
Vertex v2 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "error is not fatal");
VertexLabel vl = this.sqlgGraph.getTopology().getVertexLabel("public", "Sentence").get();
vl.ensureIndexExists(IndexType.getFullTextGIN("english"), Collections.singletonList(vl.getProperty("name").get()));
this.sqlgGraph.tx().commit();
List<Vertex> vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").where(FullText.fullTextMatch("english", false, "name", "fat & rat")).toList();
assertEquals(1, vts.size());
assertTrue(vts.contains(v0));
vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").where(FullText.fullTextMatch("english", false, "name", "fat & cow")).toList();
assertEquals(0, vts.size());
vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").where(FullText.fullTextMatch("english", false, "name", "fatal <-> error")).toList();
assertEquals(1, vts.size());
assertTrue(vts.contains(v1));
vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").where(FullText.fullTextMatch("english", false, "name", "fatal & error")).toList();
assertEquals(2, vts.size());
assertTrue(vts.contains(v1));
assertTrue(vts.contains(v2));
vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").as("a").where("a", FullText.fullTextMatch("english", true, "name", "fatal error")).toList();
assertEquals(2, vts.size());
assertTrue(vts.contains(v1));
assertTrue(vts.contains(v2));
vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").as("a").where("a", FullText.fullTextMatch("english", false, "name", "!cat")).toList();
assertEquals(2, vts.size());
assertTrue(vts.contains(v1));
assertTrue(vts.contains(v2));
/*vts=this.sqlgGraph.traversal().V().hasLabel("Sentence").or(
__.where(FullText.fullTextMatch("english",false,"name", "fat & rat")),
__.where(FullText.fullTextMatch("english",false,"name", "fatal <-> error"))
).toList();
assertEquals(2,vts.size());
assertTrue(vts.contains(v0));
assertTrue(vts.contains(v1));*/
v2.addEdge("testEdge", v0);
this.sqlgGraph.tx().commit();
vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").has("name", "error is not fatal").out("testEdge").where(FullText.fullTextMatch("english", false, "name", "fat & rat")).toList();
assertEquals(1, vts.size());
assertTrue(vts.contains(v0));
}
Aggregations