Search in sources :

Example 31 with ByteArray

use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.

the class AbstractKeyValueStorageTest method removeAll.

@Test
public void removeAll() {
    byte[] key1 = key(1);
    byte[] val1 = keyValue(1, 1);
    final byte[] key2 = key(2);
    final byte[] val2_1 = keyValue(2, 21);
    final byte[] val2_2 = keyValue(2, 22);
    final byte[] key3 = key(3);
    final byte[] val3_1 = keyValue(3, 31);
    final byte[] key4 = key(4);
    assertEquals(0, storage.revision());
    assertEquals(0, storage.updateCounter());
    // Regular put.
    storage.put(key1, val1);
    // Rewrite.
    storage.put(key2, val2_1);
    storage.put(key2, val2_2);
    // Remove. Tombstone must not be removed again.
    storage.put(key3, val3_1);
    storage.remove(key3);
    assertEquals(5, storage.revision());
    assertEquals(5, storage.updateCounter());
    storage.removeAll(List.of(key1, key2, key3, key4));
    assertEquals(6, storage.revision());
    // Only two keys are updated.
    assertEquals(7, 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(6, e1.revision());
    assertEquals(6, e1.updateCounter());
    assertTrue(e1.tombstone());
    assertFalse(e1.empty());
    // Test rewritten value.
    Entry e2 = map.get(new ByteArray(key2));
    assertNotNull(e2);
    assertEquals(6, e2.revision());
    assertEquals(7, e2.updateCounter());
    assertTrue(e2.tombstone());
    assertFalse(e2.empty());
    // Test removed value.
    Entry e3 = map.get(new ByteArray(key3));
    assertNotNull(e3);
    assertEquals(5, e3.revision());
    assertEquals(5, e3.updateCounter());
    assertTrue(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)

Example 32 with ByteArray

use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.

the class AbstractKeyValueStorageTest method getAllWithRevisionBound.

@Test
void getAllWithRevisionBound() {
    byte[] key1 = key(1);
    byte[] val1 = keyValue(1, 1);
    final byte[] key2 = key(2);
    final byte[] val2_1 = keyValue(2, 21);
    final byte[] val2_2 = keyValue(2, 22);
    final byte[] key3 = key(3);
    final byte[] val3 = keyValue(3, 3);
    final byte[] key4 = key(4);
    assertEquals(0, storage.revision());
    assertEquals(0, storage.updateCounter());
    // Regular put.
    storage.put(key1, val1);
    // Rewrite.
    storage.put(key2, val2_1);
    storage.put(key2, val2_2);
    // Remove.
    storage.put(key3, val3);
    storage.remove(key3);
    assertEquals(5, storage.revision());
    assertEquals(5, storage.updateCounter());
    // Bounded by revision 2.
    Collection<Entry> entries = storage.getAll(List.of(key1, key2, key3, key4), 2);
    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(1, e1.revision());
    assertEquals(1, e1.updateCounter());
    assertFalse(e1.tombstone());
    assertFalse(e1.empty());
    assertArrayEquals(val1, e1.value());
    // Test while not rewritten value.
    Entry e2 = map.get(new ByteArray(key2));
    assertNotNull(e2);
    assertEquals(2, e2.revision());
    assertEquals(2, e2.updateCounter());
    assertFalse(e2.tombstone());
    assertFalse(e2.empty());
    assertArrayEquals(val2_1, e2.value());
    // Values with larger revision don't exist yet.
    Entry e3 = map.get(new ByteArray(key3));
    assertNotNull(e3);
    assertTrue(e3.empty());
    Entry e4 = map.get(new ByteArray(key4));
    assertNotNull(e4);
    assertTrue(e4.empty());
    // Bounded by revision 4.
    entries = storage.getAll(List.of(key1, key2, key3, key4), 4);
    assertEquals(4, entries.size());
    map = entries.stream().collect(Collectors.toMap(e -> new ByteArray(e.key()), identity()));
    // Test regular put value.
    e1 = map.get(new ByteArray(key1));
    assertNotNull(e1);
    assertEquals(1, e1.revision());
    assertEquals(1, e1.updateCounter());
    assertFalse(e1.tombstone());
    assertFalse(e1.empty());
    assertArrayEquals(val1, e1.value());
    // Test rewritten value.
    e2 = map.get(new ByteArray(key2));
    assertNotNull(e2);
    assertEquals(3, e2.revision());
    assertEquals(3, e2.updateCounter());
    assertFalse(e2.tombstone());
    assertFalse(e2.empty());
    assertArrayEquals(val2_2, e2.value());
    // Test not removed value.
    e3 = map.get(new ByteArray(key3));
    assertNotNull(e3);
    assertEquals(4, e3.revision());
    assertEquals(4, e3.updateCounter());
    assertFalse(e3.tombstone());
    assertFalse(e3.empty());
    assertArrayEquals(val3, e3.value());
    // Value with larger revision doesn't exist yet.
    e4 = map.get(new ByteArray(key4));
    assertNotNull(e4);
    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)

Example 33 with ByteArray

use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.

the class AbstractKeyValueStorageTest method getAndPutAll.

@Test
public void getAndPutAll() {
    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());
    Collection<Entry> entries = storage.getAndPutAll(List.of(key1, key2, key3), List.of(val1, val2_2, val3_2));
    assertEquals(4, storage.revision());
    assertEquals(6, storage.updateCounter());
    assertEquals(3, 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(0, e1.revision());
    assertEquals(0, e1.updateCounter());
    assertFalse(e1.tombstone());
    assertTrue(e1.empty());
    // Test rewritten value.
    Entry e2 = map.get(new ByteArray(key2));
    assertNotNull(e2);
    assertEquals(1, e2.revision());
    assertEquals(1, e2.updateCounter());
    assertFalse(e2.tombstone());
    assertFalse(e2.empty());
    assertArrayEquals(val21, e2.value());
    // Test removed value.
    Entry e3 = map.get(new ByteArray(key3));
    assertNotNull(e3);
    assertEquals(3, e3.revision());
    assertEquals(3, e3.updateCounter());
    assertTrue(e3.tombstone());
    assertFalse(e3.empty());
    // Test state after putAll.
    entries = storage.getAll(List.of(key1, key2, key3, key4));
    assertEquals(4, entries.size());
    map = entries.stream().collect(Collectors.toMap(e -> new ByteArray(e.key()), identity()));
    // Test regular put value.
    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.
    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.
    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)

Example 34 with ByteArray

use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.

the class AbstractKeyValueStorageTest method getAndRemoveAll.

@Test
public void getAndRemoveAll() {
    byte[] key1 = key(1);
    byte[] val1 = keyValue(1, 1);
    final byte[] key2 = key(2);
    final byte[] val2_1 = keyValue(2, 21);
    final byte[] val2_2 = keyValue(2, 22);
    final byte[] key3 = key(3);
    final byte[] val3_1 = keyValue(3, 31);
    final byte[] key4 = key(4);
    assertEquals(0, storage.revision());
    assertEquals(0, storage.updateCounter());
    // Regular put.
    storage.put(key1, val1);
    // Rewrite.
    storage.put(key2, val2_1);
    storage.put(key2, val2_2);
    // Remove. Tombstone must not be removed again.
    storage.put(key3, val3_1);
    storage.remove(key3);
    assertEquals(5, storage.revision());
    assertEquals(5, storage.updateCounter());
    Collection<Entry> entries = storage.getAndRemoveAll(List.of(key1, key2, key3, key4));
    assertEquals(6, storage.revision());
    // Only two keys are updated.
    assertEquals(7, storage.updateCounter());
    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(1, e1.revision());
    assertEquals(1, e1.updateCounter());
    assertFalse(e1.tombstone());
    assertFalse(e1.empty());
    // Test rewritten value.
    Entry e2 = map.get(new ByteArray(key2));
    assertNotNull(e2);
    assertEquals(3, e2.revision());
    assertEquals(3, e2.updateCounter());
    assertFalse(e2.tombstone());
    assertFalse(e2.empty());
    // Test removed value.
    Entry e3 = map.get(new ByteArray(key3));
    assertNotNull(e3);
    assertEquals(5, e3.revision());
    assertEquals(5, e3.updateCounter());
    assertTrue(e3.tombstone());
    assertFalse(e3.empty());
    // Test empty value.
    Entry e4 = map.get(new ByteArray(key4));
    assertNotNull(e4);
    assertFalse(e4.tombstone());
    assertTrue(e4.empty());
    // Test state after getAndRemoveAll.
    entries = storage.getAll(List.of(key1, key2, key3, key4));
    assertEquals(4, entries.size());
    map = entries.stream().collect(Collectors.toMap(e -> new ByteArray(e.key()), identity()));
    // Test regular put value.
    e1 = map.get(new ByteArray(key1));
    assertNotNull(e1);
    assertEquals(6, e1.revision());
    assertEquals(6, e1.updateCounter());
    assertTrue(e1.tombstone());
    assertFalse(e1.empty());
    // Test rewritten value.
    e2 = map.get(new ByteArray(key2));
    assertNotNull(e2);
    assertEquals(6, e2.revision());
    assertEquals(7, e2.updateCounter());
    assertTrue(e2.tombstone());
    assertFalse(e2.empty());
    // Test removed value.
    e3 = map.get(new ByteArray(key3));
    assertNotNull(e3);
    assertEquals(5, e3.revision());
    assertEquals(5, e3.updateCounter());
    assertTrue(e3.tombstone());
    assertFalse(e3.empty());
    // Test empty value.
    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)

Example 35 with ByteArray

use of org.apache.ignite.lang.ByteArray in project ignite-3 by apache.

the class ItMetaStorageServiceTest method testGetAndRemove.

/**
 * Tests {@link MetaStorageService#getAndRemove(ByteArray)}.
 *
 * @throws Exception If failed.
 */
@Test
public void testGetAndRemove() throws Exception {
    EntryImpl expRes = new EntryImpl(new ByteArray(new byte[] { 1 }), new byte[] { 3 }, 10, 2);
    when(mockStorage.getAndRemove(expRes.key().bytes())).thenReturn(new org.apache.ignite.internal.metastorage.server.Entry(expRes.key().bytes(), expRes.value(), expRes.revision(), expRes.updateCounter()));
    assertEquals(expRes, metaStorageSvc.getAndRemove(expRes.key()).get());
}
Also used : 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