-
Notifications
You must be signed in to change notification settings - Fork 521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add subgraphpaths restful api #43
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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 javax.inject.Singleton; | ||
import javax.ws.rs.DefaultValue; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import com.baidu.hugegraph.HugeGraph; | ||
import com.baidu.hugegraph.api.API; | ||
import com.baidu.hugegraph.api.graph.EdgeAPI; | ||
import com.baidu.hugegraph.api.graph.VertexAPI; | ||
import com.baidu.hugegraph.backend.id.Id; | ||
import com.baidu.hugegraph.core.GraphManager; | ||
import com.baidu.hugegraph.server.RestServer; | ||
import com.baidu.hugegraph.traversal.optimize.HugeTraverser; | ||
import com.baidu.hugegraph.type.define.Directions; | ||
import com.baidu.hugegraph.util.Log; | ||
import com.codahale.metrics.annotation.Timed; | ||
|
||
@Path("graphs/{graph}/traversers/subgraphpaths") | ||
@Singleton | ||
public class SubGraphPaths extends API { | ||
|
||
private static final Logger LOG = Log.logger(RestServer.class); | ||
|
||
@GET | ||
@Timed | ||
@Produces(APPLICATION_JSON_WITH_CHARSET) | ||
public String get(@Context GraphManager manager, | ||
@PathParam("graph") String graph, | ||
@QueryParam("source") String sourceV, | ||
@QueryParam("direction") String direction, | ||
@QueryParam("label") String edgeLabel, | ||
@QueryParam("depth") int depth, | ||
@QueryParam("degree") @DefaultValue("-1") long degree, | ||
@QueryParam("capacity") @DefaultValue("-1") long capacity, | ||
@QueryParam("limit") @DefaultValue("-1") long limit) { | ||
LOG.debug("Graph [{}] get sub-graph paths from '{}' with " + | ||
"direction '{}', edge label '{}', depth '{}', " + | ||
"degree '{}' and limit '{}'", | ||
graph, sourceV, direction, edgeLabel, depth, degree, limit); | ||
|
||
Id source = VertexAPI.checkAndParseVertexId(sourceV); | ||
Directions dir = Directions.convert(EdgeAPI.parseDirection(direction)); | ||
|
||
HugeGraph g = graph(manager, graph); | ||
|
||
HugeTraverser traverser = new HugeTraverser(g); | ||
MultivaluedMap paths = traverser.subGraphPaths(source, dir, edgeLabel, | ||
depth, degree, capacity, | ||
limit); | ||
return manager.serializer(g).writeSubGraphPaths(paths, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer return a list of path |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,15 @@ | |
package com.baidu.hugegraph.serializer; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; | ||
import org.apache.tinkerpop.gremlin.structure.Edge; | ||
|
@@ -194,6 +198,33 @@ public String writePaths(String name, Collection<HugeTraverser.Path> paths, | |
return writeList(name, pathList); | ||
} | ||
|
||
@Override | ||
public String writeSubGraphPaths( | ||
MultivaluedMap<Boolean, HugeTraverser.Path> paths, | ||
boolean withCrossPoint) { | ||
try (ByteArrayOutputStream out = new ByteArrayOutputStream(LBUF_SIZE)) { | ||
out.write("{\"loop path\": ".getBytes(API.CHARSET)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. loop_path |
||
this.writePathsList(out, paths.get(true)); | ||
out.write(",\"leaf path\": ".getBytes(API.CHARSET)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leaf_path |
||
this.writePathsList(out, paths.get(false)); | ||
out.write("}".getBytes(API.CHARSET)); | ||
return out.toString(API.CHARSET); | ||
} catch (Exception e) { | ||
throw new HugeException("Failed to serialize sub-graph-paths", e); | ||
} | ||
} | ||
|
||
private void writePathsList(ByteArrayOutputStream out, | ||
List<HugeTraverser.Path> paths) | ||
throws IOException { | ||
if (paths == null) { | ||
out.write("[]".getBytes(API.CHARSET)); | ||
} else { | ||
this.writer.writeObject(out, paths.stream().map(p -> p.toMap(false)) | ||
zhoney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.collect(Collectors.toList())); | ||
} | ||
} | ||
|
||
@Override | ||
public String writeShards(List<Shard> shards) { | ||
return this.writeList("shards", shards); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer use "rays" as API name