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);
}
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();
}
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();
}
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());
}
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);
}
}
Aggregations