use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OCommandExecutorSQLDelete method execute.
public Object execute(final Map<Object, Object> iArgs) {
if (query == null && indexName == null)
throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
if (!returning.equalsIgnoreCase("COUNT"))
allDeletedRecords = new ArrayList<ORecord>();
if (query != null) {
// AGAINST CLUSTERS AND CLASSES
query.setContext(getContext());
Object prevLockValue = query.getContext().getVariable("$locking");
if (lockStrategy.equals("RECORD"))
query.getContext().setVariable("$locking", OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK);
query.execute(iArgs);
query.getContext().setVariable("$locking", prevLockValue);
if (returning.equalsIgnoreCase("COUNT"))
// RETURNS ONLY THE COUNT
return recordCount;
else
// RETURNS ALL THE DELETED RECORDS
return allDeletedRecords;
} else {
// AGAINST INDEXES
if (compiledFilter != null)
compiledFilter.bindParameters(iArgs);
final OIndex index = getDatabase().getMetadata().getIndexManager().getIndex(indexName);
if (index == null)
throw new OCommandExecutionException("Target index '" + indexName + "' not found");
Object key = null;
Object value = VALUE_NOT_FOUND;
if (compiledFilter == null || compiledFilter.getRootCondition() == null) {
if (returning.equalsIgnoreCase("COUNT")) {
// RETURNS ONLY THE COUNT
final long total = index.getSize();
index.clear();
return total;
} else {
// RETURNS ALL THE DELETED RECORDS
OIndexCursor cursor = index.cursor();
Map.Entry<Object, OIdentifiable> entry;
while ((entry = cursor.nextEntry()) != null) {
OIdentifiable rec = entry.getValue();
rec = rec.getRecord();
if (rec != null)
allDeletedRecords.add((ORecord) rec);
}
index.clear();
return allDeletedRecords;
}
} else {
if (KEYWORD_KEY.equalsIgnoreCase(compiledFilter.getRootCondition().getLeft().toString()))
// FOUND KEY ONLY
key = getIndexKey(index.getDefinition(), compiledFilter.getRootCondition().getRight());
else if (KEYWORD_RID.equalsIgnoreCase(compiledFilter.getRootCondition().getLeft().toString())) {
// BY RID
value = OSQLHelper.getValue(compiledFilter.getRootCondition().getRight());
} else if (compiledFilter.getRootCondition().getLeft() instanceof OSQLFilterCondition) {
// KEY AND VALUE
final OSQLFilterCondition leftCondition = (OSQLFilterCondition) compiledFilter.getRootCondition().getLeft();
if (KEYWORD_KEY.equalsIgnoreCase(leftCondition.getLeft().toString()))
key = getIndexKey(index.getDefinition(), leftCondition.getRight());
final OSQLFilterCondition rightCondition = (OSQLFilterCondition) compiledFilter.getRootCondition().getRight();
if (KEYWORD_RID.equalsIgnoreCase(rightCondition.getLeft().toString()))
value = OSQLHelper.getValue(rightCondition.getRight());
}
final boolean result;
if (value != VALUE_NOT_FOUND) {
assert key != null;
result = index.remove(key, (OIdentifiable) value);
} else
result = index.remove(key);
if (returning.equalsIgnoreCase("COUNT"))
return result ? 1 : 0;
else
// TODO: REFACTOR INDEX TO RETURN DELETED ITEMS
throw new UnsupportedOperationException();
}
}
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OStreamSerializerAnyRecord method toStream.
public byte[] toStream(Object iObject) throws IOException {
if (iObject == null)
return null;
if (((ORecord) iObject).getIdentity() == null)
throw new OSerializationException("Cannot serialize record without identity. Store it before serialization.");
final StringBuilder buffer = OStreamSerializerHelper.writeRecordType(iObject.getClass(), new StringBuilder(1024));
buffer.append(((ORecord) iObject).getIdentity().toString());
return buffer.toString().getBytes("UTF-8");
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OStreamSerializerAnyRecord method fromStream.
/**
* Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
*/
public Object fromStream(byte[] iStream) throws IOException {
if (iStream == null || iStream.length == 0)
// NULL VALUE
return null;
final String stream = new String(iStream, "UTF-8");
Class<?> cls = null;
try {
final StringBuilder content = new StringBuilder(1024);
cls = OStreamSerializerHelper.readRecordType(stream, content);
// TRY WITH THE DATABASE CONSTRUCTOR
for (Constructor<?> c : cls.getDeclaredConstructors()) {
Class<?>[] params = c.getParameterTypes();
if (params.length == 2 && params[1].equals(ORID.class)) {
ORecord rec = (ORecord) c.newInstance(new ORecordId(content.toString()));
// rec.load();
return rec;
}
}
} catch (Exception e) {
throw OException.wrapException(new OSerializationException("Error on unmarshalling content. Class " + (cls != null ? cls.getName() : "?")), e);
}
throw new OSerializationException("Cannot unmarshall the record since the serialized object of class " + (cls != null ? cls.getSimpleName() : "?") + " has no constructor with suitable parameters: (ORID)");
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OObjectDatabaseTx method delete.
@Override
public ODatabaseObject delete(final ORID iRID) {
checkOpeness();
if (iRID == null)
return this;
final ORecord record = iRID.getRecord();
if (record instanceof ODocument) {
Object iPojo = getUserObjectByRecord(record, null);
deleteCascade((ODocument) record);
underlying.delete(record);
if (getTransaction() instanceof OTransactionNoTx)
unregisterPojo(iPojo, (ODocument) record);
}
return this;
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OObjectLazySet method convertAllInternal.
protected void convertAllInternal() {
if (converted || !convertToRecord)
return;
final Set<Object> copy = new HashSet<Object>(underlying);
super.clear();
final ODatabasePojoAbstract<TYPE> database = getDatabase();
for (Object e : copy) {
if (e != null) {
if (e instanceof ORID)
super.add(database.getUserObjectByRecord(((ODatabaseDocument) getDatabase().getUnderlying()).load((ORID) e, fetchPlan), fetchPlan));
else if (e instanceof ODocument)
super.add(database.getUserObjectByRecord((ORecord) e, fetchPlan));
else
super.add((TYPE) e);
}
}
converted = true;
}
Aggregations