Search in sources :

Example 1 with QueryBuilder

use of org.infinispan.avro.hotrod.QueryBuilder in project gora by apache.

the class InfinispanQuery method build.

public void build() {
    LOG.debug("build()");
    FilterConditionContext context = null;
    if (q != null) {
        LOG.trace("Query already built; ignoring.");
        return;
    }
    QueryBuilder qb = ((InfinispanStore<K, T>) dataStore).getClient().getQueryBuilder();
    if (filter instanceof MapFieldValueFilter) {
        MapFieldValueFilter mfilter = (MapFieldValueFilter) filter;
        if (!(mfilter.getMapKey() instanceof Utf8))
            throw new IllegalAccessError("Invalid map key, must be a string.");
        if (mfilter.getOperands().size() > 1)
            throw new IllegalAccessError("MapFieldValueFilter operand not supported.");
        if (!(mfilter.getOperands().get(0) instanceof String))
            throw new IllegalAccessError("Invalid operand, must be a string.");
        String value = mfilter.getMapKey() + Support.DELIMITER + mfilter.getOperands().get(0).toString();
        switch(mfilter.getFilterOp()) {
            case EQUALS:
                if (value.equals("*")) {
                    context = qb.having(mfilter.getFieldName()).like(value);
                } else {
                    context = qb.having(mfilter.getFieldName()).eq(value);
                }
                if (!((MapFieldValueFilter) filter).isFilterIfMissing()) {
                    LOG.warn("Forcing isFilterMissing to true");
                    ((MapFieldValueFilter) filter).setFilterIfMissing(true);
                }
                break;
            case NOT_EQUALS:
                if (value.equals("*")) {
                    context = qb.not().having(mfilter.getFieldName()).like(value);
                } else {
                    context = qb.not().having(mfilter.getFieldName()).eq(value);
                }
                if (!((MapFieldValueFilter) filter).isFilterIfMissing()) {
                    LOG.warn("Forcing isFilterMissing to false");
                    ((MapFieldValueFilter) filter).setFilterIfMissing(false);
                }
                break;
            default:
                throw new IllegalAccessError("FilterOp not supported..");
        }
    } else if (filter instanceof SingleFieldValueFilter) {
        SingleFieldValueFilter sfilter = (SingleFieldValueFilter) filter;
        if (sfilter.getOperands().size() > 1)
            throw new IllegalAccessError("SingleFieldValueFilter operand not supported.");
        Object value = sfilter.getOperands().get(0);
        switch(sfilter.getFilterOp()) {
            case EQUALS:
                if (value.equals("*")) {
                    context = qb.having(sfilter.getFieldName()).like((String) value);
                } else {
                    context = qb.having(sfilter.getFieldName()).eq(value);
                }
                break;
            case NOT_EQUALS:
                if (value.equals("*")) {
                    context = qb.not().having(sfilter.getFieldName()).like((String) value);
                } else {
                    context = qb.not().having(sfilter.getFieldName()).eq(value);
                }
                break;
            case LESS:
                context = qb.having(sfilter.getFieldName()).lt(value);
                break;
            case LESS_OR_EQUAL:
                context = qb.having(sfilter.getFieldName()).lte(value);
                break;
            case GREATER:
                context = qb.having(sfilter.getFieldName()).gt(value);
                break;
            case GREATER_OR_EQUAL:
                context = qb.having(sfilter.getFieldName()).gte(value);
                break;
            default:
                throw new IllegalAccessError("FilterOp not supported..");
        }
    } else if (filter != null) {
        throw new IllegalAccessError("Filter not supported.");
    }
    if (this.startKey == this.endKey && this.startKey != null) {
        (context == null ? qb : context.and()).having(getPrimaryFieldName()).eq(this.startKey);
    } else {
        if (this.startKey != null && this.endKey != null)
            context = (context == null ? qb : context.and()).having(getPrimaryFieldName()).between(this.startKey, this.endKey);
        else if (this.startKey != null)
            context = (context == null ? qb : context.and()).having(getPrimaryFieldName()).between(this.startKey, null);
        else if (this.endKey != null)
            (context == null ? qb : context.and()).having(getPrimaryFieldName()).between(null, this.endKey);
    }
    // if projection enabled, keep the primary field.
    if (fields != null && fields.length > 0) {
        String[] fieldsWithPrimary;
        List<String> fieldsList = new ArrayList<>(Arrays.asList(fields));
        if (!fieldsList.contains(getPrimaryFieldName())) {
            fieldsWithPrimary = Arrays.copyOf(fields, fields.length + 1);
            fieldsWithPrimary[fields.length] = getPrimaryFieldName();
        } else {
            fieldsWithPrimary = fieldsList.toArray(new String[] {});
        }
        qb.setProjection(fieldsWithPrimary);
    }
    qb.orderBy((getSortingField().equals("")) ? getPrimaryFieldName() : getSortingField(), isAscendant ? SortOrder.ASC : SortOrder.DESC);
    if (this.getOffset() >= 0)
        qb.startOffset(this.getOffset());
    if (this.getLimit() > 0)
        qb.maxResults((int) this.getLimit());
    q = (RemoteQuery) qb.build();
    if (location != null)
        q.setLocation(location);
}
Also used : ArrayList(java.util.ArrayList) QueryBuilder(org.infinispan.avro.hotrod.QueryBuilder) FilterConditionContext(org.infinispan.query.dsl.FilterConditionContext) SingleFieldValueFilter(org.apache.gora.filter.SingleFieldValueFilter) Utf8(org.apache.avro.util.Utf8) MapFieldValueFilter(org.apache.gora.filter.MapFieldValueFilter)

Example 2 with QueryBuilder

use of org.infinispan.avro.hotrod.QueryBuilder in project gora by apache.

the class InfinispanQuery method split.

public List<PartitionQuery<K, T>> split() {
    LOG.debug("split()");
    if (!isBuilt())
        build();
    List<PartitionQuery<K, T>> splits = new ArrayList<>();
    QueryBuilder qb = ((InfinispanStore<K, T>) dataStore).getClient().getQueryBuilder();
    Collection<RemoteQuery> Queries = qb.split(this.q);
    for (RemoteQuery Query : Queries) {
        InfinispanQuery<K, T> split = (InfinispanQuery<K, T>) this.clone();
        split.q = Query;
        split.location = Query.getLocation();
        splits.add(split);
    }
    LOG.trace(splits.toString());
    return splits;
}
Also used : ArrayList(java.util.ArrayList) QueryBuilder(org.infinispan.avro.hotrod.QueryBuilder) RemoteQuery(org.infinispan.avro.hotrod.RemoteQuery) PartitionQuery(org.apache.gora.query.PartitionQuery)

Aggregations

ArrayList (java.util.ArrayList)2 QueryBuilder (org.infinispan.avro.hotrod.QueryBuilder)2 Utf8 (org.apache.avro.util.Utf8)1 MapFieldValueFilter (org.apache.gora.filter.MapFieldValueFilter)1 SingleFieldValueFilter (org.apache.gora.filter.SingleFieldValueFilter)1 PartitionQuery (org.apache.gora.query.PartitionQuery)1 RemoteQuery (org.infinispan.avro.hotrod.RemoteQuery)1 FilterConditionContext (org.infinispan.query.dsl.FilterConditionContext)1