use of com.orientechnologies.orient.core.id.ORecordId 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.id.ORecordId 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.id.ORecordId 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.id.ORecordId in project orientdb by orientechnologies.
the class OServerCommandGetFileDownload method execute.
@Override
public boolean execute(OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: fileDownload/<database>/rid/[/<fileName>][/<fileType>].");
final String fileName = urlParts.length > 3 ? encodeResponseText(urlParts[3]) : "unknown";
final String fileType = urlParts.length > 5 ? encodeResponseText(urlParts[4]) + '/' + encodeResponseText(urlParts[5]) : (urlParts.length > 4 ? encodeResponseText(urlParts[4]) : "");
final String rid = urlParts[2];
iRequest.data.commandInfo = "Download";
iRequest.data.commandDetail = rid;
final ORecordAbstract response;
ODatabaseDocument db = getProfiledDatabaseInstance(iRequest);
try {
response = db.load(new ORecordId(rid));
if (response != null) {
if (response instanceof OBlob) {
sendORecordBinaryFileContent(iRequest, iResponse, OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, fileType, (OBlob) response, fileName);
} else if (response instanceof ODocument) {
for (OProperty prop : ODocumentInternal.getImmutableSchemaClass(((ODocument) response)).properties()) {
if (prop.getType().equals(OType.BINARY))
sendBinaryFieldFileContent(iRequest, iResponse, OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, fileType, (byte[]) ((ODocument) response).field(prop.getName()), fileName);
}
} else {
iResponse.send(OHttpUtils.STATUS_INVALIDMETHOD_CODE, "Record requested is not a file nor has a readable schema", OHttpUtils.CONTENT_TEXT_PLAIN, "Record requested is not a file nor has a readable schema", null);
}
} else {
iResponse.send(OHttpUtils.STATUS_INVALIDMETHOD_CODE, "Record requested not exists", OHttpUtils.CONTENT_TEXT_PLAIN, "Record requestes not exists", null);
}
} catch (Exception e) {
iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, OHttpUtils.STATUS_INTERNALERROR_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, e.getMessage(), null);
} finally {
if (db != null)
db.close();
}
return false;
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class CollectionIndexTest method testIndexCollectionUpdateRemoveItemInTx.
public void testIndexCollectionUpdateRemoveItemInTx() throws Exception {
Collector collector = new Collector();
collector.setStringCollection(new ArrayList<String>(Arrays.asList("spam", "eggs")));
collector = database.save(collector);
try {
database.begin();
Collector loadedCollector = (Collector) database.load(new ORecordId(collector.getId()));
loadedCollector.getStringCollection().remove("spam");
loadedCollector = database.save(loadedCollector);
database.commit();
} catch (Exception e) {
database.rollback();
throw e;
}
List<ODocument> result = database.command(new OCommandSQL("select key, rid from index:Collector.stringCollection")).execute();
Assert.assertNotNull(result);
Assert.assertEquals(result.size(), 1);
for (ODocument d : result) {
Assert.assertTrue(d.containsField("key"));
Assert.assertTrue(d.containsField("rid"));
if (!d.field("key").equals("eggs")) {
Assert.fail("Unknown key found: " + d.field("key"));
}
}
}
Aggregations