use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class OStorageRemote method createRecord.
public OStorageOperationResult<OPhysicalPosition> createRecord(final ORecordId iRid, final byte[] iContent, final int iRecordVersion, final byte iRecordType, final int iMode, final ORecordCallback<Long> iCallback) {
final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
ORecordCallback<OPhysicalPosition> realCallback = null;
if (iCallback != null) {
realCallback = new ORecordCallback<OPhysicalPosition>() {
@Override
public void call(ORecordId iRID, OPhysicalPosition iParameter) {
iCallback.call(iRID, iParameter.clusterPosition);
}
};
}
final ORecordId idCopy = iRid.copy();
// The Upper layer require to return this also if it not really receive response from the network
final OPhysicalPosition ppos = new OPhysicalPosition(iRecordType);
asyncNetworkOperation(new OStorageRemoteOperationWrite() {
@Override
public void execute(final OChannelBinaryAsynchClient network, final OStorageRemoteSession session, int mode) throws IOException {
try {
beginRequest(network, OChannelBinaryProtocol.REQUEST_RECORD_CREATE, session);
network.writeShort((short) iRid.getClusterId());
network.writeBytes(iContent);
network.writeByte(iRecordType);
network.writeByte((byte) mode);
} finally {
endRequest(network);
}
}
}, new OStorageRemoteOperationRead<OPhysicalPosition>() {
@Override
public OPhysicalPosition execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
// SYNCHRONOUS
try {
beginResponse(network, session);
// FIRST READ THE ENTIRE RESPONSE
short clusterId = network.readShort();
final long clPos = network.readLong();
final int recVer = network.readVersion();
final Map<OBonsaiCollectionPointer, OPair<Long, Long>> collectionChanges = readCollectionChanges(network);
// APPLY CHANGES
ppos.clusterPosition = clPos;
ppos.recordVersion = recVer;
// THIS IS A COMPATIBILITY FIX TO AVOID TO FILL THE CLUSTER ID IN CASE OF ASYNC
if (iMode == 0) {
iRid.setClusterId(clusterId);
iRid.setClusterPosition(ppos.clusterPosition);
}
idCopy.setClusterId(clusterId);
idCopy.setClusterPosition(ppos.clusterPosition);
updateCollection(collectionChanges, collectionManager);
return ppos;
} finally {
endResponse(network);
}
}
}, iMode, idCopy, realCallback, "Error on create record in cluster " + iRid.getClusterId());
return new OStorageOperationResult<OPhysicalPosition>(ppos);
}
use of com.orientechnologies.orient.core.id.ORecordId 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.id.ORecordId in project orientdb by orientechnologies.
the class OStorageRemoteAsyncOperationTest method testSyncCall.
@Test
public void testSyncCall() {
final CallStatus status = new CallStatus();
storage.asyncNetworkOperation(new OStorageRemoteOperationWrite() {
@Override
public void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session, int mode) throws IOException {
assertNull(status.status);
status.status = "write";
}
}, new OStorageRemoteOperationRead<Object>() {
@Override
public Object execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
assertEquals(status.status, "write");
status.status = "read";
return null;
}
}, 0, new ORecordId(-1, -1), null, "");
assertEquals(status.status, "read");
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class OStorageRemoteAsyncOperationTest method testAsyncRead.
@Test
public void testAsyncRead() throws InterruptedException {
final CallStatus status = new CallStatus();
final CountDownLatch callBackWait = new CountDownLatch(1);
final CountDownLatch readDone = new CountDownLatch(1);
final CountDownLatch callBackDone = new CountDownLatch(1);
final Object res = new Object();
storage.asyncNetworkOperation(new OStorageRemoteOperationWrite() {
@Override
public void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session, int mode) throws IOException {
assertNull(status.status);
status.status = "write";
}
}, new OStorageRemoteOperationRead<Object>() {
@Override
public Object execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
try {
if (callBackWait.await(10, TimeUnit.MILLISECONDS))
readDone.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
return res;
}
}, 1, new ORecordId(-1, -1), new ORecordCallback<Object>() {
@Override
public void call(ORecordId iRID, Object iParameter) {
callBackDone.countDown();
}
}, "");
// SBLCK THE CALLBAC THAT SHOULD BE IN ANOTHER THREAD
callBackWait.countDown();
boolean called = readDone.await(10, TimeUnit.MILLISECONDS);
if (!called)
fail("Read not called");
called = callBackDone.await(10, TimeUnit.MILLISECONDS);
if (!called)
fail("Callback not called");
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class OCommandExecutorSQLSelect method unwind.
private Collection<OIdentifiable> unwind(final OIdentifiable iRecord, final List<String> unwindFields, final OCommandContext iContext) {
final List<OIdentifiable> result = new ArrayList<OIdentifiable>();
ODocument doc;
if (iRecord instanceof ODocument) {
doc = (ODocument) iRecord;
} else {
doc = iRecord.getRecord();
}
if (unwindFields.size() == 0) {
ORecordInternal.setIdentity(doc, new ORecordId(-2, getTemporaryRIDCounter(iContext)));
result.add(doc);
} else {
String firstField = unwindFields.get(0);
final List<String> nextFields = unwindFields.subList(1, unwindFields.size());
Object fieldValue = doc.field(firstField);
if (fieldValue == null || !(fieldValue instanceof Iterable) || fieldValue instanceof ODocument) {
result.addAll(unwind(doc, nextFields, iContext));
} else {
Iterator iterator = ((Iterable) fieldValue).iterator();
if (!iterator.hasNext()) {
ODocument unwindedDoc = new ODocument();
doc.copyTo(unwindedDoc);
unwindedDoc.field(firstField, (Object) null);
result.addAll(unwind(unwindedDoc, nextFields, iContext));
} else {
do {
Object o = iterator.next();
ODocument unwindedDoc = new ODocument();
doc.copyTo(unwindedDoc);
unwindedDoc.field(firstField, o);
result.addAll(unwind(unwindedDoc, nextFields, iContext));
} while (iterator.hasNext());
}
}
}
return result;
}
Aggregations