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