use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class TestOrderBy method testGermanOrderBy.
@Test
public void testGermanOrderBy() {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:testGermanOrderBy");
db.set(ATTRIBUTES.LOCALECOUNTRY, Locale.GERMANY.getCountry());
db.set(ATTRIBUTES.LOCALELANGUAGE, Locale.GERMANY.getLanguage());
db.create();
try {
db.getMetadata().getSchema().createClass("test");
ORecord res1 = db.save(new ODocument("test").field("name", "Ähhhh"));
ORecord res2 = db.save(new ODocument("test").field("name", "Ahhhh"));
ORecord res3 = db.save(new ODocument("test").field("name", "Zebra"));
List<?> queryRes = db.query(new OSQLSynchQuery<Object>("select from test order by name"));
assertEquals(queryRes.get(0), res2);
assertEquals(queryRes.get(1), res1);
assertEquals(queryRes.get(2), res3);
queryRes = db.query(new OSQLSynchQuery<Object>("select from test order by name desc "));
assertEquals(queryRes.get(0), res3);
assertEquals(queryRes.get(1), res1);
assertEquals(queryRes.get(2), res2);
} finally {
db.drop();
}
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OUpdateRecordTask method executeRecordTask.
@Override
public Object executeRecordTask(ODistributedRequestId requestId, final OServer iServer, ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Updating record %s/%s v.%d reqId=%s...", database.getName(), rid.toString(), version, requestId);
prepareUndoOperation();
if (previousRecord == null) {
// RESURRECT/CREATE IT
final OPlaceholder ph = (OPlaceholder) new OCreateRecordTask(rid, content, version, recordType).executeRecordTask(requestId, iServer, iManager, database);
record = ph.getRecord();
if (record == null)
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> Error on updating record %s", rid);
} else {
// UPDATE IT
final int loadedVersion = previousRecord.getVersion();
if (loadedVersion == version + 1 && Arrays.equals(content, previousRecordContent)) {
// OPERATION ALREADY EXECUTED
record = previousRecord;
} else {
final ORecord loadedRecord = previousRecord.copy();
if (loadedRecord instanceof ODocument) {
// APPLY CHANGES FIELD BY FIELD TO MARK DIRTY FIELDS FOR INDEXES/HOOKS
final ODocument newDocument = (ODocument) getRecord();
final ODocument loadedDocument = (ODocument) loadedRecord;
loadedDocument.merge(newDocument, false, false).getVersion();
ORecordInternal.setVersion(loadedDocument, version);
} else
ORecordInternal.fill(loadedRecord, rid, version, content, true);
loadedRecord.setDirty();
record = database.save(loadedRecord);
if (record == null)
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> Error on updating record %s", rid);
if (version < 0 && ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> Reverted %s from version %d to %d", rid, loadedVersion, record.getVersion());
}
}
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> updated record %s/%s v.%d [%s]", database.getName(), rid.toString(), record.getVersion(), record);
return record.getVersion();
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OCreateRecordTask method executeRecordTask.
@Override
public Object executeRecordTask(final ODistributedRequestId requestId, final OServer iServer, final ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Creating record %s/%s v.%d reqId=%s...", database.getName(), rid.toString(), version, requestId);
if (!rid.isPersistent())
throw new ODistributedException("Record " + rid + " has not been saved on owner node first (temporary rid)");
final OPaginatedCluster cluster = (OPaginatedCluster) ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getClusterById(rid.getClusterId());
final OPaginatedCluster.RECORD_STATUS recordStatus = cluster.getRecordStatus(rid.getClusterPosition());
switch(recordStatus) {
case REMOVED:
// RECYCLE THE RID AND OVERWRITE IT WITH THE NEW CONTENT
ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().recyclePosition(rid, new byte[] {}, version, recordType);
// CREATE A RECORD TO CALL ALL THE HOOKS (LIKE INDEXES FOR UNIQUE CONSTRAINTS)
final ORecord loadedRecordInstance = Orient.instance().getRecordFactoryManager().newInstance(recordType);
ORecordInternal.fill(loadedRecordInstance, rid, version, content, true);
loadedRecordInstance.save();
return new OPlaceholder(rid, loadedRecordInstance.getVersion());
case ALLOCATED:
case PRESENT:
final OStorageOperationResult<ORawBuffer> loadedRecord = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().readRecord(rid, null, true, false, null);
if (loadedRecord.getResult() != null) {
// ALREADY PRESENT
record = forceUpdate(iManager, database, requestId, loadedRecord.getResult());
return new OPlaceholder(record);
}
case NOT_EXISTENT:
// try {
ORecordId newRid;
do {
getRecord();
if (clusterId > -1)
record.save(database.getClusterNameById(clusterId), true);
else if (rid.getClusterId() != -1)
record.save(database.getClusterNameById(rid.getClusterId()), true);
else
record.save();
newRid = (ORecordId) record.getIdentity();
if (newRid.getClusterPosition() >= rid.getClusterPosition())
break;
// CREATE AN HOLE
record.delete();
record = null;
} while (newRid.getClusterPosition() < rid.getClusterPosition());
if (!rid.equals(newRid)) {
ODistributedServerLog.warn(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Record %s has been saved with the RID %s instead of the expected %s reqId=%s", record, newRid, rid, requestId);
// DELETE THE INVALID RECORD FIRST
record.delete();
throw new ODistributedException("Record " + rid + " has been saved with the different RID " + newRid + " on server " + iManager.getLocalNodeName());
}
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> assigned new rid %s/%s v.%d reqId=%s", database.getName(), rid.toString(), record.getVersion(), requestId);
}
// IMPROVED TRANSPORT BY AVOIDING THE RECORD CONTENT, BUT JUST RID + VERSION
return new OPlaceholder(record);
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OCreateRecordTask method getFixTask.
@Override
public ORemoteTask getFixTask(final ODistributedRequest iRequest, ORemoteTask iOriginalTask, final Object iBadResponse, final Object iGoodResponse, final String executorNode, final ODistributedServerManager dManager) {
if (iBadResponse == null || iBadResponse instanceof Throwable)
return null;
final OPlaceholder badResult = (OPlaceholder) iBadResponse;
final OPlaceholder goodResult = (OPlaceholder) iGoodResponse;
ORemoteTask result = null;
if (!badResult.equals(goodResult)) {
// CREATE RECORD FAILED TO HAVE THE SAME RIDS. FORCE REALIGNING OF DATA CLUSTERS
if (badResult.getIdentity().getClusterId() == goodResult.getIdentity().getClusterId() && badResult.getIdentity().getClusterPosition() < goodResult.getIdentity().getClusterPosition()) {
final long minPos = Math.max(badResult.getIdentity().getClusterPosition() - 1, 0);
for (long pos = minPos; pos < goodResult.getIdentity().getClusterPosition(); ++pos) {
// UPDATE INTERMEDIATE RECORDS
final ORecordId toUpdateRid = new ORecordId(goodResult.getIdentity().getClusterId(), pos);
final ORecord toUpdateRecord;
if (dManager.getLocalNodeName().equals(executorNode)) {
// SAME SERVER: LOAD THE RECORD FROM ANOTHER NODE
final ODistributedConfiguration dCfg = dManager.getDatabaseConfiguration(iRequest.getDatabaseName());
final List<String> nodes = dCfg.getServers(ODatabaseRecordThreadLocal.INSTANCE.get().getClusterNameById(clusterId), dManager.getLocalNodeName());
final ODistributedResponse response = dManager.sendRequest(iRequest.getDatabaseName(), null, nodes, new OReadRecordTask(toUpdateRid), dManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
final ORawBuffer remoteReadRecord = (ORawBuffer) response.getPayload();
if (remoteReadRecord != null) {
toUpdateRecord = Orient.instance().getRecordFactoryManager().newInstance(recordType);
ORecordInternal.fill(toUpdateRecord, toUpdateRid, remoteReadRecord.version, remoteReadRecord.buffer, false);
} else
toUpdateRecord = null;
} else
// LOAD IT LOCALLY
toUpdateRecord = toUpdateRid.getRecord();
if (toUpdateRecord != null)
result = new OFixUpdateRecordTask(toUpdateRid, toUpdateRecord.toStream(), toUpdateRecord.getVersion(), ORecordInternal.getRecordType(toUpdateRecord));
}
// CREATE LAST RECORD
result = new OCreateRecordTask((ORecordId) goodResult.getIdentity(), content, version, recordType);
} else if (badResult.getIdentity().getClusterId() == goodResult.getIdentity().getClusterId() && badResult.getIdentity().getClusterPosition() > goodResult.getIdentity().getClusterPosition()) {
} else
// ANY OTHER CASE JUST DELETE IT
result = new OFixCreateRecordTask(new ORecordId(badResult.getIdentity()), badResult.getVersion());
}
return result;
}
use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.
the class OCreateRecordTask method forceUpdate.
protected ORecord forceUpdate(final ODistributedServerManager manager, final ODatabaseDocumentInternal database, final ODistributedRequestId requestId, final ORawBuffer loadedRecord) {
// LOAD IT AS RECORD
final ORecord loadedRecordInstance = Orient.instance().getRecordFactoryManager().newInstance(loadedRecord.recordType);
ORecordInternal.fill(loadedRecordInstance, rid, loadedRecord.version, loadedRecord.getBuffer(), false);
// RECORD HAS BEEN ALREADY CREATED (PROBABLY DURING DATABASE SYNC) CHECKING COHERENCY
if (Arrays.equals(loadedRecord.getBuffer(), content))
// SAME CONTENT
return loadedRecordInstance;
ODistributedServerLog.info(this, manager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Error on creating record in an existent position. toStore=%s stored=%s reqId=%s", getRecord(), loadedRecordInstance, requestId);
throw new ODistributedOperationException("Cannot create the record " + rid + " in an already existent position");
}
Aggregations