use of com.orientechnologies.orient.core.sql.query.OLiveResultListener in project orientdb by orientechnologies.
the class OLiveQueryShotdownTest method testShutDown.
@Test
public void testShutDown() throws Exception {
bootServer();
ODatabaseDocument db = new ODatabaseDocumentTx("remote:localhost/" + OLiveQueryShotdownTest.class.getSimpleName());
db.open("admin", "admin");
db.getMetadata().getSchema().createClass("Test");
final CountDownLatch error = new CountDownLatch(1);
try {
db.command(new OLiveQuery("live select from Test", new OLiveResultListener() {
@Override
public void onUnsubscribe(int iLiveToken) {
}
@Override
public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
}
@Override
public void onError(int iLiveToken) {
error.countDown();
}
})).execute();
shutdownServer();
assertTrue("onError method never called on shutdow", error.await(2, TimeUnit.SECONDS));
} finally {
// db.close();
}
}
use of com.orientechnologies.orient.core.sql.query.OLiveResultListener in project orientdb by orientechnologies.
the class OStorageRemoteAsynchEventListener method onRequest.
public void onRequest(final byte iRequestCode, final Object obj) {
// Using get status to avoid to check the session.
if (storage.getStatus() == STATUS.CLOSED)
return;
if (iRequestCode == OChannelBinaryProtocol.REQUEST_PUSH_DISTRIB_CONFIG) {
storage.updateClusterConfiguration(null, (byte[]) obj);
if (OLogManager.instance().isDebugEnabled()) {
synchronized (storage.getClusterConfiguration()) {
OLogManager.instance().debug(this, "Received new cluster configuration: %s", storage.getClusterConfiguration().toJSON("prettyPrint"));
}
}
} else if (iRequestCode == OChannelBinaryProtocol.REQUEST_PUSH_LIVE_QUERY) {
byte[] bytes = (byte[]) obj;
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
Integer id = null;
try {
byte what = dis.readByte();
if (what == 'r') {
byte op = dis.readByte();
id = dis.readInt();
final ORecord record = Orient.instance().getRecordFactoryManager().newInstance(dis.readByte());
final int version = readVersion(dis);
final ORecordId rid = readRID(dis);
final byte[] content = readBytes(dis);
ORecordInternal.fill(record, rid, version, content, false);
OLiveResultListener listener = liveQueryListeners.get(id);
if (listener != null) {
listener.onLiveResult(id, new ORecordOperation(record, op));
} else {
OLogManager.instance().warn(this, "Receiving invalid LiveQuery token: " + id);
}
} else if (what == 'u') {
id = dis.readInt();
OLiveResultListener listener = liveQueryListeners.get(id);
listener.onUnsubscribe(id);
}
} catch (IOException e) {
if (id != null) {
OLiveResultListener listener = liveQueryListeners.get(id);
if (listener != null) {
listener.onError(id);
}
}
e.printStackTrace();
}
}
byte op;
}
use of com.orientechnologies.orient.core.sql.query.OLiveResultListener in project orientdb by orientechnologies.
the class OStorageRemote method command.
/**
* Execute the command remotely and get the results back.
*/
public Object command(final OCommandRequestText iCommand) {
if (!(iCommand instanceof OSerializableStream))
throw new OCommandExecutionException("Cannot serialize the command to be executed to the server side.");
final boolean live = iCommand instanceof OLiveQuery;
final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
return networkOperation(new OStorageRemoteOperation<Object>() {
@Override
public Object execute(final OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
Object result = null;
session.commandExecuting = true;
try {
final boolean asynch = iCommand instanceof OCommandRequestAsynch && ((OCommandRequestAsynch) iCommand).isAsynchronous();
try {
beginRequest(network, OChannelBinaryProtocol.REQUEST_COMMAND, session);
if (live) {
network.writeByte((byte) 'l');
} else {
// ASYNC / SYNC
network.writeByte((byte) (asynch ? 'a' : 's'));
}
network.writeBytes(OStreamSerializerAnyStreamable.INSTANCE.toStream(iCommand));
} finally {
endRequest(network);
}
try {
beginResponse(network, session);
// Collection of prefetched temporary record (nested projection record), to refer for avoid garbage collection.
List<ORecord> temporaryResults = new ArrayList<ORecord>();
boolean addNextRecord = true;
if (asynch) {
byte status;
// ASYNCH: READ ONE RECORD AT TIME
while ((status = network.readByte()) > 0) {
final ORecord record = (ORecord) OChannelBinaryProtocol.readIdentifiable(network);
if (record == null)
continue;
switch(status) {
case 1:
// PUT AS PART OF THE RESULT SET. INVOKE THE LISTENER
if (addNextRecord) {
addNextRecord = iCommand.getResultListener().result(record);
database.getLocalCache().updateRecord(record);
}
break;
case 2:
if (record.getIdentity().getClusterId() == -2)
temporaryResults.add(record);
// PUT IN THE CLIENT LOCAL CACHE
database.getLocalCache().updateRecord(record);
}
}
} else {
result = readSynchResult(network, database, temporaryResults);
if (live) {
final ODocument doc = ((List<ODocument>) result).get(0);
final Integer token = doc.field("token");
final Boolean unsubscribe = doc.field("unsubscribe");
if (token != null) {
if (Boolean.TRUE.equals(unsubscribe)) {
if (OStorageRemote.this.asynchEventListener != null)
OStorageRemote.this.asynchEventListener.unregisterLiveListener(token);
} else {
final OLiveResultListener listener = (OLiveResultListener) iCommand.getResultListener();
ODatabaseDocumentInternal current = ODatabaseRecordThreadLocal.INSTANCE.get();
final ODatabaseDocument dbCopy = current.copy();
ORemoteConnectionPool pool = OStorageRemote.this.connectionManager.getPool(network.getServerURL());
OStorageRemote.this.asynchEventListener.registerLiveListener(pool, token, new OLiveResultListener() {
@Override
public void onUnsubscribe(int iLiveToken) {
listener.onUnsubscribe(iLiveToken);
dbCopy.close();
}
@Override
public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
dbCopy.activateOnCurrentThread();
listener.onLiveResult(iLiveToken, iOp);
}
@Override
public void onError(int iLiveToken) {
listener.onError(iLiveToken);
dbCopy.close();
}
});
}
} else {
throw new OStorageException("Cannot execute live query, returned null token");
}
}
}
if (!temporaryResults.isEmpty()) {
if (result instanceof OBasicResultSet<?>) {
((OBasicResultSet<?>) result).setTemporaryRecordCache(temporaryResults);
}
}
return result;
} finally {
endResponse(network);
}
} finally {
session.commandExecuting = false;
if (iCommand.getResultListener() != null && !live)
iCommand.getResultListener().end();
}
}
}, "Error on executing command: " + iCommand);
}
use of com.orientechnologies.orient.core.sql.query.OLiveResultListener in project orientdb by orientechnologies.
the class OCommandExecutorSQLLiveSelect method onLiveResultEnd.
public void onLiveResultEnd() {
if (request.getResultListener() instanceof OLiveResultListener) {
((OLiveResultListener) request.getResultListener()).onUnsubscribe(token);
}
if (execDb != null) {
ODatabaseDocumentInternal oldThreadDB = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
execDb.activateOnCurrentThread();
execDb.close();
if (oldThreadDB == null) {
ODatabaseRecordThreadLocal.INSTANCE.remove();
} else {
ODatabaseRecordThreadLocal.INSTANCE.set(oldThreadDB);
}
}
}
use of com.orientechnologies.orient.core.sql.query.OLiveResultListener in project orientdb by orientechnologies.
the class OCommandExecutorSQLLiveSelect method onLiveResult.
public void onLiveResult(final ORecordOperation iOp) {
ODatabaseDocumentInternal oldThreadLocal = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
execDb.activateOnCurrentThread();
try {
final OIdentifiable value = iOp.getRecord();
if (!matchesTarget(value)) {
return;
}
if (!matchesFilters(value)) {
return;
}
if (!checkSecurity(value)) {
return;
}
} finally {
if (oldThreadLocal == null) {
ODatabaseRecordThreadLocal.INSTANCE.remove();
} else {
ODatabaseRecordThreadLocal.INSTANCE.set(oldThreadLocal);
}
}
final OCommandResultListener listener = request.getResultListener();
if (listener instanceof OLiveResultListener) {
execInSeparateDatabase(new OCallable() {
@Override
public Object call(Object iArgument) {
execDb.activateOnCurrentThread();
((OLiveResultListener) listener).onLiveResult(token, iOp);
return null;
}
});
}
}
Aggregations