Search in sources :

Example 26 with KvEntry

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();
}
Also used : BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) KvEntriesInput(com.baidu.hugegraph.computer.core.store.buffer.KvEntriesInput) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Test(org.junit.Test)

Example 27 with KvEntry

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);
    }
}
Also used : HgkvDirReaderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.reader.HgkvDirReaderImpl) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) Test(org.junit.Test)

Example 28 with KvEntry

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());
}
Also used : EntryOutput(com.baidu.hugegraph.computer.core.store.entry.EntryOutput) EntryOutputImpl(com.baidu.hugegraph.computer.core.store.entry.EntryOutputImpl) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) KvEntryWriter(com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) Test(org.junit.Test)

Example 29 with KvEntry

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);
    }
}
Also used : EntryOutput(com.baidu.hugegraph.computer.core.store.entry.EntryOutput) EntryOutputImpl(com.baidu.hugegraph.computer.core.store.entry.EntryOutputImpl) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) KvEntryWriter(com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) KvEntryWriter(com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter) SubKvEntriesInput(com.baidu.hugegraph.computer.core.store.buffer.SubKvEntriesInput) Test(org.junit.Test)

Example 30 with KvEntry

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);
}
Also used : BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) Pointer(com.baidu.hugegraph.computer.core.store.entry.Pointer) Test(org.junit.Test)

Aggregations

KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)38 Test (org.junit.Test)13 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)9 Id (com.baidu.hugegraph.computer.core.graph.id.Id)9 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)9 Pointer (com.baidu.hugegraph.computer.core.store.entry.Pointer)9 BytesOutput (com.baidu.hugegraph.computer.core.io.BytesOutput)6 RandomAccessInput (com.baidu.hugegraph.computer.core.io.RandomAccessInput)5 Sorter (com.baidu.hugegraph.computer.core.sort.Sorter)5 ArrayList (java.util.ArrayList)5 IntValueSumCombiner (com.baidu.hugegraph.computer.core.combiner.IntValueSumCombiner)4 PointerCombiner (com.baidu.hugegraph.computer.core.combiner.PointerCombiner)4 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)4 CombineKvOuterSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.CombineKvOuterSortFlusher)4 DefaultKvEntry (com.baidu.hugegraph.computer.core.store.entry.DefaultKvEntry)4 File (java.io.File)4 IOException (java.io.IOException)4 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)3 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)3 OuterSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.OuterSortFlusher)3