use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class OSQLFunctionAstarTest method testSql.
@Test
public void testSql() {
Iterable r = graph.command(new OSQLSynchQuery("select expand(astar(" + v1.getId() + ", " + v4.getId() + ", 'weight', {'direction':'out', 'parallel':true, 'edgeTypeNames':'has_path'}))")).execute();
List result = new ArrayList();
for (Object x : r) {
result.add(x);
}
assertEquals(16, graph.countEdges("has_path"));
assertEquals(4, result.size());
assertEquals(v1, result.get(0));
assertEquals(v2, result.get(1));
assertEquals(v3, result.get(2));
assertEquals(v4, result.get(3));
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method command.
protected void command(OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "Execute remote command");
byte type = channel.readByte();
final boolean live = type == 'l';
final boolean asynch = type == 'a';
if (connection == null && connection.getDatabase() == null)
throw new IOException("Found invalid session");
String dbSerializerName = connection.getDatabase().getSerializer().toString();
String name = getRecordSerializerName(connection);
if (!dbSerializerName.equals(name)) {
ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(name);
ONetworkThreadLocalSerializer.setNetworkSerializer(ser);
}
OCommandRequestText command = (OCommandRequestText) OStreamSerializerAnyStreamable.INSTANCE.fromStream(channel.readBytes());
ONetworkThreadLocalSerializer.setNetworkSerializer(null);
final Map<Object, Object> params = command.getParameters();
if (asynch && command instanceof OSQLSynchQuery) {
// CONVERT IT IN ASYNCHRONOUS QUERY
final OSQLAsynchQuery asynchQuery = new OSQLAsynchQuery(command.getText());
asynchQuery.setFetchPlan(command.getFetchPlan());
asynchQuery.setLimit(command.getLimit());
asynchQuery.setTimeout(command.getTimeoutTime(), command.getTimeoutStrategy());
asynchQuery.setUseCache(((OSQLSynchQuery) command).isUseCache());
command = asynchQuery;
}
connection.getData().commandDetail = command.getText();
beginResponse();
try {
connection.getData().command = command;
OAbstractCommandResultListener listener = null;
OLiveCommandResultListener liveListener = null;
OCommandResultListener cmdResultListener = command.getResultListener();
if (live) {
liveListener = new OLiveCommandResultListener(server, connection, clientTxId, cmdResultListener);
listener = new OSyncCommandResultListener(null);
command.setResultListener(liveListener);
} else if (asynch) {
// IF COMMAND CACHE IS ENABLED, RESULT MUST BE COLLECTED
final OCommandCache cmdCache = connection.getDatabase().getMetadata().getCommandCache();
if (cmdCache.isEnabled())
// CREATE E COLLECTOR OF RESULT IN RAM TO CACHE THE RESULT
cmdResultListener = new OCommandCacheRemoteResultListener(cmdResultListener, cmdCache);
listener = new OAsyncCommandResultListener(connection, this, clientTxId, cmdResultListener);
command.setResultListener(listener);
} else {
listener = new OSyncCommandResultListener(null);
}
final long serverTimeout = OGlobalConfiguration.COMMAND_TIMEOUT.getValueAsLong();
if (serverTimeout > 0 && command.getTimeoutTime() > serverTimeout)
// FORCE THE SERVER'S TIMEOUT
command.setTimeout(serverTimeout, command.getTimeoutStrategy());
if (!isConnectionAlive(connection))
return;
// REQUEST CAN'T MODIFY THE RESULT, SO IT'S CACHEABLE
command.setCacheableResult(true);
// ASSIGNED THE PARSED FETCHPLAN
final OCommandRequest commandImpl = connection.getDatabase().command(command);
listener.setFetchPlan(commandImpl.getFetchPlan());
final Object result;
if (params == null)
result = commandImpl.execute();
else
result = commandImpl.execute(params);
// FETCHPLAN HAS TO BE ASSIGNED AGAIN, because it can be changed by SQL statement
listener.setFetchPlan(commandImpl.getFetchPlan());
if (asynch) {
// ASYNCHRONOUS
if (listener.isEmpty())
try {
sendOk(connection, clientTxId);
} catch (IOException ignored) {
}
// NO MORE RECORDS
channel.writeByte((byte) 0);
} else {
// SYNCHRONOUS
sendOk(connection, clientTxId);
boolean isRecordResultSet = true;
if (command instanceof OCommandRequestInternal)
isRecordResultSet = command.isRecordResultSet();
serializeValue(connection, listener, result, false, isRecordResultSet);
if (listener instanceof OSyncCommandResultListener) {
// SEND FETCHED RECORDS TO LOAD IN CLIENT CACHE
for (ORecord rec : ((OSyncCommandResultListener) listener).getFetchedRecordsToSend()) {
// CLIENT CACHE RECORD. IT
channel.writeByte((byte) 2);
// ISN'T PART OF THE
// RESULT SET
writeIdentifiable(connection, rec);
}
// NO MORE RECORDS
channel.writeByte((byte) 0);
}
}
} finally {
connection.getData().command = null;
endResponse(connection);
}
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class CompositeIndexWithNullTest method testPointQueryInTx.
public void testPointQueryInTx() {
final OSchema schema = database.getMetadata().getSchema();
OClass clazz = schema.createClass("compositeIndexNullPointQueryInTxClass");
clazz.createProperty("prop1", OType.INTEGER);
clazz.createProperty("prop2", OType.INTEGER);
clazz.createProperty("prop3", OType.INTEGER);
final ODocument metadata = new ODocument();
metadata.field("ignoreNullValues", false);
clazz.createIndex("compositeIndexNullPointQueryInTxIndex", OClass.INDEX_TYPE.NOTUNIQUE.toString(), null, metadata, new String[] { "prop1", "prop2", "prop3" });
database.begin();
for (int i = 0; i < 20; i++) {
ODocument document = new ODocument("compositeIndexNullPointQueryInTxClass");
document.field("prop1", i / 10);
document.field("prop2", i / 5);
if (i % 2 == 0)
document.field("prop3", i);
document.save();
}
database.commit();
String query = "select from compositeIndexNullPointQueryInTxClass where prop1 = 1 and prop2 = 2";
List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>(query));
Assert.assertEquals(result.size(), 5);
for (int k = 0; k < 5; k++) {
ODocument document = result.get(k);
Assert.assertEquals(document.field("prop1"), 1);
Assert.assertEquals(document.field("prop2"), 2);
}
ODocument explain = database.command(new OCommandSQL("explain " + query)).execute();
Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("compositeIndexNullPointQueryInTxIndex"));
query = "select from compositeIndexNullPointQueryInTxClass where prop1 = 1 and prop2 = 2 and prop3 is null";
result = database.query(new OSQLSynchQuery<ODocument>(query));
Assert.assertEquals(result.size(), 2);
for (ODocument document : result) Assert.assertNull(document.field("prop3"));
explain = database.command(new OCommandSQL("explain " + query)).execute();
Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("compositeIndexNullPointQueryInTxIndex"));
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class CompositeIndexWithNullTest method testRangeQueryInMiddleTx.
public void testRangeQueryInMiddleTx() {
if (database.getURL().startsWith("remote:"))
return;
final OSchema schema = database.getMetadata().getSchema();
OClass clazz = schema.createClass("compositeIndexNullRangeQueryInMiddleTxClass");
clazz.createProperty("prop1", OType.INTEGER);
clazz.createProperty("prop2", OType.INTEGER);
clazz.createProperty("prop3", OType.INTEGER);
final ODocument metadata = new ODocument();
metadata.field("ignoreNullValues", false);
clazz.createIndex("compositeIndexNullRangeQueryInMiddleTxIndex", OClass.INDEX_TYPE.NOTUNIQUE.toString(), null, metadata, null, new String[] { "prop1", "prop2", "prop3" });
database.begin();
for (int i = 0; i < 20; i++) {
ODocument document = new ODocument("compositeIndexNullRangeQueryInMiddleTxClass");
document.field("prop1", i / 10);
document.field("prop2", i / 5);
if (i % 2 == 0)
document.field("prop3", i);
document.save();
}
String query = "select from compositeIndexNullRangeQueryInMiddleTxClass where prop1 = 1 and prop2 > 2";
List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>(query));
Assert.assertEquals(result.size(), 5);
for (int k = 0; k < 5; k++) {
ODocument document = result.get(k);
Assert.assertEquals(document.field("prop1"), 1);
Assert.assertTrue(document.<Integer>field("prop2") > 2);
}
ODocument explain = database.command(new OCommandSQL("explain " + query)).execute();
Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("compositeIndexNullRangeQueryInMiddleTxIndex"));
query = "select from compositeIndexNullRangeQueryInMiddleTxClass where prop1 > 0";
result = database.query(new OSQLSynchQuery<ODocument>(query));
Assert.assertEquals(result.size(), 10);
for (int k = 0; k < 10; k++) {
ODocument document = result.get(k);
Assert.assertTrue(document.<Integer>field("prop1") > 0);
}
database.commit();
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class CompositeIndexWithNullTest method testPointQueryNullInTheMiddle.
public void testPointQueryNullInTheMiddle() {
final OSchema schema = database.getMetadata().getSchema();
OClass clazz = schema.createClass("compositeIndexNullPointQueryNullInTheMiddleClass");
clazz.createProperty("prop1", OType.INTEGER);
clazz.createProperty("prop2", OType.INTEGER);
clazz.createProperty("prop3", OType.INTEGER);
final ODocument metadata = new ODocument();
metadata.field("ignoreNullValues", false);
clazz.createIndex("compositeIndexNullPointQueryNullInTheMiddleIndex", OClass.INDEX_TYPE.NOTUNIQUE.toString(), null, metadata, null, new String[] { "prop1", "prop2", "prop3" });
for (int i = 0; i < 20; i++) {
ODocument document = new ODocument("compositeIndexNullPointQueryNullInTheMiddleClass");
document.field("prop1", i / 10);
if (i % 2 == 0)
document.field("prop2", i);
document.field("prop3", i);
document.save();
}
String query = "select from compositeIndexNullPointQueryNullInTheMiddleClass where prop1 = 1";
List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>(query));
Assert.assertEquals(result.size(), 10);
for (int k = 0; k < 10; k++) {
ODocument document = result.get(k);
Assert.assertEquals(document.field("prop1"), 1);
}
ODocument explain = database.command(new OCommandSQL("explain " + query)).execute();
Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("compositeIndexNullPointQueryNullInTheMiddleIndex"));
query = "select from compositeIndexNullPointQueryNullInTheMiddleClass where prop1 = 1 and prop2 is null";
result = database.query(new OSQLSynchQuery<ODocument>(query));
Assert.assertEquals(result.size(), 5);
for (ODocument document : result) {
Assert.assertEquals(document.field("prop1"), 1);
Assert.assertNull(document.field("prop2"));
}
explain = database.command(new OCommandSQL("explain " + query)).execute();
Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("compositeIndexNullPointQueryNullInTheMiddleIndex"));
query = "select from compositeIndexNullPointQueryNullInTheMiddleClass where prop1 = 1 and prop2 is null and prop3 = 13";
result = database.query(new OSQLSynchQuery<ODocument>(query));
Assert.assertEquals(result.size(), 1);
explain = database.command(new OCommandSQL("explain " + query)).execute();
Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("compositeIndexNullPointQueryNullInTheMiddleIndex"));
}
Aggregations