Search in sources :

Example 6 with ByteArray

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

the class ItMetaStorageServiceTest method testInvoke.

@Test
public void testInvoke() throws Exception {
    ByteArray expKey = new ByteArray(new byte[] { 1 });
    byte[] expVal = { 2 };
    when(mockStorage.invoke(any(), any(), any())).thenReturn(true);
    Condition condition = Conditions.notExists(expKey);
    Operation success = Operations.put(expKey, expVal);
    Operation failure = Operations.noop();
    assertTrue(metaStorageSvc.invoke(condition, success, failure).get());
    var conditionCaptor = ArgumentCaptor.forClass(AbstractSimpleCondition.class);
    ArgumentCaptor<Collection<org.apache.ignite.internal.metastorage.server.Operation>> successCaptor = ArgumentCaptor.forClass(Collection.class);
    ArgumentCaptor<Collection<org.apache.ignite.internal.metastorage.server.Operation>> failureCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(mockStorage).invoke(conditionCaptor.capture(), successCaptor.capture(), failureCaptor.capture());
    assertArrayEquals(expKey.bytes(), conditionCaptor.getValue().key());
    assertArrayEquals(expKey.bytes(), successCaptor.getValue().iterator().next().key());
    assertArrayEquals(expVal, successCaptor.getValue().iterator().next().value());
    assertEquals(OperationType.NO_OP, failureCaptor.getValue().iterator().next().type());
}
Also used : ValueCondition(org.apache.ignite.internal.metastorage.server.ValueCondition) RevisionCondition(org.apache.ignite.internal.metastorage.server.RevisionCondition) AbstractSimpleCondition(org.apache.ignite.internal.metastorage.server.AbstractSimpleCondition) AndCondition(org.apache.ignite.internal.metastorage.server.AndCondition) OrCondition(org.apache.ignite.internal.metastorage.server.OrCondition) AbstractCompoundCondition(org.apache.ignite.internal.metastorage.server.AbstractCompoundCondition) ByteArray(org.apache.ignite.lang.ByteArray) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 7 with ByteArray

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

the class AbstractKeyValueStorageTest method watchCursorForRange.

@Test
public void watchCursorForRange() throws Exception {
    byte[] key1 = key(1);
    final byte[] val1_1 = keyValue(1, 11);
    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);
    assertEquals(0, storage.revision());
    assertEquals(0, storage.updateCounter());
    // Watch for all updates starting from revision 2.
    Cursor<WatchEvent> cur = storage.watch(key1, null, 2);
    Iterator<WatchEvent> it = cur.iterator();
    assertFalse(it.hasNext());
    assertThrows(NoSuchElementException.class, it::next);
    storage.putAll(List.of(key1, key2), List.of(val1_1, val2_1));
    assertEquals(1, storage.revision());
    assertEquals(2, storage.updateCounter());
    // Revision is less than 2.
    assertFalse(it.hasNext());
    assertThrows(NoSuchElementException.class, it::next);
    storage.putAll(List.of(key2, key3), List.of(val2_2, val3_1));
    assertEquals(2, storage.revision());
    assertEquals(4, storage.updateCounter());
    // Revision is 2.
    assertTrue(it.hasNext());
    WatchEvent watchEvent = it.next();
    assertFalse(watchEvent.single());
    Map<ByteArray, EntryEvent> map = watchEvent.entryEvents().stream().collect(Collectors.toMap(evt -> new ByteArray(evt.entry().key()), identity()));
    assertEquals(2, map.size());
    // First update under revision.
    EntryEvent e2 = map.get(new ByteArray(key2));
    assertNotNull(e2);
    Entry oldEntry2 = e2.oldEntry();
    assertFalse(oldEntry2.empty());
    assertFalse(oldEntry2.tombstone());
    assertEquals(1, oldEntry2.revision());
    assertEquals(2, oldEntry2.updateCounter());
    assertArrayEquals(key2, oldEntry2.key());
    assertArrayEquals(val2_1, oldEntry2.value());
    Entry newEntry2 = e2.entry();
    assertFalse(newEntry2.empty());
    assertFalse(newEntry2.tombstone());
    assertEquals(2, newEntry2.revision());
    assertEquals(3, newEntry2.updateCounter());
    assertArrayEquals(key2, newEntry2.key());
    assertArrayEquals(val2_2, newEntry2.value());
    // Second update under revision.
    EntryEvent e3 = map.get(new ByteArray(key3));
    assertNotNull(e3);
    Entry oldEntry3 = e3.oldEntry();
    assertTrue(oldEntry3.empty());
    assertFalse(oldEntry3.tombstone());
    assertArrayEquals(key3, oldEntry3.key());
    Entry newEntry3 = e3.entry();
    assertFalse(newEntry3.empty());
    assertFalse(newEntry3.tombstone());
    assertEquals(2, newEntry3.revision());
    assertEquals(4, newEntry3.updateCounter());
    assertArrayEquals(key3, newEntry3.key());
    assertArrayEquals(val3_1, newEntry3.value());
    assertFalse(it.hasNext());
    storage.remove(key1);
    assertTrue(it.hasNext());
    watchEvent = it.next();
    assertTrue(watchEvent.single());
    EntryEvent e1 = watchEvent.entryEvent();
    Entry oldEntry1 = e1.oldEntry();
    assertFalse(oldEntry1.empty());
    assertFalse(oldEntry1.tombstone());
    assertEquals(1, oldEntry1.revision());
    assertEquals(1, oldEntry1.updateCounter());
    assertArrayEquals(key1, oldEntry1.key());
    assertArrayEquals(val1_1, oldEntry1.value());
    Entry newEntry1 = e1.entry();
    assertFalse(newEntry1.empty());
    assertTrue(newEntry1.tombstone());
    assertEquals(3, newEntry1.revision());
    assertEquals(5, newEntry1.updateCounter());
    assertArrayEquals(key1, newEntry1.key());
    assertNull(newEntry1.value());
    assertFalse(it.hasNext());
    cur.close();
}
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 8 with ByteArray

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

the class AbstractKeyValueStorageTest method getAll.

@Test
void getAll() {
    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());
    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(1, e1.revision());
    assertEquals(1, 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(3, e2.revision());
    assertEquals(3, 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(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 9 with ByteArray

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

the class MetaStorageServiceImpl method watchResponse.

private static WatchEvent watchResponse(Object obj) {
    MultipleEntryResponse resp = (MultipleEntryResponse) obj;
    List<EntryEvent> evts = new ArrayList<>(resp.entries().size() / 2);
    Entry o = null;
    Entry n;
    for (int i = 0; i < resp.entries().size(); i++) {
        SingleEntryResponse s = resp.entries().get(i);
        EntryImpl e = new EntryImpl(new ByteArray(s.key()), s.value(), s.revision(), s.updateCounter());
        if (i % 2 == 0) {
            o = e;
        } else {
            n = e;
            evts.add(new EntryEvent(o, n));
        }
    }
    return new WatchEvent(evts);
}
Also used : ArrayList(java.util.ArrayList) ByteArray(org.apache.ignite.lang.ByteArray) MultipleEntryResponse(org.apache.ignite.internal.metastorage.common.command.MultipleEntryResponse) SingleEntryResponse(org.apache.ignite.internal.metastorage.common.command.SingleEntryResponse)

Example 10 with ByteArray

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

the class ConcurrentHashMapPartitionStorage method restoreSnapshot.

/**
 * {@inheritDoc}
 */
@Override
public void restoreSnapshot(Path snapshotPath) {
    try (InputStream in = Files.newInputStream(snapshotPath.resolve(SNAPSHOT_FILE));
        ObjectInputStream objIn = new ObjectInputStream(in)) {
        var keys = (List<byte[]>) objIn.readObject();
        var values = (List<byte[]>) objIn.readObject();
        map.clear();
        for (int i = 0; i < keys.size(); i++) {
            map.put(new ByteArray(keys.get(i)), values.get(i));
        }
    } catch (Exception e) {
        throw new IgniteInternalException(e);
    }
}
Also used : IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) ByteArray(org.apache.ignite.lang.ByteArray) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) StorageException(org.apache.ignite.internal.storage.StorageException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) ObjectInputStream(java.io.ObjectInputStream)

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