use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class EntryOutputTest method testSubKvNeedSort.
@Test
public void testSubKvNeedSort() throws Exception {
List<Integer> entries = ImmutableList.of(5, 6, 6, 2, 1, 4, 8, 1, 2, 2, 6, 1);
BytesInput input = inputFromEntries(entries, true);
EntryIterator iter = new KvEntriesInput(input, true);
// Assert entry1
KvEntry kvEntry1 = iter.next();
Id key1 = StoreTestUtil.idFromPointer(kvEntry1.key());
Assert.assertEquals(BytesId.of(5), key1);
EntryIterator kvEntry1SubKvs = EntriesUtil.subKvIterFromEntry(kvEntry1);
KvEntry subKv1 = kvEntry1SubKvs.next();
Assert.assertEquals(0, subKv1.numSubEntries());
SorterTestUtil.assertKvEntry(subKv1, BytesId.of(2), BytesId.of(1));
KvEntry subKv2 = kvEntry1SubKvs.next();
Assert.assertEquals(0, subKv2.numSubEntries());
SorterTestUtil.assertKvEntry(subKv2, BytesId.of(4), BytesId.of(8));
KvEntry subKv3 = kvEntry1SubKvs.next();
Assert.assertEquals(0, subKv3.numSubEntries());
SorterTestUtil.assertKvEntry(subKv3, BytesId.of(6), BytesId.of(6));
// Assert entry2
KvEntry kvEntry2 = iter.next();
Id key2 = StoreTestUtil.idFromPointer(kvEntry2.key());
Assert.assertEquals(BytesId.of(1), key2);
EntryIterator kvEntry2SubKvs = EntriesUtil.subKvIterFromEntry(kvEntry2);
SorterTestUtil.assertKvEntry(kvEntry2SubKvs.next(), BytesId.of(2), BytesId.of(2));
SorterTestUtil.assertKvEntry(kvEntry2SubKvs.next(), BytesId.of(6), BytesId.of(1));
iter.close();
}
use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class HgkvDirTest method testHgkvDirReader.
@Test
public void testHgkvDirReader() throws Exception {
// The keys in the data must be ordered
List<Integer> data = ImmutableList.of(2, 3, 2, 1, 5, 2, 5, 5, 5, 9, 6, 2);
String path = StoreTestUtil.availablePathById("1");
StoreTestUtil.hgkvDirFromKvMap(CONFIG, data, path);
KvEntryFileReader reader = new HgkvDirReaderImpl(path, false);
try (EntryIterator iterator = reader.iterator()) {
int i = 0;
while (iterator.hasNext()) {
KvEntry entry = iterator.next();
int key = StoreTestUtil.byteArrayToInt(entry.key().bytes());
Assert.assertEquals(data.get(i).intValue(), key);
i += 2;
}
Assert.assertThrows(NoSuchElementException.class, iterator::next);
}
}
use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class EntriesUtilTest method testKvEntryWithFirstSubKv.
@Test
public void testKvEntryWithFirstSubKv() throws IOException {
BytesOutput output = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
EntryOutput entryOutput = new EntryOutputImpl(output);
KvEntryWriter subKvWriter = entryOutput.writeEntry(BytesId.of(100));
subKvWriter.writeSubKv(BytesId.of(1), BytesId.of(1));
subKvWriter.writeSubKv(BytesId.of(1), BytesId.of(1));
subKvWriter.writeSubKv(BytesId.of(1), BytesId.of(1));
subKvWriter.writeSubKv(BytesId.of(1), BytesId.of(1));
subKvWriter.writeFinish();
BytesInput input = EntriesUtil.inputFromOutput(output);
// Read entry from buffer
KvEntry entry = EntriesUtil.kvEntryFromInput(input, true, true);
entry = EntriesUtil.kvEntryWithFirstSubKv(entry);
// Assert subKvEntry size
Assert.assertEquals(4, entry.numSubEntries());
}
use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class EntriesUtilTest method testSubKvEntriesInput.
@Test
public void testSubKvEntriesInput() throws Exception {
BytesOutput output = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
EntryOutput entryOutput = new EntryOutputImpl(output);
KvEntryWriter subKvWriter = entryOutput.writeEntry(BytesId.of(100));
subKvWriter.writeSubKv(BytesId.of(20), BytesId.of(1));
subKvWriter.writeSubKv(BytesId.of(10), BytesId.of(1));
subKvWriter.writeSubKv(BytesId.of(50), BytesId.of(1));
subKvWriter.writeSubKv(BytesId.of(40), BytesId.of(1));
subKvWriter.writeSubKv(BytesId.of(10), BytesId.of(1));
subKvWriter.writeFinish();
BytesInput input = EntriesUtil.inputFromOutput(output);
// Test inlinePointer kvEntry
KvEntry entry = EntriesUtil.kvEntryFromInput(input, true, true);
Assert.assertEquals(BytesId.of(100), StoreTestUtil.idFromPointer(entry.key()));
try (EntryIterator iter = new SubKvEntriesInput(entry, true)) {
Assert.assertEquals(BytesId.of(10), StoreTestUtil.idFromPointer(iter.next().key()));
Assert.assertEquals(BytesId.of(10), StoreTestUtil.idFromPointer(iter.next().key()));
Assert.assertEquals(BytesId.of(20), StoreTestUtil.idFromPointer(iter.next().key()));
Assert.assertEquals(BytesId.of(40), StoreTestUtil.idFromPointer(iter.next().key()));
Assert.assertEquals(BytesId.of(50), StoreTestUtil.idFromPointer(iter.next().key()));
}
input.seek(0);
// Test cachedPointer kvEntry
entry = EntriesUtil.kvEntryFromInput(input, false, true);
Assert.assertEquals(BytesId.of(100), StoreTestUtil.idFromPointer(entry.key()));
try (EntryIterator iter = new SubKvEntriesInput(entry, false)) {
Assert.assertEquals(BytesId.of(10), StoreTestUtil.idFromPointer(iter.next().key()));
Assert.assertEquals(BytesId.of(10), StoreTestUtil.idFromPointer(iter.next().key()));
Assert.assertEquals(BytesId.of(20), StoreTestUtil.idFromPointer(iter.next().key()));
Assert.assertEquals(BytesId.of(40), StoreTestUtil.idFromPointer(iter.next().key()));
Assert.assertEquals(BytesId.of(50), StoreTestUtil.idFromPointer(iter.next().key()));
Assert.assertThrows(NoSuchElementException.class, iter::next);
}
}
use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class PointerTest method test.
@Test
public void test() throws IOException {
byte[] data = new byte[] { 100, 0, 0, 0 };
byte[] expectedWriteResult = { 4, 0, 0, 0, 100, 0, 0, 0, 4, 0, 0, 0, 100, 0, 0, 0 };
BytesOutput output = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
output.writeFixedInt(data.length);
output.write(data);
output.writeFixedInt(data.length);
output.write(data);
BytesInput input = EntriesUtil.inputFromOutput(output);
KvEntry inlineKvEntry = EntriesUtil.kvEntryFromInput(input, true, false);
Pointer inlineKey = inlineKvEntry.key();
Pointer inlineValue = inlineKvEntry.value();
Assert.assertEquals(0L, inlineKey.offset());
Assert.assertEquals(4L, inlineKey.length());
Assert.assertEquals(0, BytesUtil.compare(data, inlineKey.bytes()));
Assert.assertEquals(0L, inlineValue.offset());
Assert.assertEquals(4L, inlineValue.length());
Assert.assertEquals(0, BytesUtil.compare(data, inlineValue.bytes()));
BytesOutput writeOutput = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
inlineKey.write(writeOutput);
inlineValue.write(writeOutput);
int result = BytesUtil.compare(expectedWriteResult, expectedWriteResult.length, writeOutput.buffer(), (int) writeOutput.position());
Assert.assertEquals(0, result);
input.seek(0);
KvEntry cachedKvEntry = EntriesUtil.kvEntryFromInput(input, false, false);
Pointer cachedKey = cachedKvEntry.key();
Pointer cachedValue = cachedKvEntry.value();
Assert.assertEquals(4L, cachedKey.offset());
Assert.assertEquals(4L, cachedKey.length());
Assert.assertEquals(0, BytesUtil.compare(data, cachedKey.bytes()));
Assert.assertEquals(12L, cachedValue.offset());
Assert.assertEquals(4L, cachedValue.length());
Assert.assertEquals(0, BytesUtil.compare(data, cachedValue.bytes()));
writeOutput = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
cachedKey.write(writeOutput);
cachedValue.write(writeOutput);
result = BytesUtil.compare(expectedWriteResult, expectedWriteResult.length, writeOutput.buffer(), (int) writeOutput.position());
Assert.assertEquals(0, result);
}
Aggregations