diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Condition.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Condition.java index 3ef9cd5fa0..09e223e4ca 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Condition.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Condition.java @@ -137,6 +137,12 @@ public enum RelationType implements BiPredicate { return true; }); + private static final Set RANGE_TYPES = + ImmutableSet.of(GT, GTE, LT, LTE); + + private static final Set UNFLATTEN_TYPES = + ImmutableSet.of(IN, NOT_IN, TEXT_CONTAINS_ANY); + private final String operator; private final BiFunction tester; private final Class v1Class; @@ -261,7 +267,7 @@ public boolean test(Object first, Object second) { } public boolean isRangeType() { - return ImmutableSet.of(GT, GTE, LT, LTE).contains(this); + return RANGE_TYPES.contains(this); } public boolean isSearchType() { @@ -628,10 +634,6 @@ public abstract static class Relation extends Condition { // The value serialized(code/string) by backend store. protected Object serialValue; - protected static final Set UNFLATTEN_RELATION_TYPES = - ImmutableSet.of(RelationType.IN, RelationType.NOT_IN, - RelationType.TEXT_CONTAINS_ANY); - @Override public ConditionType type() { return ConditionType.RELATION; @@ -672,7 +674,7 @@ public boolean test(Object value) { @Override public boolean isFlattened() { - return !UNFLATTEN_RELATION_TYPES.contains(this.relation); + return !RelationType.UNFLATTEN_TYPES.contains(this.relation); } @Override diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Query.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Query.java index 3a024283ff..1816bff237 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Query.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Query.java @@ -180,7 +180,9 @@ public long offset() { } public void offset(long offset) { - E.checkArgument(offset >= 0L, "Invalid offset %s", offset); + if (offset < 0L) { + E.checkArgument(false, "Invalid offset %s", offset); + } this.offset = offset; } @@ -202,7 +204,9 @@ public void resetActualOffset() { } public long goOffset(long offset) { - E.checkArgument(offset >= 0L, "Invalid offset value: %s", offset); + if (offset < 0L) { + E.checkArgument(false, "Invalid offset value: %s", offset); + } if (this.originQuery != null) { this.goParentOffset(offset); } @@ -250,9 +254,11 @@ public Set skipOffsetIfNeeded(Set elems) { } else if (fromIndex > 0L) { this.goOffset(fromIndex); } - E.checkArgument(fromIndex <= Integer.MAX_VALUE, - "Offset must be <= 0x7fffffff, but got '%s'", - fromIndex); + if (fromIndex > Integer.MAX_VALUE) { + E.checkArgument(false, + "Offset must be <= 0x7fffffff, but got '%s'", + fromIndex); + } if (fromIndex >= elems.size()) { return ImmutableSet.of(); @@ -287,17 +293,27 @@ public long total() { public long limit() { if (this.capacity != NO_CAPACITY) { - E.checkArgument(this.limit == Query.NO_LIMIT || - this.limit <= this.capacity, - "Invalid limit %s, must be <= capacity(%s)", - this.limit, this.capacity); + if (this.limit == NO_LIMIT) { + /* + * TODO: may need to return this.capacity here. + * In most cases, capacity == DEFAULT_CAPACITY, then limit() + * will return NO_LIMIT, thus rely on Query.checkCapacity() + * to check in the next step. + */ + } + if (this.limit != NO_LIMIT && this.limit > this.capacity) { + E.checkArgument(false, + "Invalid limit %s, must be <= capacity(%s)", + this.limit, this.capacity); + } } return this.limit; } public void limit(long limit) { - E.checkArgument(limit >= 0L || limit == NO_LIMIT, - "Invalid limit %s", limit); + if (limit != NO_LIMIT && limit < 0L) { + E.checkArgument(false, "Invalid limit %s", limit); + } this.limit = limit; } @@ -337,8 +353,9 @@ public long range(long start, long end) { } else { assert end < Query.NO_LIMIT; } - E.checkArgument(end >= start, - "Invalid range: [%s, %s)", start, end); + if (end < start) { + E.checkArgument(false, "Invalid range: [%s, %s)", start, end); + } this.limit(end - start); } else { // Keep the origin limit @@ -349,11 +366,15 @@ public long range(long start, long end) { public String page() { if (this.page != null) { - E.checkState(this.limit() != 0L, - "Can't set limit=0 when using paging"); - E.checkState(this.offset() == 0L, - "Can't set offset when using paging, but got '%s'", - this.offset()); + if (this.limit() == 0L) { + E.checkState(false, + "Can't set limit=0 when using paging"); + } + if (this.offset() != 0L) { + E.checkState(false, + "Can't set offset when using paging, but got '%s'", + this.offset()); + } } return this.page; } @@ -409,7 +430,7 @@ public boolean bigCapacity() { public void checkCapacity(long count) throws LimitExceedException { // Throw LimitExceedException if reach capacity - if (this.capacity != Query.NO_CAPACITY && count > this.capacity) { + if (this.capacity != NO_CAPACITY && count > this.capacity) { final int MAX_CHARS = 256; String query = this.toString(); if (query.length() > MAX_CHARS) { @@ -426,8 +447,10 @@ public Aggregate aggregate() { } public Aggregate aggregateNotNull() { - E.checkArgument(this.aggregate != null, - "The aggregate must be set for number query"); + if (this.aggregate == null) { + E.checkArgument(false, + "The aggregate must be set for number query"); + } return this.aggregate; } diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java index 2c890ebe46..fb52347677 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java @@ -91,8 +91,11 @@ public BytesBuffer() { } public BytesBuffer(int capacity) { - E.checkArgument(capacity <= MAX_BUFFER_CAPACITY, - "Capacity exceeds max buffer capacity: %s", MAX_BUFFER_CAPACITY); + if (capacity > MAX_BUFFER_CAPACITY) { + E.checkArgument(false, + "Capacity %s exceeds max buffer capacity: %s", + capacity, MAX_BUFFER_CAPACITY); + } this.buffer = ByteBuffer.allocate(capacity); this.resize = true; } @@ -162,16 +165,21 @@ public int remaining() { private void require(int size) { // Does need to resize? - if (this.buffer.limit() - this.buffer.position() >= size) { + if (this.buffer.remaining() >= size) { return; } // Can't resize for wrapped buffer since will change the origin ref - E.checkState(this.resize, "Can't resize for wrapped buffer"); + if (!this.resize) { + E.checkState(false, "Can't resize for wrapped buffer"); + } // Extra capacity as buffer int newCapacity = size + this.buffer.limit() + DEFAULT_CAPACITY; - E.checkArgument(newCapacity <= MAX_BUFFER_CAPACITY, - "Capacity exceeds max buffer capacity: %s", MAX_BUFFER_CAPACITY); + if (newCapacity > MAX_BUFFER_CAPACITY) { + E.checkArgument(false, + "Capacity %s exceeds max buffer capacity: %s", + newCapacity, MAX_BUFFER_CAPACITY); + } ByteBuffer newBuffer = ByteBuffer.allocate(newCapacity); this.buffer.flip(); newBuffer.put(this.buffer); @@ -249,7 +257,7 @@ public byte peek() { } public byte peekLast() { - return this.buffer.get(this.buffer.capacity() - 1); + return this.buffer.get(this.buffer.limit() - 1); } public byte read() { @@ -291,8 +299,11 @@ public double readDouble() { } public BytesBuffer writeBytes(byte[] bytes) { - E.checkArgument(bytes.length <= BYTES_LEN_MAX, "The max length of bytes is %s, but got %s", - BYTES_LEN_MAX, bytes.length); + if (bytes.length > BYTES_LEN_MAX) { + E.checkArgument(false, + "The max length of bytes is %s, but got %s", + BYTES_LEN_MAX, bytes.length); + } require(BYTES_LEN + bytes.length); this.writeVInt(bytes.length); this.write(bytes); @@ -306,9 +317,12 @@ public byte[] readBytes() { } public BytesBuffer writeBigBytes(byte[] bytes) { - // TODO: note the max blob size should be 128MB (due to MAX_BUFFER_CAPACITY) - E.checkArgument(bytes.length <= BLOB_LEN_MAX, "The max length of bytes is %s, but got %s", - BLOB_LEN_MAX, bytes.length); + if (bytes.length > BLOB_LEN_MAX) { + // TODO: note the max blob size should be 128MB (due to MAX_BUFFER_CAPACITY) + E.checkArgument(false, + "The max length of bytes is %s, but got %s", + BLOB_LEN_MAX, bytes.length); + } require(BLOB_LEN + bytes.length); this.writeVInt(bytes.length); this.write(bytes); @@ -430,8 +444,11 @@ public BytesBuffer writeVInt(int value) { public int readVInt() { byte leading = this.read(); - E.checkArgument(leading != 0x80, "Unexpected varint with leading byte '0x%s'", - Bytes.toHex(leading)); + if (leading == 0x80) { + E.checkArgument(false, + "Unexpected varint with leading byte '0x%s'", + Bytes.toHex(leading)); + } int value = leading & 0x7f; if (leading >= 0) { assert (leading & 0x80) == 0; @@ -449,11 +466,16 @@ public int readVInt() { } } - E.checkArgument(i < 5, "Unexpected varint %s with too many bytes(%s)", - value, i + 1); - E.checkArgument(i < 4 || (leading & 0x70) == 0, - "Unexpected varint %s with leading byte '0x%s'", - value, Bytes.toHex(leading)); + if (i >= 5) { + E.checkArgument(false, + "Unexpected varint %s with too many bytes(%s)", + value, i + 1); + } + if (i >= 4 && (leading & 0x70) != 0) { + E.checkArgument(false, + "Unexpected varint %s with leading byte '0x%s'", + value, Bytes.toHex(leading)); + } return value; } @@ -492,8 +514,11 @@ public BytesBuffer writeVLong(long value) { public long readVLong() { byte leading = this.read(); - E.checkArgument(leading != 0x80, "Unexpected varlong with leading byte '0x%s'", - Bytes.toHex(leading)); + if (leading == 0x80) { + E.checkArgument(false, + "Unexpected varlong with leading byte '0x%s'", + Bytes.toHex(leading)); + } long value = leading & 0x7fL; if (leading >= 0) { assert (leading & 0x80) == 0; @@ -511,11 +536,16 @@ public long readVLong() { } } - E.checkArgument(i < 10, "Unexpected varlong %s with too many bytes(%s)", - value, i + 1); - E.checkArgument(i < 9 || (leading & 0x7e) == 0, - "Unexpected varlong %s with leading byte '0x%s'", - value, Bytes.toHex(leading)); + if (i >= 10) { + E.checkArgument(false, + "Unexpected varlong %s with too many bytes(%s)", + value, i + 1); + } + if (i >= 9 && (leading & 0x7e) != 0) { + E.checkArgument(false, + "Unexpected varlong %s with leading byte '0x%s'", + value, Bytes.toHex(leading)); + } return value; } @@ -648,9 +678,13 @@ public BytesBuffer writeId(Id id) { // String Id (VertexID) bytes = id.asBytes(); int len = bytes.length; - E.checkArgument(len > 0, "Can't write empty id"); - E.checkArgument(len <= ID_LEN_MAX, "Big id max length is %s, but got %s {%s}", - ID_LEN_MAX, len, id); + if (len <= 0) { + E.checkArgument(false, "Can't write empty id"); + } + if (len > ID_LEN_MAX) { + E.checkArgument(false, "Big id max length is %s, but got %s {%s}", + ID_LEN_MAX, len, id); + } len -= 1; // mapping [1, 16384] to [0, 16383] if (len <= 0x3f) { // If length is <= 63, use a single byte with the highest bit set to 1 @@ -721,7 +755,9 @@ public BytesBuffer writeIndexId(Id id, HugeType type) { public BytesBuffer writeIndexId(Id id, HugeType type, boolean withEnding) { byte[] bytes = id.asBytes(); int len = bytes.length; - E.checkArgument(len > 0, "Can't write empty id"); + if (len == 0) { + E.checkArgument(false, "Can't write empty id"); + } this.write(bytes); if (type.isStringIndex()) { @@ -873,8 +909,11 @@ private void writeNumber(long val) { } private long readNumber(byte b) { - E.checkArgument((b & 0x80) == 0, "Not a number type with prefix byte '0x%s'", - Bytes.toHex(b)); + if ((b & 0x80) != 0) { + E.checkArgument(false, + "Not a number type with prefix byte '0x%s'", + Bytes.toHex(b)); + } // Parse the kind from byte 0kkksxxx int kind = b >>> 4; boolean positive = (b & 0x08) > 0; @@ -930,7 +969,11 @@ private byte[] readBytesWithEnding() { break; } } - E.checkArgument(foundEnding, "Not found ending '0x%s'", Bytes.toHex(STRING_ENDING_BYTE)); + if (!foundEnding) { + E.checkArgument(false, + "Not found ending '0x%s'", + Bytes.toHex(STRING_ENDING_BYTE)); + } int end = this.buffer.position() - 1; int len = end - start; if (len <= 0) {