use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.
the class EntryOutputTest method testWriteKvEntry.
@Test
public void testWriteKvEntry() throws Exception {
List<Integer> entries = ImmutableList.of(1, 5, 6, 6, 2, 1, 4, 8);
List<Id> data = intListToLongIds(entries);
BytesOutput output = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
EntryOutput entryOutput = new EntryOutputImpl(output);
for (int i = 0; i < data.size(); ) {
Id id = data.get(i++);
Writable value = data.get(i++);
entryOutput.writeEntry(id, value);
}
// Assert result
BytesInput input = EntriesUtil.inputFromOutput(output);
EntryIterator iter = new KvEntriesInput(input);
SorterTestUtil.assertKvEntry(iter.next(), BytesId.of(1), BytesId.of(5));
SorterTestUtil.assertKvEntry(iter.next(), BytesId.of(6), BytesId.of(6));
SorterTestUtil.assertKvEntry(iter.next(), BytesId.of(2), BytesId.of(1));
SorterTestUtil.assertKvEntry(iter.next(), BytesId.of(4), BytesId.of(8));
iter.close();
}
use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.
the class EntryOutputTest method testSubKvNotNeedSort.
@Test
public void testSubKvNotNeedSort() throws Exception {
List<Integer> entries = ImmutableList.of(5, 6, 6, 2, 1, 4, 8, 1, 2, 2, 6, 1);
BytesInput input = inputFromEntries(entries, false);
EntryIterator iter = new KvEntriesInput(input, true);
// Assert entry1
KvEntry kvEntry1 = iter.next();
Assert.assertEquals(3, kvEntry1.numSubEntries());
Id key1 = StoreTestUtil.idFromPointer(kvEntry1.key());
Assert.assertEquals(BytesId.of(5), key1);
EntryIterator kvEntry1SubKvs = EntriesUtil.subKvIterFromEntry(kvEntry1);
KvEntry subKv1 = kvEntry1SubKvs.next();
Assert.assertEquals(0, subKv1.numSubEntries());
SorterTestUtil.assertKvEntry(subKv1, BytesId.of(6), BytesId.of(6));
SorterTestUtil.assertKvEntry(kvEntry1SubKvs.next(), BytesId.of(2), BytesId.of(1));
SorterTestUtil.assertKvEntry(kvEntry1SubKvs.next(), BytesId.of(4), BytesId.of(8));
// Assert entry2
KvEntry kvEntry2 = iter.next();
Id key2 = StoreTestUtil.idFromPointer(kvEntry2.key());
Assert.assertEquals(BytesId.of(1), key2);
EntryIterator kvEntry2SubKvs = EntriesUtil.subKvIterFromEntry(kvEntry2);
SorterTestUtil.assertKvEntry(kvEntry2SubKvs.next(), BytesId.of(2), BytesId.of(2));
SorterTestUtil.assertKvEntry(kvEntry2SubKvs.next(), BytesId.of(6), BytesId.of(1));
iter.close();
}
use of com.baidu.hugegraph.computer.core.graph.id.Id 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.graph.id.Id in project hugegraph-computer by hugegraph.
the class EdgesInputTest method checkEdgesInput.
private void checkEdgesInput(EdgesInput edgesInput, EdgeFrequency freq) throws IOException {
for (long i = 0L; i < 200L; i += 2) {
Id id = BytesId.of(i);
ReusablePointer idPointer = idToReusablePointer(id);
Edges edges = edgesInput.edges(idPointer);
Iterator<Edge> edgesIt = edges.iterator();
Assert.assertEquals(i, edges.size());
for (int j = 0; j < edges.size(); j++) {
Assert.assertTrue(edgesIt.hasNext());
Edge edge = edgesIt.next();
switch(freq) {
case SINGLE:
Assert.assertEquals(BytesId.of(j), edge.targetId());
break;
case SINGLE_PER_LABEL:
Assert.assertEquals(BytesId.of(j), edge.targetId());
Assert.assertEquals(String.valueOf(j), edge.label());
break;
case MULTIPLE:
Assert.assertEquals(BytesId.of(j), edge.targetId());
Assert.assertEquals(String.valueOf(j), edge.label());
Assert.assertEquals(String.valueOf(j), edge.name());
break;
default:
throw new ComputerException("Illegal edge frequency %s", freq);
}
}
Assert.assertFalse(edgesIt.hasNext());
}
}
use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.
the class MessageInputTest method expectedMessages.
private static Map<Id, List<IdList>> expectedMessages() {
Random random = new Random(1);
Map<Id, List<IdList>> globalMessages = new HashMap<>();
for (long i = 0L; i < 200L; i++) {
int count = random.nextInt(5);
for (int j = 0; j < count; j++) {
Id id = BytesId.of(random.nextInt(200));
IdList message = new IdList();
message.add(id);
List<IdList> messages = globalMessages.computeIfAbsent(id, nid -> new ArrayList<>());
messages.add(message);
}
}
return globalMessages;
}
Aggregations