use of org.umlg.sqlg.structure.SqlgGraph 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.SqlgGraph in project sqlg by pietermartin.
the class TestMultipleThreadMultipleJvm method testMultiThreadedSchemaCreation2.
@Test
public void testMultiThreadedSchemaCreation2() 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 {
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 {
sqlgGraphAsync.getTopology().ensureSchemaExist("schema_" + count);
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;
}));
}
}
poolPerGraph.shutdown();
for (Future<SqlgGraph> result : results) {
result.get(100, TimeUnit.SECONDS);
}
Thread.sleep(1000);
for (SqlgGraph graph : graphs) {
assertEquals(this.sqlgGraph.getTopology(), graph.getTopology());
}
} finally {
for (SqlgGraph graph : graphs) {
graph.close();
}
}
}
use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.
the class TestMultipleThreadMultipleJvm method testMultiThreadedLocking.
@Test
public void testMultiThreadedLocking() throws Exception {
// number graphs, pretending its a separate jvm
int NUMBER_OF_GRAPHS = 10;
ExecutorService sqlgGraphsExecutorService = Executors.newFixedThreadPool(100);
CompletionService<Boolean> sqlgGraphsExecutorCompletionService = new ExecutorCompletionService<>(sqlgGraphsExecutorService);
List<SqlgGraph> graphs = new ArrayList<>();
try {
// Pre-create all the graphs
for (int i = 0; i < NUMBER_OF_GRAPHS; i++) {
graphs.add(SqlgGraph.open(configuration));
}
List<Future<Boolean>> results = new ArrayList<>();
for (SqlgGraph sqlgGraphAsync : graphs) {
results.add(sqlgGraphsExecutorCompletionService.submit(() -> {
((SqlSchemaChangeDialect) sqlgGraphAsync.getSqlDialect()).lock(sqlgGraphAsync);
sqlgGraphAsync.tx().rollback();
return true;
}));
}
sqlgGraphsExecutorService.shutdown();
for (Future<Boolean> result : results) {
result.get(10, TimeUnit.SECONDS);
}
} finally {
for (SqlgGraph graph : graphs) {
graph.close();
}
}
}
use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.
the class TestMultipleThreadMultipleJvm method testMultiThreadedSchemaCreation.
@Test
public void testMultiThreadedSchemaCreation() 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 {
List<Future<SqlgGraph>> results = new ArrayList<>();
for (final SqlgGraph sqlgGraphAsync : graphs) {
results.add(poolPerGraphsExecutorCompletionService.submit(() -> {
for (int i = 0; i < NUMBER_OF_SCHEMAS; i++) {
// noinspection Duplicates
try {
sqlgGraphAsync.getTopology().ensureSchemaExist("schema_" + i);
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;
}));
}
poolPerGraph.shutdown();
for (Future<SqlgGraph> result : results) {
result.get(100, TimeUnit.SECONDS);
}
Thread.sleep(1000);
for (SqlgGraph graph : graphs) {
assertEquals(this.sqlgGraph.getTopology(), graph.getTopology());
for (Schema schema : graph.getTopology().getSchemas()) {
assertTrue(schema.isCommitted());
}
}
} finally {
for (SqlgGraph graph : graphs) {
graph.close();
}
}
}
use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.
the class TestLocalDate method testZonedDateTime.
@Test
public void testZonedDateTime() throws Exception {
ZoneId zoneIdShanghai = ZoneId.of("Asia/Shanghai");
ZonedDateTime zonedDateTimeAGT = ZonedDateTime.of(LocalDateTime.now(), zoneIdShanghai);
this.sqlgGraph.addVertex(T.label, "A", "zonedDateTime", zonedDateTimeAGT);
this.sqlgGraph.tx().commit();
// Create a new sqlgGraph
try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
List<? extends Property<ZonedDateTime>> properties = sqlgGraph1.traversal().V().hasLabel("A").<ZonedDateTime>properties("zonedDateTime").toList();
Assert.assertEquals(1, properties.size());
Assert.assertTrue(properties.get(0).isPresent());
ZonedDateTime value = properties.get(0).<ZonedDateTime>value();
Assert.assertEquals(zonedDateTimeAGT, value);
}
}
Aggregations