From c5028b6b2d70cd5ab1a051195ade47bd6fec72f9 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Mon, 14 Apr 2008 14:09:45 +0000 Subject: Added "after" method and removed fetchAfter slice method. --- src/main/java/com/amazon/carbonado/Query.java | 47 ++++++++--------- .../java/com/amazon/carbonado/qe/EmptyQuery.java | 12 ++--- .../com/amazon/carbonado/qe/StandardQuery.java | 61 ++++++++++------------ .../carbonado/repo/logging/LoggingQuery.java | 13 ++--- 4 files changed, 56 insertions(+), 77 deletions(-) (limited to 'src/main/java/com/amazon/carbonado') diff --git a/src/main/java/com/amazon/carbonado/Query.java b/src/main/java/com/amazon/carbonado/Query.java index d1cebca..36a8117 100644 --- a/src/main/java/com/amazon/carbonado/Query.java +++ b/src/main/java/com/amazon/carbonado/Query.java @@ -254,6 +254,25 @@ public interface Query { */ Query orderBy(String... properties) throws FetchException; + /** + * Returns a query which fetches results for this query after a given + * starting point, which is useful for re-opening a cursor. This is only + * effective when query has been given an explicit {@link #orderBy + * ordering}. If not a total ordering, then query may start at an earlier + * position. + * + *

Note: The returned query can be very expensive to fetch from + * repeatedly, if the query needs to perform a sort operation. Ideally, the + * query ordering should match the natural ordering of an index or key. + * + * @param start storable to attempt to start after; if null, this query is + * returned + * @throws IllegalStateException if any blank parameters in this query + * @throws FetchException if storage layer throws an exception + * @since 1.2 + */ + Query after(S start) throws FetchException; + /** * Fetches results for this query. If any updates or deletes might be * performed on the results, consider enclosing the fetch in a @@ -288,7 +307,7 @@ public interface Query { * Fetches results for this query after a given starting point, which is * useful for re-opening a cursor. This is only effective when query has * been given an explicit {@link #orderBy ordering}. If not a total - * ordering, then returned cursor may start at an earlier position. + * ordering, then cursor may start at an earlier position. * *

Note: This method can be very expensive to call repeatedly, if the * query needs to perform a sort operation. Ideally, the query ordering @@ -299,34 +318,10 @@ public interface Query { * @throws IllegalStateException if any blank parameters in this query * @throws FetchException if storage layer throws an exception * @see Repository#enterTransaction(IsolationLevel) + * @see #after */ Cursor fetchAfter(S start) throws FetchException; - /** - * Fetches a slice of results for this query after a given starting point, - * which is useful for re-opening a cursor. This is only effective when - * query has been given an explicit {@link #orderBy ordering}. If not a - * total ordering, then returned cursor may start at an earlier position. - * It is strongly recommended that the query be given a total ordering in - * order for the slice results to be deterministic. - * - *

Note: This method can be very expensive to call repeatedly, if the - * query needs to perform a sort operation. Ideally, the query ordering - * should match the natural ordering of an index or key. - * - * @param start storable to attempt to start after; if null, fetch all results - * @param from zero-based {@code from} record number, inclusive - * @param to optional zero-based {@code to} record number, exclusive - * @return fetch results - * @throws IllegalStateException if any blank parameters in this query - * @throws IllegalArgumentException if {@code from} is negative or if - * {@code from} is more than {@code to} - * @throws FetchException if storage layer throws an exception - * @see Repository#enterTransaction(IsolationLevel) - * @since 1.2 - */ - Cursor fetchAfter(S start, long from, Long to) throws FetchException; - /** * Attempts to load exactly one matching object. If the number of matching * records is zero or exceeds one, then an exception is thrown instead. diff --git a/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java b/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java index 9754019..83c09a6 100644 --- a/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java +++ b/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java @@ -205,6 +205,10 @@ public final class EmptyQuery extends AbstractQuery { return new EmptyQuery(mFactory, properties); } + public Query after(S start) { + return this; + } + /** * Always returns an {@link EmptyCursor}. */ @@ -227,14 +231,6 @@ public final class EmptyQuery extends AbstractQuery { return EmptyCursor.the(); } - /** - * Always returns an {@link EmptyCursor}. - */ - public Cursor fetchAfter(S start, long from, Long to) { - checkSliceArguments(from, to); - return EmptyCursor.the(); - } - /** * Always throws {@link PersistNoneException}. */ diff --git a/src/main/java/com/amazon/carbonado/qe/StandardQuery.java b/src/main/java/com/amazon/carbonado/qe/StandardQuery.java index 1ee3acb..f8db1df 100644 --- a/src/main/java/com/amazon/carbonado/qe/StandardQuery.java +++ b/src/main/java/com/amazon/carbonado/qe/StandardQuery.java @@ -209,43 +209,12 @@ public abstract class StandardQuery extends AbstractQuery OrderingList.get(getStorableType(), properties), mHints); } - public Cursor fetch() throws FetchException { - try { - return executor().fetch(mValues); - } catch (RepositoryException e) { - throw e.toFetchException(); - } - } - - public Cursor fetch(long from, Long to) throws FetchException { - if (!checkSliceArguments(from, to)) { - return fetch(); - } - try { - QueryHints hints = QueryHints.emptyHints().with(QueryHint.CONSUME_SLICE); - return executorFactory().executor(mFilter, mOrdering, hints).fetch(mValues, from, to); - } catch (RepositoryException e) { - throw e.toFetchException(); - } - } - - public Cursor fetchAfter(S start) throws FetchException { + public Query after(S start) throws FetchException { OrderingList orderings; if (start == null || (orderings = mOrdering).size() == 0) { - return fetch(); - } - return buildAfter(start, orderings).fetch(); - } - - public Cursor fetchAfter(S start, long from, Long to) throws FetchException { - if (!checkSliceArguments(from, to)) { - return fetchAfter(start); - } - OrderingList orderings; - if (start == null || (orderings = mOrdering).size() == 0) { - return fetch(from, to); + return this; } - return buildAfter(start, orderings).fetch(from, to); + return buildAfter(start, orderings); } private Query buildAfter(S start, OrderingList orderings) throws FetchException { @@ -271,6 +240,30 @@ public abstract class StandardQuery extends AbstractQuery return and(orderFilter); } + public Cursor fetch() throws FetchException { + try { + return executor().fetch(mValues); + } catch (RepositoryException e) { + throw e.toFetchException(); + } + } + + public Cursor fetch(long from, Long to) throws FetchException { + if (!checkSliceArguments(from, to)) { + return fetch(); + } + try { + QueryHints hints = QueryHints.emptyHints().with(QueryHint.CONSUME_SLICE); + return executorFactory().executor(mFilter, mOrdering, hints).fetch(mValues, from, to); + } catch (RepositoryException e) { + throw e.toFetchException(); + } + } + + public Cursor fetchAfter(S start) throws FetchException { + return after(start).fetch(); + } + public boolean tryDeleteOne() throws PersistException { Transaction txn = enterTransaction(IsolationLevel.READ_COMMITTED); try { diff --git a/src/main/java/com/amazon/carbonado/repo/logging/LoggingQuery.java b/src/main/java/com/amazon/carbonado/repo/logging/LoggingQuery.java index 56e4e33..e737288 100644 --- a/src/main/java/com/amazon/carbonado/repo/logging/LoggingQuery.java +++ b/src/main/java/com/amazon/carbonado/repo/logging/LoggingQuery.java @@ -129,6 +129,10 @@ class LoggingQuery implements Query { return newInstance(mQuery.orderBy(strings)); } + public Query after(S start) throws FetchException { + return newInstance(mQuery.after(start)); + } + public Cursor fetch() throws FetchException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -154,15 +158,6 @@ class LoggingQuery implements Query { return mQuery.fetchAfter(start); } - public Cursor fetchAfter(S start, long from, Long to) throws FetchException { - Log log = mStorage.mLog; - if (log.isEnabled()) { - log.write("Query.fetchAfter(start, from, to) on " + this + ", start: " + start + - ", from: " + from + ", to: " + to); - } - return mQuery.fetchAfter(start); - } - public S loadOne() throws FetchException { Log log = mStorage.mLog; if (log.isEnabled()) { -- cgit v1.2.3