From b666ff2807499f5f7f0d3163a04257bba9bacca5 Mon Sep 17 00:00:00 2001 From: houzhizhen Date: Sat, 8 May 2021 19:24:41 +0800 Subject: [PATCH] add createId methods in GraphFactory (#45) --- .../computer/core/graph/GraphFactory.java | 7 +++ .../core/graph/BuiltinGraphFactory.java | 19 ++++++ .../core/graph/BuiltinGraphFactoryTest.java | 62 +++++++++++++++++++ .../computer/core/graph/GraphTestSuite.java | 1 + 4 files changed, 89 insertions(+) create mode 100644 computer-test/src/main/java/com/baidu/hugegraph/computer/core/graph/BuiltinGraphFactoryTest.java diff --git a/computer-api/src/main/java/com/baidu/hugegraph/computer/core/graph/GraphFactory.java b/computer-api/src/main/java/com/baidu/hugegraph/computer/core/graph/GraphFactory.java index 2918cbcb7..3dc0736ca 100644 --- a/computer-api/src/main/java/com/baidu/hugegraph/computer/core/graph/GraphFactory.java +++ b/computer-api/src/main/java/com/baidu/hugegraph/computer/core/graph/GraphFactory.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; +import java.util.UUID; import com.baidu.hugegraph.computer.core.graph.edge.Edge; import com.baidu.hugegraph.computer.core.graph.edge.Edges; @@ -31,6 +32,12 @@ public interface GraphFactory { + Id createId(long id); + + Id createId(String id); + + Id createId(UUID id); + Vertex createVertex(); > Vertex createVertex(Id id, V value); diff --git a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/graph/BuiltinGraphFactory.java b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/graph/BuiltinGraphFactory.java index c80f7700b..46ddf59d4 100644 --- a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/graph/BuiltinGraphFactory.java +++ b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/graph/BuiltinGraphFactory.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; import com.baidu.hugegraph.computer.core.config.ComputerOptions; import com.baidu.hugegraph.computer.core.config.Config; @@ -31,6 +32,9 @@ import com.baidu.hugegraph.computer.core.graph.edge.Edge; import com.baidu.hugegraph.computer.core.graph.edge.Edges; import com.baidu.hugegraph.computer.core.graph.id.Id; +import com.baidu.hugegraph.computer.core.graph.id.LongId; +import com.baidu.hugegraph.computer.core.graph.id.Utf8Id; +import com.baidu.hugegraph.computer.core.graph.id.UuidId; import com.baidu.hugegraph.computer.core.graph.properties.DefaultProperties; import com.baidu.hugegraph.computer.core.graph.properties.Properties; import com.baidu.hugegraph.computer.core.graph.value.Value; @@ -45,6 +49,21 @@ public BuiltinGraphFactory(Config config) { this.config = config; } + @Override + public Id createId(long id) { + return new LongId(id); + } + + @Override + public Id createId(String id) { + return new Utf8Id(id); + } + + @Override + public Id createId(UUID id) { + return new UuidId(id); + } + @Override public Vertex createVertex() { return new DefaultVertex(this); diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/graph/BuiltinGraphFactoryTest.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/graph/BuiltinGraphFactoryTest.java new file mode 100644 index 000000000..39d3a64e0 --- /dev/null +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/graph/BuiltinGraphFactoryTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.computer.core.graph; + +import java.util.UUID; + +import org.junit.Assert; +import org.junit.Test; + +import com.baidu.hugegraph.computer.core.UnitTestBase; +import com.baidu.hugegraph.computer.core.graph.id.Id; +import com.baidu.hugegraph.computer.core.graph.id.IdType; +import com.baidu.hugegraph.computer.core.graph.id.LongId; +import com.baidu.hugegraph.computer.core.graph.id.Utf8Id; +import com.baidu.hugegraph.computer.core.graph.id.UuidId; + +public class BuiltinGraphFactoryTest extends UnitTestBase { + + @Test + public void testCreateLongId() { + long value = 1L; + GraphFactory graphFactory = graphFactory(); + Id id = graphFactory.createId(value); + Assert.assertEquals(IdType.LONG, id.type()); + Assert.assertEquals(new LongId(value), id); + } + + @Test + public void testCreateUtf8Id() { + String value = "John"; + GraphFactory graphFactory = graphFactory(); + Id id = graphFactory.createId(value); + Assert.assertEquals(IdType.UTF8, id.type()); + Assert.assertEquals(new Utf8Id(value), id); + } + + @Test + public void testCreateUuidId() { + UUID uuid = UUID.randomUUID(); + GraphFactory graphFactory = graphFactory(); + Id id = graphFactory.createId(uuid); + Assert.assertEquals(IdType.UUID, id.type()); + Assert.assertEquals(new UuidId(uuid), id); + } +} diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/graph/GraphTestSuite.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/graph/GraphTestSuite.java index 0456f6e65..8881db2fa 100644 --- a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/graph/GraphTestSuite.java +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/graph/GraphTestSuite.java @@ -63,6 +63,7 @@ ListValueTest.class, ValueTypeTest.class, BuiltinValueFactoryTest.class, + BuiltinGraphFactoryTest.class, ContainerInfoTest.class, PartitionStatTest.class, HashPartitionerTest.class,