-
Notifications
You must be signed in to change notification settings - Fork 45
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/com/baidu/hugegraph/api/traverser/CountAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
196 changes: 196 additions & 0 deletions
196
src/main/java/com/baidu/hugegraph/api/traverser/structure/CountRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
count()