Skip to content

Commit

Permalink
format&clean example
Browse files Browse the repository at this point in the history
  • Loading branch information
msgui committed Feb 2, 2024
1 parent 171e831 commit 164bc64
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 159 deletions.
4 changes: 2 additions & 2 deletions hugegraph-server/hugegraph-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
License for the specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hugegraph-server</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@
import java.util.Iterator;
import java.util.List;

import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;

import org.apache.hugegraph.HugeFactory;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.backend.BackendException;
Expand All @@ -45,6 +37,13 @@
import org.apache.hugegraph.type.define.Directions;
import org.apache.hugegraph.type.define.HugeKeys;
import org.apache.hugegraph.util.Log;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;

public class Example1 {

Expand Down Expand Up @@ -79,9 +78,9 @@ private static void thread(HugeGraph graph) throws InterruptedException {
graph.tx().commit();

// New tx
GraphTransaction tx = Whitebox.invoke(graph.getClass(),
"openGraphTransaction",
graph);
GraphTransaction tx = Whitebox.invoke(graph.getClass(),
"openGraphTransaction",
graph);

tx.addVertex(T.label, "book", "name", "java-21");
tx.addVertex(T.label, "book", "name", "java-22");
Expand Down Expand Up @@ -214,13 +213,13 @@ public static void loadData(final HugeGraph graph) {
graph.tx().commit();

// must commit manually with new backend tx (independent of tinkerpop)
GraphTransaction tx = Whitebox.invoke(graph.getClass(),
"openGraphTransaction",
graph);
GraphTransaction tx = Whitebox.invoke(graph.getClass(),
"openGraphTransaction",
graph);

LOG.info("=============== addVertex ================");
Vertex james = tx.addVertex(T.label, "author", "id", 1,
"name", "James Gosling", "age", 62,
"name", "James Gosling", "age", 62,
"lived", "San Francisco Bay Area");

Vertex java = tx.addVertex(T.label, "language", "name", "java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import java.util.List;

import org.apache.hugegraph.HugeFactory;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.schema.SchemaManager;
import org.apache.hugegraph.util.Log;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
Expand All @@ -29,11 +33,6 @@
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;

import org.apache.hugegraph.HugeFactory;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.schema.SchemaManager;
import org.apache.hugegraph.util.Log;

public class Example2 {

private static final Logger LOG = Log.logger(Example2.class);
Expand Down Expand Up @@ -93,7 +92,7 @@ public static void traversal(final HugeGraph graph) {

String person = graph.schema().getVertexLabel("person").id().asString();
String software = graph.schema().getVertexLabel("software").id()
.asString();
.asString();
String markoId = String.format("%s:%s", person, "marko");
String joshId = String.format("%s:%s", person, "josh");
String lopId = String.format("%s:%s", software, "lop");
Expand Down Expand Up @@ -122,30 +121,30 @@ public static void traversal(final HugeGraph graph) {
assert paths.get(0).get(1).equals("lop");

LOG.info(">>>> query with out() optimize: {}",
graph.traversal().V(markoId)
.out()
.out()
.values("name").toList());
graph.traversal().V(markoId)
.out()
.out()
.values("name").toList());

LOG.info(">>>> query with out() optimize and path(): {}",
graph.traversal().V()
.out("knows")
.out("created")
.values("name")
.path()
.toList());
graph.traversal().V()
.out("knows")
.out("created")
.values("name")
.path()
.toList());
}

public static List<Path> shortestPath(final HugeGraph graph,
Object from, Object to,
int maxDepth) {
GraphTraversal<Vertex, Path> t = graph.traversal()
.V(from)
.repeat(__.out().simplePath())
.until(__.hasId(to).or().loops().is(P.gt(maxDepth)))
.hasId(to)
.path().by("name")
.limit(1);
.V(from)
.repeat(__.out().simplePath())
.until(__.hasId(to).or().loops().is(P.gt(maxDepth)))
.hasId(to)
.path().by("name")
.limit(1);
return t.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

package org.apache.hugegraph.example;

import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;

import org.apache.hugegraph.HugeFactory;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.schema.SchemaManager;
import org.apache.hugegraph.util.Log;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;

public class Example3 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.slf4j.Logger;

public class ExampleUtil {

private static final Logger LOG = Log.logger(ExampleUtil.class);

private static boolean registered = false;
Expand Down Expand Up @@ -65,13 +66,13 @@ public static HugeGraph loadGraph(boolean needClear, boolean needProfile) {
String conf = "hugegraph.properties";
try {
String path = ExampleUtil.class.getClassLoader()
.getResource(conf).getPath();
.getResource(conf).getPath();
File file = new File(path);
if (file.exists() && file.isFile()) {
conf = path;
}
} catch (Exception ignored) {
LOG.warn("loadGraph warn {} ",ignored);
LOG.warn("loadGraph warn {} ", ignored);
}

HugeGraph graph = HugeFactory.open(conf);
Expand Down
Loading

0 comments on commit 164bc64

Please sign in to comment.