Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package com.baidu.hugegraph.api.traversers;

import java.util.List;

import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_CAPACITY;
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_DEGREE;

Expand All @@ -45,6 +47,7 @@
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.util.Log;
import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.ImmutableList;

@Path("graphs/{graph}/traversers/allshortestpaths")
@Singleton
Expand Down Expand Up @@ -81,9 +84,10 @@ public String get(@Context GraphManager manager,
HugeGraph g = graph(manager, graph);

ShortestPathTraverser traverser = new ShortestPathTraverser(g);
List<String> edgeLabels = ImmutableList.of(edgeLabel);
HugeTraverser.PathSet paths = traverser.allShortestPaths(
sourceId, targetId, dir, edgeLabel, depth,
degree, skipDegree, capacity);
sourceId, targetId, dir, edgeLabels,
depth, degree, skipDegree, capacity);
return manager.serializer(g).writePaths("paths", paths, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public String post(@Context GraphManager manager,
request.capacity, request.limit);

HugeGraph g = graph(manager, graph);
Iterator<Vertex> sources = request.sources.sourcesVertices(g);
Iterator<Vertex> sources = request.sources.vertices(g);
List<CustomizedCrosspointsTraverser.PathPattern> patterns;
patterns = pathPatterns(g, request);

Expand Down Expand Up @@ -132,7 +132,7 @@ public String post(@Context GraphManager manager,
private static class CrosspointsRequest {

@JsonProperty("sources")
public SourceVertices sources;
public Vertices sources;
@JsonProperty("path_patterns")
public List<PathPattern> pathPatterns;
@JsonProperty("capacity")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* 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.api.traversers;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

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

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.QueryResults;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.server.RestServer;
import com.baidu.hugegraph.structure.HugeVertex;
import com.baidu.hugegraph.traversal.algorithm.CustomizedKneighborTraverser;
import com.baidu.hugegraph.traversal.algorithm.EdgeStep;
import com.baidu.hugegraph.traversal.algorithm.HugeTraverser;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.annotation.JsonProperty;

import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.*;

@Path("graphs/{graph}/traversers/customizedkneighbor")
@Singleton
public class CustomizedKneighborAPI extends TraverserAPI {

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

@POST
@Timed
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String post(@Context GraphManager manager,
@PathParam("graph") String graph,
Request request) {
E.checkArgumentNotNull(request, "The request body can't be null");
E.checkArgumentNotNull(request.source,
"The source of request can't be null");
E.checkArgument(request.step != null,
"The steps of request can't be null");
if (request.countOnly) {
E.checkArgument(!request.withVertex && !request.withPath,
"Can't return vertex or path when count only");
}

LOG.debug("Graph [{}] get customized kneighbor from source vertex " +
"'{}', with step '{}', limit '{}', count_only '{}', " +
"with_vertex '{}' and with_path '{}'",
graph, request.source, request.step, request.limit,
request.countOnly, request.withVertex, request.withPath);

HugeGraph g = graph(manager, graph);
Id sourceId = HugeVertex.getIdValue(request.source);

EdgeStep step = step(g, request.step);

CustomizedKneighborTraverser traverser =
new CustomizedKneighborTraverser(g);
Set<Node> results = traverser.customizedKneighbor(sourceId, step,
request.maxDepth,
request.limit);
Set<Id> neighbors = new HashSet<>();
for (Node node : results) {
neighbors.add(node.id());
}

List<HugeTraverser.Path> paths = new ArrayList<>();
if (request.withPath) {
for (Node node : results) {
paths.add(new HugeTraverser.Path(node.path()));
}
}
Iterator<Vertex> iter = QueryResults.emptyIterator();
if (request.withVertex) {
Set<Id> ids = new HashSet<>();
for (HugeTraverser.Path p : paths) {
ids.addAll(p.vertices());
}
if (!ids.isEmpty()) {
iter = g.vertices(ids.toArray());
}
}
return manager.serializer(g).writeNodes("kneighbor", neighbors,
paths, iter, request.countOnly);
}

private static class Request {

@JsonProperty("source")
public Object source;
@JsonProperty("step")
public TraverserAPI.Step step;
@JsonProperty("max_depth")
public int maxDepth;
@JsonProperty("limit")
public long limit = Long.valueOf(DEFAULT_PATHS_LIMIT);
@JsonProperty("count_only")
public boolean countOnly = false;
@JsonProperty("with_vertex")
public boolean withVertex = false;
@JsonProperty("with_path")
public boolean withPath = false;

@Override
public String toString() {
return String.format("PathRequest{source=%s,step=%s,maxDepth=%s" +
"limit=%s,countOnly=%s,withVertex=%s," +
"withPath=%s}", this.source, this.step,
this.maxDepth, this.limit, this.countOnly,
this.withVertex, this.withPath);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.api.traversers;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

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

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.QueryResults;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.server.RestServer;
import com.baidu.hugegraph.structure.HugeVertex;
import com.baidu.hugegraph.traversal.algorithm.CustomizedKoutTraverser;
import com.baidu.hugegraph.traversal.algorithm.EdgeStep;
import com.baidu.hugegraph.traversal.algorithm.HugeTraverser;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.annotation.JsonProperty;

import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.*;

@Path("graphs/{graph}/traversers/customizedkout")
@Singleton
public class CustomizedKoutAPI extends TraverserAPI {

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

@POST
@Timed
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String post(@Context GraphManager manager,
@PathParam("graph") String graph,
Request request) {
E.checkArgumentNotNull(request, "The request body can't be null");
E.checkArgumentNotNull(request.source,
"The source of request can't be null");
E.checkArgument(request.step != null,
"The steps of request can't be null");
if (request.countOnly) {
E.checkArgument(!request.withVertex && !request.withPath,
"Can't return vertex or path when count only");
}

LOG.debug("Graph [{}] get customized kout from source vertex '{}', " +
"with step '{}', max_depth '{}', nearest '{}', " +
"count_only '{}', capacity '{}', limit '{}', " +
"with_vertex '{}' and with_path '{}'",
graph, request.source, request.step, request.maxDepth,
request.nearest, request.countOnly, request.capacity,
request.limit, request.withVertex, request.withPath);

HugeGraph g = graph(manager, graph);
Id sourceId = HugeVertex.getIdValue(request.source);

EdgeStep step = step(g, request.step);

CustomizedKoutTraverser traverser = new CustomizedKoutTraverser(g);
System.out.println(request);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to keep this?

Set<Node> results = traverser.customizedKout(sourceId, step,
request.maxDepth,
request.nearest,
request.capacity,
request.limit);
Set<Id> neighbors = new HashSet<>();
for (Node node : results) {
neighbors.add(node.id());
}

List<HugeTraverser.Path> paths = new ArrayList<>();
if (request.withPath) {
for (Node node : results) {
paths.add(new HugeTraverser.Path(node.path()));
}
}
Iterator<Vertex> iter = QueryResults.emptyIterator();
if (request.withVertex) {
Set<Id> ids = new HashSet<>();
for (HugeTraverser.Path p : paths) {
ids.addAll(p.vertices());
}
if (!ids.isEmpty()) {
iter = g.vertices(ids.toArray());
}
}
return manager.serializer(g).writeNodes("kout", neighbors,
paths, iter, request.countOnly);
}

private static class Request {

@JsonProperty("source")
public Object source;
@JsonProperty("step")
public TraverserAPI.Step step;
@JsonProperty("max_depth")
public int maxDepth;
@JsonProperty("nearest")
public boolean nearest = true;
@JsonProperty("count_only")
public boolean countOnly = false;
@JsonProperty("capacity")
public long capacity = Long.valueOf(DEFAULT_CAPACITY);
@JsonProperty("limit")
public long limit = Long.valueOf(DEFAULT_PATHS_LIMIT);
@JsonProperty("with_vertex")
public boolean withVertex = false;
@JsonProperty("with_path")
public boolean withPath = false;

@Override
public String toString() {
return String.format("KoutRequest{source=%s,step=%s,maxDepth=%s" +
"nearest=%s,countOnly=%s,capacity=%s," +
"limit=%s,withVertex=%s,withPath=%s}",
this.source, this.step, this.maxDepth,
this.nearest, this.countOnly, this.capacity,
this.limit, this.withVertex, this.withPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public String post(@Context GraphManager manager,
request.withVertex);

HugeGraph g = graph(manager, graph);
Iterator<Vertex> sources = request.sources.sourcesVertices(g);
Iterator<Vertex> sources = request.sources.vertices(g);
List<CustomizePathsTraverser.Step> steps = step(g, request);
boolean sorted = request.sortBy != SortBy.NONE;

Expand Down Expand Up @@ -129,7 +129,7 @@ private static List<CustomizePathsTraverser.Step> step(HugeGraph graph,
private static class PathRequest {

@JsonProperty("sources")
public SourceVertices sources;
public Vertices sources;
@JsonProperty("steps")
public List<Step> steps;
@JsonProperty("sort_by")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public String post(@Context GraphManager manager,
request.groupProperty, request.minGroups);

HugeGraph g = graph(manager, graph);
Iterator<Vertex> sources = request.sources.sourcesVertices(g);
Iterator<Vertex> sources = request.sources.vertices(g);
E.checkArgument(sources != null && sources.hasNext(),
"The source vertices can't be empty");
EdgeLabel edgeLabel = request.label == null ?
Expand Down Expand Up @@ -125,7 +125,7 @@ public String post(@Context GraphManager manager,
private static class FusiformSimilarityRequest {

@JsonProperty("sources")
public SourceVertices sources;
public Vertices sources;
@JsonProperty("label")
public String label;
@JsonProperty("direction")
Expand Down
Loading