Search in sources :

Example 26 with BytesInput

use of com.baidu.hugegraph.computer.core.io.BytesInput 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 BytesInput

use of com.baidu.hugegraph.computer.core.io.BytesInput 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 28 with BytesInput

use of com.baidu.hugegraph.computer.core.io.BytesInput 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 29 with BytesInput

use of com.baidu.hugegraph.computer.core.io.BytesInput 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)

Example 30 with BytesInput

use of com.baidu.hugegraph.computer.core.io.BytesInput in project hugegraph-computer by hugegraph.

the class IdValueTest method testReadWriteUtf8IdValue.

@Test
public void testReadWriteUtf8IdValue() throws IOException {
    Value value1 = BytesId.of("long id");
    Value value2 = BytesId.of("short");
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        value1.write(bao);
        value2.write(bao);
        bytes = bao.toByteArray();
    }
    Value value3 = BytesId.of(Constants.EMPTY_STR);
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        value3.read(bai);
        Assert.assertEquals(value1, value3);
        value3.read(bai);
        Assert.assertEquals(value2, value3);
    }
}
Also used : BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) Test(org.junit.Test)

Aggregations

BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)32 BytesOutput (com.baidu.hugegraph.computer.core.io.BytesOutput)16 Test (org.junit.Test)16 KvEntriesInput (com.baidu.hugegraph.computer.core.store.buffer.KvEntriesInput)10 KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)8 Sorter (com.baidu.hugegraph.computer.core.sort.Sorter)7 Config (com.baidu.hugegraph.computer.core.config.Config)5 Id (com.baidu.hugegraph.computer.core.graph.id.Id)5 PointerCombiner (com.baidu.hugegraph.computer.core.combiner.PointerCombiner)4 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)4 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)4 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)4 CombineKvInnerSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.CombineKvInnerSortFlusher)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 IntValueSumCombiner (com.baidu.hugegraph.computer.core.combiner.IntValueSumCombiner)3 RandomAccessInput (com.baidu.hugegraph.computer.core.io.RandomAccessInput)3 EntryOutput (com.baidu.hugegraph.computer.core.store.entry.EntryOutput)3 EntryOutputImpl (com.baidu.hugegraph.computer.core.store.entry.EntryOutputImpl)3 Pointer (com.baidu.hugegraph.computer.core.store.entry.Pointer)3