Search in sources :

Example 26 with ByteArray

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;
}
Also used : Entry(org.apache.ignite.internal.metastorage.client.Entry) VaultEntry(org.apache.ignite.internal.vault.VaultEntry) HashMap(java.util.HashMap) ByteArray(org.apache.ignite.lang.ByteArray)

Example 27 with ByteArray

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);
}
Also used : HashMap(java.util.HashMap) VaultEntry(org.apache.ignite.internal.vault.VaultEntry) ByteArray(org.apache.ignite.lang.ByteArray)

Example 28 with ByteArray

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);
        }
    };
}
Also used : ReadOptions(org.rocksdb.ReadOptions) Slice(org.rocksdb.Slice) VaultEntry(org.apache.ignite.internal.vault.VaultEntry) ByteArray(org.apache.ignite.lang.ByteArray) RocksIterator(org.rocksdb.RocksIterator) RocksIteratorAdapter(org.apache.ignite.internal.rocksdb.RocksIteratorAdapter) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with ByteArray

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))));
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Example 30 with ByteArray

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());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Iterator(java.util.Iterator) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collection(java.util.Collection) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ByteArray(org.apache.ignite.lang.ByteArray) Cursor(org.apache.ignite.internal.util.Cursor) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Type(org.apache.ignite.internal.metastorage.server.ValueCondition.Type) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) OperationType(org.apache.ignite.internal.metastorage.common.OperationType) Function.identity(java.util.function.Function.identity) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArray (org.apache.ignite.lang.ByteArray)51 Test (org.junit.jupiter.api.Test)35 Cursor (org.apache.ignite.internal.util.Cursor)13 List (java.util.List)12 WatchAggregator (org.apache.ignite.internal.metastorage.watch.WatchAggregator)12 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)11 Map (java.util.Map)10 NotNull (org.jetbrains.annotations.NotNull)9 Collection (java.util.Collection)8 Iterator (java.util.Iterator)8 Collectors (java.util.stream.Collectors)8 AfterEach (org.junit.jupiter.api.AfterEach)8 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)8 Assertions.fail (org.junit.jupiter.api.Assertions.fail)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ByteBuffer (java.nio.ByteBuffer)7 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)7 ArrayList (java.util.ArrayList)7 NoSuchElementException (java.util.NoSuchElementException)7 Function.identity (java.util.function.Function.identity)7