Skip to content

Commit

Permalink
refact: fix some bugs & clean code (apache#1741)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
imbajin and imbajin authored Mar 21, 2022
1 parent 91f1cec commit b591f7d
Show file tree
Hide file tree
Showing 32 changed files with 82 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private static void checkUpdate(BatchVertexRequest req) {
E.checkArgument(req.updateStrategies != null &&
!req.updateStrategies.isEmpty(),
"Parameter 'update_strategies' can't be empty");
E.checkArgument(req.createIfNotExist == true,
E.checkArgument(req.createIfNotExist,
"Parameter 'create_if_not_exist' " +
"dose not support false now");
}
Expand Down Expand Up @@ -462,6 +462,7 @@ public Object[] properties() {
}
if (this.id != null) {
newProps[appendIndex++] = T.id;
// Keep value++ to avoid code trap
newProps[appendIndex++] = this.id;
}
return newProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public String post(@Context GraphManager manager,
if (request.withPath) {
paths.addAll(results.paths(request.limit));
}

Iterator<Vertex> iter = QueryResults.emptyIterator();
if (request.withVertex && !request.countOnly) {
Set<Id> ids = new HashSet<>(neighbors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private String inputPassword() {
String notEmptyPrompt = "The admin password can't be empty";
Console console = System.console();
while (true) {
String password = "";
String password;
if (console != null) {
char[] chars = console.readPassword(inputPrompt);
password = new String(chars);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ public void drop() {
*/
this.close();
} catch (Throwable e) {
LOG.warn("Failed to close graph {}", e, this);
LOG.warn("Failed to close graph {} {}", this, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ public boolean exists(Id id) {
Iterator<Vertex> vertices = this.tx().queryVertices(id);
if (vertices.hasNext()) {
Vertex vertex = vertices.next();
if (this.label.equals(vertex.label())) {
return true;
}
return this.label.equals(vertex.label());
}
return false;
}
Expand All @@ -145,7 +143,7 @@ protected List<T> toList(Iterator<Vertex> vertices) {
}

private Iterator<Vertex> queryById(List<Id> ids) {
Object[] idArray = ids.toArray(new Id[ids.size()]);
Object[] idArray = ids.toArray(new Id[0]);
return this.tx().queryVertices(idArray);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ public boolean filter(ResourceObject<?> resourceObject) {
private boolean filter(AuthElement element) {
assert this.type.match(element.type());
if (element instanceof Namifiable) {
if (!this.filter((Namifiable) element)) {
return false;
}
return this.filter((Namifiable) element);
}
return true;
}
Expand All @@ -149,10 +147,7 @@ private boolean filter(Namifiable element) {
assert !(element instanceof Typifiable) || this.type.match(
ResourceType.from(((Typifiable) element).type()));

if (!this.matchLabel(element.name())) {
return false;
}
return true;
return this.matchLabel(element.name());
}

private boolean filter(HugeElement element) {
Expand Down Expand Up @@ -193,10 +188,7 @@ private boolean matchLabel(String other) {
return false;
}
// It's ok if wildcard match or regular match
if (!this.label.equals(ANY) && !other.matches(this.label)) {
return false;
}
return true;
return this.label.equals(ANY) || other.matches(this.label);
}

private boolean matchProperties(Map<String, Object> other) {
Expand Down Expand Up @@ -229,10 +221,7 @@ protected boolean contains(HugeResource other) {
if (!this.matchLabel(other.label)) {
return false;
}
if (!this.matchProperties(other.properties)) {
return false;
}
return true;
return this.matchProperties(other.properties);
}

@Override
Expand Down Expand Up @@ -260,9 +249,7 @@ public static boolean allowed(ResourceObject<?> resourceObject) {
// Allowed to access system(hidden) schema by anyone
if (resourceObject.type().isSchema()) {
Namifiable schema = (Namifiable) resourceObject.operated();
if (Hidden.isHidden(schema.name())) {
return true;
}
return Hidden.isHidden(schema.name());
}

return false;
Expand Down Expand Up @@ -339,19 +326,19 @@ public HugeResource deserialize(JsonParser parser,
HugeResource res = new HugeResource();
while (parser.nextToken() != JsonToken.END_OBJECT) {
String key = parser.getCurrentName();
if (key.equals("type")) {
if ("type".equals(key)) {
if (parser.nextToken() != JsonToken.VALUE_NULL) {
res.type = ctxt.readValue(parser, ResourceType.class);
} else {
res.type = null;
}
} else if (key.equals("label")) {
} else if ("label".equals(key)) {
if (parser.nextToken() != JsonToken.VALUE_NULL) {
res.label = parser.getValueAsString();
} else {
res.label = null;
}
} else if (key.equals("properties")) {
} else if ("properties".equals(key)) {
if (parser.nextToken() != JsonToken.VALUE_NULL) {
@SuppressWarnings("unchecked")
Map<String, Object> prop = ctxt.readValue(parser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ public boolean exists(Id id) {
Iterator<Edge> edges = this.tx().queryEdges(id);
if (edges.hasNext()) {
Edge edge = edges.next();
if (this.label.equals(edge.label())) {
return true;
}
return this.label.equals(edge.label());
}
return false;
}
Expand Down Expand Up @@ -161,7 +159,7 @@ protected List<T> toList(Iterator<Edge> edges) {
}

private Iterator<Edge> queryById(List<Id> ids) {
Object[] idArray = ids.toArray(new Id[ids.size()]);
Object[] idArray = ids.toArray(new Id[0]);
return this.tx().queryEdges(idArray);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public final class LevelCache extends AbstractCache<Id, Object> {

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

@SuppressWarnings("unchecked")
public LevelCache(AbstractCache<Id, Object> lavel1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,13 @@ public static String escape(char splitor, char escape, String... values) {

public static String[] unescape(String id, String splitor, String escape) {
/*
* Note that the `splitor`/`escape` maybe special characters in regular
* Note that the `splitter`/`escape` maybe special characters in regular
* expressions, but this is a frequently called method, for faster
* execution, we forbid the use of special characters as delimiter
* or escape sign.
*
* The `limit` param -1 in split method can ensure empty string be
* splited to a part.
* split to a part.
*/
String[] parts = id.split("(?<!" + escape + ")" + splitor, -1);
for (int i = 0; i < parts.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public enum RelationType implements BiPredicate<Object, Object> {
assert v2 != null;
/*
* TODO: we still have no way to determine accurately, since
* some backends may scan with token(column) like cassandra.
* some backends may scan with token(column) like cassandra.
*/
return true;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class ConditionQuery extends IdQuery {
public static final String INDEX_SYM_EMPTY = "\u0002";
public static final char INDEX_SYM_MAX = '\u0003';

// Note: here we use "new String" to distinguish normal string code
public static final String INDEX_VALUE_NULL = new String("<null>");
public static final String INDEX_VALUE_EMPTY = new String("<empty>");

Expand All @@ -74,7 +75,7 @@ public class ConditionQuery extends IdQuery {

private static final List<Condition> EMPTY_CONDITIONS = ImmutableList.of();

// Conditions will be concated with `and` by default
// Conditions will be contacted with `and` by default
private List<Condition> conditions = EMPTY_CONDITIONS;

private OptimizedType optimizedType = OptimizedType.NONE;
Expand Down Expand Up @@ -659,8 +660,6 @@ public static String concatValues(Object value) {
return concatValues((List<?>) value);
} else if (needConvertNumber(value)) {
return LongEncoding.encodeNumber(value);
} else if (value == INDEX_VALUE_NULL) {
return INDEX_SYM_NULL;
} else {
return escapeSpecialValueIfNeeded(value.toString());
}
Expand All @@ -677,6 +676,8 @@ private static String escapeSpecialValueIfNeeded(String value) {
value = INDEX_SYM_EMPTY;
} else if (value == INDEX_VALUE_EMPTY) {
value = "";
} else if (value == INDEX_VALUE_NULL) {
value = INDEX_SYM_NULL;
} else {
char ch = value.charAt(0);
if (ch <= INDEX_SYM_MAX) {
Expand Down Expand Up @@ -822,8 +823,8 @@ private static boolean numberEquals(Object number1, Object number2) {
// Otherwise convert to BigDecimal to make two numbers comparable
Number n1 = NumericUtil.convertToNumber(number1);
Number n2 = NumericUtil.convertToNumber(number2);
BigDecimal b1 = new BigDecimal(n1.doubleValue());
BigDecimal b2 = new BigDecimal(n2.doubleValue());
BigDecimal b1 = BigDecimal.valueOf(n1.doubleValue());
BigDecimal b2 = BigDecimal.valueOf(n2.doubleValue());
return b1.compareTo(b2) == 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ public static List<ConditionQuery> flatten(ConditionQuery query,
continue;
}
ConditionQuery cq = newQueryFromRelations(query, relations);
if (cq != null) {
queries.add(cq);
}
queries.add(cq);
}

return queries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

public class Query implements Cloneable {

// TODO: we should better not use Long.Max as the unify limit number
public static final long NO_LIMIT = Long.MAX_VALUE;

public static final long COMMIT_BATCH = 500L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ public <T extends Idfiable> Iterator<T> keepInputOrderIfNeeded(
ids = map.keySet();
}

return new MapperIterator<>(ids.iterator(), id -> {
return map.get(id);
});
return new MapperIterator<>(ids.iterator(), map::get);
}

private boolean mustSortByInputIds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,7 @@ public boolean equals(Object obj) {
if (this.columns.size() != other.columns.size()) {
return false;
}
if (!this.columns.containsAll(other.columns)) {
return false;
}
return true;
return this.columns.containsAll(other.columns);
}

protected static final class BinaryId implements Id {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ public BytesBuffer writeBytes(byte[] bytes) {
public byte[] readBytes() {
int length = this.readVInt();
assert length >= 0;
byte[] bytes = this.read(length);
return bytes;
return this.read(length);
}

public BytesBuffer writeBigBytes(byte[] bytes) {
Expand All @@ -317,8 +316,7 @@ public BytesBuffer writeBigBytes(byte[] bytes) {
public byte[] readBigBytes() {
int length = this.readVInt();
assert length >= 0;
byte[] bytes = this.read(length);
return bytes;
return this.read(length);
}

public BytesBuffer writeStringRaw(String val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ public class SerializerFactory {

public static AbstractSerializer serializer(String name) {
name = name.toLowerCase();
if ("binary".equals(name)) {
return new BinarySerializer();
} else if ("binaryscatter".equals(name)) {
return new BinaryScatterSerializer();
} else if ("text".equals(name)) {
return new TextSerializer();
switch (name) {
case "binary":
return new BinarySerializer();
case "binaryscatter":
return new BinaryScatterSerializer();
case "text":
return new TextSerializer();
default:
}

Class<? extends AbstractSerializer> clazz = serializers.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public String toString() {
private final List<Row> subRows;

// NOTE: selfChanged is false when the row has not changed but subRows has.
private boolean selfChanged = true;
private boolean olap = false;
private boolean selfChanged;
private boolean olap;

public TableBackendEntry(Id id) {
this(null, id);
Expand All @@ -149,6 +149,7 @@ public TableBackendEntry(Row row) {
this.row = row;
this.subRows = new ArrayList<>();
this.selfChanged = true;
this.olap = false;
}

@Override
Expand Down Expand Up @@ -195,6 +196,7 @@ public void olap(boolean olap) {
this.olap = olap;
}

@Override
public boolean olap() {
return this.olap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void append(TextBackendEntry entry) {
}

// TODO: ensure the old value is a list and json format (for index)
if (oldValue.equals("[]")) {
if ("[]".equals(oldValue)) {
this.column(col.getKey(), newValue);
continue;
}
Expand Down Expand Up @@ -222,12 +222,11 @@ public void eliminate(TextBackendEntry entry) {
}

// TODO: ensure the old value is a list and json format (for index)
List<Object> values = new ArrayList<>();
@SuppressWarnings("unchecked")
List<Object> oldValues = JsonUtil.fromJson(oldValue, List.class);
@SuppressWarnings("unchecked")
List<Object> newValues = JsonUtil.fromJson(newValue, List.class);
values.addAll(oldValues);
List<Object> values = new ArrayList<>(oldValues);
values.removeAll(newValues);
// Update the old value
this.column(col.getKey(), JsonUtil.toJson(values));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ public boolean matched(Query query) {
for (Condition cond : cq.conditions()) {
if (cond.equals(BOTH_COND)) {
direction = Directions.BOTH;
break;
}
}
}
Expand Down
Loading

0 comments on commit b591f7d

Please sign in to comment.