Search in sources :

Example 1 with HgkvDirBuilderImpl

use of com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl in project hugegraph-computer by hugegraph.

the class HgkvDirTest method testHgkvDirBuilder.

@Test
public void testHgkvDirBuilder() throws IOException {
    // The data must be ordered
    List<Integer> data = ImmutableList.of(2, 3, 2, 1, 5, 2, 5, 9, 6, 2);
    List<KvEntry> kvEntries = StoreTestUtil.kvEntriesFromMap(data);
    String path = StoreTestUtil.availablePathById("1");
    try (KvEntryFileWriter builder = new HgkvDirBuilderImpl(CONFIG, path)) {
        for (KvEntry entry : kvEntries) {
            builder.write(entry);
        }
        builder.finish();
        // Open the file and determine the footer is as expected
        HgkvDir dir = HgkvDirImpl.open(path);
        Assert.assertEquals(HgkvFileImpl.MAGIC, dir.magic());
        String version = HgkvDirImpl.MAJOR_VERSION + "." + HgkvDirImpl.MINOR_VERSION;
        Assert.assertEquals(version, dir.version());
        Assert.assertEquals(5, dir.numEntries());
        int maxKey = StoreTestUtil.byteArrayToInt(dir.max());
        Assert.assertEquals(6, maxKey);
        int minKey = StoreTestUtil.byteArrayToInt(dir.min());
        Assert.assertEquals(2, minKey);
    } finally {
        FileUtils.deleteQuietly(new File(path));
    }
}
Also used : KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) HgkvDir(com.baidu.hugegraph.computer.core.store.file.hgkvfile.HgkvDir) File(java.io.File) HgkvDirBuilderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl) Test(org.junit.Test)

Example 2 with HgkvDirBuilderImpl

use of com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl in project hugegraph-computer by hugegraph.

the class StoreTestUtil method hgkvDirFromKvMap.

public static void hgkvDirFromKvMap(Config config, List<Integer> map, String path) throws IOException {
    File file = new File(path);
    try (KvEntryFileWriter builder = new HgkvDirBuilderImpl(config, path)) {
        List<KvEntry> entries = StoreTestUtil.kvEntriesFromMap(map);
        for (KvEntry entry : entries) {
            builder.write(entry);
        }
        builder.finish();
    } catch (Exception e) {
        FileUtils.deleteQuietly(file);
        throw e;
    }
}
Also used : KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) File(java.io.File) HgkvDirBuilderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl) IOException(java.io.IOException)

Example 3 with HgkvDirBuilderImpl

use of com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl in project hugegraph-computer by hugegraph.

the class HgkvFileSorter method mergeInputs.

@Override
public void mergeInputs(List<String> inputs, OuterSortFlusher flusher, List<String> outputs, boolean withSubKv) throws Exception {
    Function<String, EntryIterator> fileToInput;
    Function<String, KvEntryFileWriter> fileToWriter;
    if (withSubKv) {
        fileToInput = o -> new HgkvDir4SubKvReaderImpl(o).iterator();
    } else {
        fileToInput = o -> new HgkvDirReaderImpl(o).iterator();
    }
    fileToWriter = path -> new HgkvDirBuilderImpl(this.config, path);
    InputFilesSelector selector = new DisperseEvenlySelector();
    List<SelectedFiles> selectResult = selector.selectedByHgkvFile(inputs, outputs);
    this.sorter.mergeFile(selectResult, fileToInput, fileToWriter, flusher);
}
Also used : DisperseEvenlySelector(com.baidu.hugegraph.computer.core.store.file.select.DisperseEvenlySelector) HgkvDir4SubKvReaderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.reader.HgkvDir4SubKvReaderImpl) HgkvDirReaderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.reader.HgkvDirReaderImpl) SelectedFiles(com.baidu.hugegraph.computer.core.store.file.select.SelectedFiles) EntryIterator(com.baidu.hugegraph.computer.core.store.EntryIterator) KvEntryFileWriter(com.baidu.hugegraph.computer.core.store.KvEntryFileWriter) HgkvDirBuilderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl) InputFilesSelector(com.baidu.hugegraph.computer.core.store.file.select.InputFilesSelector)

Example 4 with HgkvDirBuilderImpl

use of com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl in project hugegraph-computer by hugegraph.

the class StoreTestUtil method hgkvDirFromSubKvMap.

public static void hgkvDirFromSubKvMap(Config config, List<List<Integer>> map, String path) throws IOException {
    BytesInput input = SorterTestUtil.inputFromSubKvMap(map);
    KvEntriesInput iter = new KvEntriesInput(input, true);
    try (KvEntryFileWriter builder = new HgkvDirBuilderImpl(config, path)) {
        while (iter.hasNext()) {
            builder.write(iter.next());
        }
    }
    iter.close();
}
Also used : BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) KvEntriesInput(com.baidu.hugegraph.computer.core.store.buffer.KvEntriesInput) HgkvDirBuilderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl)

Example 5 with HgkvDirBuilderImpl

use of com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl in project hugegraph-computer by hugegraph.

the class SortLargeDataTest method testDiffNumEntriesFileMerge.

@Test
public void testDiffNumEntriesFileMerge() throws Exception {
    Config config = UnitTestBase.updateWithRequiredOptions(ComputerOptions.HGKV_MERGE_FILES_NUM, "3", ComputerOptions.TRANSPORT_RECV_FILE_MODE, "false");
    List<Integer> sizeList = ImmutableList.of(200, 500, 20, 50, 300, 250, 10, 33, 900, 89, 20);
    List<String> inputs = new ArrayList<>();
    for (int j = 0; j < sizeList.size(); j++) {
        String file = StoreTestUtil.availablePathById(j + 10);
        inputs.add(file);
        try (KvEntryFileWriter builder = new HgkvDirBuilderImpl(config, file)) {
            for (int i = 0; i < sizeList.get(j); i++) {
                byte[] keyBytes = StoreTestUtil.intToByteArray(i);
                byte[] valueBytes = StoreTestUtil.intToByteArray(1);
                Pointer key = new InlinePointer(keyBytes);
                Pointer value = new InlinePointer(valueBytes);
                KvEntry entry = new DefaultKvEntry(key, value);
                builder.write(entry);
            }
        }
    }
    List<String> outputs = ImmutableList.of(StoreTestUtil.availablePathById(0), StoreTestUtil.availablePathById(1), StoreTestUtil.availablePathById(2), StoreTestUtil.availablePathById(3));
    Sorter sorter = SorterTestUtil.createSorter(config);
    sorter.mergeInputs(inputs, new KvOuterSortFlusher(), outputs, false);
    int total = sizeList.stream().mapToInt(i -> i).sum();
    int mergeTotal = 0;
    for (String output : outputs) {
        mergeTotal += HgkvDirImpl.open(output).numEntries();
    }
    Assert.assertEquals(total, mergeTotal);
}
Also used : ComputerOptions(com.baidu.hugegraph.computer.core.config.ComputerOptions) BeforeClass(org.junit.BeforeClass) Random(java.util.Random) EntriesUtil(com.baidu.hugegraph.computer.core.store.entry.EntriesUtil) Pointer(com.baidu.hugegraph.computer.core.store.entry.Pointer) ArrayList(java.util.ArrayList) IntValueSumCombiner(com.baidu.hugegraph.computer.core.combiner.IntValueSumCombiner) IOFactory(com.baidu.hugegraph.computer.core.io.IOFactory) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) After(org.junit.After) StoreTestUtil(com.baidu.hugegraph.computer.core.store.StoreTestUtil) UnitTestBase(com.baidu.hugegraph.computer.suite.unit.UnitTestBase) Before(org.junit.Before) Logger(org.slf4j.Logger) OuterSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.OuterSortFlusher) Constants(com.baidu.hugegraph.computer.core.common.Constants) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) HgkvDir(com.baidu.hugegraph.computer.core.store.file.hgkvfile.HgkvDir) StopWatch(org.apache.commons.lang3.time.StopWatch) CombineKvInnerSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.CombineKvInnerSortFlusher) HgkvDirImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.HgkvDirImpl) DefaultKvEntry(com.baidu.hugegraph.computer.core.store.entry.DefaultKvEntry) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) File(java.io.File) Config(com.baidu.hugegraph.computer.core.config.Config) KvOuterSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.KvOuterSortFlusher) Bytes(com.baidu.hugegraph.util.Bytes) List(java.util.List) Log(com.baidu.hugegraph.util.Log) CombineKvOuterSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.CombineKvOuterSortFlusher) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) HgkvDirBuilderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl) PointerCombiner(com.baidu.hugegraph.computer.core.combiner.PointerCombiner) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) SorterTestUtil(com.baidu.hugegraph.computer.core.sort.SorterTestUtil) KvEntryFileWriter(com.baidu.hugegraph.computer.core.store.KvEntryFileWriter) Sorter(com.baidu.hugegraph.computer.core.sort.Sorter) Assert(com.baidu.hugegraph.testutil.Assert) PeekableIterator(com.baidu.hugegraph.computer.core.sort.flusher.PeekableIterator) RandomAccessInput(com.baidu.hugegraph.computer.core.io.RandomAccessInput) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) InnerSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.InnerSortFlusher) KvOuterSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.KvOuterSortFlusher) CombineKvOuterSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.CombineKvOuterSortFlusher) Config(com.baidu.hugegraph.computer.core.config.Config) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) ArrayList(java.util.ArrayList) DefaultKvEntry(com.baidu.hugegraph.computer.core.store.entry.DefaultKvEntry) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) Pointer(com.baidu.hugegraph.computer.core.store.entry.Pointer) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) DefaultKvEntry(com.baidu.hugegraph.computer.core.store.entry.DefaultKvEntry) Sorter(com.baidu.hugegraph.computer.core.sort.Sorter) KvEntryFileWriter(com.baidu.hugegraph.computer.core.store.KvEntryFileWriter) HgkvDirBuilderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl) Test(org.junit.Test)

Aggregations

HgkvDirBuilderImpl (com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl)5 KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)3 File (java.io.File)3 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)2 KvEntryFileWriter (com.baidu.hugegraph.computer.core.store.KvEntryFileWriter)2 HgkvDir (com.baidu.hugegraph.computer.core.store.file.hgkvfile.HgkvDir)2 IOException (java.io.IOException)2 IntValueSumCombiner (com.baidu.hugegraph.computer.core.combiner.IntValueSumCombiner)1 PointerCombiner (com.baidu.hugegraph.computer.core.combiner.PointerCombiner)1 Constants (com.baidu.hugegraph.computer.core.common.Constants)1 ComputerOptions (com.baidu.hugegraph.computer.core.config.ComputerOptions)1 Config (com.baidu.hugegraph.computer.core.config.Config)1 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)1 BytesOutput (com.baidu.hugegraph.computer.core.io.BytesOutput)1 IOFactory (com.baidu.hugegraph.computer.core.io.IOFactory)1 RandomAccessInput (com.baidu.hugegraph.computer.core.io.RandomAccessInput)1 Sorter (com.baidu.hugegraph.computer.core.sort.Sorter)1 SorterTestUtil (com.baidu.hugegraph.computer.core.sort.SorterTestUtil)1 CombineKvInnerSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.CombineKvInnerSortFlusher)1 CombineKvOuterSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.CombineKvOuterSortFlusher)1