IndexQuery Class Architecture

来源:互联网 发布:局域网分析软件 编辑:程序博客网 时间:2024/04/30 18:53
  • Query
    Standard Query interface specifying that a query may have a limit.
public interface Query {    public static final int NO_LIMIT = Integer.MAX_VALUE;    /**     * Whether this query has a defined limit     *     * @return     */    public boolean hasLimit();    /**     *     * @return The maximum number of results this query should return     */    public int getLimit();}
  • BackendQuery
    A BackendQuery is a query that can be updated to a new limit.
    This is useful in query execution where the query limit is successively relaxed to find all the needed elements of the result set.
public interface BackendQuery<Q extends BackendQuery> extends Query {    /**     * Creates a new query identical to the current one but with the specified limit.     *     * @param newLimit     * @return     */    public Q updateLimit(int newLimit);}

BaseQuery

public class BaseQuery implements Query {    private int limit;    public BaseQuery() {        this(NO_LIMIT);    }    public BaseQuery(final int limit) {        assert limit >= 0;        this.limit = limit;    }    /**     * Sets the limit of the query if it wasn't specified in the constructor     * @param limit     * @return     */    public BaseQuery setLimit(final int limit) {        assert limit >= 0;        this.limit = limit;        return this;    }    @Override    public int getLimit() {        return limit;    }    @Override    public boolean hasLimit() {        return limit != Query.NO_LIMIT;    }}
  • IndexQuery
/** * An external index query executed on an {@link IndexProvider}. * <p/> * A query is comprised of the store identifier against which the query ought to be executed and a query condition * which defines which entries match the query. * */public class IndexQuery extends BaseQuery implements BackendQuery<IndexQuery> {    public static final ImmutableList<OrderEntry> NO_ORDER = ImmutableList.of();    private final String store;    private final Condition condition;    private final ImmutableList<OrderEntry> orders;    private final int hashcode;    public IndexQuery(String store, Condition condition, ImmutableList<OrderEntry> orders, int limit) {        super(limit);        Preconditions.checkNotNull(store);        Preconditions.checkNotNull(condition);        Preconditions.checkArgument(orders != null);        Preconditions.checkArgument(QueryUtil.isQueryNormalForm(condition));        this.condition = condition;        this.orders = orders;        this.store = store;        this.hashcode = new HashCodeBuilder().append(condition).append(store).append(orders).append(limit).toHashCode();    }    public IndexQuery(String store, Condition condition, ImmutableList<OrderEntry> orders) {        this(store, condition, orders, Query.NO_LIMIT);    }    public IndexQuery(String store, Condition condition) {        this(store, condition, NO_ORDER, Query.NO_LIMIT);    }    public IndexQuery(String store, Condition condition, int limit) {        this(store, condition, NO_ORDER, limit);    }    public Condition<HugeGraphElement> getCondition() {        return condition;    }    public List<OrderEntry> getOrder() {        return orders;    }    public String getStore() {        return store;    }    @Override    public IndexQuery setLimit(int limit) {        throw new UnsupportedOperationException();    }    @Override    public IndexQuery updateLimit(int newLimit) {        return new IndexQuery(store, condition, orders, newLimit);    }    @Override    public int hashCode() {        return hashcode;    }    @Override    public boolean equals(Object other) {        if (this == other) return true;        else if (other == null) return false;        else if (!getClass().isInstance(other)) return false;        IndexQuery oth = (IndexQuery) other;        return store.equals(oth.store) && orders.equals(oth.orders)                && condition.equals(oth.condition) && getLimit() == oth.getLimit();    }    @Override    public String toString() {        StringBuilder b = new StringBuilder();        b.append("[").append(condition.toString()).append("]");        if (!orders.isEmpty()) b.append(orders);        if (hasLimit()) b.append("(").append(getLimit()).append(")");        b.append(":").append(store);        return b.toString();    }}

IndexQuery class has static inner class OrderEntry.

public static class OrderEntry {        private final String key;        private final Order order;        private final Class<?> datatype;        public OrderEntry(String key, Order order, Class<?> datatype) {            Preconditions.checkNotNull(key);            Preconditions.checkNotNull(order);            Preconditions.checkNotNull(datatype);            this.key = key;            this.order = order;            this.datatype = datatype;        }        public String getKey() {            return key;        }        public Order getOrder() {            return order;        }        public Class<?> getDatatype() {            return datatype;        }        @Override        public int hashCode() {            return key.hashCode() * 4021 + order.hashCode();        }        @Override        public boolean equals(Object oth) {            if (this == oth) return true;            else if (oth == null) return false;            else if (!getClass().isInstance(oth)) return false;            OrderEntry o = (OrderEntry) oth;            return key.equals(o.key) && order == o.order;        }        @Override        public String toString() {            return order + "(" + key + ")";        }    }
0 0