[
https://issues.apache.org/jira/browse/GORA-377?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15213000#comment-15213000
]
ASF GitHub Bot commented on GORA-377:
-------------------------------------
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 :)
> Implement gora-metamodel module
> -------------------------------
>
> Key: GORA-377
> URL: https://issues.apache.org/jira/browse/GORA-377
> Project: Apache Gora
> Issue Type: New Feature
> Components: gora-metamodel
> Reporter: Lewis John McGibbney
> Labels: memex
> Fix For: 0.8
>
> Attachments: GORA-377_patch1.diff
>
>
> There have been recent discussions [0] around implementing MetaModel [1] in a
> drive to improving the Gora Query model and functionality.
> This issue is merely a placeholder for implementing exctly that.
> [0] http://www.mail-archive.com/dev%40gora.apache.org/msg05149.html
> [1] http://metamodel.incubator.apache.org/
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)