}
case OChannelBinaryProtocol.REQUEST_RECORD_LOAD: {
data.commandInfo = "Load record";
final ORecordId rid = channel.readRID();
final String fetchPlanString = channel.readString();
if (rid.clusterId == 0 && rid.clusterPosition == 0) {
// @COMPATIBILITY 0.9.25
// SEND THE DB CONFIGURATION INSTEAD SINCE IT WAS ON RECORD 0:0
OFetchHelper.checkFetchPlanValid(fetchPlanString);
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeByte((byte) 1);
channel.writeBytes(connection.database.getStorage().getConfiguration().toStream());
channel.writeInt(0);
channel.writeByte(ORecordBytes.RECORD_TYPE);
} finally {
channel.releaseExclusiveLock();
}
} else {
final ORecordInternal<?> record = connection.database.load(rid, fetchPlanString);
// if (rid.equals(((OSchemaImpl) connection.database.getMetadata().getSchema()).getDocument().getIdentity()))
// connection.database.getMetadata().getSchema().reload();
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
if (record != null) {
channel.writeByte((byte) 1);
channel.writeBytes(record.toStream());
channel.writeInt(record.getVersion());
channel.writeByte(record.getRecordType());
if (fetchPlanString.length() > 0) {
// BUILD THE SERVER SIDE RECORD TO ACCES TO THE FETCH
// PLAN
if (record instanceof ODocument) {
final Map<String, Integer> fetchPlan = OFetchHelper.buildFetchPlan(fetchPlanString);
final Set<ODocument> recordsToSend = new HashSet<ODocument>();
OFetchHelper.fetch((ODocument) record, record, fetchPlan, null, 0, -1, new OFetchListener() {
@Override
public int size() {
return recordsToSend.size();
}
// ADD TO THE SET OF OBJECTS TO SEND
@Override
public Object fetchLinked(final ODocument iRoot, final Object iUserObject, final String iFieldName,
final Object iLinked) {
if (iLinked instanceof ODocument) {
if (((ODocument) iLinked).getIdentity().isValid())
return recordsToSend.add((ODocument) iLinked) ? iLinked : null;
return null;
} else if (iLinked instanceof Collection<?>)
return recordsToSend.addAll((Collection<? extends ODocument>) iLinked) ? iLinked : null;
else if (iLinked instanceof Map<?, ?>)
return recordsToSend.addAll(((Map<String, ? extends ODocument>) iLinked).values()) ? iLinked : null;
else
throw new IllegalArgumentException("Unrecognized type while fetching records: " + iLinked);
}
});
// SEND RECORDS TO LOAD IN CLIENT CACHE
for (ODocument doc : recordsToSend) {
if (doc.getIdentity().isValid()) {
channel.writeByte((byte) 2); // CLIENT CACHE
// RECORD. IT ISN'T PART OF THE RESULT SET
writeIdentifiable(doc);
}
}
}
}
}
} finally {
channel.releaseExclusiveLock();
}
}
channel.writeByte((byte) 0); // NO MORE RECORDS
break;
}
case OChannelBinaryProtocol.REQUEST_RECORD_CREATE: {
data.commandInfo = "Create record";
final ORecordId rid = new ORecordId(channel.readShort(), ORID.CLUSTER_POS_INVALID);
final byte[] buffer = channel.readBytes();
final byte recordType = channel.readByte();
final ORecordInternal<?> record = Orient.instance().getRecordFactoryManager().newInstance(connection.database, recordType);
record.fill(connection.database, rid, 0, buffer, true);
connection.database.save(record);
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeLong(record.getIdentity().getClusterPosition());
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_RECORD_UPDATE: {
data.commandInfo = "Update record";
final ORecordId rid = channel.readRID();
final byte[] buffer = channel.readBytes();
final int version = channel.readInt();
final byte recordType = channel.readByte();
final ORecordInternal<?> newRecord = Orient.instance().getRecordFactoryManager().newInstance(connection.database, recordType);
newRecord.fill(connection.database, rid, version, buffer, true);
if (((OSchemaProxy) connection.database.getMetadata().getSchema()).getIdentity().equals(rid))
// || ((OIndexManagerImpl) connection.database.getMetadata().getIndexManager()).getDocument().getIdentity().equals(rid)) {
throw new OSecurityAccessException("Can't update internal record " + rid);
final ORecordInternal<?> currentRecord;
if (newRecord instanceof ODocument) {
currentRecord = connection.database.load(rid);
if (currentRecord == null)
throw new ORecordNotFoundException(rid.toString());
final ODocument doc = (ODocument) currentRecord;
doc.merge((ODocument) newRecord, false, false);
} else