use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OServerCommandGetDictionary method execute.
@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
iRequest.data.commandInfo = "Dictionary lookup";
String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: dictionary/<database>/<key>");
ODatabaseDocument db = null;
try {
db = getProfiledDatabaseInstance(iRequest);
final ORecord record = db.getDictionary().get(urlParts[2]);
if (record == null)
throw new ORecordNotFoundException(null, "Key '" + urlParts[2] + "' was not found in the database dictionary");
iResponse.writeRecord(record);
} finally {
if (db != null)
db.close();
}
return false;
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OServerCommandGetDocument method execute.
@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
ODatabaseDocument db = null;
final String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: document/<database>/<record-id>[/fetchPlan]");
final String fetchPlan = urlParts.length > 3 ? urlParts[3] : null;
iRequest.data.commandInfo = "Load document";
final ORecord rec;
final int parametersPos = urlParts[2].indexOf('?');
final String rid = parametersPos > -1 ? urlParts[2].substring(0, parametersPos) : urlParts[2];
try {
db = getProfiledDatabaseInstance(iRequest);
rec = db.load(new ORecordId(rid), fetchPlan);
if (rec == null)
iResponse.send(OHttpUtils.STATUS_NOTFOUND_CODE, "Not Found", OHttpUtils.CONTENT_JSON, "Record with id '" + urlParts[2] + "' was not found.", null);
else if (iRequest.httpMethod.equals("HEAD"))
// JUST SEND HTTP CODE 200
iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, null, null, OHttpUtils.HEADER_ETAG + rec.getVersion());
else {
final String ifNoneMatch = iRequest.getHeader("If-None-Match");
if (ifNoneMatch != null && Integer.toString(rec.getVersion()).equals(ifNoneMatch)) {
// SAME CONTENT, DON'T SEND BACK RECORD
iResponse.send(OHttpUtils.STATUS_OK_NOMODIFIED_CODE, OHttpUtils.STATUS_OK_NOMODIFIED_DESCRIPTION, null, null, OHttpUtils.HEADER_ETAG + rec.getVersion());
}
// SEND THE DOCUMENT BACK
iResponse.writeRecord(rec, fetchPlan, null);
}
} finally {
if (db != null)
db.close();
}
return false;
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method createRecord.
protected void createRecord(OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "Create record");
if (!isConnectionAlive(connection))
return;
final int dataSegmentId = connection.getData().protocolVersion < 24 ? channel.readInt() : 0;
final ORecordId rid = new ORecordId(channel.readShort(), ORID.CLUSTER_POS_INVALID);
final byte[] buffer = channel.readBytes();
final byte recordType = channel.readByte();
final byte mode = channel.readByte();
final ORecord record = createRecord(connection, rid, buffer, recordType);
if (mode < 2) {
beginResponse();
try {
sendOk(connection, clientTxId);
if (connection.getData().protocolVersion > OChannelBinaryProtocol.PROTOCOL_VERSION_25)
channel.writeShort((short) record.getIdentity().getClusterId());
channel.writeLong(record.getIdentity().getClusterPosition());
channel.writeVersion(record.getVersion());
if (connection.getData().protocolVersion >= 20)
sendCollectionChanges(connection);
} finally {
endResponse(connection);
}
}
}
use of com.orientechnologies.orient.core.record.ORecord 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);
}
}
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OMatchStatement method addResult.
private boolean addResult(MatchContext matchContext, OSQLAsynchQuery<ODocument> request, OCommandContext ctx) {
ODocument doc = null;
if (returnsElements()) {
for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
if (isExplicitAlias(entry.getKey()) && entry.getValue() != null) {
ORecord record = entry.getValue().getRecord();
if (request.getResultListener() != null && record != null) {
if (!addSingleResult(request, (OBasicCommandContext) ctx, record))
return false;
}
}
}
} else if (returnsPathElements()) {
for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
if (entry.getValue() != null) {
ORecord record = entry.getValue().getRecord();
if (request.getResultListener() != null && record != null) {
if (!addSingleResult(request, (OBasicCommandContext) ctx, record))
return false;
}
}
}
} else if (returnsPatterns()) {
doc = getDatabase().newInstance();
doc.setTrackingChanges(false);
for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
if (isExplicitAlias(entry.getKey())) {
doc.field(entry.getKey(), entry.getValue());
}
}
} else if (returnsPaths()) {
doc = getDatabase().newInstance();
doc.setTrackingChanges(false);
for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
doc.field(entry.getKey(), entry.getValue());
}
} else if (returnsJson()) {
doc = jsonToDoc(matchContext, ctx);
} else {
doc = getDatabase().newInstance();
doc.setTrackingChanges(false);
int i = 0;
ODocument mapDoc = new ODocument();
mapDoc.setTrackingChanges(false);
mapDoc.fromMap((Map) matchContext.matched);
ctx.setVariable("$current", mapDoc);
for (OExpression item : returnItems) {
OIdentifier returnAliasIdentifier = returnAliases.get(i);
OIdentifier returnAlias;
if (returnAliasIdentifier == null) {
returnAlias = item.getDefaultAlias();
} else {
returnAlias = returnAliasIdentifier;
}
doc.field(returnAlias.getStringValue(), item.execute(mapDoc, ctx));
i++;
}
doc.setTrackingChanges(true);
}
if (request.getResultListener() != null && doc != null) {
if (!addSingleResult(request, (OBasicCommandContext) ctx, doc))
return false;
}
return true;
}
Aggregations