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());
}
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();
}
}
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();
}
}
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));
}
}
Aggregations