Search in sources :

Example 11 with Id

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();
}
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) Writable(com.baidu.hugegraph.computer.core.io.Writable) KvEntriesInput(com.baidu.hugegraph.computer.core.store.buffer.KvEntriesInput) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Test(org.junit.Test)

Example 12 with Id

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();
}
Also used : BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) KvEntriesInput(com.baidu.hugegraph.computer.core.store.buffer.KvEntriesInput) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Test(org.junit.Test)

Example 13 with Id

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();
}
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 14 with Id

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());
    }
}
Also used : Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) ConnectionId(com.baidu.hugegraph.computer.core.network.ConnectionId) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 15 with Id

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;
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList) List(java.util.List) IdListList(com.baidu.hugegraph.computer.core.graph.value.IdListList) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) ConnectionId(com.baidu.hugegraph.computer.core.network.ConnectionId) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList)

Aggregations

Id (com.baidu.hugegraph.computer.core.graph.id.Id)74 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)51 Test (org.junit.Test)29 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)18 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)12 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)12 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)12 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)12 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)11 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)10 KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)9 ConnectionId (com.baidu.hugegraph.computer.core.network.ConnectionId)8 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)7 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)6 File (java.io.File)6 Value (com.baidu.hugegraph.computer.core.graph.value.Value)5 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)5 Config (com.baidu.hugegraph.computer.core.config.Config)4 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)4 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)4