use of com.orientechnologies.orient.core.record.ORecord 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.record.ORecord in project orientdb by orientechnologies.
the class SQLInsertTest method getValidPositions.
private List<Long> getValidPositions(int clusterId) {
final List<Long> positions = new ArrayList<Long>();
final ORecordIteratorCluster<?> iteratorCluster = database.browseCluster(database.getClusterNameById(clusterId));
for (int i = 0; i < 100; i++) {
if (!iteratorCluster.hasNext())
break;
ORecord doc = iteratorCluster.next();
positions.add(doc.getIdentity().getClusterPosition());
}
return positions;
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OTraverseRecordSetProcess method process.
@SuppressWarnings("unchecked")
public OIdentifiable process() {
while (target.hasNext()) {
record = target.next();
index++;
final ORecord rec = record.getRecord();
if (rec instanceof ODocument) {
ODocument doc = (ODocument) rec;
if (doc.getIdentity().getClusterId() == -2 && doc.fields() == 1) {
// projection
// EXTRACT THE FIELD CONTEXT
Object fieldvalue = doc.field(doc.fieldNames()[0]);
if (fieldvalue instanceof Collection<?>) {
command.getContext().push(new OTraverseRecordSetProcess(command, ((Collection<OIdentifiable>) fieldvalue).iterator(), getPath()));
} else if (fieldvalue instanceof ODocument) {
command.getContext().push(new OTraverseRecordProcess(command, (ODocument) fieldvalue, getPath()));
}
} else {
command.getContext().push(new OTraverseRecordProcess(command, (ODocument) rec, getPath()));
}
return null;
}
}
return pop();
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class ORecordSerializerCSVAbstract method embeddedMapToStream.
public void embeddedMapToStream(ODatabase<?> iDatabase, final OUserObject2RecordHandler iObjHandler, final StringBuilder iOutput, final OClass iLinkedClass, OType iLinkedType, final Object iValue, final boolean iSaveOnlyDirty) {
iOutput.append(OStringSerializerHelper.MAP_BEGIN);
if (iValue != null) {
int items = 0;
// EMBEDDED OBJECTS
for (Entry<String, Object> o : ((Map<String, Object>) iValue).entrySet()) {
if (items > 0)
iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
if (o != null) {
fieldTypeToString(iOutput, OType.STRING, o.getKey());
iOutput.append(OStringSerializerHelper.ENTRY_SEPARATOR);
if (o.getValue() instanceof ODocument && ((ODocument) o.getValue()).getIdentity().isValid()) {
fieldTypeToString(iOutput, OType.LINK, o.getValue());
} else if (o.getValue() instanceof ORecord || o.getValue() instanceof ODocumentSerializable) {
final ODocument record;
if (o.getValue() instanceof ODocument)
record = (ODocument) o.getValue();
else if (o.getValue() instanceof ODocumentSerializable) {
record = ((ODocumentSerializable) o.getValue()).toDocument();
record.field(ODocumentSerializable.CLASS_NAME, o.getValue().getClass().getName());
} else {
if (iDatabase == null && ODatabaseRecordThreadLocal.INSTANCE.isDefined())
iDatabase = ODatabaseRecordThreadLocal.INSTANCE.get();
record = OObjectSerializerHelperManager.getInstance().toStream(o.getValue(), new ODocument(o.getValue().getClass().getSimpleName()), iDatabase instanceof ODatabaseObject ? ((ODatabaseObject) iDatabase).getEntityManager() : OEntityManagerInternal.INSTANCE, iLinkedClass, iObjHandler != null ? iObjHandler : new OUserObject2RecordHandler() {
public Object getUserObjectByRecord(OIdentifiable iRecord, final String iFetchPlan) {
return iRecord;
}
public ORecord getRecordByUserObject(Object iPojo, boolean iCreateIfNotAvailable) {
return new ODocument(iLinkedClass);
}
public boolean existsUserObjectByRID(ORID iRID) {
return false;
}
public void registerUserObject(Object iObject, ORecord iRecord) {
}
public void registerUserObjectAfterLinkSave(ORecord iRecord) {
}
}, null, iSaveOnlyDirty);
}
iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
toString(record, iOutput, null, iObjHandler, false, true);
iOutput.append(OStringSerializerHelper.EMBEDDED_END);
} else if (o.getValue() instanceof Set<?>) {
// SUB SET
fieldTypeToString(iOutput, OType.EMBEDDEDSET, o.getValue());
} else if (o.getValue() instanceof Collection<?>) {
// SUB LIST
fieldTypeToString(iOutput, OType.EMBEDDEDLIST, o.getValue());
} else if (o.getValue() instanceof Map<?, ?>) {
// SUB MAP
fieldTypeToString(iOutput, OType.EMBEDDEDMAP, o.getValue());
} else {
// EMBEDDED LITERALS
if (iLinkedType == null && o.getValue() != null) {
fieldTypeToString(iOutput, OType.getTypeByClass(o.getValue().getClass()), o.getValue());
} else {
fieldTypeToString(iOutput, iLinkedType, o.getValue());
}
}
}
items++;
}
}
iOutput.append(OStringSerializerHelper.MAP_END);
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class ORecordSerializerNetworkV0 method writeOptimizedLink.
private int writeOptimizedLink(final BytesContainer bytes, OIdentifiable link) {
if (!link.getIdentity().isPersistent()) {
final ORecord real = link.getRecord();
if (real != null)
link = real;
}
final int pos = OVarIntSerializer.write(bytes, link.getIdentity().getClusterId());
OVarIntSerializer.write(bytes, link.getIdentity().getClusterPosition());
return pos;
}
Aggregations