Search in sources :

Example 66 with ORecord

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();
    }
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecord(com.orientechnologies.orient.core.record.ORecord) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 67 with ORecord

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();
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) ORecord(com.orientechnologies.orient.core.record.ORecord) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 68 with ORecord

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);
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) OPaginatedCluster(com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 69 with ORecord

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;
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) ORemoteTask(com.orientechnologies.orient.server.distributed.task.ORemoteTask) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 70 with ORecord

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");
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODistributedOperationException(com.orientechnologies.orient.server.distributed.task.ODistributedOperationException)

Aggregations

ORecord (com.orientechnologies.orient.core.record.ORecord)177 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)80 ORecordId (com.orientechnologies.orient.core.id.ORecordId)37 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)35 ORID (com.orientechnologies.orient.core.id.ORID)24 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)19 IOException (java.io.IOException)18 Test (org.junit.Test)15 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)14 OException (com.orientechnologies.common.exception.OException)13 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)13 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)12 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)11 ArrayList (java.util.ArrayList)10 Test (org.testng.annotations.Test)8 OIOException (com.orientechnologies.common.io.OIOException)7 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)7 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)7 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)6 ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)6