use of com.orientechnologies.orient.core.exception.OTransactionException in project orientdb by orientechnologies.
the class OTransactionOptimisticProxy method begin.
@Override
public void begin() {
super.begin();
// Needed for keep the exception and insure that all data is read from the socket.
OException toThrow = null;
try {
setUsingLog(channel.readByte() == 1);
Map<ORecord, byte[]> lazyDeserialize = new IdentityHashMap<ORecord, byte[]>();
byte lastTxStatus;
for (lastTxStatus = channel.readByte(); lastTxStatus == 1; lastTxStatus = channel.readByte()) {
final byte recordStatus = channel.readByte();
final ORecordId rid = channel.readRID();
final byte recordType = channel.readByte();
final ORecordOperation entry = new OTransactionEntryProxy(recordType);
entry.type = recordStatus;
switch(recordStatus) {
case ORecordOperation.CREATED:
byte[] content = channel.readBytes();
ORecordInternal.fill(entry.getRecord(), rid, 0, null, true);
// oNetworkProtocolBinary.fillRecord(rid, content, 0, entry.getRecord(), database);
lazyDeserialize.put(entry.getRecord(), content);
// SAVE THE RECORD TO RETRIEVE THEM FOR THE NEW RID TO SEND BACK TO THE REQUESTER
createdRecords.put(rid.copy(), entry.getRecord());
break;
case ORecordOperation.UPDATED:
int version = channel.readVersion();
byte[] bytes = channel.readBytes();
ORecordInternal.fill(entry.getRecord(), rid, version, null, true);
// oNetworkProtocolBinary.fillRecord(rid, bytes, version, entry.getRecord(), database);
lazyDeserialize.put(entry.getRecord(), bytes);
if (protocolVersion >= 23)
ORecordInternal.setContentChanged(entry.getRecord(), channel.readBoolean());
break;
case ORecordOperation.DELETED:
// LOAD RECORD TO BE SURE IT HASN'T BEEN DELETED BEFORE + PROVIDE CONTENT FOR ANY HOOK
final ORecord rec = rid.getRecord();
int deleteVersion = channel.readVersion();
if (rec == null)
toThrow = new ORecordNotFoundException(rid);
else {
ORecordInternal.setVersion(rec, deleteVersion);
entry.setRecord(rec);
}
break;
default:
throw new OTransactionException("Unrecognized tx command: " + recordStatus);
}
// PUT IN TEMPORARY LIST TO GET FETCHED AFTER ALL FOR CACHE
tempEntries.put(entry.getRecord().getIdentity(), entry);
}
String dbSerializerName = "";
if (database != null)
dbSerializerName = database.getSerializer().toString();
String name = oNetworkProtocolBinary.getRecordSerializerName(connection);
for (Map.Entry<ORecord, byte[]> entry : lazyDeserialize.entrySet()) {
ORecord record = entry.getKey();
final boolean contentChanged = ORecordInternal.isContentChanged(record);
if (ORecordInternal.getRecordType(record) == ODocument.RECORD_TYPE && !dbSerializerName.equals(name)) {
ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(name);
ser.fromStream(entry.getValue(), record, null);
record.setDirty();
ORecordInternal.setContentChanged(record, contentChanged);
} else {
record.fromStream(entry.getValue());
record.setDirty();
ORecordInternal.setContentChanged(record, contentChanged);
}
}
if (toThrow != null)
throw toThrow;
if (lastTxStatus == -1)
// ABORT TX
throw new OTransactionAbortedException("Transaction aborted by the client");
final ODocument remoteIndexEntries = new ODocument(channel.readBytes());
fillIndexOperations(remoteIndexEntries);
// FIRE THE TRIGGERS ONLY AFTER HAVING PARSED THE REQUEST
for (Entry<ORID, ORecordOperation> entry : tempEntries.entrySet()) {
if (entry.getValue().type == ORecordOperation.UPDATED) {
// SPECIAL CASE FOR UPDATE: WE NEED TO LOAD THE RECORD AND APPLY CHANGES TO GET WORKING HOOKS (LIKE INDEXES)
final ORecord record = entry.getValue().record.getRecord();
final boolean contentChanged = ORecordInternal.isContentChanged(record);
final ORecord loadedRecord = record.getIdentity().copy().getRecord();
if (loadedRecord == null)
throw new ORecordNotFoundException(record.getIdentity());
if (ORecordInternal.getRecordType(loadedRecord) == ODocument.RECORD_TYPE && ORecordInternal.getRecordType(loadedRecord) == ORecordInternal.getRecordType(record)) {
((ODocument) loadedRecord).merge((ODocument) record, false, false);
loadedRecord.setDirty();
ORecordInternal.setContentChanged(loadedRecord, contentChanged);
ORecordInternal.setVersion(loadedRecord, record.getVersion());
entry.getValue().record = loadedRecord;
// SAVE THE RECORD TO RETRIEVE THEM FOR THE NEW VERSIONS TO SEND BACK TO THE REQUESTER
updatedRecords.put((ORecordId) entry.getKey(), entry.getValue().getRecord());
}
}
addRecord(entry.getValue().getRecord(), entry.getValue().type, null);
}
tempEntries.clear();
// UNMARSHALL ALL THE RECORD AT THE END TO BE SURE ALL THE RECORD ARE LOADED IN LOCAL TX
for (ORecord record : createdRecords.values()) {
unmarshallRecord(record);
if (record instanceof ODocument) {
// Force conversion of value to class for trigger default values.
ODocumentInternal.autoConvertValueToClass(connection.getDatabase(), (ODocument) record);
}
}
for (ORecord record : updatedRecords.values()) unmarshallRecord(record);
} catch (IOException e) {
rollback();
throw OException.wrapException(new OSerializationException("Cannot read transaction record from the network. Transaction aborted"), e);
}
}
use of com.orientechnologies.orient.core.exception.OTransactionException in project orientdb by orientechnologies.
the class OTransactionOptimisticProxy method fillIndexOperations.
private void fillIndexOperations(final ODocument remoteIndexEntries) {
for (Entry<String, Object> indexEntry : remoteIndexEntries) {
final String indexName = indexEntry.getKey();
final ODocument indexDoc = (ODocument) indexEntry.getValue();
if (indexDoc == null)
continue;
OTransactionIndexChanges transactionIndexChanges = indexEntries.get(indexEntry.getKey());
if (transactionIndexChanges == null) {
transactionIndexChanges = new OTransactionIndexChanges();
indexEntries.put(indexEntry.getKey(), transactionIndexChanges);
}
final Boolean clearAll = indexDoc.field("clear");
if (clearAll != null && clearAll)
transactionIndexChanges.setCleared();
final Collection<ODocument> entries = indexDoc.field("entries");
if (entries == null)
continue;
for (final ODocument entry : entries) {
final List<ODocument> operations = entry.field("ops");
if (operations == null)
continue;
final Object key;
try {
ODocument keyContainer;
if (protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_24) {
final String serializedKey = OStringSerializerHelper.decode((String) entry.field("k"));
if (serializedKey.equals("*"))
keyContainer = null;
else {
keyContainer = new ODocument();
keyContainer.setLazyLoad(false);
ORecordSerializerSchemaAware2CSV.INSTANCE.fromString(serializedKey, keyContainer, null);
}
} else {
keyContainer = entry.field("k");
}
if (keyContainer != null) {
final Object storedKey = keyContainer.field("key");
if (storedKey instanceof List)
key = new OCompositeKey((List<? extends Comparable<?>>) storedKey);
else if (Boolean.TRUE.equals(keyContainer.field("binary"))) {
key = OStreamSerializerAnyStreamable.INSTANCE.fromStream((byte[]) storedKey);
} else
key = storedKey;
} else
key = null;
} catch (IOException ioe) {
throw OException.wrapException(new OTransactionException("Error during index changes deserialization. "), ioe);
}
for (final ODocument op : operations) {
final int operation = (Integer) op.rawField("o");
final OTransactionIndexChanges.OPERATION indexOperation = OTransactionIndexChanges.OPERATION.values()[operation];
final OIdentifiable value = op.field("v");
transactionIndexChanges.getChangesPerKey(key).add(value, indexOperation);
if (value == null)
continue;
final ORID rid = value.getIdentity();
List<OTransactionRecordIndexOperation> txIndexOperations = recordIndexOperations.get(rid);
if (txIndexOperations == null) {
txIndexOperations = new ArrayList<OTransactionRecordIndexOperation>();
recordIndexOperations.put(rid, txIndexOperations);
}
txIndexOperations.add(new OTransactionRecordIndexOperation(indexName, key, indexOperation));
}
}
}
}
Aggregations