Search in sources :

Example 1 with Entry

use of org.apache.ignite.internal.metastorage.server.Entry in project ignite-3 by apache.

the class RangeCursor method createIterator.

/**
 * Creates an iterator for this cursor.
 *
 * @return Iterator.
 */
@NotNull
private Iterator<Entry> createIterator() {
    return new Iterator<>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean hasNext() {
            storage.lock().readLock().lock();
            try {
                while (true) {
                    if (finished) {
                        return false;
                    }
                    if (nextRetEntry != null) {
                        return true;
                    }
                    byte[] key = lastRetKey;
                    while (nextRetEntry == null) {
                        Map.Entry<byte[], long[]> e = key == null ? storage.revisionCeilingEntry(keyFrom) : storage.revisionHigherEntry(key);
                        if (e == null) {
                            finished = true;
                            break;
                        }
                        key = e.getKey();
                        if (keyTo != null && RocksDbKeyValueStorage.CMP.compare(key, keyTo) >= 0) {
                            finished = true;
                            break;
                        }
                        long[] revs = e.getValue();
                        assert revs != null && revs.length != 0 : "Revisions should not be empty or null: [revs=" + Arrays.toString(revs) + ']';
                        long lastRev = RocksDbKeyValueStorage.maxRevision(revs, rev);
                        if (lastRev == -1) {
                            continue;
                        }
                        Entry entry = storage.doGetValue(key, lastRev);
                        assert !entry.empty() : "Iterator should not return empty entry.";
                        nextRetEntry = entry;
                    }
                }
            } finally {
                storage.lock().readLock().unlock();
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Entry next() {
            storage.lock().readLock().lock();
            try {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                Entry e = nextRetEntry;
                nextRetEntry = null;
                assert e != null;
                lastRetKey = e.key();
                return e;
            } finally {
                storage.lock().readLock().unlock();
            }
        }
    };
}
Also used : Entry(org.apache.ignite.internal.metastorage.server.Entry) Iterator(java.util.Iterator) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Entry

use of org.apache.ignite.internal.metastorage.server.Entry in project ignite-3 by apache.

the class RocksDbKeyValueStorage method addToBatchForRemoval.

/**
 * Adds a key to a batch marking the value as a tombstone.
 *
 * @param batch   Write batch.
 * @param key     Target key.
 * @param curRev  Revision.
 * @param counter Update counter.
 * @return {@code true} if an entry can be deleted.
 * @throws RocksDBException If failed.
 */
private boolean addToBatchForRemoval(WriteBatch batch, byte[] key, long curRev, long counter) throws RocksDBException {
    Entry e = doGet(key, LATEST_REV, false);
    if (e.empty() || e.tombstone()) {
        return false;
    }
    addDataToBatch(batch, key, TOMBSTONE, curRev, counter);
    return true;
}
Also used : Entry(org.apache.ignite.internal.metastorage.server.Entry)

Example 3 with Entry

use of org.apache.ignite.internal.metastorage.server.Entry in project ignite-3 by apache.

the class RocksDbKeyValueStorage method invoke.

/**
 * {@inheritDoc}
 */
@Override
public boolean invoke(Condition condition, Collection<Operation> success, Collection<Operation> failure) {
    rwLock.writeLock().lock();
    try {
        Entry[] entries = getAll(Arrays.asList(condition.keys())).toArray(new Entry[] {});
        boolean branch = condition.test(entries);
        Collection<Operation> ops = branch ? success : failure;
        applyOperations(ops);
        return branch;
    } catch (RocksDBException e) {
        throw new IgniteInternalException(e);
    } finally {
        rwLock.writeLock().unlock();
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Entry(org.apache.ignite.internal.metastorage.server.Entry) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Operation(org.apache.ignite.internal.metastorage.server.Operation)

Example 4 with Entry

use of org.apache.ignite.internal.metastorage.server.Entry in project ignite-3 by apache.

the class RocksDbKeyValueStorage method invoke.

@Override
public StatementResult invoke(If iif) {
    rwLock.writeLock().lock();
    try {
        If currIf = iif;
        byte maximumNumOfNestedBranch = 100;
        while (true) {
            if (maximumNumOfNestedBranch-- <= 0) {
                throw new IgniteInternalException("Too many nested (" + maximumNumOfNestedBranch + ") statements in multi-invoke command.");
            }
            Entry[] entries = getAll(Arrays.asList(currIf.cond().keys())).toArray(new Entry[] {});
            Statement branch = (currIf.cond().test(entries)) ? currIf.andThen() : currIf.orElse();
            if (branch.isTerminal()) {
                Update update = branch.update();
                applyOperations(update.operations());
                return update.result();
            } else {
                currIf = branch.iif();
            }
        }
    } catch (RocksDBException e) {
        throw new IgniteInternalException(e);
    } finally {
        rwLock.writeLock().unlock();
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Entry(org.apache.ignite.internal.metastorage.server.Entry) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Statement(org.apache.ignite.internal.metastorage.server.Statement) Update(org.apache.ignite.internal.metastorage.server.Update) If(org.apache.ignite.internal.metastorage.server.If)

Example 5 with Entry

use of org.apache.ignite.internal.metastorage.server.Entry in project ignite-3 by apache.

the class RocksDbKeyValueStorage method doGetValue.

/**
 * Gets the value by a key and a revision.
 *
 * @param key      Target key.
 * @param revision Target revision.
 * @return Entry.
 */
@NotNull
Entry doGetValue(byte[] key, long revision) {
    if (revision == 0) {
        return Entry.empty(key);
    }
    byte[] valueBytes;
    try {
        valueBytes = data.get(keyToRocksKey(revision, key));
    } catch (RocksDBException e) {
        throw new IgniteInternalException(e);
    }
    if (valueBytes == null || valueBytes.length == 0) {
        return Entry.empty(key);
    }
    Value lastVal = bytesToValue(valueBytes);
    if (lastVal.tombstone()) {
        return Entry.tombstone(key, revision, lastVal.updateCounter());
    }
    return new Entry(key, lastVal.bytes(), revision, lastVal.updateCounter());
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Entry(org.apache.ignite.internal.metastorage.server.Entry) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Value(org.apache.ignite.internal.metastorage.server.Value) RocksStorageUtils.bytesToValue(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.bytesToValue) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Entry (org.apache.ignite.internal.metastorage.server.Entry)10 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)6 RocksDBException (org.rocksdb.RocksDBException)5 ArrayList (java.util.ArrayList)4 NotNull (org.jetbrains.annotations.NotNull)4 NoSuchElementException (java.util.NoSuchElementException)3 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 List (java.util.List)2 MultipleEntryResponse (org.apache.ignite.internal.metastorage.common.command.MultipleEntryResponse)2 SingleEntryResponse (org.apache.ignite.internal.metastorage.common.command.SingleEntryResponse)2 EntryEvent (org.apache.ignite.internal.metastorage.server.EntryEvent)2 Value (org.apache.ignite.internal.metastorage.server.Value)2 WatchEvent (org.apache.ignite.internal.metastorage.server.WatchEvent)2 RocksStorageUtils.bytesToValue (org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.bytesToValue)2 WriteBatch (org.rocksdb.WriteBatch)2 Map (java.util.Map)1 StatementResultInfo (org.apache.ignite.internal.metastorage.common.StatementResultInfo)1 GetAllCommand (org.apache.ignite.internal.metastorage.common.command.GetAllCommand)1 GetAndPutAllCommand (org.apache.ignite.internal.metastorage.common.command.GetAndPutAllCommand)1