Search in sources :

Example 11 with OPair

use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.

the class ORuntimeResult method applyRecord.

@SuppressWarnings("unchecked")
public static ODocument applyRecord(final ODocument iValue, final Map<String, Object> iProjections, final OCommandContext iContext, final OIdentifiable iRecord) {
    // APPLY PROJECTIONS
    ORecord record = (iRecord != null ? iRecord.getRecord() : null);
    //MANAGE SPECIFIC CASES FOR RECORD BYTES
    if (ORecordBytes.RECORD_TYPE == ORecordInternal.getRecordType(record)) {
        for (Entry<String, Object> projection : iProjections.entrySet()) {
            if ("@rid".equalsIgnoreCase("" + projection.getValue())) {
                iValue.field(projection.getKey(), record.getIdentity());
            } else if ("@size".equalsIgnoreCase("" + projection.getValue())) {
                iValue.field(projection.getKey(), record.getSize());
            } else if ("@version".equalsIgnoreCase("" + projection.getValue())) {
                iValue.field(projection.getKey(), record.getVersion());
            } else {
                Object val = projection.getValue();
                if (val instanceof Number || val instanceof String || val instanceof Boolean) {
                    iValue.field(projection.getKey(), val);
                } else {
                    iValue.field(projection.getKey(), (Object) null);
                }
            }
        }
        return iValue;
    }
    final ODocument inputDocument = (ODocument) record;
    if (iProjections.isEmpty())
        // SELECT * CASE
        inputDocument.copyTo(iValue);
    else {
        for (Entry<String, Object> projection : iProjections.entrySet()) {
            final String prjName = projection.getKey();
            final Object v = projection.getValue();
            if (v == null && prjName != null) {
                iValue.field(prjName, (Object) null);
                continue;
            }
            final Object projectionValue;
            if (v != null && v.equals("*")) {
                // COPY ALL
                inputDocument.copyTo(iValue);
                // CONTINUE WITH NEXT ITEM
                continue;
            } else if (v instanceof OSQLFilterItemVariable || v instanceof OSQLFilterItemField) {
                final OSQLFilterItemAbstract var = (OSQLFilterItemAbstract) v;
                final OPair<OSQLMethodRuntime, Object[]> last = var.getLastChainOperator();
                if (last != null && last.getKey().getMethod() instanceof OSQLMethodField && last.getValue() != null && last.getValue().length == 1 && last.getValue()[0].equals("*")) {
                    final Object value = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
                    if (inputDocument != null && value != null && inputDocument instanceof ODocument && value instanceof ODocument) {
                        // COPY FIELDS WITH PROJECTION NAME AS PREFIX
                        for (String fieldName : ((ODocument) value).fieldNames()) {
                            iValue.field(prjName + fieldName, ((ODocument) value).field(fieldName));
                        }
                    }
                    projectionValue = null;
                } else
                    // RETURN A VARIABLE FROM THE CONTEXT
                    projectionValue = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
            } else if (v instanceof OSQLFunctionRuntime) {
                final OSQLFunctionRuntime f = (OSQLFunctionRuntime) v;
                projectionValue = f.execute(inputDocument, inputDocument, iValue, iContext);
            } else {
                if (v == null) {
                    // SIMPLE NULL VALUE: SET IT IN DOCUMENT
                    iValue.field(prjName, v);
                    continue;
                }
                projectionValue = v;
            }
            if (projectionValue != null)
                if (projectionValue instanceof ORidBag)
                    iValue.field(prjName, new ORidBag((ORidBag) projectionValue));
                else if (projectionValue instanceof OIdentifiable && !(projectionValue instanceof ORID) && !(projectionValue instanceof ORecord))
                    iValue.field(prjName, ((OIdentifiable) projectionValue).getRecord());
                else if (projectionValue instanceof Iterator) {
                    boolean link = true;
                    // make temporary value typical case graph database elemenet's iterator edges
                    if (projectionValue instanceof OResettable)
                        ((OResettable) projectionValue).reset();
                    final List<Object> iteratorValues = new ArrayList<Object>();
                    final Iterator projectionValueIterator = (Iterator) projectionValue;
                    while (projectionValueIterator.hasNext()) {
                        Object value = projectionValueIterator.next();
                        if (value instanceof OIdentifiable) {
                            value = ((OIdentifiable) value).getRecord();
                            if (value != null && !((OIdentifiable) value).getIdentity().isPersistent())
                                link = false;
                        }
                        if (value != null)
                            iteratorValues.add(value);
                    }
                    iValue.field(prjName, iteratorValues, link ? OType.LINKLIST : OType.EMBEDDEDLIST);
                } else if (projectionValue instanceof ODocument && !((ODocument) projectionValue).getIdentity().isPersistent()) {
                    iValue.field(prjName, projectionValue, OType.EMBEDDED);
                } else if (projectionValue instanceof Set<?>) {
                    OType type = OType.getTypeByValue(projectionValue);
                    if (type == OType.LINKSET && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
                        type = OType.EMBEDDEDSET;
                    iValue.field(prjName, projectionValue, type);
                } else if (projectionValue instanceof Map<?, ?>) {
                    OType type = OType.getTypeByValue(projectionValue);
                    if (type == OType.LINKMAP && !entriesPersistent(((Map<?, OIdentifiable>) projectionValue).values()))
                        type = OType.EMBEDDEDMAP;
                    iValue.field(prjName, projectionValue, type);
                } else if (projectionValue instanceof List<?>) {
                    OType type = OType.getTypeByValue(projectionValue);
                    if (type == OType.LINKLIST && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
                        type = OType.EMBEDDEDLIST;
                    iValue.field(prjName, projectionValue, type);
                } else
                    iValue.field(prjName, projectionValue);
        }
    }
    return iValue;
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) OSQLFilterItemAbstract(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemAbstract) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OSQLMethodField(com.orientechnologies.orient.core.sql.method.misc.OSQLMethodField) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) OResettable(com.orientechnologies.common.util.OResettable) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OType(com.orientechnologies.orient.core.metadata.schema.OType) OSQLFilterItemVariable(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable) OPair(com.orientechnologies.common.util.OPair) ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 12 with OPair

use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.

the class O2QCache method restoreQueueWithPageLoad.

/**
   * Restores queues state if it is needed to load cache page from disk to cache.
   * <p>
   * Following format is used to store queue state:
   * <p>
   * <ol>
   * <li>File id or -1 if end of queue is reached (int)</li>
   * <li>Page index (long), is absent if end of the queue is reached</li>
   * </ol>
   *
   * @param dataInputStream Stream of file which contains state of the cache.
   * @param queue           Queue, state of which should be restored.
   * @param writeCache      Write cache is used to load data from disk if needed.
   */
private void restoreQueueWithPageLoad(OWriteCache writeCache, LRUList queue, DataInputStream dataInputStream) throws IOException {
    // used only for statistics, and there is passed merely as stub
    final OModifiableBoolean cacheHit = new OModifiableBoolean();
    // first step, we will create two tree maps to sort data by position in file and load them with maximum speed and
    // then to put data into the queue to restore position of entries in LRU list.
    final TreeMap<PageKey, OPair<Long, OCacheEntry>> filePositionMap = new TreeMap<PageKey, OPair<Long, OCacheEntry>>();
    final TreeMap<Long, OCacheEntry> queuePositionMap = new TreeMap<Long, OCacheEntry>();
    long position = 0;
    int internalFileId = dataInputStream.readInt();
    while (internalFileId >= 0) {
        final long pageIndex = dataInputStream.readLong();
        try {
            final long fileId = writeCache.externalFileId(internalFileId);
            final OCacheEntry entry = new OCacheEntry(fileId, pageIndex, null, false);
            filePositionMap.put(new PageKey(fileId, pageIndex), new OPair<Long, OCacheEntry>(position, entry));
            queuePositionMap.put(position, entry);
            position++;
        } finally {
            internalFileId = dataInputStream.readInt();
        }
    }
    // second step: load pages sorted by position in file
    for (final Map.Entry<PageKey, OPair<Long, OCacheEntry>> entry : filePositionMap.entrySet()) {
        final PageKey pageKey = entry.getKey();
        final OPair<Long, OCacheEntry> pair = entry.getValue();
        final OCachePointer[] pointers = writeCache.load(pageKey.fileId, pageKey.pageIndex, 1, false, cacheHit);
        if (pointers.length == 0) {
            queuePositionMap.remove(pair.key);
            continue;
        }
        final OCacheEntry cacheEntry = pair.value;
        cacheEntry.setCachePointer(pointers[0]);
    }
    // third step: add pages according to their order in LRU queue
    for (final OCacheEntry cacheEntry : queuePositionMap.values()) {
        final long fileId = cacheEntry.getFileId();
        Set<Long> pages = filePages.get(fileId);
        if (pages == null) {
            pages = new HashSet<Long>();
            Set<Long> op = filePages.putIfAbsent(fileId, pages);
            if (op != null) {
                pages = op;
            }
        }
        queue.putToMRU(cacheEntry);
        pages.add(cacheEntry.getPageIndex());
    }
}
Also used : OPair(com.orientechnologies.common.util.OPair) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 13 with OPair

use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.

the class OLogSegment method readRecord.

@SuppressFBWarnings(value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS")
public byte[] readRecord(OLogSequenceNumber lsn, ByteBuffer byteBuffer) throws IOException {
    final OPair<OLogSequenceNumber, byte[]> lastRecord = lastReadRecord.get();
    if (lastRecord != null && lastRecord.getKey().equals(lsn))
        return lastRecord.getValue();
    assert lsn.getSegment() == order;
    if (lsn.getPosition() >= filledUpTo)
        return null;
    if (!logCache.isEmpty())
        flush();
    long pageIndex = lsn.getPosition() / OWALPage.PAGE_SIZE;
    byte[] record = null;
    int pageOffset = (int) (lsn.getPosition() % OWALPage.PAGE_SIZE);
    long pageCount = (filledUpTo + OWALPage.PAGE_SIZE - 1) / OWALPage.PAGE_SIZE;
    while (pageIndex < pageCount) {
        fileLock.lock();
        try {
            final RandomAccessFile rndFile = getRndFile();
            final FileChannel channel = rndFile.getChannel();
            byteBuffer.position(0);
            channel.read(byteBuffer, pageIndex * OWALPage.PAGE_SIZE);
        } finally {
            fileLock.unlock();
        }
        if (!checkPageIntegrity(byteBuffer))
            throw new OWALPageBrokenException("WAL page with index " + pageIndex + " is broken");
        OWALPage page = new OWALPage(byteBuffer, false);
        byte[] content = page.getRecord(pageOffset);
        if (record == null)
            record = content;
        else {
            byte[] oldRecord = record;
            record = new byte[record.length + content.length];
            System.arraycopy(oldRecord, 0, record, 0, oldRecord.length);
            System.arraycopy(content, 0, record, oldRecord.length, record.length - oldRecord.length);
        }
        if (page.mergeWithNextPage(pageOffset)) {
            pageOffset = OWALPage.RECORDS_OFFSET;
            pageIndex++;
            if (pageIndex >= pageCount)
                throw new OWALPageBrokenException("WAL page with index " + pageIndex + " is broken");
        } else {
            if (page.getFreeSpace() >= OWALPage.MIN_RECORD_SIZE && pageIndex < pageCount - 1)
                throw new OWALPageBrokenException("WAL page with index " + pageIndex + " is broken");
            break;
        }
    }
    lastReadRecord = new WeakReference<OPair<OLogSequenceNumber, byte[]>>(new OPair<OLogSequenceNumber, byte[]>(lsn, record));
    return record;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) OPair(com.orientechnologies.common.util.OPair) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 14 with OPair

use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.

the class OAtomicOperationsManager method dumpActiveAtomicOperations.

@Override
public String dumpActiveAtomicOperations() {
    if (!trackAtomicOperations)
        activeAtomicOperations.clear();
    final StringWriter writer = new StringWriter();
    writer.append("List of active atomic operations: \r\n");
    writer.append("------------------------------------------------------------------------------------------------\r\n");
    for (Map.Entry<OOperationUnitId, OPair<String, Deque<OPair<String, StackTraceElement[]>>>> entry : activeAtomicOperations.entrySet()) {
        writer.append("Operation unit id :").append(entry.getKey().toString()).append("\r\n");
        writer.append("Started at thread : ").append(entry.getValue().getKey()).append("\r\n");
        writer.append("Stack trace of methods which participated in this operation : \r\n");
        for (OPair<String, StackTraceElement[]> pair : entry.getValue().getValue()) {
            writer.append("------------------------------------------------------------------------------------------------\r\n");
            writer.append("Lock name :").append(pair.getKey()).append("\r\n");
            StackTraceElement[] stackTraceElements = pair.getValue();
            for (int i = 1; i < stackTraceElements.length; i++) {
                writer.append("\tat ").append(stackTraceElements[i].toString()).append("\r\n");
            }
        }
        writer.append("------------------------------------------------------------------------------------------------\r\n");
        writer.append("\r\n\r\n\r\n\r\n\r\n\r\n");
    }
    writer.append("-------------------------------------------------------------------------------------------------\r\n");
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) OPair(com.orientechnologies.common.util.OPair) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OOperationUnitId(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OOperationUnitId)

Example 15 with OPair

use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.

the class ODistributedTransactionManager method executeAsyncTx.

protected void executeAsyncTx(final Set<String> nodes, final OTxTaskResult localResult, final Set<String> involvedClusters, final OAbstractReplicatedTask txTask, final long messageId, final String localNodeName, final OCallable<Void, ODistributedRequestId> afterSendCallback) {
    final OAsyncReplicationOk onAsyncReplicationOk = OExecutionThreadLocal.INSTANCE.get().onAsyncReplicationOk;
    final OAsyncReplicationError onAsyncReplicationError = storage.getAsyncReplicationError();
    // ASYNCHRONOUSLY REPLICATE IT TO ALL THE OTHER NODES
    storage.asynchronousExecution(new OAsynchDistributedOperation(storage.getName(), involvedClusters, nodes, txTask, messageId, localResult, afterSendCallback, new OCallable<Object, OPair<ODistributedRequestId, Object>>() {

        @Override
        public Object call(final OPair<ODistributedRequestId, Object> iArgument) {
            try {
                final Object value = iArgument.getValue();
                final ODistributedRequestId reqId = iArgument.getKey();
                if (value instanceof OTxTaskResult) {
                    // SEND 2-PHASE DISTRIBUTED COMMIT TX
                    sendTxCompleted(localNodeName, involvedClusters, nodes, reqId, true, txTask.getPartitionKey());
                    if (onAsyncReplicationOk != null)
                        onAsyncReplicationOk.onAsyncReplicationOk();
                    return null;
                } else if (value instanceof Exception) {
                    try {
                        storage.executeUndoOnLocalServer(reqId, txTask);
                        if (ODistributedServerLog.isDebugEnabled())
                            ODistributedServerLog.debug(this, localNodeName, null, ODistributedServerLog.DIRECTION.NONE, "Async distributed transaction failed: %s", value);
                        // SEND 2-PHASE DISTRIBUTED ROLLBACK TX
                        sendTxCompleted(localNodeName, involvedClusters, nodes, reqId, false, txTask.getPartitionKey());
                        if (value instanceof RuntimeException)
                            throw (RuntimeException) value;
                        else
                            throw OException.wrapException(new OTransactionException("Error on execution async distributed transaction"), (Exception) value);
                    } finally {
                        if (onAsyncReplicationError != null)
                            onAsyncReplicationError.onAsyncReplicationError((Throwable) value, 0);
                    }
                }
                // UNKNOWN RESPONSE TYPE
                if (ODistributedServerLog.isDebugEnabled())
                    ODistributedServerLog.debug(this, localNodeName, null, ODistributedServerLog.DIRECTION.NONE, "Async distributed transaction error, received unknown response type: %s", iArgument);
                throw new OTransactionException("Error on committing async distributed transaction, received unknown response type " + iArgument);
            } finally {
                try {
                    afterSendCallback.call(iArgument.getKey());
                } catch (Exception e) {
                    ODistributedServerLog.debug(this, localNodeName, null, ODistributedServerLog.DIRECTION.NONE, "Error on unlocking Async distributed transaction", e);
                }
            }
        }
    }));
}
Also used : OAsyncReplicationError(com.orientechnologies.orient.core.replication.OAsyncReplicationError) OCallable(com.orientechnologies.common.util.OCallable) OPair(com.orientechnologies.common.util.OPair) OException(com.orientechnologies.common.exception.OException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) IOException(java.io.IOException) OAsyncReplicationOk(com.orientechnologies.orient.core.replication.OAsyncReplicationOk)

Aggregations

OPair (com.orientechnologies.common.util.OPair)16 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OBonsaiCollectionPointer (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OBonsaiCollectionPointer)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 OOperationUnitId (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OOperationUnitId)2 StringWriter (java.io.StringWriter)2 Deque (java.util.Deque)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)1 OException (com.orientechnologies.common.exception.OException)1 OModifiableBoolean (com.orientechnologies.common.types.OModifiableBoolean)1 OCallable (com.orientechnologies.common.util.OCallable)1 OResettable (com.orientechnologies.common.util.OResettable)1 OBasicCommandContext (com.orientechnologies.orient.core.command.OBasicCommandContext)1 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1