use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class VertexApiTest method testOlapPropertyWrite.
@Test
public void testOlapPropertyWrite() {
List<Vertex> vertices = super.create100PersonBatch();
List<Object> ids = vertexAPI.create(vertices);
// Create olap property key
PropertyKey pagerank = schema().propertyKey("pagerank").asDouble().writeType(WriteType.OLAP_RANGE).build();
PropertyKey.PropertyKeyWithTask propertyKeyWithTask;
propertyKeyWithTask = propertyKeyAPI.create(pagerank);
long taskId1 = propertyKeyWithTask.taskId();
Assert.assertNotEquals(0L, taskId1);
PropertyKey wcc = schema().propertyKey("wcc").asText().writeType(WriteType.OLAP_SECONDARY).build();
propertyKeyWithTask = propertyKeyAPI.create(wcc);
long taskId2 = propertyKeyWithTask.taskId();
Assert.assertNotEquals(0L, taskId2);
PropertyKey none = schema().propertyKey("none").asText().writeType(WriteType.OLAP_COMMON).build();
propertyKeyWithTask = propertyKeyAPI.create(none);
long taskId3 = propertyKeyWithTask.taskId();
Assert.assertNotEquals(0L, taskId3);
waitUntilTaskCompleted(taskId1);
waitUntilTaskCompleted(taskId2);
waitUntilTaskCompleted(taskId3);
// Add olap properties
vertices = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
Vertex vertex = new Vertex(null);
vertex.id(ids.get(i));
vertex.property("pagerank", 0.1D * i);
vertices.add(vertex);
}
ids = vertexAPI.create(vertices);
Assert.assertEquals(100, ids.size());
vertices = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
Vertex vertex = new Vertex(null);
vertex.id(ids.get(i));
vertex.property("wcc", "wcc" + i);
vertices.add(vertex);
}
ids = vertexAPI.create(vertices);
Assert.assertEquals(100, ids.size());
vertices = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
Vertex vertex = new Vertex(null);
vertex.id(ids.get(i));
vertex.property("none", "none" + i);
vertices.add(vertex);
}
ids = vertexAPI.create(vertices);
Assert.assertEquals(100, ids.size());
// Query vertices by id before set graph read mode to 'ALL'
for (int i = 0; i < 100; i++) {
Vertex person = vertexAPI.get(ids.get(i));
Assert.assertEquals("person", person.label());
Map<String, Object> props = ImmutableMap.of("name", "Person-" + i, "city", "Beijing", "age", 30);
Assert.assertEquals(props, person.properties());
}
// Set graph read mode to 'ALL'
graphsAPI.readMode("hugegraph", GraphReadMode.ALL);
// Query vertices by id after set graph read mode to 'ALL'
for (int i = 0; i < 100; i++) {
Vertex person = vertexAPI.get(ids.get(i));
Assert.assertEquals("person", person.label());
Map<String, Object> props = ImmutableMap.<String, Object>builder().put("name", "Person-" + i).put("city", "Beijing").put("age", 30).put("pagerank", 0.1D * i).put("wcc", "wcc" + i).put("none", "none" + i).build();
Assert.assertEquals(props, person.properties());
}
// Query vertices by olap properties
GremlinRequest request = new GremlinRequest("g.V().has(\"pagerank\", P.gte(5))");
ResultSet resultSet = gremlin().execute(request);
Assert.assertEquals(50, resultSet.size());
request = new GremlinRequest("g.V().has(\"wcc\", P.within(\"wcc10\", \"wcc20\"))");
resultSet = gremlin().execute(request);
Assert.assertEquals(2, resultSet.size());
// Clear olap property key
propertyKeyWithTask = propertyKeyAPI.clear(pagerank);
taskId1 = propertyKeyWithTask.taskId();
Assert.assertNotEquals(0L, taskId1);
propertyKeyWithTask = propertyKeyAPI.clear(wcc);
taskId2 = propertyKeyWithTask.taskId();
Assert.assertNotEquals(0L, taskId2);
propertyKeyWithTask = propertyKeyAPI.clear(none);
taskId3 = propertyKeyWithTask.taskId();
Assert.assertNotEquals(0L, taskId3);
waitUntilTaskCompleted(taskId1);
waitUntilTaskCompleted(taskId2);
waitUntilTaskCompleted(taskId3);
// Query after clear olap property key
request = new GremlinRequest("g.V().has(\"pagerank\", P.gte(5))");
resultSet = gremlin().execute(request);
Assert.assertEquals(0, resultSet.size());
request = new GremlinRequest("g.V().has(\"wcc\", P.within(\"wcc10\", \"wcc20\"))");
resultSet = gremlin().execute(request);
Assert.assertEquals(0, resultSet.size());
// Delete olap property key
taskId1 = propertyKeyAPI.delete(pagerank.name());
Assert.assertNotEquals(0L, taskId1);
taskId2 = propertyKeyAPI.delete(wcc.name());
Assert.assertNotEquals(0L, taskId2);
taskId3 = propertyKeyAPI.delete(none.name());
Assert.assertNotEquals(0L, taskId3);
waitUntilTaskCompleted(taskId1);
waitUntilTaskCompleted(taskId2);
waitUntilTaskCompleted(taskId3);
// Query after delete olap property key
Assert.assertThrows(ServerException.class, () -> {
gremlin().execute(new GremlinRequest("g.V().has(\"pagerank\", P.gte(5))"));
}, e -> {
Assert.assertContains("Undefined property key: 'pagerank'", e.getMessage());
});
Assert.assertThrows(ServerException.class, () -> {
gremlin().execute(new GremlinRequest("g.V().has(\"wcc\", P.within(\"wcc10\", \"wcc20\"))"));
}, e -> {
Assert.assertContains("Undefined property key: 'wcc'", e.getMessage());
});
// Resume graph read mode to 'OLTP_ONLY'
graphsAPI.readMode("hugegraph", GraphReadMode.OLTP_ONLY);
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinApiTest method testAsyncRemoveAllVertices.
@Test
public void testAsyncRemoveAllVertices() {
GremlinRequest request = new GremlinRequest("g.V()");
ResultSet resultSet = gremlin().execute(request);
Assert.assertEquals(6, resultSet.size());
String gremlin = "hugegraph.traversal().V().drop()";
request = new GremlinRequest(gremlin);
long id = gremlin().executeAsTask(request);
waitUntilTaskCompleted(id);
request = new GremlinRequest("g.V()");
resultSet = gremlin().execute(request);
Assert.assertEquals(0, resultSet.size());
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinApiTest method testPrimitiveObject.
@Test
public void testPrimitiveObject() {
GremlinRequest request = new GremlinRequest("1 + 2");
ResultSet resultSet = gremlin().execute(request);
Assert.assertEquals(1, resultSet.size());
Iterator<Result> results = resultSet.iterator();
while (results.hasNext()) {
Result result = results.next();
Object object = result.getObject();
Assert.assertEquals(Integer.class, object.getClass());
Assert.assertEquals(3, object);
}
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinApiTest method testAttachedManager.
@Test
public void testAttachedManager() {
GremlinRequest request = new GremlinRequest("g.V()");
ResultSet resultSet = gremlin().execute(request);
Assert.assertEquals(6, resultSet.size());
Iterator<Result> results = resultSet.iterator();
while (results.hasNext()) {
Result result = results.next();
Object object = result.getObject();
Assert.assertEquals(Vertex.class, object.getClass());
Vertex vertex = (Vertex) object;
Assert.assertNotNull(Whitebox.getInternalState(vertex, "manager"));
}
request = new GremlinRequest("g.E()");
resultSet = gremlin().execute(request);
Assert.assertEquals(6, resultSet.size());
results = resultSet.iterator();
while (results.hasNext()) {
Result result = results.next();
Object object = result.getObject();
Assert.assertEquals(Edge.class, object.getClass());
Edge edge = (Edge) object;
Assert.assertNotNull(Whitebox.getInternalState(edge, "manager"));
}
request = new GremlinRequest("g.V().outE().path()");
resultSet = gremlin().execute(request);
Assert.assertEquals(6, resultSet.size());
results = resultSet.iterator();
while (results.hasNext()) {
Result result = results.next();
Object object = result.getObject();
Assert.assertEquals(Path.class, object.getClass());
Path path = (Path) object;
Assert.assertNotNull(path.objects());
for (Object pathObject : path.objects()) {
Assert.assertTrue(pathObject instanceof GraphAttachable);
Assert.assertNotNull(Whitebox.getInternalState(pathObject, "manager"));
}
Assert.assertNull(path.crosspoint());
}
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class HugeClientUtil method tryConnect.
public static HugeClient tryConnect(GraphConnection connection) {
String graph = connection.getGraph();
String host = connection.getHost();
Integer port = connection.getPort();
String username = connection.getUsername();
String password = connection.getPassword();
int timeout = connection.getTimeout();
String protocol = connection.getProtocol() == null ? DEFAULT_PROTOCOL : connection.getProtocol();
String trustStoreFile = connection.getTrustStoreFile();
String trustStorePassword = connection.getTrustStorePassword();
String url = UriComponentsBuilder.newInstance().scheme(protocol).host(host).port(port).toUriString();
if (username == null) {
username = "";
password = "";
}
HugeClient client;
try {
client = HugeClient.builder(url, graph).configUser(username, password).configTimeout(timeout).configSSL(trustStoreFile, trustStorePassword).build();
} catch (IllegalStateException e) {
String message = e.getMessage();
if (message != null && message.startsWith("The version")) {
throw new ExternalException("client-server.version.unmatched", e);
}
if (message != null && (message.startsWith("Error loading trust store from") || message.startsWith("Cannot find trust store file"))) {
throw new ExternalException("https.load.truststore.error", e);
}
throw e;
} catch (ServerException e) {
String message = e.getMessage();
if (Constant.STATUS_UNAUTHORIZED == e.status() || (message != null && message.startsWith("Authentication"))) {
throw new ExternalException("graph-connection.username-or-password.incorrect", e);
}
if (message != null && message.contains("Invalid syntax for " + "username and password")) {
throw new ExternalException("graph-connection.missing-username-password", e);
}
throw e;
} catch (ClientException e) {
Throwable cause = e.getCause();
if (cause == null || cause.getMessage() == null) {
throw e;
}
String message = cause.getMessage();
if (message.contains("Connection refused")) {
throw new ExternalException("service.unavailable", e, host, port);
} else if (message.contains("java.net.UnknownHostException") || message.contains("Host name may not be null")) {
throw new ExternalException("service.unknown-host", e, host);
} else if (message.contains("<!doctype html>")) {
throw new ExternalException("service.suspected-web", e, host, port);
}
throw e;
}
try {
ResultSet rs = client.gremlin().gremlin("g.V().limit(1)").execute();
rs.iterator().forEachRemaining(Result::getObject);
} catch (ServerException e) {
if (Constant.STATUS_UNAUTHORIZED == e.status()) {
throw new ExternalException("graph-connection.username-or-password.incorrect", e);
}
String message = e.message();
if (message != null && message.contains("Could not rebind [g]")) {
throw new ExternalException("graph-connection.graph.unexist", e, graph, host, port);
}
if (!isAcceptable(message)) {
throw e;
}
} catch (Exception e) {
client.close();
throw e;
}
return client;
}
Aggregations