use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class OServerCommandPutIndex method execute.
@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
final String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: index/<database>/<index-name>/<key>[/<value>]");
iRequest.data.commandInfo = "Index put";
ODatabaseDocument db = null;
try {
db = getProfiledDatabaseInstance(iRequest);
final OIndex<?> index = db.getMetadata().getIndexManager().getIndex(urlParts[2]);
if (index == null)
throw new IllegalArgumentException("Index name '" + urlParts[2] + "' not found");
final OIdentifiable record;
if (urlParts.length > 4)
// GET THE RECORD ID AS VALUE
record = new ORecordId(urlParts[4]);
else {
// GET THE REQUEST CONTENT AS DOCUMENT
if (iRequest.content == null || iRequest.content.length() == 0)
throw new IllegalArgumentException("Index's entry value is null");
record = new ODocument().fromJSON(iRequest.content);
}
final OIndexDefinition indexDefinition = index.getDefinition();
final Object key;
if (indexDefinition != null)
key = indexDefinition.createValue(urlParts[3]);
else
key = urlParts[3];
if (key == null)
throw new IllegalArgumentException("Invalid key value : " + urlParts[3]);
final boolean existent = record.getIdentity().isPersistent();
if (existent && record instanceof ORecord)
((ORecord) record).save();
index.put(key, record);
if (existent)
iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
else
iResponse.send(OHttpUtils.STATUS_CREATED_CODE, OHttpUtils.STATUS_CREATED_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
} finally {
if (db != null)
db.close();
}
return false;
}
use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class OAsyncCommandResultListener method linkdedBySimpleValue.
@Override
public void linkdedBySimpleValue(ODocument doc) {
ORemoteFetchListener listener = new ORemoteFetchListener() {
@Override
protected void sendRecord(ORecord iLinked) {
if (!alreadySent.contains(iLinked.getIdentity())) {
alreadySent.add(iLinked.getIdentity());
try {
// CACHE IT ON THE CLIENT
protocol.channel.writeByte((byte) 2);
protocol.writeIdentifiable(connection, iLinked);
} catch (IOException e) {
OLogManager.instance().error(this, "Cannot write against channel", e);
}
}
}
@Override
public void parseLinked(ODocument iRootRecord, OIdentifiable iLinked, Object iUserObject, String iFieldName, OFetchContext iContext) throws OFetchException {
if (iLinked instanceof ORecord)
sendRecord((ORecord) iLinked);
}
@Override
public void parseLinkedCollectionValue(ODocument iRootRecord, OIdentifiable iLinked, Object iUserObject, String iFieldName, OFetchContext iContext) throws OFetchException {
if (iLinked instanceof ORecord)
sendRecord((ORecord) iLinked);
}
};
final OFetchContext context = new ORemoteFetchContext();
OFetchHelper.fetch(doc, doc, OFetchHelper.buildFetchPlan(""), listener, context, "");
}
use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class OServerCommandGetCluster method execute.
@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: cluster/<database>/<cluster-name>[/<limit>]<br>Limit is optional and is setted to 20 by default. Set expressely to 0 to have no limits.");
iRequest.data.commandInfo = "Browse cluster";
iRequest.data.commandDetail = urlParts[2];
ODatabaseDocument db = null;
try {
db = getProfiledDatabaseInstance(iRequest);
if (db.getClusterIdByName(urlParts[2]) > -1) {
final int limit = urlParts.length > 3 ? Integer.parseInt(urlParts[3]) : 20;
final List<OIdentifiable> response = new ArrayList<OIdentifiable>();
for (ORecord rec : db.browseCluster(urlParts[2])) {
if (limit > 0 && response.size() >= limit)
break;
response.add(rec);
}
iResponse.writeRecords(response);
} else
iResponse.send(OHttpUtils.STATUS_NOTFOUND_CODE, null, null, null, null);
} finally {
if (db != null)
db.close();
}
return false;
}
use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class OServerCommandGetIndex method execute.
@SuppressWarnings("unchecked")
@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
final String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: index/<database>/<index-name>/<key>");
iRequest.data.commandInfo = "Index get";
ODatabaseDocument db = null;
try {
db = getProfiledDatabaseInstance(iRequest);
final OIndex<?> index = db.getMetadata().getIndexManager().getIndex(urlParts[2]);
if (index == null)
throw new IllegalArgumentException("Index name '" + urlParts[2] + "' not found");
final Object content = index.get(urlParts[3]);
if (content == null)
iResponse.send(OHttpUtils.STATUS_NOTFOUND_CODE, OHttpUtils.STATUS_NOTFOUND_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
else {
final StringBuilder buffer = new StringBuilder(128);
buffer.append('[');
if (content instanceof Collection<?>) {
Collection<OIdentifiable> collection = (Collection<OIdentifiable>) content;
int count = 0;
for (OIdentifiable item : collection) {
if (count > 0) {
buffer.append(", ");
}
buffer.append(item.getRecord().toJSON());
count++;
}
} else
buffer.append(((OIdentifiable) content).getRecord().toJSON());
buffer.append(']');
if (isJsonResponse(iResponse)) {
iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, buffer.toString(), null);
} else {
iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_TEXT_PLAIN, buffer.toString(), null);
}
}
} finally {
if (db != null)
db.close();
}
return false;
}
use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class SQLDeleteEdgeTest method testFromInString.
public void testFromInString() {
database.command(new OCommandSQL("CREATE CLASS FromInStringE extends E")).execute();
database.command(new OCommandSQL("CREATE CLASS FromInStringV extends V")).execute();
OIdentifiable v1 = database.command(new OCommandSQL("create vertex FromInStringV set name = ' from '")).execute();
OIdentifiable v2 = database.command(new OCommandSQL("create vertex FromInStringV set name = ' FROM '")).execute();
OIdentifiable v3 = database.command(new OCommandSQL("create vertex FromInStringV set name = ' TO '")).execute();
database.command(new OCommandSQL("create edge FromInStringE from " + v1.getIdentity() + " to " + v2.getIdentity())).execute();
database.command(new OCommandSQL("create edge FromInStringE from " + v1.getIdentity() + " to " + v3.getIdentity())).execute();
List<OIdentifiable> result = database.query(new OSQLSynchQuery<ODocument>("SELECT expand(out()[name = ' FROM ']) FROM FromInStringV"));
Assert.assertEquals(result.size(), 1);
result = database.query(new OSQLSynchQuery<ODocument>("SELECT expand(in()[name = ' from ']) FROM FromInStringV"));
Assert.assertEquals(result.size(), 2);
result = database.query(new OSQLSynchQuery<ODocument>("SELECT expand(out()[name = ' TO ']) FROM FromInStringV"));
Assert.assertEquals(result.size(), 1);
}
Aggregations