Search in sources :

Example 1 with BytesOutput

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

the class StoreTestUtil method kvEntriesFromMap.

public static List<KvEntry> kvEntriesFromMap(List<Integer> map) throws IOException {
    BytesOutput data = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
    Iterator<Integer> iterator = map.iterator();
    while (iterator.hasNext()) {
        // Write key length
        writeData(data, iterator.next());
        // Write value length
        writeData(data, iterator.next());
    }
    BytesInput input = IOFactory.createBytesInput(data.buffer(), (int) data.position());
    KvEntriesInput iter = new KvEntriesInput(input);
    List<KvEntry> entries = new ArrayList<>();
    while (iter.hasNext()) {
        entries.add(iter.next());
    }
    iter.close();
    return entries;
}
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) ArrayList(java.util.ArrayList) KvEntriesInput(com.baidu.hugegraph.computer.core.store.buffer.KvEntriesInput)

Example 2 with BytesOutput

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

the class SortManager method sort.

public CompletableFuture<ByteBuffer> sort(MessageType type, WriteBuffers buffer) {
    return CompletableFuture.supplyAsync(() -> {
        RandomAccessInput bufferForRead = buffer.wrapForRead();
        // TODOļ¼šThis ByteBuffer should be allocated from the off-heap
        BytesOutput output = IOFactory.createBytesOutput(this.capacity);
        InnerSortFlusher flusher = this.createSortFlusher(type, output, this.flushThreshold);
        try {
            this.sorter.sortBuffer(bufferForRead, flusher, type == MessageType.EDGE);
        } catch (Exception e) {
            throw new ComputerException("Failed to sort buffers of %s " + "message", e, type.name());
        }
        return ByteBuffer.wrap(output.buffer(), 0, (int) output.position());
    }, this.sortExecutor);
}
Also used : RandomAccessInput(com.baidu.hugegraph.computer.core.io.RandomAccessInput) CombineSubKvInnerSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.CombineSubKvInnerSortFlusher) KvInnerSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.KvInnerSortFlusher) CombineKvInnerSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.CombineKvInnerSortFlusher) InnerSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.InnerSortFlusher) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) IOException(java.io.IOException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 3 with BytesOutput

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

the class SortLargeDataTest method testMergeBuffers.

@Test
public void testMergeBuffers() throws Exception {
    StopWatch watcher = new StopWatch();
    // Sort buffers total size 100M, each buffer is 50KB
    final long bufferSize = Bytes.KB * 50;
    final long bufferNum = 2000;
    final int keyRange = 10000000;
    long totalValue = 0L;
    Random random = new Random();
    List<RandomAccessInput> buffers = new ArrayList<>();
    for (int i = 0; i < bufferNum; i++) {
        BytesOutput buffer = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
        while (buffer.position() < bufferSize) {
            // Write data
            int key = random.nextInt(keyRange);
            SorterTestUtil.writeData(buffer, key);
            int value = random.nextInt(100);
            SorterTestUtil.writeData(buffer, value);
            totalValue += value;
        }
        buffers.add(EntriesUtil.inputFromOutput(buffer));
    }
    // Sort buffer
    Sorter sorter = SorterTestUtil.createSorter(CONFIG);
    watcher.start();
    List<RandomAccessInput> sortedBuffers = new ArrayList<>();
    for (RandomAccessInput buffer : buffers) {
        RandomAccessInput sortedBuffer = sortBuffer(sorter, buffer);
        sortedBuffers.add(sortedBuffer);
    }
    watcher.stop();
    LOG.info("testMergeBuffers sort buffer cost time: {}", watcher.getTime());
    String resultFile = StoreTestUtil.availablePathById("0");
    // Sort buffers
    watcher.reset();
    watcher.start();
    sorter.mergeBuffers(sortedBuffers, new KvOuterSortFlusher(), resultFile, false);
    watcher.stop();
    LOG.info("testMergeBuffers merge buffers cost time: {}", watcher.getTime());
    // Assert result
    long result = sumOfEntryValue(sorter, ImmutableList.of(resultFile));
    Assert.assertEquals(totalValue, result);
    assertFileOrder(sorter, ImmutableList.of(resultFile));
}
Also used : RandomAccessInput(com.baidu.hugegraph.computer.core.io.RandomAccessInput) Random(java.util.Random) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) KvOuterSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.KvOuterSortFlusher) CombineKvOuterSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.CombineKvOuterSortFlusher) ArrayList(java.util.ArrayList) Sorter(com.baidu.hugegraph.computer.core.sort.Sorter) StopWatch(org.apache.commons.lang3.time.StopWatch) Test(org.junit.Test)

Example 4 with BytesOutput

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

the class SortLargeDataTest method testAllProcess.

@Test
public void testAllProcess() throws Exception {
    StopWatch watcher = new StopWatch();
    final long bufferSize = Bytes.MB;
    final int mergeBufferNum = 300;
    final int dataSize = 1000000;
    long value = 0;
    Random random = new Random();
    BytesOutput output = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
    List<RandomAccessInput> buffers = new ArrayList<>(mergeBufferNum);
    List<String> mergeBufferFiles = new ArrayList<>();
    int fileNum = 10;
    Sorter sorter = SorterTestUtil.createSorter(CONFIG);
    watcher.start();
    for (int i = 0; i < dataSize; i++) {
        SorterTestUtil.writeData(output, random.nextInt(dataSize));
        int entryValue = random.nextInt(5);
        SorterTestUtil.writeData(output, entryValue);
        value = value + entryValue;
        // Write data to buffer and sort buffer
        if (output.position() >= bufferSize || (i + 1) == dataSize) {
            BytesInput input = EntriesUtil.inputFromOutput(output);
            buffers.add(sortBuffer(sorter, input));
            output.seek(0);
        }
        // Merge buffers to HgkvDir
        if (buffers.size() >= mergeBufferNum || (i + 1) == dataSize) {
            String outputFile = StoreTestUtil.availablePathById(fileNum++);
            mergeBufferFiles.add(outputFile);
            mergeBuffers(sorter, buffers, outputFile);
            buffers.clear();
        }
    }
    // Merge file
    String resultFile = StoreTestUtil.availablePathById("0");
    mergeFiles(sorter, mergeBufferFiles, Lists.newArrayList(resultFile));
    watcher.stop();
    LOG.info("testAllProcess sort time: {}", watcher.getTime());
    long result = sumOfEntryValue(sorter, ImmutableList.of(resultFile));
    Assert.assertEquals(value, result);
}
Also used : RandomAccessInput(com.baidu.hugegraph.computer.core.io.RandomAccessInput) Random(java.util.Random) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) ArrayList(java.util.ArrayList) Sorter(com.baidu.hugegraph.computer.core.sort.Sorter) StopWatch(org.apache.commons.lang3.time.StopWatch) Test(org.junit.Test)

Example 5 with BytesOutput

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

the class SortLargeDataTest method sortBuffer.

private static RandomAccessInput sortBuffer(Sorter sorter, RandomAccessInput input) throws Exception {
    BytesOutput output = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
    PointerCombiner combiner = SorterTestUtil.createPointerCombiner(IntValue::new, new IntValueSumCombiner());
    InnerSortFlusher flusher = new CombineKvInnerSortFlusher(output, combiner);
    sorter.sortBuffer(input, flusher, false);
    return EntriesUtil.inputFromOutput(output);
}
Also used : IntValueSumCombiner(com.baidu.hugegraph.computer.core.combiner.IntValueSumCombiner) CombineKvInnerSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.CombineKvInnerSortFlusher) InnerSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.InnerSortFlusher) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) PointerCombiner(com.baidu.hugegraph.computer.core.combiner.PointerCombiner) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) CombineKvInnerSortFlusher(com.baidu.hugegraph.computer.core.sort.flusher.CombineKvInnerSortFlusher)

Aggregations

BytesOutput (com.baidu.hugegraph.computer.core.io.BytesOutput)36 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)16 Test (org.junit.Test)16 EntryOutput (com.baidu.hugegraph.computer.core.store.entry.EntryOutput)11 EntryOutputImpl (com.baidu.hugegraph.computer.core.store.entry.EntryOutputImpl)11 Sorter (com.baidu.hugegraph.computer.core.sort.Sorter)7 CombineKvInnerSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.CombineKvInnerSortFlusher)7 InnerSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.InnerSortFlusher)5 KvEntriesInput (com.baidu.hugegraph.computer.core.store.buffer.KvEntriesInput)5 KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)5 ArrayList (java.util.ArrayList)5 PointerCombiner (com.baidu.hugegraph.computer.core.combiner.PointerCombiner)4 Config (com.baidu.hugegraph.computer.core.config.Config)4 Id (com.baidu.hugegraph.computer.core.graph.id.Id)4 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)4 GraphComputeOutput (com.baidu.hugegraph.computer.core.io.GraphComputeOutput)4 RandomAccessInput (com.baidu.hugegraph.computer.core.io.RandomAccessInput)4 StreamGraphOutput (com.baidu.hugegraph.computer.core.io.StreamGraphOutput)4 KvInnerSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.KvInnerSortFlusher)4 KvEntryWriter (com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter)4