use of com.orientechnologies.orient.core.fetch.OFetchPlan in project orientdb by orientechnologies.
the class ORecordSerializerJSON method toString.
@Override
public StringBuilder toString(final ORecord iRecord, final StringBuilder iOutput, final String iFormat, final OUserObject2RecordHandler iObjHandler, boolean iOnlyDelta, boolean autoDetectCollectionType) {
try {
final StringWriter buffer = new StringWriter(INITIAL_SIZE);
final OJSONWriter json = new OJSONWriter(buffer, iFormat);
final FormatSettings settings = new FormatSettings(iFormat);
json.beginObject();
OJSONFetchContext context = new OJSONFetchContext(json, settings);
context.writeSignature(json, iRecord);
if (iRecord instanceof ODocument) {
final OFetchPlan fp = OFetchHelper.buildFetchPlan(settings.fetchPlan);
OFetchHelper.fetch(iRecord, null, fp, new OJSONFetchListener(), context, iFormat);
} else if (iRecord instanceof ORecordStringable) {
// STRINGABLE
final ORecordStringable record = (ORecordStringable) iRecord;
json.writeAttribute(settings.indentLevel, true, "value", record.value());
} else if (iRecord instanceof OBlob) {
// BYTES
final OBlob record = (OBlob) iRecord;
json.writeAttribute(settings.indentLevel, true, "value", OBase64Utils.encodeBytes(record.toStream()));
} else
throw new OSerializationException("Error on marshalling record of type '" + iRecord.getClass() + "' to JSON. The record type cannot be exported to JSON");
json.endObject(settings.indentLevel, true);
iOutput.append(buffer);
return iOutput;
} catch (IOException e) {
throw OException.wrapException(new OSerializationException("Error on marshalling of record to JSON"), e);
}
}
use of com.orientechnologies.orient.core.fetch.OFetchPlan in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method readRecord.
protected void readRecord(final OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "Load record");
if (!isConnectionAlive(connection))
return;
final ORecordId rid = channel.readRID();
final String fetchPlanString = channel.readString();
boolean ignoreCache = false;
ignoreCache = channel.readByte() == 1;
boolean loadTombstones = false;
loadTombstones = channel.readByte() > 0;
if (rid.getClusterId() == 0 && rid.getClusterPosition() == 0) {
// @COMPATIBILITY 0.9.25
// SEND THE DB CONFIGURATION INSTEAD SINCE IT WAS ON RECORD 0:0
OFetchHelper.checkFetchPlanValid(fetchPlanString);
beginResponse();
try {
sendOk(connection, clientTxId);
channel.writeByte((byte) 1);
final byte[] storageStream = connection.getDatabase().getStorage().callInLock(new Callable<byte[]>() {
@Override
public byte[] call() throws Exception {
return connection.getDatabase().getStorage().getConfiguration().toStream(connection.getData().protocolVersion);
}
}, false);
if (connection.getData().protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_27) {
channel.writeBytes(storageStream);
channel.writeVersion(0);
channel.writeByte(OBlob.RECORD_TYPE);
} else {
channel.writeByte(OBlob.RECORD_TYPE);
channel.writeVersion(0);
channel.writeBytes(storageStream);
}
// NO MORE RECORDS
channel.writeByte((byte) 0);
} finally {
endResponse(connection);
}
} else {
final ORecord record = connection.getDatabase().load(rid, fetchPlanString, ignoreCache, loadTombstones, OStorage.LOCKING_STRATEGY.NONE);
beginResponse();
try {
sendOk(connection, clientTxId);
if (record != null) {
// HAS RECORD
channel.writeByte((byte) 1);
byte[] bytes = getRecordBytes(connection, record);
int length = trimCsvSerializedContent(connection, bytes);
if (connection.getData().protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_27) {
channel.writeBytes(bytes, length);
channel.writeVersion(record.getVersion());
channel.writeByte(ORecordInternal.getRecordType(record));
} else {
channel.writeByte(ORecordInternal.getRecordType(record));
channel.writeVersion(record.getVersion());
channel.writeBytes(bytes, length);
}
if (fetchPlanString.length() > 0) {
// PLAN
if (record instanceof ODocument) {
final OFetchPlan fetchPlan = OFetchHelper.buildFetchPlan(fetchPlanString);
final Set<ORecord> recordsToSend = new HashSet<ORecord>();
final ODocument doc = (ODocument) record;
final OFetchListener listener = new ORemoteFetchListener() {
@Override
protected void sendRecord(ORecord iLinked) {
recordsToSend.add(iLinked);
}
};
final OFetchContext context = new ORemoteFetchContext();
OFetchHelper.fetch(doc, doc, fetchPlan, listener, context, "");
// SEND RECORDS TO LOAD IN CLIENT CACHE
for (ORecord d : recordsToSend) {
if (d.getIdentity().isValid()) {
// CLIENT CACHE
channel.writeByte((byte) 2);
// RECORD. IT ISN'T PART OF THE RESULT SET
writeIdentifiable(connection, d);
}
}
}
}
}
// NO MORE RECORDS
channel.writeByte((byte) 0);
} finally {
endResponse(connection);
}
}
}
use of com.orientechnologies.orient.core.fetch.OFetchPlan in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method readRecordIfVersionIsNotLatest.
protected void readRecordIfVersionIsNotLatest(final OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "Load record if version is not latest");
if (!isConnectionAlive(connection))
return;
final ORecordId rid = channel.readRID();
final int recordVersion = channel.readVersion();
final String fetchPlanString = channel.readString();
boolean ignoreCache = channel.readByte() == 1;
if (rid.getClusterId() == 0 && rid.getClusterPosition() == 0) {
// @COMPATIBILITY 0.9.25
// SEND THE DB CONFIGURATION INSTEAD SINCE IT WAS ON RECORD 0:0
OFetchHelper.checkFetchPlanValid(fetchPlanString);
beginResponse();
try {
sendOk(connection, clientTxId);
channel.writeByte((byte) 1);
final byte[] storageStream = connection.getDatabase().getStorage().callInLock(new Callable<byte[]>() {
@Override
public byte[] call() throws Exception {
return connection.getDatabase().getStorage().getConfiguration().toStream(connection.getData().protocolVersion);
}
}, false);
if (connection.getData().protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_27) {
channel.writeBytes(storageStream);
channel.writeVersion(0);
channel.writeByte(OBlob.RECORD_TYPE);
} else {
channel.writeByte(OBlob.RECORD_TYPE);
channel.writeVersion(0);
channel.writeBytes(storageStream);
}
// NO MORE RECORDS
channel.writeByte((byte) 0);
} finally {
endResponse(connection);
}
} else {
final ORecord record = connection.getDatabase().loadIfVersionIsNotLatest(rid, recordVersion, fetchPlanString, ignoreCache);
beginResponse();
try {
sendOk(connection, clientTxId);
if (record != null) {
// HAS RECORD
channel.writeByte((byte) 1);
byte[] bytes = getRecordBytes(connection, record);
int length = trimCsvSerializedContent(connection, bytes);
channel.writeByte(ORecordInternal.getRecordType(record));
channel.writeVersion(record.getVersion());
channel.writeBytes(bytes, length);
if (fetchPlanString.length() > 0) {
// PLAN
if (record instanceof ODocument) {
final OFetchPlan fetchPlan = OFetchHelper.buildFetchPlan(fetchPlanString);
final Set<ORecord> recordsToSend = new HashSet<ORecord>();
final ODocument doc = (ODocument) record;
final OFetchListener listener = new ORemoteFetchListener() {
@Override
protected void sendRecord(ORecord iLinked) {
recordsToSend.add(iLinked);
}
};
final OFetchContext context = new ORemoteFetchContext();
OFetchHelper.fetch(doc, doc, fetchPlan, listener, context, "");
// SEND RECORDS TO LOAD IN CLIENT CACHE
for (ORecord d : recordsToSend) {
if (d.getIdentity().isValid()) {
// CLIENT CACHE
channel.writeByte((byte) 2);
// RECORD. IT ISN'T PART OF THE RESULT SET
writeIdentifiable(connection, d);
}
}
}
}
}
// NO MORE RECORDS
channel.writeByte((byte) 0);
} finally {
endResponse(connection);
}
}
}
Aggregations