Skip to content

Commit

Permalink
HugeGraph-1302: limit ids count in an IdQuery
Browse files Browse the repository at this point in the history
Change-Id: Ife925e5b6a216a23974b77a2af3d87cb9f34e2b9
  • Loading branch information
javeme authored and Linary committed Aug 9, 2018
1 parent 78e0b98 commit 30bbf0d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,25 @@
package com.baidu.hugegraph.backend.query;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.structure.HugeElement;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.InsertionOrderUtil;

public class IdQuery extends Query {

// The id(s) will be concated with `or`
private Set<Id> ids;
private Set<Id> ids = InsertionOrderUtil.newSet();

public IdQuery(HugeType resultType) {
super(resultType);
this.ids = new LinkedHashSet<>();
}

public IdQuery(HugeType resultType, Query originQuery) {
super(resultType, originQuery);
this.ids = new LinkedHashSet<>();
}

public IdQuery(HugeType resultType, Set<Id> ids) {
Expand Down Expand Up @@ -69,12 +67,13 @@ public Set<Id> ids() {
}

public void resetIds() {
this.ids = new LinkedHashSet<>();
this.ids = InsertionOrderUtil.newSet();
}

public IdQuery query(Id id) {
E.checkArgumentNotNull(id, "Query id can't be null");
this.ids.add(id);
this.checkCapacity(this.ids.size());
return this;
}

Expand All @@ -93,7 +92,7 @@ public boolean test(HugeElement element) {
@Override
public IdQuery copy() {
IdQuery query = (IdQuery) super.copy();
query.ids = new LinkedHashSet<>(this.ids);
query.ids = InsertionOrderUtil.newSet(this.ids);
return query;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ public void capacity(long capacity) {
this.capacity = capacity;
}

public void checkCapacity(long count) {
// Throw BackendException if reach capacity
if (this.capacity != Query.NO_CAPACITY && count > this.capacity) {
final int MAX_CHARS = 256;
String query = this.toString();
if (query.length() > MAX_CHARS) {
query = query.substring(0, MAX_CHARS) + "...";
}
throw new BackendException(
"Too many records(must <=%s) for a query: %s",
this.capacity, query);
}
}

public boolean showHidden() {
return this.showHidden;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Iterator;
import java.util.NoSuchElementException;

import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.exception.NotSupportException;
import com.baidu.hugegraph.iterator.Metadatable;
Expand Down Expand Up @@ -98,12 +97,7 @@ protected final long fetched() {

protected final void checkCapacity() {
// Stop if reach capacity
if (this.query.capacity() != Query.NO_CAPACITY &&
this.count > this.query.capacity()) {
throw new BackendException(
"Too many records(must <=%s) for a query",
this.query.capacity());
}
this.query.checkCapacity(this.count);
}

protected final boolean exceedLimit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,23 @@ public static <K, V> Map<K, V> newMap() {
return new LinkedHashMap<>();
}

public static <K, V> Map<K, V> newMap(Map<K, V> origin) {
return new LinkedHashMap<>(origin);
}

public static <V> Set<V> newSet() {
return new LinkedHashSet<>();
}

public static <V> Set<V> newSet(Set<V> origin) {
return new LinkedHashSet<>(origin);
}

public static <V> List<V> newList() {
return new ArrayList<>();
}

public static <V> List<V> newList(List<V> origin) {
return new ArrayList<>(origin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public static Object castNumber(Object object, Class<?> clazz) {

private static class DateSerializer extends StdSerializer<Date> {

DateSerializer() {
private static final long serialVersionUID = -6615155657857746161L;

public DateSerializer() {
super(Date.class);
}

Expand All @@ -116,7 +118,9 @@ public void serialize(Date date, JsonGenerator jsonGenerator,

private static class DateDeserializer extends StdDeserializer<Date> {

DateDeserializer() {
private static final long serialVersionUID = 1209944821349424949L;

public DateDeserializer() {
super(Date.class);
}

Expand Down

0 comments on commit 30bbf0d

Please sign in to comment.