Search in sources :

Example 21 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class OIndexFullText method put.

/**
   * Indexes a value and save the index. Splits the value in single words and index each one. Save of the index is responsibility of
   * the caller.
   */
@Override
public OIndexFullText put(Object key, final OIdentifiable singleValue) {
    if (key == null)
        return this;
    key = getCollatingValue(key);
    final ODatabase database = getDatabase();
    final boolean txIsActive = database.getTransaction().isActive();
    if (!txIsActive)
        keyLockManager.acquireExclusiveLock(key);
    try {
        final Set<String> words = splitIntoWords(key.toString());
        // FOREACH WORD CREATE THE LINK TO THE CURRENT DOCUMENT
        for (final String word : words) {
            acquireSharedLock();
            try {
                Set<OIdentifiable> refs;
                while (true) {
                    try {
                        refs = (Set<OIdentifiable>) storage.getIndexValue(indexId, word);
                        break;
                    } catch (OInvalidIndexEngineIdException e) {
                        doReloadIndexEngine();
                    }
                }
                final boolean durable;
                if (metadata != null && Boolean.TRUE.equals(metadata.field("durableInNonTxMode")))
                    durable = true;
                else
                    durable = false;
                final Set<OIdentifiable> refsc = refs;
                // SAVE THE INDEX ENTRY
                while (true) {
                    try {
                        storage.updateIndexEntry(indexId, word, new Callable<Object>() {

                            @Override
                            public Object call() throws Exception {
                                Set<OIdentifiable> result = null;
                                if (refsc == null) {
                                    // WORD NOT EXISTS: CREATE THE KEYWORD CONTAINER THE FIRST TIME THE WORD IS FOUND
                                    if (ODefaultIndexFactory.SBTREEBONSAI_VALUE_CONTAINER.equals(valueContainerAlgorithm)) {
                                        result = new OIndexRIDContainer(getName(), durable);
                                    } else {
                                        throw new IllegalStateException("MBRBTreeContainer is not supported any more");
                                    }
                                } else {
                                    result = refsc;
                                }
                                // ADD THE CURRENT DOCUMENT AS REF FOR THAT WORD
                                result.add(singleValue);
                                return result;
                            }
                        });
                        break;
                    } catch (OInvalidIndexEngineIdException e) {
                        doReloadIndexEngine();
                    }
                }
            } finally {
                releaseSharedLock();
            }
        }
        return this;
    } finally {
        if (!txIsActive)
            keyLockManager.releaseExclusiveLock(key);
    }
}
Also used : OIndexRIDContainer(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OIndexRIDContainer) Set(java.util.Set) HashSet(java.util.HashSet) OInvalidIndexEngineIdException(com.orientechnologies.orient.core.exception.OInvalidIndexEngineIdException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OInvalidIndexEngineIdException(com.orientechnologies.orient.core.exception.OInvalidIndexEngineIdException)

Example 22 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class OIndexRemoteOneValue method iterator.

public Iterator<Entry<Object, OIdentifiable>> iterator() {
    final OCommandRequest cmd = formatCommand(QUERY_ENTRIES, name);
    final Collection<ODocument> result = getDatabase().command(cmd).execute();
    final Map<Object, OIdentifiable> map = new LinkedHashMap<Object, OIdentifiable>();
    for (final ODocument d : result) {
        d.setLazyLoad(false);
        map.put(d.field("key"), (OIdentifiable) d.field("rid"));
    }
    return map.entrySet().iterator();
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OCommandRequest(com.orientechnologies.orient.core.command.OCommandRequest) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 23 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class OIndexRemoteOneValue method inverseIterator.

public Iterator<Entry<Object, OIdentifiable>> inverseIterator() {
    final OCommandRequest cmd = formatCommand(QUERY_ENTRIES, name);
    final List<ODocument> result = getDatabase().command(cmd).execute();
    final Map<Object, OIdentifiable> map = new LinkedHashMap<Object, OIdentifiable>();
    for (ListIterator<ODocument> it = result.listIterator(); it.hasPrevious(); ) {
        ODocument d = it.previous();
        d.setLazyLoad(false);
        map.put(d.field("key"), (OIdentifiable) d.field("rid"));
    }
    return map.entrySet().iterator();
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OCommandRequest(com.orientechnologies.orient.core.command.OCommandRequest) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 24 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class OIndexRemoteOneValue method get.

public OIdentifiable get(final Object iKey) {
    final OCommandRequest cmd = formatCommand(QUERY_GET, name);
    final List<OIdentifiable> result = getDatabase().command(cmd).execute(iKey);
    if (result != null && !result.isEmpty())
        return ((OIdentifiable) ((ODocument) result.get(0).getRecord()).field("rid")).getIdentity();
    return null;
// return (OIdentifiable) ((OStorageProxy) getDatabase().getStorage()).indexGet(name, iKey, null);
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OCommandRequest(com.orientechnologies.orient.core.command.OCommandRequest) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 25 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class OIndexTxAwareMultiValue method iterateEntries.

@Override
public OIndexCursor iterateEntries(Collection<?> keys, boolean ascSortOrder) {
    final OTransactionIndexChanges indexChanges = database.getTransaction().getIndexChanges(delegate.getName());
    if (indexChanges == null)
        return super.iterateEntries(keys, ascSortOrder);
    final List<Object> sortedKeys = new ArrayList<Object>(keys.size());
    for (Object key : keys) sortedKeys.add(getCollatingValue(key));
    if (ascSortOrder)
        Collections.sort(sortedKeys, ODefaultComparator.INSTANCE);
    else
        Collections.sort(sortedKeys, Collections.reverseOrder(ODefaultComparator.INSTANCE));
    final OIndexCursor txCursor = new OIndexAbstractCursor() {

        private Iterator<Object> keysIterator = sortedKeys.iterator();

        private Iterator<OIdentifiable> valuesIterator = new OEmptyIterator<OIdentifiable>();

        private Object key;

        @Override
        public Map.Entry<Object, OIdentifiable> nextEntry() {
            if (valuesIterator.hasNext())
                return nextEntryInternal();
            if (keysIterator == null)
                return null;
            Set<OIdentifiable> result = null;
            while (result == null && keysIterator.hasNext()) {
                key = keysIterator.next();
                result = calculateTxValue(key, indexChanges);
                if (result != null && result.isEmpty())
                    result = null;
            }
            if (result == null) {
                keysIterator = null;
                return null;
            }
            valuesIterator = result.iterator();
            return nextEntryInternal();
        }

        private Map.Entry<Object, OIdentifiable> nextEntryInternal() {
            final OIdentifiable identifiable = valuesIterator.next();
            return new Map.Entry<Object, OIdentifiable>() {

                @Override
                public Object getKey() {
                    return key;
                }

                @Override
                public OIdentifiable getValue() {
                    return identifiable;
                }

                @Override
                public OIdentifiable setValue(OIdentifiable value) {
                    throw new UnsupportedOperationException("setValue");
                }
            };
        }
    };
    if (indexChanges.cleared)
        return txCursor;
    final OIndexCursor backedCursor = super.iterateEntries(keys, ascSortOrder);
    return new OIndexTxCursor(txCursor, backedCursor, ascSortOrder, indexChanges);
}
Also used : OTransactionIndexChanges(com.orientechnologies.orient.core.tx.OTransactionIndexChanges) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OTransactionIndexEntry(com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry) OEmptyIterator(com.orientechnologies.orient.core.iterator.OEmptyIterator)

Aggregations

OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)536 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)278 ORecordId (com.orientechnologies.orient.core.id.ORecordId)120 Test (org.testng.annotations.Test)104 HashSet (java.util.HashSet)89 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)79 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)70 ORID (com.orientechnologies.orient.core.id.ORID)56 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)47 Test (org.junit.Test)43 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)42 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)41 ArrayList (java.util.ArrayList)39 ORecord (com.orientechnologies.orient.core.record.ORecord)35 Map (java.util.Map)31 ByteBuffer (java.nio.ByteBuffer)28 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)26 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)22 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)22 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)21