Search in sources :

Example 1 with KvEntryWriter

use of com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter in project hugegraph-computer by hugegraph.

the class EntryOutputTest method inputFromEntries.

private static BytesInput inputFromEntries(List<Integer> entries, boolean needSort) throws IOException {
    /*
         * All integer data will convert to Id type, so upper layer also
         * needs to use the Id type to make a judgment
         */
    List<Id> data = intListToLongIds(entries);
    BytesOutput output = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
    EntryOutput entryOutput = new EntryOutputImpl(output, needSort);
    int index = 0;
    KvEntryWriter entry1 = entryOutput.writeEntry(data.get(index++));
    entry1.writeSubKv(data.get(index++), data.get(index++));
    entry1.writeSubKv(data.get(index++), data.get(index++));
    entry1.writeSubKv(data.get(index++), data.get(index++));
    entry1.writeFinish();
    KvEntryWriter entry2 = entryOutput.writeEntry(data.get(index++));
    entry2.writeSubKv(data.get(index++), data.get(index++));
    entry2.writeSubKv(data.get(index++), data.get(index));
    entry2.writeFinish();
    return EntriesUtil.inputFromOutput(output);
}
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) KvEntryWriter(com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId)

Example 2 with KvEntryWriter

use of com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter in project hugegraph-computer by hugegraph.

the class StreamGraphOutput method writeEdges.

@Override
public void writeEdges(Vertex vertex) throws IOException {
    KvEntryWriter writer = this.out.writeEntry(out -> {
        // Write id
        this.writeId(out, vertex.id());
    });
    if (this.frequency == EdgeFrequency.SINGLE) {
        for (Edge edge : vertex.edges()) {
            // Only use targetId as subKey, use properties as subValue
            writer.writeSubKv(out -> {
                this.writeId(out, edge.targetId());
            }, out -> {
                this.writeProperties(out, edge.properties());
            });
        }
    } else if (this.frequency == EdgeFrequency.SINGLE_PER_LABEL) {
        for (Edge edge : vertex.edges()) {
            // Use label + targetId as subKey, use properties as subValue
            writer.writeSubKv(out -> {
                this.writeLabel(out, edge.label());
                this.writeId(out, edge.targetId());
            }, out -> {
                this.writeProperties(out, edge.properties());
            });
        }
    } else {
        assert this.frequency == EdgeFrequency.MULTIPLE;
        for (Edge edge : vertex.edges()) {
            /*
                 * Use label + sortValues + targetId as subKey,
                 * use properties as subValue
                 */
            writer.writeSubKv(out -> {
                this.writeLabel(out, edge.label());
                this.writeLabel(out, edge.name());
                this.writeId(out, edge.targetId());
            }, out -> {
                this.writeProperties(out, edge.properties());
            });
        }
    }
    writer.writeFinish();
}
Also used : ComputerOptions(com.baidu.hugegraph.computer.core.config.ComputerOptions) Id(com.baidu.hugegraph.computer.core.graph.id.Id) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) Map(java.util.Map) EdgeFrequency(com.baidu.hugegraph.computer.core.config.EdgeFrequency) Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) EntryOutput(com.baidu.hugegraph.computer.core.store.entry.EntryOutput) IOException(java.io.IOException) KvEntryWriter(com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Value(com.baidu.hugegraph.computer.core.graph.value.Value) KvEntryWriter(com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge)

Example 3 with KvEntryWriter

use of com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter in project hugegraph-computer by hugegraph.

the class EdgeMessageRecvPartitionTest method writeEdges.

private static byte[] writeEdges(Vertex vertex) throws IOException {
    BytesOutput bytesOutput = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
    EntryOutput entryOutput = new EntryOutputImpl(bytesOutput);
    Id id = vertex.id();
    KvEntryWriter subKvWriter = entryOutput.writeEntry(out -> {
        id.write(out);
    });
    for (Edge edge : vertex.edges()) {
        Id targetId = edge.targetId();
        subKvWriter.writeSubKv(out -> {
            targetId.write(out);
        }, out -> {
            edge.properties().write(out);
        });
    }
    subKvWriter.writeFinish();
    return bytesOutput.toByteArray();
}
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) KvEntryWriter(com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge)

Example 4 with KvEntryWriter

use of com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter 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 5 with KvEntryWriter

use of com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter 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)

Aggregations

EntryOutput (com.baidu.hugegraph.computer.core.store.entry.EntryOutput)5 KvEntryWriter (com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter)5 BytesOutput (com.baidu.hugegraph.computer.core.io.BytesOutput)4 EntryOutputImpl (com.baidu.hugegraph.computer.core.store.entry.EntryOutputImpl)4 Id (com.baidu.hugegraph.computer.core.graph.id.Id)3 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)2 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)2 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)2 KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)2 Test (org.junit.Test)2 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)1 ComputerOptions (com.baidu.hugegraph.computer.core.config.ComputerOptions)1 EdgeFrequency (com.baidu.hugegraph.computer.core.config.EdgeFrequency)1 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)1 Value (com.baidu.hugegraph.computer.core.graph.value.Value)1 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)1 SubKvEntriesInput (com.baidu.hugegraph.computer.core.store.buffer.SubKvEntriesInput)1 IOException (java.io.IOException)1 Map (java.util.Map)1