Search in sources :

Example 1 with Value

use of org.apache.ignite.internal.metastorage.server.Value 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)

Example 2 with Value

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

the class WatchCursor method next.

/**
 * {@inheritDoc}
 */
@Override
public WatchEvent next() {
    storage.lock().readLock().lock();
    try {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        List<EntryEvent> evts = new ArrayList<>();
        long lastSeenRevision = currentRevision;
        // Iterate over the keys of the current revision and get all matching entries.
        for (; nativeIterator.isValid(); nativeIterator.next()) {
            byte[] rocksKey = nativeIterator.key();
            byte[] rocksValue = nativeIterator.value();
            long revision = revisionFromRocksKey(rocksKey);
            lastSeenRevision = revision;
            if (revision > currentRevision) {
                // There are no more keys for the current revision
                break;
            }
            byte[] key = rocksKeyToBytes(rocksKey);
            if (predicate.test(key)) {
                Value val = bytesToValue(rocksValue);
                Entry newEntry;
                if (val.tombstone()) {
                    newEntry = Entry.tombstone(key, revision, val.updateCounter());
                } else {
                    newEntry = new Entry(key, val.bytes(), revision, val.updateCounter());
                }
                Entry oldEntry = storage.doGet(key, revision - 1, false);
                evts.add(new EntryEvent(oldEntry, newEntry));
            }
        }
        currentHasNext = false;
        // Go to the next revision
        currentRevision = lastSeenRevision > currentRevision ? lastSeenRevision : currentRevision + 1;
        checkIterator(nativeIterator);
        return new WatchEvent(evts);
    } finally {
        storage.lock().readLock().unlock();
    }
}
Also used : Entry(org.apache.ignite.internal.metastorage.server.Entry) EntryEvent(org.apache.ignite.internal.metastorage.server.EntryEvent) ArrayList(java.util.ArrayList) Value(org.apache.ignite.internal.metastorage.server.Value) RocksStorageUtils.bytesToValue(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.bytesToValue) WatchEvent(org.apache.ignite.internal.metastorage.server.WatchEvent) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with Value

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

the class WatchCursor method hasNext.

/**
 * {@inheritDoc}
 */
@Override
public boolean hasNext() {
    storage.lock().readLock().lock();
    try {
        if (currentHasNext) {
            return true;
        }
        if (!nativeIterator.isValid()) {
            nativeIterator.refresh();
            nativeIterator.seek(longToBytes(currentRevision));
        }
        // Check all keys to see if any one of them match the predicate.
        currentHasNext = RocksUtils.find(nativeIterator, (rocksKey, value) -> {
            byte[] key = rocksKeyToBytes(rocksKey);
            if (predicate.test(key)) {
                // We may have jumped to the next revision if there were no matching keys in previous.
                currentRevision = revisionFromRocksKey(rocksKey);
                return true;
            }
            return false;
        });
        return currentHasNext;
    } catch (RocksDBException e) {
        throw new IgniteInternalException(e);
    } finally {
        storage.lock().readLock().unlock();
    }
}
Also used : Value(org.apache.ignite.internal.metastorage.server.Value) ReadOptions(org.rocksdb.ReadOptions) RocksUtils.checkIterator(org.apache.ignite.internal.rocksdb.RocksUtils.checkIterator) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) EntryEvent(org.apache.ignite.internal.metastorage.server.EntryEvent) RocksStorageUtils.rocksKeyToBytes(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.rocksKeyToBytes) Cursor(org.apache.ignite.internal.util.Cursor) RocksStorageUtils.revisionFromRocksKey(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.revisionFromRocksKey) RocksIterator(org.rocksdb.RocksIterator) ArrayList(java.util.ArrayList) List(java.util.List) RocksStorageUtils.longToBytes(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.longToBytes) WatchEvent(org.apache.ignite.internal.metastorage.server.WatchEvent) Entry(org.apache.ignite.internal.metastorage.server.Entry) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) RocksDBException(org.rocksdb.RocksDBException) NoSuchElementException(java.util.NoSuchElementException) RocksUtils(org.apache.ignite.internal.rocksdb.RocksUtils) RocksStorageUtils.bytesToValue(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.bytesToValue) RocksDBException(org.rocksdb.RocksDBException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException)

Example 4 with Value

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

the class RocksDbKeyValueStorage method compactForKey.

/**
 * Compacts all entries by the given key, removing all previous revisions and deleting the last entry if it is a tombstone.
 *
 * @param batch Write batch.
 * @param key   Target key.
 * @param revs  Revisions.
 * @throws RocksDBException If failed.
 */
private void compactForKey(WriteBatch batch, byte[] key, long[] revs) throws RocksDBException {
    long lastRev = lastRevision(revs);
    for (int i = 0; i < revs.length - 1; i++) {
        data.delete(batch, keyToRocksKey(revs[i], key));
    }
    byte[] rocksKey = keyToRocksKey(lastRev, key);
    Value value = bytesToValue(data.get(rocksKey));
    if (value.tombstone()) {
        index.delete(batch, rocksKey);
        index.delete(batch, key);
    } else {
        index.put(batch, key, longToBytes(lastRev));
    }
}
Also used : Value(org.apache.ignite.internal.metastorage.server.Value) RocksStorageUtils.bytesToValue(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.bytesToValue)

Aggregations

Value (org.apache.ignite.internal.metastorage.server.Value)4 RocksStorageUtils.bytesToValue (org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.bytesToValue)4 Entry (org.apache.ignite.internal.metastorage.server.Entry)3 ArrayList (java.util.ArrayList)2 NoSuchElementException (java.util.NoSuchElementException)2 EntryEvent (org.apache.ignite.internal.metastorage.server.EntryEvent)2 WatchEvent (org.apache.ignite.internal.metastorage.server.WatchEvent)2 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)2 RocksDBException (org.rocksdb.RocksDBException)2 Iterator (java.util.Iterator)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 RocksStorageUtils.longToBytes (org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.longToBytes)1 RocksStorageUtils.revisionFromRocksKey (org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.revisionFromRocksKey)1 RocksStorageUtils.rocksKeyToBytes (org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.rocksKeyToBytes)1 RocksUtils (org.apache.ignite.internal.rocksdb.RocksUtils)1 RocksUtils.checkIterator (org.apache.ignite.internal.rocksdb.RocksUtils.checkIterator)1 Cursor (org.apache.ignite.internal.util.Cursor)1 IgniteUtils (org.apache.ignite.internal.util.IgniteUtils)1 NotNull (org.jetbrains.annotations.NotNull)1