ignite-sql - wip
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/4f5b36bb Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/4f5b36bb Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/4f5b36bb Branch: refs/heads/ignite-sql Commit: 4f5b36bb98e9f0fe762f4472ac4e1a9cff101da1 Parents: d94d98f Author: S.Vladykin <[email protected]> Authored: Tue Feb 3 17:04:17 2015 +0300 Committer: S.Vladykin <[email protected]> Committed: Tue Feb 3 17:04:17 2015 +0300 ---------------------------------------------------------------------- .../java/org/apache/ignite/IgniteCache.java | 15 +--- .../apache/ignite/cache/query/QueryCursor.java | 2 +- .../ignite/cache/query/QueryPredicate.java | 7 +- .../ignite/cache/query/QueryScanPredicate.java | 2 +- .../ignite/cache/query/QuerySpiPredicate.java | 2 +- .../processors/cache/IgniteCacheProxy.java | 50 ++++++++---- .../processors/cache/QueryCursorImpl.java | 86 ++++++++++++++++++++ .../processors/query/GridQueryIndexing.java | 12 +++ .../processors/query/h2/QueryCursorImpl.java | 85 ------------------- .../h2/twostep/GridReduceQueryExecutor.java | 1 + 10 files changed, 142 insertions(+), 120 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/core/src/main/java/org/apache/ignite/IgniteCache.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java index ca88d40..479aaed 100644 --- a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java +++ b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java @@ -44,21 +44,8 @@ import java.util.concurrent.locks.*; * These methods don't specify any keys to load, and leave it to the underlying storage to load cache * data based on the optionally passed in arguments. * </li> - * <li> - * Method {@link #affinity()} provides {@link org.apache.ignite.cache.affinity.CacheAffinityFunction} service for information on - * data partitioning and mapping keys to grid nodes responsible for caching those keys. - * </li> - * <li> - * Method {@link #dataStructures()} provides {@link org.apache.ignite.cache.datastructures.CacheDataStructures} service for - * creating and working with distributed concurrent data structures, such as - * {@link IgniteAtomicLong}, {@link IgniteAtomicReference}, {@link org.apache.ignite.cache.datastructures.CacheQueue}, etc. - * </li> - * <li> - * Methods like {@code 'tx{Un}Synchronize(..)'} witch allow to get notifications for transaction state changes. - * This feature is very useful when integrating cache transactions with some other in-house transactions. - * </li> + * <li>Various {@code 'query(..)'} methods to allow cache data querying.</li> * <li>Method {@link #metrics()} to provide metrics for the whole cache.</li> - * <li>Method {@link #getConfiguration()} to provide cache configuration bean.</li> * </ul> * * @param <K> Cache key type. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/core/src/main/java/org/apache/ignite/cache/query/QueryCursor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/query/QueryCursor.java b/modules/core/src/main/java/org/apache/ignite/cache/query/QueryCursor.java index 6f75e0e..b0c896a 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/query/QueryCursor.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/query/QueryCursor.java @@ -34,7 +34,7 @@ public interface QueryCursor<T> extends Iterable<T>, AutoCloseable { * Since all the results will be fetched, all the resources will be closed * automatically after this call, e.g. there is no need to call {@link #close()} method in this case. * - * @return Collection containing full query result. + * @return List containing all query results. */ public List<T> getAll(); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/core/src/main/java/org/apache/ignite/cache/query/QueryPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/query/QueryPredicate.java b/modules/core/src/main/java/org/apache/ignite/cache/query/QueryPredicate.java index 15ceb2e..e2a8569 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/query/QueryPredicate.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/query/QueryPredicate.java @@ -17,6 +17,7 @@ package org.apache.ignite.cache.query; +import org.apache.ignite.configuration.*; import org.apache.ignite.internal.util.typedef.internal.*; import java.io.*; @@ -39,7 +40,7 @@ public abstract class QueryPredicate implements Serializable { /** * Constructs query predicate with optional page size, if {@code 0}, - * then {@link QueryConfiguration#getPageSize()} is used. + * then {@link CacheQueryConfiguration#getPageSize()} is used. * * @param pageSize Optional page size. */ @@ -48,7 +49,7 @@ public abstract class QueryPredicate implements Serializable { } /** - * Gets optional page size, if {@code 0}, then {@link QueryConfiguration#getPageSize()} is used. + * Gets optional page size, if {@code 0}, then {@link CacheQueryConfiguration#getPageSize()} is used. * * @return Optional page size. */ @@ -57,7 +58,7 @@ public abstract class QueryPredicate implements Serializable { } /** - * Sets optional page size, if {@code 0}, then {@link QueryConfiguration#getPageSize()} is used. + * Sets optional page size, if {@code 0}, then {@link CacheQueryConfiguration#getPageSize()} is used. * * @param pageSize Optional page size. */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/core/src/main/java/org/apache/ignite/cache/query/QueryScanPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/query/QueryScanPredicate.java b/modules/core/src/main/java/org/apache/ignite/cache/query/QueryScanPredicate.java index 3aa6536..c6b355f 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/query/QueryScanPredicate.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/query/QueryScanPredicate.java @@ -20,7 +20,7 @@ package org.apache.ignite.cache.query; import org.apache.ignite.lang.*; /** - * Scan predicate over cache entries. + * Scan predicate over cache entries. By default will accept all the entries. */ public class QueryScanPredicate<K, V> extends QueryPredicate implements IgniteBiPredicate<K, V> { /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/core/src/main/java/org/apache/ignite/cache/query/QuerySpiPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/query/QuerySpiPredicate.java b/modules/core/src/main/java/org/apache/ignite/cache/query/QuerySpiPredicate.java index d3439d7..17cb916 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/query/QuerySpiPredicate.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/query/QuerySpiPredicate.java @@ -20,7 +20,7 @@ package org.apache.ignite.cache.query; import org.apache.ignite.spi.indexing.*; /** - * Predicate to be used by {@link GridIndexingSpi} implementations. + * Predicate to be used by {@link IndexingSpi} implementations. */ public class QuerySpiPredicate extends QueryPredicate { /** Arguments. */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java index 6af69c4..914639c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java @@ -22,6 +22,7 @@ import org.apache.ignite.cache.*; import org.apache.ignite.cache.query.*; import org.apache.ignite.internal.*; import org.apache.ignite.internal.processors.cache.query.*; +import org.apache.ignite.internal.util.*; import org.apache.ignite.lang.*; import org.apache.ignite.mxbean.*; import org.apache.ignite.internal.util.tostring.*; @@ -233,6 +234,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter<IgniteCach } /** {@inheritDoc} */ + @SuppressWarnings("unchecked") @Override public QueryCursor<Entry<K,V>> query(QueryPredicate filter) { A.notNull(filter, "filter"); @@ -241,35 +243,53 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter<IgniteCach try { if (filter instanceof QuerySqlPredicate) { // TODO query over entries on indexing + ctx.kernalContext().query().query() return null; } + final CacheQuery<Map.Entry<K,V>> qry; + final CacheQueryFuture<Map.Entry<K,V>> fut; + if (filter instanceof QueryScanPredicate) { - CacheQuery<Map.Entry<K,V>> res = delegate.queries().createScanQuery((IgniteBiPredicate<K,V>)filter); + qry = delegate.queries().createScanQuery((IgniteBiPredicate<K,V>)filter); - // TODO convert to QueryCursor. - return null; + fut = qry.execute(); } - - if (filter instanceof QueryTextPredicate) { + else if (filter instanceof QueryTextPredicate) { QueryTextPredicate p = (QueryTextPredicate)filter; - CacheQueryFuture<Map.Entry<K,V>> res = delegate.queries().createFullTextQuery(p.getType(), p.getText()).execute(); + qry = delegate.queries().createFullTextQuery(p.getType(), p.getText()); - // TODO convert to QueryCursor. - return null; + fut = qry.execute(); } + else if (filter instanceof QuerySpiPredicate) { + qry = ((GridCacheQueriesEx)delegate.queries()).createSpiQuery(); - if (filter instanceof QuerySpiPredicate) { - CacheQueryFuture<Object> res = ((GridCacheQueriesEx)delegate.queries()).createSpiQuery() - .execute(((QuerySpiPredicate)filter).getArgs()); - - // TODO convert to QueryCursor. - return null; + fut = qry.execute(((QuerySpiPredicate)filter).getArgs()); } + else + throw new IgniteException("Unsupported query predicate: " + filter); - throw new IgniteException("Unsupported query predicate: " + filter); + return new QueryCursorImpl<>(new GridCloseableIteratorAdapter<Entry<K,V>>() { + /** */ + Map.Entry<K,V> cur; + + @Override protected Entry<K,V> onNext() throws IgniteCheckedException { + if (!onHasNext()) + throw new NoSuchElementException(); + + return new CacheEntryImpl<>(cur.getKey(), cur.getValue()); + } + + @Override protected boolean onHasNext() throws IgniteCheckedException { + return cur != null || (cur = fut.next()) != null; + } + + @Override protected void onClose() throws IgniteCheckedException { + fut.cancel(); + } + }); } finally { gate.leave(prev); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/QueryCursorImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/QueryCursorImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/QueryCursorImpl.java new file mode 100644 index 0000000..0c04c1e --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/QueryCursorImpl.java @@ -0,0 +1,86 @@ +/* + * 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.ignite.internal.processors.cache; + +import org.apache.ignite.*; +import org.apache.ignite.cache.query.*; +import org.apache.ignite.internal.util.lang.*; + +import java.util.*; + +/** + * Query cursor implementation. + */ +public class QueryCursorImpl<T> implements QueryCursor<T> { + /** */ + private GridCloseableIterator<T> iter; + + /** */ + private boolean iterTaken; + + /** + * @param iter Iterator. + */ + public QueryCursorImpl(GridCloseableIterator<T> iter) { + this.iter = iter; + } + + /** {@inheritDoc} */ + @Override public Iterator<T> iterator() { + if (iter == null) + throw new IgniteException("Cursor is closed."); + + if (iterTaken) + throw new IgniteException("Iterator is already taken from this cursor."); + + iterTaken = true; + + return iter; + } + + /** {@inheritDoc} */ + @Override public List<T> getAll() { + ArrayList<T> all = new ArrayList<>(); + + try { + for (T t : this) // Implicitly calls iterator() to do all checks. + all.add(t); + } + finally { + close(); + } + + return all; + } + + /** {@inheritDoc} */ + @Override public void close() { + GridCloseableIterator<T> i; + + if ((i = iter) != null) { + iter = null; + + try { + i.close(); + } + catch (IgniteCheckedException e) { + throw new IgniteException(e); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java index 536fc1e..d09f1cf 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java @@ -26,6 +26,7 @@ import org.apache.ignite.lang.*; import org.apache.ignite.spi.indexing.*; import org.jetbrains.annotations.*; +import javax.cache.*; import java.util.*; /** @@ -68,6 +69,17 @@ public interface GridQueryIndexing { public QueryCursor<List<?>> queryTwoStep(String space, String sqlQry, Object[] params); /** + * Parses SQL query into two step query and executes it. + * + * @param space Space. + * @param type Type name. + * @param sqlQry Query. + * @param params Parameters. + * @return Cursor. + */ + public QueryCursor<Cache.Entry<?,?>> queryTwoStep(String space, String type, String sqlQry, Object[] params); + + /** * Queries individual fields (generally used by JDBC drivers). * * @param spaceName Space name. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/QueryCursorImpl.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/QueryCursorImpl.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/QueryCursorImpl.java deleted file mode 100644 index ca51d6f..0000000 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/QueryCursorImpl.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.ignite.internal.processors.query.h2; - -import org.apache.ignite.*; -import org.apache.ignite.cache.query.*; - -import java.util.*; - -/** - * Query cursor implementation. - */ -public class QueryCursorImpl<T> implements QueryCursor<T> { - /** */ - private GridH2ResultSetIterator<T> iter; - - /** */ - private boolean iterTaken; - - /** - * @param iter Iterator. - */ - public QueryCursorImpl(GridH2ResultSetIterator<T> iter) { - this.iter = iter; - } - - /** {@inheritDoc} */ - @Override public Iterator<T> iterator() { - if (iter == null) - throw new IgniteException("Cursor is closed."); - - if (iterTaken) - throw new IgniteException("Iterator is already taken from this cursor."); - - iterTaken = true; - - return iter; - } - - /** {@inheritDoc} */ - @Override public List<T> getAll() { - ArrayList<T> all = new ArrayList<>(); - - try { - for (T t : this) // Implicitly calls iterator() to do all checks. - all.add(t); - } - finally { - close(); - } - - return all; - } - - /** {@inheritDoc} */ - @Override public void close() { - GridH2ResultSetIterator<T> i; - - if ((i = iter) != null) { - iter = null; - - try { - i.close(); - } - catch (IgniteCheckedException e) { - throw new IgniteException(e); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4f5b36bb/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java index 8b7ca13..afc526d 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java @@ -13,6 +13,7 @@ import org.apache.ignite.*; import org.apache.ignite.cache.query.*; import org.apache.ignite.cluster.*; import org.apache.ignite.internal.*; +import org.apache.ignite.internal.processors.cache.*; import org.apache.ignite.internal.processors.cache.query.*; import org.apache.ignite.internal.processors.query.h2.*; import org.apache.ignite.internal.processors.query.h2.twostep.messages.*;
