gora-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kaspersorensen <...@git.apache.org>
Subject [GitHub] gora pull request: GORA-377 Implement gora-metamodel module
Date Sat, 26 Mar 2016 13:02:06 GMT
Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/gora/pull/13#discussion_r57513312
  
    --- Diff: gora-metamodel/src/main/java/org/apache/gora/metamodel/query/MetaModelQuery.java
---
    @@ -0,0 +1,151 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.gora.metamodel.query;
    +
    +import org.apache.gora.metamodel.store.MetaModelStore;
    +import org.apache.gora.persistency.impl.PersistentBase;
    +import org.apache.gora.query.impl.QueryBase;
    +import org.apache.metamodel.DataContext;
    +import org.apache.metamodel.data.DataSet;
    +import org.apache.metamodel.query.FilterItem;
    +import org.apache.metamodel.query.LogicalOperator;
    +import org.apache.metamodel.query.OperatorType;
    +import org.apache.metamodel.query.Query;
    +import org.apache.metamodel.query.SelectItem;
    +import org.apache.metamodel.schema.Column;
    +import org.apache.metamodel.schema.Table;
    +
    +/**
    + * A {@link org.apache.gora.query.Query} implementation for
    + * {@link MetaModelStore}.
    + * 
    + * TODO: Gora Filters are not implemented. Not sure about how "local filters" work.
    + *
    + * @param <K>
    + * @param <T>
    + */
    +public final class MetaModelQuery<K, T extends PersistentBase> extends QueryBase<K,
T> {
    +
    +  public MetaModelQuery(MetaModelStore<K, T> store) {
    +    super(store);
    +  }
    +
    +  @Override
    +  public MetaModelStore<K, T> getDataStore() {
    +    // overridden to expose MetaModelDatastore
    +    return (MetaModelStore<K, T>) super.getDataStore();
    +  }
    +
    +  @Override
    +  public MetaModelResult<K, T> execute() {
    +    // overridden to expose MetaModelResult as return type
    +    final MetaModelStore<K, T> store = getDataStore();
    +    return store.execute(this);
    +  }
    +
    +  public Query toMetaModelQuery() {
    +    final MetaModelStore<K, T> store = getDataStore();
    +    final Table table = store.getTable();
    +
    +    final Query query = new Query();
    +    query.from(table);
    +
    +    final String[] fields = getFields();
    +    for (String fieldName : fields) {
    +      query.select(fieldName);
    +    }
    +
    +    final SelectItem primaryKeySelectItem = new SelectItem(store.getPrimaryKey(), query.getFromClause().getItem(0));
    +
    +    final K key = getKey();
    +    if (key != null) {
    +      query.where(primaryKeySelectItem, OperatorType.EQUALS_TO, key);
    +    } else {
    +      final K startKey = getStartKey();
    +      if (startKey != null) {
    +        final FilterItem filter = createGreaterThanOrEqualFilter(primaryKeySelectItem,
startKey);
    +        query.where(filter);
    +      }
    +
    +      final K endKey = getEndKey();
    +      if (endKey != null) {
    +        final FilterItem filter = createLessThanOrEqualFilter(primaryKeySelectItem, endKey);
    +        query.where(filter);
    +      }
    +    }
    +
    +    final long limit = getLimit();
    +    if (limit > 0) {
    +      query.setMaxRows((int) limit);
    +    }
    +
    +    final Column timestampColumn = store.getTimestampColumn();
    +    final long timestamp = getTimestamp();
    +    if (timestamp != -1) {
    +      if (timestampColumn == null) {
    +        throw new IllegalStateException("Cannot query by timestamp when no timestamp
Column is set");
    +      }
    +      query.where(timestampColumn, OperatorType.EQUALS_TO, timestamp);
    +    } else {
    +      final long startTime = getStartTime();
    +      final long endTime = getEndTime();
    +
    +      if (startTime != -1 || endTime != -1) {
    +        final SelectItem timestampSelectItem = new SelectItem(timestampColumn, query.getFromClause().getItem(0));
    +
    +        if (startTime != -1) {
    +          if (timestampColumn == null) {
    +            throw new IllegalStateException("Cannot query by timestamp when no timestamp
Column is set");
    +          }
    +          final FilterItem filter = createGreaterThanOrEqualFilter(timestampSelectItem,
startTime);
    +          query.where(filter);
    +        }
    +
    +        if (endTime != -1) {
    +          if (timestampColumn == null) {
    +            throw new IllegalStateException("Cannot query by timestamp when no timestamp
Column is set");
    +          }
    +          final FilterItem filter = createLessThanOrEqualFilter(timestampSelectItem,
endTime);
    +          query.where(filter);
    +        }
    +      }
    +    }
    +
    +    return query;
    +  }
    +
    +  private FilterItem createGreaterThanOrEqualFilter(SelectItem selectItem, Object operand)
{
    +    // TODO: In MetaModel 4.3 and onwards there will be a single
    +    // OperatorType for this. But for now we need a composite FilterItem.
    +    return new FilterItem(LogicalOperator.OR, new FilterItem(selectItem, OperatorType.EQUALS_TO,
operand),
    +        new FilterItem(selectItem, OperatorType.GREATER_THAN, operand));
    +  }
    +
    +  private FilterItem createLessThanOrEqualFilter(SelectItem selectItem, Object operand)
{
    +    // TODO: In MetaModel 4.3 and onwards there will be a single
    +    // OperatorType for this. But for now we need a composite FilterItem.
    --- End diff --
    
    At least this thing can now be implemented using `OperatorType.LESS_THAN_OR_EQUALS` if
you update the dependency :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message