Search in sources :

Example 11 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class ODatabaseExport method exportManualIndexes.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void exportManualIndexes() throws IOException {
    listener.onMessage("\nExporting manual indexes content...");
    final OIndexManagerProxy indexManager = database.getMetadata().getIndexManager();
    indexManager.reload();
    final Collection<? extends OIndex<?>> indexes = indexManager.getIndexes();
    ODocument exportEntry = new ODocument();
    int manualIndexes = 0;
    writer.beginCollection(1, true, "manualIndexes");
    for (OIndex<?> index : indexes) {
        if (index.getName().equals(ODatabaseImport.EXPORT_IMPORT_MAP_NAME))
            continue;
        if (!index.isAutomatic()) {
            listener.onMessage("\n- Exporting index " + index.getName() + " ...");
            writer.beginObject(2, true, null);
            writer.writeAttribute(3, true, "name", index.getName());
            List<ODocument> indexContent = database.query(new OSQLSynchQuery<ODocument>("select from index:" + index.getName()));
            writer.beginCollection(3, true, "content");
            int i = 0;
            for (ODocument indexEntry : indexContent) {
                if (i > 0)
                    writer.append(",");
                indexEntry.setLazyLoad(false);
                final OIndexDefinition indexDefinition = index.getDefinition();
                exportEntry.reset();
                exportEntry.setLazyLoad(false);
                if (indexDefinition instanceof ORuntimeKeyIndexDefinition && ((ORuntimeKeyIndexDefinition) indexDefinition).getSerializer() != null) {
                    final OBinarySerializer binarySerializer = ((ORuntimeKeyIndexDefinition) indexDefinition).getSerializer();
                    final int dataSize = binarySerializer.getObjectSize(indexEntry.field("key"));
                    final byte[] binaryContent = new byte[dataSize];
                    binarySerializer.serialize(indexEntry.field("key"), binaryContent, 0);
                    exportEntry.field("binary", true);
                    exportEntry.field("key", binaryContent);
                } else {
                    exportEntry.field("binary", false);
                    exportEntry.field("key", indexEntry.field("key"));
                }
                exportEntry.field("rid", indexEntry.field("rid"));
                i++;
                writer.append(exportEntry.toJSON());
                final long percent = indexContent.size() / 10;
                if (percent > 0 && (i % percent) == 0)
                    listener.onMessage(".");
            }
            writer.endCollection(3, true);
            writer.endObject(2, true);
            listener.onMessage("OK (entries=" + index.getSize() + ")");
            manualIndexes++;
        }
    }
    writer.endCollection(1, true);
    listener.onMessage("\nOK (" + manualIndexes + " manual indexes)");
}
Also used : OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) ORuntimeKeyIndexDefinition(com.orientechnologies.orient.core.index.ORuntimeKeyIndexDefinition) OBinarySerializer(com.orientechnologies.common.serialization.types.OBinarySerializer) OIndexManagerProxy(com.orientechnologies.orient.core.index.OIndexManagerProxy) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 12 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OAutoMergeRecordConflictStrategy method onUpdate.

@Override
public byte[] onUpdate(OStorage storage, byte iRecordType, final ORecordId rid, final int iRecordVersion, final byte[] iRecordContent, final AtomicInteger iDatabaseVersion) {
    if (iRecordType == ODocument.RECORD_TYPE) {
        // No need lock, is already inside a lock. Use database to read temporary objects too
        OStorageOperationResult<ORawBuffer> res = storage.readRecord(rid, null, false, false, null);
        final ODocument storedRecord = new ODocument(rid).fromStream(res.getResult().getBuffer());
        ODocument newRecord = (ODocument) ORecordSaveThreadLocal.getLast();
        if (newRecord == null || !newRecord.getIdentity().equals(rid))
            newRecord = new ODocument(rid).fromStream(iRecordContent);
        storedRecord.merge(newRecord, true, true);
        iDatabaseVersion.set(Math.max(iDatabaseVersion.get(), iRecordVersion) + 1);
        return storedRecord.toStream();
    } else
        // NO DOCUMENT, CANNOT MERGE SO RELY TO THE VERSION CHECK
        checkVersions(rid, iRecordVersion, iDatabaseVersion.get());
    return null;
}
Also used : ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 13 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OContentRecordConflictStrategy method onUpdate.

@Override
public byte[] onUpdate(OStorage storage, final byte iRecordType, final ORecordId rid, final int iRecordVersion, final byte[] iRecordContent, final AtomicInteger iDatabaseVersion) {
    final boolean hasSameContent;
    if (iRecordType == ODocument.RECORD_TYPE) {
        // No need lock, is already inside a lock.
        OStorageOperationResult<ORawBuffer> res = storage.readRecord(rid, null, false, false, null);
        final ODocument storedRecord = new ODocument(rid).fromStream(res.getResult().getBuffer());
        final ODocument newRecord = new ODocument().fromStream(iRecordContent);
        final ODatabaseDocumentInternal currentDb = ODatabaseRecordThreadLocal.INSTANCE.get();
        hasSameContent = ODocumentHelper.hasSameContentOf(storedRecord, currentDb, newRecord, currentDb, null, false);
    } else {
        // CHECK BYTE PER BYTE
        final ORecordAbstract storedRecord = rid.getRecord();
        hasSameContent = Arrays.equals(storedRecord.toStream(), iRecordContent);
    }
    if (hasSameContent)
        // OK
        iDatabaseVersion.set(Math.max(iDatabaseVersion.get(), iRecordVersion));
    else
        // NO DOCUMENT, CANNOT MERGE SO RELY TO THE VERSION CHECK
        checkVersions(rid, iRecordVersion, iDatabaseVersion.get());
    return null;
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 14 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class ODatabaseDocumentTx method executeReadRecord.

/**
   * This method is internal, it can be subject to signature change or be removed, do not use.
   *
   * @Internal
   */
public <RET extends ORecord> RET executeReadRecord(final ORecordId rid, ORecord iRecord, final int recordVersion, final String fetchPlan, final boolean ignoreCache, final boolean iUpdateCache, final boolean loadTombstones, final OStorage.LOCKING_STRATEGY lockingStrategy, RecordReader recordReader) {
    checkOpeness();
    checkIfActive();
    getMetadata().makeThreadLocalSchemaSnapshot();
    ORecordSerializationContext.pushContext();
    try {
        checkSecurity(ORule.ResourceGeneric.CLUSTER, ORole.PERMISSION_READ, getClusterNameById(rid.getClusterId()));
        // SEARCH IN LOCAL TX
        ORecord record = getTransaction().getRecord(rid);
        if (record == OTransactionRealAbstract.DELETED_RECORD)
            // DELETED IN TX
            return null;
        if (record == null && !ignoreCache)
            // SEARCH INTO THE CACHE
            record = getLocalCache().findRecord(rid);
        if (record != null) {
            if (iRecord != null) {
                iRecord.fromStream(record.toStream());
                ORecordInternal.setVersion(iRecord, record.getVersion());
                record = iRecord;
            }
            OFetchHelper.checkFetchPlanValid(fetchPlan);
            if (callbackHooks(ORecordHook.TYPE.BEFORE_READ, record) == ORecordHook.RESULT.SKIP)
                return null;
            if (record.getInternalStatus() == ORecordElement.STATUS.NOT_LOADED)
                record.reload();
            if (lockingStrategy == OStorage.LOCKING_STRATEGY.KEEP_SHARED_LOCK) {
                OLogManager.instance().warn(this, "You use deprecated record locking strategy: %s it may lead to deadlocks " + lockingStrategy);
                record.lock(false);
            } else if (lockingStrategy == OStorage.LOCKING_STRATEGY.KEEP_EXCLUSIVE_LOCK) {
                OLogManager.instance().warn(this, "You use deprecated record locking strategy: %s it may lead to deadlocks " + lockingStrategy);
                record.lock(true);
            }
            callbackHooks(ORecordHook.TYPE.AFTER_READ, record);
            if (record instanceof ODocument)
                ODocumentInternal.checkClass((ODocument) record, this);
            return (RET) record;
        }
        final ORawBuffer recordBuffer;
        if (!rid.isValid())
            recordBuffer = null;
        else {
            OFetchHelper.checkFetchPlanValid(fetchPlan);
            int version;
            if (iRecord != null)
                version = iRecord.getVersion();
            else
                version = recordVersion;
            recordBuffer = recordReader.readRecord(storage, rid, fetchPlan, ignoreCache, version);
        }
        if (recordBuffer == null)
            return null;
        if (iRecord == null || ORecordInternal.getRecordType(iRecord) != recordBuffer.recordType)
            // NO SAME RECORD TYPE: CAN'T REUSE OLD ONE BUT CREATE A NEW ONE FOR IT
            iRecord = Orient.instance().getRecordFactoryManager().newInstance(recordBuffer.recordType);
        ORecordInternal.fill(iRecord, rid, recordBuffer.version, recordBuffer.buffer, false);
        if (iRecord instanceof ODocument)
            ODocumentInternal.checkClass((ODocument) iRecord, this);
        if (ORecordVersionHelper.isTombstone(iRecord.getVersion()))
            return (RET) iRecord;
        if (callbackHooks(ORecordHook.TYPE.BEFORE_READ, iRecord) == ORecordHook.RESULT.SKIP)
            return null;
        iRecord.fromStream(recordBuffer.buffer);
        callbackHooks(ORecordHook.TYPE.AFTER_READ, iRecord);
        if (iUpdateCache)
            getLocalCache().updateRecord(iRecord);
        return (RET) iRecord;
    } catch (OOfflineClusterException t) {
        throw t;
    } catch (ORecordNotFoundException t) {
        throw t;
    } catch (Throwable t) {
        if (rid.isTemporary())
            throw OException.wrapException(new ODatabaseException("Error on retrieving record using temporary RID: " + rid), t);
        else
            throw OException.wrapException(new ODatabaseException("Error on retrieving record " + rid + " (cluster: " + storage.getPhysicalClusterNameById(rid.getClusterId()) + ")"), t);
    } finally {
        ORecordSerializationContext.pullContext();
        getMetadata().clearThreadLocalSchemaSnapshot();
    }
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OOfflineClusterException(com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)

Example 15 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OTraverseRecordProcess method processFields.

private void processFields(Iterator<Object> target) {
    final ODocument doc = this.target.getRecord();
    while (target.hasNext()) {
        Object field = target.next();
        final Object fieldValue;
        if (field instanceof OSQLFilterItem)
            fieldValue = ((OSQLFilterItem) field).getValue(doc, null, null);
        else
            fieldValue = doc.rawField(field.toString());
        if (fieldValue != null) {
            final OTraverseAbstractProcess<?> subProcess;
            if (fieldValue instanceof Iterator<?> || OMultiValue.isMultiValue(fieldValue)) {
                final Iterator<?> coll;
                if (fieldValue instanceof ORecordLazyMultiValue)
                    coll = ((ORecordLazyMultiValue) fieldValue).rawIterator();
                else
                    coll = OMultiValue.getMultiValueIterator(fieldValue, false);
                subProcess = new OTraverseMultiValueProcess(command, (Iterator<Object>) coll, getPath().appendField(field.toString()));
            } else if (fieldValue instanceof OIdentifiable && ((OIdentifiable) fieldValue).getRecord() instanceof ODocument) {
                subProcess = new OTraverseRecordProcess(command, (ODocument) ((OIdentifiable) fieldValue).getRecord(), getPath().appendField(field.toString()));
            } else
                continue;
            command.getContext().push(subProcess);
        }
    }
}
Also used : Iterator(java.util.Iterator) OSQLFilterItem(com.orientechnologies.orient.core.sql.filter.OSQLFilterItem) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)

Aggregations

ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2200 Test (org.testng.annotations.Test)651 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)426 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)422 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)277 Test (org.junit.Test)267 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)257 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)244 ORID (com.orientechnologies.orient.core.id.ORID)196 ORecordId (com.orientechnologies.orient.core.id.ORecordId)139 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)122 ArrayList (java.util.ArrayList)118 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)103 HashMap (java.util.HashMap)103 HashSet (java.util.HashSet)96 ORecord (com.orientechnologies.orient.core.record.ORecord)80 Set (java.util.Set)76 OETLBaseTest (com.orientechnologies.orient.etl.OETLBaseTest)75 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)68 Collection (java.util.Collection)55