Skip to content
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

support count api #84

Merged
merged 3 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
<version>1.8.7</version>
<version>1.8.8</version>
<packaging>jar</packaging>

<name>hugegraph-client</name>
Expand Down Expand Up @@ -113,7 +113,7 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
<Implementation-Version>1.8.7.0</Implementation-Version>
<Implementation-Version>1.8.8.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/baidu/hugegraph/api/traverser/CountAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.traverser;

import java.util.Map;

import com.baidu.hugegraph.api.traverser.structure.CountRequest;
import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.rest.RestResult;
import com.baidu.hugegraph.util.E;

public class CountAPI extends TraversersAPI {

private static final String COUNT = "count";

public CountAPI(RestClient client, String graph) {
super(client, graph);
}

@Override
protected String type() {
return "count";
}

public long post(CountRequest request) {
this.client.checkApiVersion("0.55", "count");
RestResult result = this.client.post(this.path(), request);
@SuppressWarnings("unchecked")
Map<String, Number> countMap = result.readObject(Map.class);
E.checkState(countMap.containsKey(COUNT),
"The result doesn't have key '%s'", COUNT);
return countMap.get(COUNT).longValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* 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.traverser.structure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.api.traverser.TraversersAPI;
import com.baidu.hugegraph.structure.constant.Direction;
import com.baidu.hugegraph.structure.constant.Traverser;
import com.baidu.hugegraph.util.E;
import com.fasterxml.jackson.annotation.JsonProperty;

public class CountRequest {

@JsonProperty("source")
private Object source;
@JsonProperty("steps")
private List<Step> steps;
@JsonProperty("contains_traversed")
public boolean containsTraversed;
@JsonProperty("dedup_size")
public long dedupSize;

private CountRequest() {
this.source = null;
this.steps = new ArrayList<>();
this.containsTraversed = false;
this.dedupSize = Traverser.DEFAULT_DEDUP_SIZE;
}

@Override
public String toString() {
return String.format("CountRequest{source=%s,steps=%s," +
"containsTraversed=%s,dedupSize=%s",
this.source, this.steps, this.containsTraversed,
this.dedupSize);
}

public static class Builder {

private CountRequest request;
private List<Step.Builder> stepBuilders;

public Builder() {
this.request = new CountRequest();
this.stepBuilders = new ArrayList<>();
}

public Builder source(Object source) {
E.checkArgumentNotNull(source, "The source can't be null");
this.request.source = source;
return this;
}

public Step.Builder steps() {
Step.Builder builder = new Step.Builder();
this.stepBuilders.add(builder);
return builder;
}

public Builder containsTraversed(boolean containsTraversed) {
this.request.containsTraversed = containsTraversed;
return this;
}

public Builder dedupSize(long dedupSize) {
checkDedupSize(dedupSize);
this.request.dedupSize = dedupSize;
return this;
}

public CountRequest build() {
E.checkArgumentNotNull(this.request.source,
"The source can't be null");
for (Step.Builder builder : this.stepBuilders) {
this.request.steps.add(builder.build());
}
E.checkArgument(this.request.steps != null &&
!this.request.steps.isEmpty(),
"The steps can't be null or empty");
checkDedupSize(this.request.dedupSize);
return this.request;
}
}

private static void checkDedupSize(long dedupSize) {
E.checkArgument(dedupSize >= 0L || dedupSize == API.NO_LIMIT,
"The dedup size must be >= 0 or == %s, but got: %s",
API.NO_LIMIT, dedupSize);
}

public static class Step {

@JsonProperty("direction")
private String direction;
@JsonProperty("labels")
private List<String> labels;
@JsonProperty("properties")
private Map<String, Object> properties;
@JsonProperty("degree")
private long degree;
@JsonProperty("skip_degree")
private long skipDegree;

private Step() {
this.direction = "BOTH";
this.labels = new ArrayList<>();
this.properties = new HashMap<>();
this.degree = Traverser.DEFAULT_DEGREE;
this.skipDegree = Traverser.DEFAULT_SKIP_DEGREE;
}

@Override
public String toString() {
return String.format("Step{direction=%s,labels=%s,properties=%s," +
"degree=%s,skipDegree=%s}",
this.direction, this.labels, this.properties,
this.degree, this.skipDegree);
}

public static class Builder {

private Step step;

private Builder() {
this.step = new Step();
}

public Builder direction(Direction direction) {
this.step.direction = direction.toString();
return this;
}

public Builder labels(List<String> labels) {
this.step.labels = labels;
return this;
}

public Builder labels(String label) {
this.step.labels.add(label);
return this;
}

public Builder properties(Map<String, Object> properties) {
this.step.properties = properties;
return this;
}

public Builder properties(String key, Object value) {
this.step.properties.put(key, value);
return this;
}

public Builder degree(long degree) {
TraversersAPI.checkDegree(degree);
this.step.degree = degree;
return this;
}

public Builder skipDegree(long skipDegree) {
TraversersAPI.checkSkipDegree(skipDegree, this.step.degree,
API.NO_LIMIT);
this.step.skipDegree = skipDegree;
return this;
}

private Step build() {
TraversersAPI.checkDegree(this.step.degree);
TraversersAPI.checkSkipDegree(this.step.skipDegree,
this.step.degree, API.NO_LIMIT);
return this.step;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;

import com.baidu.hugegraph.api.traverser.AllShortestPathsAPI;
import com.baidu.hugegraph.api.traverser.CountAPI;
import com.baidu.hugegraph.api.traverser.CrosspointsAPI;
import com.baidu.hugegraph.api.traverser.CustomizedCrosspointsAPI;
import com.baidu.hugegraph.api.traverser.CustomizedPathsAPI;
Expand All @@ -41,6 +42,7 @@
import com.baidu.hugegraph.api.traverser.SingleSourceShortestPathAPI;
import com.baidu.hugegraph.api.traverser.VerticesAPI;
import com.baidu.hugegraph.api.traverser.WeightedShortestPathAPI;
import com.baidu.hugegraph.api.traverser.structure.CountRequest;
import com.baidu.hugegraph.api.traverser.structure.CrosspointsRequest;
import com.baidu.hugegraph.api.traverser.structure.CustomizedCrosspoints;
import com.baidu.hugegraph.api.traverser.structure.CustomizedPaths;
Expand Down Expand Up @@ -81,6 +83,7 @@ public class TraverserManager {
private CrosspointsAPI crosspointsAPI;
private KoutAPI koutAPI;
private KneighborAPI kneighborAPI;
private CountAPI countAPI;
private RingsAPI ringsAPI;
private RaysAPI raysAPI;
private CustomizedPathsAPI customizedPathsAPI;
Expand All @@ -106,6 +109,7 @@ public TraverserManager(RestClient client, GraphManager graphManager) {
this.crosspointsAPI = new CrosspointsAPI(client, graph);
this.koutAPI = new KoutAPI(client, graph);
this.kneighborAPI = new KneighborAPI(client, graph);
this.countAPI = new CountAPI(client, graph);
this.ringsAPI = new RingsAPI(client, graph);
this.raysAPI = new RaysAPI(client, graph);
this.customizedPathsAPI = new CustomizedPathsAPI(client, graph);
Expand Down Expand Up @@ -379,6 +383,10 @@ public List<Object> kneighbor(Object sourceId, Direction direction,
degree, limit);
}

public long kneighbor(CountRequest request) {
Copy link
Contributor

Choose a reason for hiding this comment

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

count()

return this.countAPI.post(request);
}

public List<Path> rings(Object sourceId, int depth) {
return this.rings(sourceId, Direction.BOTH, null, depth, true,
DEFAULT_DEGREE, DEFAULT_CAPACITY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Traverser {
public static final long DEFAULT_DEGREE = 10_000L;
public static final long DEFAULT_CROSSPOINT_LIMIT = 10_000L;
public static final long DEFAULT_PATHS_LIMIT = 10L;
public static final long DEFAULT_DEDUP_SIZE = 1_000_000L;
public static final long DEFAULT_SKIP_DEGREE = 100_000L;
public static final long DEFAULT_SAMPLE = 100L;
public static final double DEFAULT_WEIGHT = 0.0D;
public static final long DEFAULT_PAGE_LIMIT = 100_000L;
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/baidu/hugegraph/api/BaseApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.baidu.hugegraph.api.schema.VertexLabelAPI;
import com.baidu.hugegraph.api.task.TaskAPI;
import com.baidu.hugegraph.api.traverser.AllShortestPathsAPI;
import com.baidu.hugegraph.api.traverser.CountAPI;
import com.baidu.hugegraph.api.traverser.CrosspointsAPI;
import com.baidu.hugegraph.api.traverser.CustomizedCrosspointsAPI;
import com.baidu.hugegraph.api.traverser.CustomizedPathsAPI;
Expand Down Expand Up @@ -82,6 +83,7 @@ public class BaseApiTest extends BaseClientTest {
protected static CrosspointsAPI crosspointsAPI;
protected static KoutAPI koutAPI;
protected static KneighborAPI kneighborAPI;
protected static CountAPI countAPI;
protected static RingsAPI ringsAPI;
protected static RaysAPI raysAPI;
protected static CustomizedPathsAPI customizedPathsAPI;
Expand Down Expand Up @@ -128,6 +130,7 @@ public static void init() {
crosspointsAPI = new CrosspointsAPI(client, GRAPH);
koutAPI = new KoutAPI(client, GRAPH);
kneighborAPI = new KneighborAPI(client, GRAPH);
countAPI = new CountAPI(client, GRAPH);
ringsAPI = new RingsAPI(client, GRAPH);
raysAPI = new RaysAPI(client, GRAPH);
customizedPathsAPI = new CustomizedPathsAPI(client, GRAPH);
Expand Down
Loading