Search in sources :

Example 16 with BytesInput

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

the class PointerCombinerTest method testMessageCombiner.

@Test
public void testMessageCombiner() throws IOException {
    Config config = UnitTestBase.updateWithRequiredOptions(ComputerOptions.WORKER_COMBINER_CLASS, DoubleValueSumCombiner.class.getName());
    Combiner<DoubleValue> valueCombiner = config.createObject(ComputerOptions.WORKER_COMBINER_CLASS);
    PointerCombiner combiner = SorterTestUtil.createPointerCombiner(DoubleValue::new, new DoubleValueSumCombiner());
    try (BytesOutput bytesOutput1 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
        BytesOutput bytesOutput2 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        DoubleValue value1 = new DoubleValue(1.0D);
        DoubleValue value2 = new DoubleValue(2.0D);
        value1.write(bytesOutput1);
        value2.write(bytesOutput2);
        Pointer pointer1 = new InlinePointer(bytesOutput1.buffer(), bytesOutput1.position());
        Pointer pointer2 = new InlinePointer(bytesOutput2.buffer(), bytesOutput2.position());
        Pointer pointer = combiner.combine(pointer1, pointer2);
        BytesInput input = IOFactory.createBytesInput(pointer.bytes());
        DoubleValue combinedValue = new DoubleValue();
        combinedValue.read(input);
        Assert.assertEquals(new DoubleValue(3.0D), combinedValue);
    }
}
Also used : BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Config(com.baidu.hugegraph.computer.core.config.Config) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) Pointer(com.baidu.hugegraph.computer.core.store.entry.Pointer) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) Test(org.junit.Test)

Example 17 with BytesInput

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

the class UnitTestBase method assertValueEqualAfterWriteAndRead.

public static void assertValueEqualAfterWriteAndRead(Value oldValue) throws IOException {
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        oldValue.write(bao);
        bytes = bao.toByteArray();
    }
    Value newValue = graphFactory().createValue(oldValue.valueType());
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        newValue.read(bai);
        Assert.assertEquals(oldValue, newValue);
    }
}
Also used : BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) Value(com.baidu.hugegraph.computer.core.graph.value.Value)

Example 18 with BytesInput

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

the class UnitTestBase method assertEqualAfterWriteAndRead.

public static void assertEqualAfterWriteAndRead(Writable writeObj, Readable readObj) throws IOException {
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        writeObj.write(bao);
        bytes = bao.toByteArray();
    }
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        readObj.read(bai);
        Assert.assertEquals(writeObj, readObj);
    }
}
Also used : BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput)

Example 19 with BytesInput

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

the class SerializeUtil method fromBytes.

public static <V extends Readable> List<V> fromBytes(byte[] bytes, Supplier<V> supplier) {
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        int size = bai.readInt();
        List<V> list = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            V obj = supplier.get();
            obj.read(bai);
            list.add(obj);
        }
        return list;
    } catch (IOException e) {
        throw new ComputerException("Failed to read from byte array", e);
    }
}
Also used : BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 20 with BytesInput

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

the class PointerCombinerTest method testVertexPropertiesCombiner.

@Test
public void testVertexPropertiesCombiner() throws IOException {
    Config config = UnitTestBase.updateWithRequiredOptions(ComputerOptions.WORKER_COMBINER_CLASS, DoubleValueSumCombiner.class.getName(), ComputerOptions.WORKER_VERTEX_PROPERTIES_COMBINER_CLASS, MergeOldPropertiesCombiner.class.getName());
    Combiner<Properties> valueCombiner = config.createObject(ComputerOptions.WORKER_VERTEX_PROPERTIES_COMBINER_CLASS);
    GraphFactory graphFactory = graphFactory();
    PointerCombiner combiner = SorterTestUtil.createPointerCombiner(graphFactory::createProperties, valueCombiner);
    try (BytesOutput bytesOutput1 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
        BytesOutput bytesOutput2 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        Properties value1 = graphFactory.createProperties();
        value1.put("p1", new LongValue(1L));
        Properties value2 = graphFactory.createProperties();
        value2.put("p2", new LongValue(2L));
        value1.write(bytesOutput1);
        value2.write(bytesOutput2);
        Pointer pointer1 = new InlinePointer(bytesOutput1.buffer(), bytesOutput1.position());
        Pointer pointer2 = new InlinePointer(bytesOutput2.buffer(), bytesOutput2.position());
        Pointer pointer = combiner.combine(pointer1, pointer2);
        BytesInput input = IOFactory.createBytesInput(pointer.bytes());
        Properties combinedValue = graphFactory.createProperties();
        combinedValue.read(input);
        Map<String, Value> map = combinedValue.get();
        Assert.assertEquals(2, map.size());
        Assert.assertEquals(new LongValue(1L), map.get("p1"));
        Assert.assertEquals(new LongValue(2L), map.get("p2"));
    }
}
Also used : GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) Config(com.baidu.hugegraph.computer.core.config.Config) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) Pointer(com.baidu.hugegraph.computer.core.store.entry.Pointer) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Value(com.baidu.hugegraph.computer.core.graph.value.Value) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) 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