Skip to content

Instantly share code, notes, and snippets.

@dongwq
Created June 21, 2015 11:08
Show Gist options
  • Select an option

  • Save dongwq/6116eb9a2e0791637a50 to your computer and use it in GitHub Desktop.

Select an option

Save dongwq/6116eb9a2e0791637a50 to your computer and use it in GitHub Desktop.
orientdb live query test
/**
* TODO: failed
*/
@Test
public void testLiveQuery() {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + OrientDbAsDocTest.class.getSimpleName());
((ODatabaseDocumentTx) db).setSerializer(new ORecordSerializerBinary());
db.create();
ODocument document = new ODocument("Test");
document.save();
db.commit();
// Instantiate the query listener
MyLiveQueryListener listener = new MyLiveQueryListener();
// Execute the query
List<ODocument> result = db.query(new OLiveQuery<ODocument>("live select * from Test", listener));
// Get the query token, it is needed for unsubscribe
Integer token = result.get(0).field("token"); // 1234567
System.out.println("token, " + token);
// From now you will receive results from the server for every change that matches your query criteria.
// If you or someone else executes an INSERT on the server
db.command(new OCommandSQL("insert into Test set name = 'foo', surname = 'bar'")).execute();
// Your MyLiveQueryListener.onLiveResult() will be invoked. In this case the result will be
// New result from server for live query 1234567 <- a token generated by the server
// operation: 3 <- ORecordOperation.CREATED
// content: {@Rid: "#12:0", name: "foo", surname: "bar"}
db.command(new OCommandSQL("update Test set name = 'baz' where surname = 'bar'")).execute();
db.commit();
try {
Thread.sleep(3600);
} catch (Exception e) {
e.printStackTrace();
}
db.command(new OCommandSQL("update Test set name = 'baz' where surname = 'bar'")).execute();
System.out.println("end--");
// New result from server for live query 1234567
// operation: 1 <- ORecordOperation.UPDATED
// content: {@Rid: "#12:0", name: "baz", surname: "bar"}
// db.command(new OCommandSQL("live unsubscribe " + token)).execute();
// From now you will not receive any other results
}
class MyLiveQueryListener implements OLiveResultListener {
public List<ORecordOperation> ops = new ArrayList<ORecordOperation>();
@Override
public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
System.out.println("New result from server for live query " + iLiveToken);
System.out.println("operation: " + iOp.type);
System.out.println("content: " + iOp.record);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment