use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method sbTreeBonsaiFirstKey.
private void sbTreeBonsaiFirstKey(OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "SB-Tree bonsai get first key");
OBonsaiCollectionPointer collectionPointer = OCollectionNetworkSerializer.INSTANCE.readCollectionPointer(channel);
final OSBTreeCollectionManager sbTreeCollectionManager = connection.getDatabase().getSbTreeCollectionManager();
final OSBTreeBonsai<OIdentifiable, Integer> tree = sbTreeCollectionManager.loadSBTree(collectionPointer);
if (tree == null)
throw new ORecordContentNotFoundException(collectionPointer);
try {
OIdentifiable result = tree.firstKey();
final OBinarySerializer<? super OIdentifiable> keySerializer;
if (result == null) {
keySerializer = ONullSerializer.INSTANCE;
} else {
keySerializer = tree.getKeySerializer();
}
byte[] stream = new byte[OByteSerializer.BYTE_SIZE + keySerializer.getObjectSize(result)];
OByteSerializer.INSTANCE.serialize(keySerializer.getId(), stream, 0);
keySerializer.serialize(result, stream, OByteSerializer.BYTE_SIZE);
beginResponse();
try {
sendOk(connection, clientTxId);
channel.writeBytes(stream);
} finally {
endResponse(connection);
}
} finally {
sbTreeCollectionManager.releaseSBTree(collectionPointer);
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method ridBagSize.
private void ridBagSize(OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "RidBag get size");
OBonsaiCollectionPointer collectionPointer = OCollectionNetworkSerializer.INSTANCE.readCollectionPointer(channel);
final byte[] changeStream = channel.readBytes();
final OSBTreeCollectionManager sbTreeCollectionManager = connection.getDatabase().getSbTreeCollectionManager();
final OSBTreeBonsai<OIdentifiable, Integer> tree = sbTreeCollectionManager.loadSBTree(collectionPointer);
if (tree == null)
throw new ORecordContentNotFoundException(collectionPointer);
try {
final Map<OIdentifiable, OSBTreeRidBag.Change> changes = OSBTreeRidBag.ChangeSerializationHelper.INSTANCE.deserializeChanges(changeStream, 0);
int realSize = tree.getRealBagSize(changes);
beginResponse();
try {
sendOk(connection, clientTxId);
channel.writeInt(realSize);
} finally {
endResponse(connection);
}
} finally {
sbTreeCollectionManager.releaseSBTree(collectionPointer);
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method sbTreeBonsaiGetEntriesMajor.
private void sbTreeBonsaiGetEntriesMajor(OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "SB-Tree bonsai get values major");
OBonsaiCollectionPointer collectionPointer = OCollectionNetworkSerializer.INSTANCE.readCollectionPointer(channel);
byte[] keyStream = channel.readBytes();
boolean inclusive = channel.readBoolean();
int pageSize = 128;
if (connection.getData().protocolVersion >= 21)
pageSize = channel.readInt();
final OSBTreeCollectionManager sbTreeCollectionManager = connection.getDatabase().getSbTreeCollectionManager();
final OSBTreeBonsai<OIdentifiable, Integer> tree = sbTreeCollectionManager.loadSBTree(collectionPointer);
if (tree == null)
throw new ORecordContentNotFoundException(collectionPointer);
try {
final OBinarySerializer<OIdentifiable> keySerializer = tree.getKeySerializer();
OIdentifiable key = keySerializer.deserialize(keyStream, 0);
final OBinarySerializer<Integer> valueSerializer = tree.getValueSerializer();
OTreeInternal.AccumulativeListener<OIdentifiable, Integer> listener = new OTreeInternal.AccumulativeListener<OIdentifiable, Integer>(pageSize);
tree.loadEntriesMajor(key, inclusive, true, listener);
List<Entry<OIdentifiable, Integer>> result = listener.getResult();
byte[] stream = serializeSBTreeEntryCollection(result, keySerializer, valueSerializer);
beginResponse();
try {
sendOk(connection, clientTxId);
channel.writeBytes(stream);
} finally {
endResponse(connection);
}
} finally {
sbTreeCollectionManager.releaseSBTree(collectionPointer);
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method sbTreeBonsaiGet.
private void sbTreeBonsaiGet(OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "SB-Tree bonsai get");
OBonsaiCollectionPointer collectionPointer = OCollectionNetworkSerializer.INSTANCE.readCollectionPointer(channel);
final byte[] keyStream = channel.readBytes();
final OSBTreeCollectionManager sbTreeCollectionManager = connection.getDatabase().getSbTreeCollectionManager();
final OSBTreeBonsai<OIdentifiable, Integer> tree = sbTreeCollectionManager.loadSBTree(collectionPointer);
if (tree == null)
throw new ORecordContentNotFoundException(collectionPointer);
try {
final OIdentifiable key = tree.getKeySerializer().deserialize(keyStream, 0);
Integer result = tree.get(key);
final OBinarySerializer<? super Integer> valueSerializer;
if (result == null) {
valueSerializer = ONullSerializer.INSTANCE;
} else {
valueSerializer = tree.getValueSerializer();
}
byte[] stream = new byte[OByteSerializer.BYTE_SIZE + valueSerializer.getObjectSize(result)];
OByteSerializer.INSTANCE.serialize(valueSerializer.getId(), stream, 0);
valueSerializer.serialize(result, stream, OByteSerializer.BYTE_SIZE);
beginResponse();
try {
sendOk(connection, clientTxId);
channel.writeBytes(stream);
} finally {
endResponse(connection);
}
} finally {
sbTreeCollectionManager.releaseSBTree(collectionPointer);
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method commit.
protected void commit(OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "Transaction commit");
if (!isConnectionAlive(connection))
return;
final OTransactionOptimisticProxy tx = new OTransactionOptimisticProxy(connection, this);
try {
try {
connection.getDatabase().begin(tx);
} catch (final ORecordNotFoundException e) {
sendShutdown();
throw e.getCause() instanceof OOfflineClusterException ? (OOfflineClusterException) e.getCause() : e;
}
try {
try {
connection.getDatabase().commit();
} catch (final ORecordNotFoundException e) {
throw e.getCause() instanceof OOfflineClusterException ? (OOfflineClusterException) e.getCause() : e;
}
beginResponse();
try {
sendOk(connection, clientTxId);
// SEND BACK ALL THE RECORD IDS FOR THE CREATED RECORDS
channel.writeInt(tx.getCreatedRecords().size());
for (Entry<ORecordId, ORecord> entry : tx.getCreatedRecords().entrySet()) {
channel.writeRID(entry.getKey());
channel.writeRID(entry.getValue().getIdentity());
// IF THE NEW OBJECT HAS VERSION > 0 MEANS THAT HAS BEEN UPDATED IN THE SAME TX. THIS HAPPENS FOR GRAPHS
if (entry.getValue().getVersion() > 0)
tx.getUpdatedRecords().put((ORecordId) entry.getValue().getIdentity(), entry.getValue());
}
// SEND BACK ALL THE NEW VERSIONS FOR THE UPDATED RECORDS
channel.writeInt(tx.getUpdatedRecords().size());
for (Entry<ORecordId, ORecord> entry : tx.getUpdatedRecords().entrySet()) {
channel.writeRID(entry.getKey());
channel.writeVersion(entry.getValue().getVersion());
}
if (connection.getData().protocolVersion >= 20)
sendCollectionChanges(connection);
} finally {
endResponse(connection);
}
} catch (Exception e) {
if (connection != null && connection.getDatabase() != null) {
if (connection.getDatabase().getTransaction().isActive())
connection.getDatabase().rollback(true);
final OSBTreeCollectionManager collectionManager = connection.getDatabase().getSbTreeCollectionManager();
if (collectionManager != null)
collectionManager.clearChangedIds();
}
sendErrorOrDropConnection(connection, clientTxId, e);
}
} catch (OTransactionAbortedException e) {
// TX ABORTED BY THE CLIENT
} catch (Exception e) {
// Error during TX initialization, possibly index constraints violation.
if (tx.isActive())
tx.rollback(true, -1);
sendErrorOrDropConnection(connection, clientTxId, e);
}
}
Aggregations