Skip to content

Commit b591f7d

Browse files
authored
refact: fix some bugs & clean code (#1741)
* feat(api): Support adamic-adar & resource-allocation algorithms * split into 2 class * enhance a string of code logic * keep NO_LIMIT now * Update EdgeStep.java Co-authored-by: imbajin <[email protected]>
1 parent 91f1cec commit b591f7d

32 files changed

+82
-112
lines changed

hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/VertexAPI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ private static void checkUpdate(BatchVertexRequest req) {
405405
E.checkArgument(req.updateStrategies != null &&
406406
!req.updateStrategies.isEmpty(),
407407
"Parameter 'update_strategies' can't be empty");
408-
E.checkArgument(req.createIfNotExist == true,
408+
E.checkArgument(req.createIfNotExist,
409409
"Parameter 'create_if_not_exist' " +
410410
"dose not support false now");
411411
}
@@ -462,6 +462,7 @@ public Object[] properties() {
462462
}
463463
if (this.id != null) {
464464
newProps[appendIndex++] = T.id;
465+
// Keep value++ to avoid code trap
465466
newProps[appendIndex++] = this.id;
466467
}
467468
return newProps;

hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KoutAPI.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public String post(@Context GraphManager manager,
154154
if (request.withPath) {
155155
paths.addAll(results.paths(request.limit));
156156
}
157+
157158
Iterator<Vertex> iter = QueryResults.emptyIterator();
158159
if (request.withVertex && !request.countOnly) {
159160
Set<Id> ids = new HashSet<>(neighbors);

hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private String inputPassword() {
8282
String notEmptyPrompt = "The admin password can't be empty";
8383
Console console = System.console();
8484
while (true) {
85-
String password = "";
85+
String password;
8686
if (console != null) {
8787
char[] chars = console.readPassword(inputPrompt);
8888
password = new String(chars);

hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ public void drop() {
933933
*/
934934
this.close();
935935
} catch (Throwable e) {
936-
LOG.warn("Failed to close graph {}", e, this);
936+
LOG.warn("Failed to close graph {} {}", this, e);
937937
}
938938
}
939939

hugegraph-core/src/main/java/com/baidu/hugegraph/auth/EntityManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ public boolean exists(Id id) {
118118
Iterator<Vertex> vertices = this.tx().queryVertices(id);
119119
if (vertices.hasNext()) {
120120
Vertex vertex = vertices.next();
121-
if (this.label.equals(vertex.label())) {
122-
return true;
123-
}
121+
return this.label.equals(vertex.label());
124122
}
125123
return false;
126124
}
@@ -145,7 +143,7 @@ protected List<T> toList(Iterator<Vertex> vertices) {
145143
}
146144

147145
private Iterator<Vertex> queryById(List<Id> ids) {
148-
Object[] idArray = ids.toArray(new Id[ids.size()]);
146+
Object[] idArray = ids.toArray(new Id[0]);
149147
return this.tx().queryVertices(idArray);
150148
}
151149

hugegraph-core/src/main/java/com/baidu/hugegraph/auth/HugeResource.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ public boolean filter(ResourceObject<?> resourceObject) {
138138
private boolean filter(AuthElement element) {
139139
assert this.type.match(element.type());
140140
if (element instanceof Namifiable) {
141-
if (!this.filter((Namifiable) element)) {
142-
return false;
143-
}
141+
return this.filter((Namifiable) element);
144142
}
145143
return true;
146144
}
@@ -149,10 +147,7 @@ private boolean filter(Namifiable element) {
149147
assert !(element instanceof Typifiable) || this.type.match(
150148
ResourceType.from(((Typifiable) element).type()));
151149

152-
if (!this.matchLabel(element.name())) {
153-
return false;
154-
}
155-
return true;
150+
return this.matchLabel(element.name());
156151
}
157152

158153
private boolean filter(HugeElement element) {
@@ -193,10 +188,7 @@ private boolean matchLabel(String other) {
193188
return false;
194189
}
195190
// It's ok if wildcard match or regular match
196-
if (!this.label.equals(ANY) && !other.matches(this.label)) {
197-
return false;
198-
}
199-
return true;
191+
return this.label.equals(ANY) || other.matches(this.label);
200192
}
201193

202194
private boolean matchProperties(Map<String, Object> other) {
@@ -229,10 +221,7 @@ protected boolean contains(HugeResource other) {
229221
if (!this.matchLabel(other.label)) {
230222
return false;
231223
}
232-
if (!this.matchProperties(other.properties)) {
233-
return false;
234-
}
235-
return true;
224+
return this.matchProperties(other.properties);
236225
}
237226

238227
@Override
@@ -260,9 +249,7 @@ public static boolean allowed(ResourceObject<?> resourceObject) {
260249
// Allowed to access system(hidden) schema by anyone
261250
if (resourceObject.type().isSchema()) {
262251
Namifiable schema = (Namifiable) resourceObject.operated();
263-
if (Hidden.isHidden(schema.name())) {
264-
return true;
265-
}
252+
return Hidden.isHidden(schema.name());
266253
}
267254

268255
return false;
@@ -339,19 +326,19 @@ public HugeResource deserialize(JsonParser parser,
339326
HugeResource res = new HugeResource();
340327
while (parser.nextToken() != JsonToken.END_OBJECT) {
341328
String key = parser.getCurrentName();
342-
if (key.equals("type")) {
329+
if ("type".equals(key)) {
343330
if (parser.nextToken() != JsonToken.VALUE_NULL) {
344331
res.type = ctxt.readValue(parser, ResourceType.class);
345332
} else {
346333
res.type = null;
347334
}
348-
} else if (key.equals("label")) {
335+
} else if ("label".equals(key)) {
349336
if (parser.nextToken() != JsonToken.VALUE_NULL) {
350337
res.label = parser.getValueAsString();
351338
} else {
352339
res.label = null;
353340
}
354-
} else if (key.equals("properties")) {
341+
} else if ("properties".equals(key)) {
355342
if (parser.nextToken() != JsonToken.VALUE_NULL) {
356343
@SuppressWarnings("unchecked")
357344
Map<String, Object> prop = ctxt.readValue(parser,

hugegraph-core/src/main/java/com/baidu/hugegraph/auth/RelationshipManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ public boolean exists(Id id) {
122122
Iterator<Edge> edges = this.tx().queryEdges(id);
123123
if (edges.hasNext()) {
124124
Edge edge = edges.next();
125-
if (this.label.equals(edge.label())) {
126-
return true;
127-
}
125+
return this.label.equals(edge.label());
128126
}
129127
return false;
130128
}
@@ -161,7 +159,7 @@ protected List<T> toList(Iterator<Edge> edges) {
161159
}
162160

163161
private Iterator<Edge> queryById(List<Id> ids) {
164-
Object[] idArray = ids.toArray(new Id[ids.size()]);
162+
Object[] idArray = ids.toArray(new Id[0]);
165163
return this.tx().queryEdges(idArray);
166164
}
167165

hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/LevelCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
public final class LevelCache extends AbstractCache<Id, Object> {
3030

3131
// For multi-layer caches
32-
private final AbstractCache<Id, Object> caches[];
32+
private final AbstractCache<Id, Object>[] caches;
3333

3434
@SuppressWarnings("unchecked")
3535
public LevelCache(AbstractCache<Id, Object> lavel1,

hugegraph-core/src/main/java/com/baidu/hugegraph/backend/id/IdUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,13 @@ public static String escape(char splitor, char escape, String... values) {
134134

135135
public static String[] unescape(String id, String splitor, String escape) {
136136
/*
137-
* Note that the `splitor`/`escape` maybe special characters in regular
137+
* Note that the `splitter`/`escape` maybe special characters in regular
138138
* expressions, but this is a frequently called method, for faster
139139
* execution, we forbid the use of special characters as delimiter
140140
* or escape sign.
141+
*
141142
* The `limit` param -1 in split method can ensure empty string be
142-
* splited to a part.
143+
* split to a part.
143144
*/
144145
String[] parts = id.split("(?<!" + escape + ")" + splitor, -1);
145146
for (int i = 0; i < parts.length; i++) {

hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public enum RelationType implements BiPredicate<Object, Object> {
107107
assert v2 != null;
108108
/*
109109
* TODO: we still have no way to determine accurately, since
110-
* some backends may scan with token(column) like cassandra.
110+
* some backends may scan with token(column) like cassandra.
111111
*/
112112
return true;
113113
});

0 commit comments

Comments
 (0)