use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.
the class DistributedConfigurationStorage method readAllLatest.
/**
* {@inheritDoc}
*/
@Override
public Map<String, Serializable> readAllLatest(String prefix) {
var data = new HashMap<String, Serializable>();
var rangeStart = new ByteArray(DISTRIBUTED_PREFIX + prefix);
var rangeEnd = new ByteArray(incrementLastChar(DISTRIBUTED_PREFIX + prefix));
try (Cursor<Entry> entries = metaStorageMgr.range(rangeStart, rangeEnd)) {
for (Entry entry : entries) {
ByteArray key = entry.key();
byte[] value = entry.value();
if (entry.tombstone()) {
continue;
}
// Meta Storage should not return nulls as values
assert value != null;
if (key.equals(MASTER_KEY)) {
continue;
}
String dataKey = key.toString().substring(DISTRIBUTED_PREFIX.length());
data.put(dataKey, ConfigurationSerializationUtil.fromBytes(value));
}
} catch (Exception e) {
throw new StorageException("Exception when closing a Meta Storage cursor", e);
}
return data;
}
use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.
the class DistributedConfigurationStorage method readAll.
/**
* {@inheritDoc}
*/
@Override
public Data readAll() throws StorageException {
var data = new HashMap<String, Serializable>();
VaultEntry appliedRevEntry = vaultMgr.get(MetaStorageManager.APPLIED_REV).join();
long appliedRevision = appliedRevEntry.value() == null ? 0L : ByteUtils.bytesToLong(appliedRevEntry.value());
try (Cursor<VaultEntry> entries = storedDistributedConfigKeys()) {
for (VaultEntry entry : entries) {
ByteArray key = entry.key();
byte[] value = entry.value();
// vault iterator should not return nulls as values
assert value != null;
if (key.equals(MASTER_KEY)) {
continue;
}
String dataKey = key.toString().substring(DISTRIBUTED_PREFIX.length());
data.put(dataKey, ConfigurationSerializationUtil.fromBytes(value));
}
} catch (Exception e) {
throw new StorageException("Exception when closing a Vault cursor", e);
}
assert data.isEmpty() || appliedRevision > 0;
changeId.set(data.isEmpty() ? 0 : appliedRevision);
return new Data(data, appliedRevision);
}
use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.
the class PersistentVaultService method range.
/**
* {@inheritDoc}
*/
@Override
@NotNull
public Cursor<VaultEntry> range(@NotNull ByteArray fromKey, @NotNull ByteArray toKey) {
var readOpts = new ReadOptions();
var upperBound = new Slice(toKey.bytes());
readOpts.setIterateUpperBound(upperBound);
RocksIterator it = db.newIterator(readOpts);
it.seek(fromKey.bytes());
return new RocksIteratorAdapter<>(it) {
@Override
protected VaultEntry decodeEntry(byte[] key, byte[] value) {
return new VaultEntry(new ByteArray(key), value);
}
@Override
public void close() throws Exception {
super.close();
IgniteUtils.closeAll(upperBound, readOpts);
}
};
}
use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.
the class VaultServiceTest method testPut.
/**
* Tests regular behaviour of the {@link VaultService#put} method.
*/
@Test
public void testPut() throws Exception {
ByteArray key = getKey(1);
assertThat(vaultService.get(key), willBe(equalTo(new VaultEntry(key, null))));
byte[] val = getValue(1);
doAwait(() -> vaultService.put(key, val));
assertThat(vaultService.get(key), willBe(equalTo(new VaultEntry(key, val))));
// test idempotency
doAwait(() -> vaultService.put(key, val));
assertThat(vaultService.get(key), willBe(equalTo(new VaultEntry(key, val))));
}
use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.
the class AbstractKeyValueStorageTest method putAll.
@Test
public void putAll() {
final byte[] key1 = key(1);
final byte[] val1 = keyValue(1, 1);
byte[] key2 = key(2);
byte[] val21 = keyValue(2, 21);
final byte[] val2_2 = keyValue(2, 22);
final byte[] key3 = key(3);
final byte[] val3_1 = keyValue(3, 31);
final byte[] val3_2 = keyValue(3, 32);
final byte[] key4 = key(4);
assertEquals(0, storage.revision());
assertEquals(0, storage.updateCounter());
// Must be rewritten.
storage.put(key2, val21);
// Remove. Tombstone must be replaced by new value.
storage.put(key3, val3_1);
storage.remove(key3);
assertEquals(3, storage.revision());
assertEquals(3, storage.updateCounter());
storage.putAll(List.of(key1, key2, key3), List.of(val1, val2_2, val3_2));
assertEquals(4, storage.revision());
assertEquals(6, storage.updateCounter());
Collection<Entry> entries = storage.getAll(List.of(key1, key2, key3, key4));
assertEquals(4, entries.size());
Map<ByteArray, Entry> map = entries.stream().collect(Collectors.toMap(e -> new ByteArray(e.key()), identity()));
// Test regular put value.
Entry e1 = map.get(new ByteArray(key1));
assertNotNull(e1);
assertEquals(4, e1.revision());
assertEquals(4, e1.updateCounter());
assertFalse(e1.tombstone());
assertFalse(e1.empty());
assertArrayEquals(val1, e1.value());
// Test rewritten value.
Entry e2 = map.get(new ByteArray(key2));
assertNotNull(e2);
assertEquals(4, e2.revision());
assertEquals(5, e2.updateCounter());
assertFalse(e2.tombstone());
assertFalse(e2.empty());
assertArrayEquals(val2_2, e2.value());
// Test removed value.
Entry e3 = map.get(new ByteArray(key3));
assertNotNull(e3);
assertEquals(4, e3.revision());
assertEquals(6, e3.updateCounter());
assertFalse(e3.tombstone());
assertFalse(e3.empty());
// Test empty value.
Entry e4 = map.get(new ByteArray(key4));
assertNotNull(e4);
assertFalse(e4.tombstone());
assertTrue(e4.empty());
}
Aggregations