From e85d59d3ec0839617a2c4b394301dc6c3c4e31b4 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 25 Jul 2009 18:09:50 +0000 Subject: Query.after and Query.fetchAfter supports subclass parameter. --- src/main/java/com/amazon/carbonado/Query.java | 4 +- .../com/amazon/carbonado/qe/AbstractQuery.java | 12 +++++- .../java/com/amazon/carbonado/qe/EmptyQuery.java | 31 ++++++++++++++- .../com/amazon/carbonado/qe/StandardQuery.java | 35 +++++++++++++++- .../carbonado/repo/logging/LoggingQuery.java | 46 +++++++++++++++++++--- 5 files changed, 116 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main/java/com/amazon/carbonado/Query.java b/src/main/java/com/amazon/carbonado/Query.java index 3c3e6e2..f987727 100644 --- a/src/main/java/com/amazon/carbonado/Query.java +++ b/src/main/java/com/amazon/carbonado/Query.java @@ -271,7 +271,7 @@ public interface Query { * @throws FetchException if storage layer throws an exception * @since 1.2 */ - Query after(S start) throws FetchException; + Query after(T start) throws FetchException; /** * Fetches results for this query. If any updates or deletes might be @@ -323,7 +323,7 @@ public interface Query { * @see Repository#enterTransaction(IsolationLevel) * @see #after */ - Cursor fetchAfter(S start) throws FetchException; + Cursor fetchAfter(T start) throws FetchException; /** * Attempts to load exactly one matching object. If the number of matching diff --git a/src/main/java/com/amazon/carbonado/qe/AbstractQuery.java b/src/main/java/com/amazon/carbonado/qe/AbstractQuery.java index bd27d9a..21c7e3f 100644 --- a/src/main/java/com/amazon/carbonado/qe/AbstractQuery.java +++ b/src/main/java/com/amazon/carbonado/qe/AbstractQuery.java @@ -45,18 +45,22 @@ public abstract class AbstractQuery implements Query, App protected AbstractQuery() { } + @Override public Query and(String filter) throws FetchException { return and(Filter.filterFor(getStorableType(), filter)); } + @Override public Query or(String filter) throws FetchException { return or(Filter.filterFor(getStorableType(), filter)); } - public Cursor fetchAfter(S start) throws FetchException { + @Override + public Cursor fetchAfter(T start) throws FetchException { return after(start).fetch(); } + @Override public S loadOne() throws FetchException { S obj = tryLoadOne(); if (obj == null) { @@ -65,6 +69,7 @@ public abstract class AbstractQuery implements Query, App return obj; } + @Override public S tryLoadOne() throws FetchException { Cursor cursor = fetch(); try { @@ -82,12 +87,14 @@ public abstract class AbstractQuery implements Query, App } } + @Override public void deleteOne() throws PersistException { if (!tryDeleteOne()) { throw new PersistNoneException(toString()); } } + @Override public boolean printNative() { try { return printNative(System.out); @@ -97,10 +104,12 @@ public abstract class AbstractQuery implements Query, App } } + @Override public boolean printNative(Appendable app) throws IOException { return printNative(app, 0); } + @Override public boolean printPlan() { try { return printPlan(System.out); @@ -110,6 +119,7 @@ public abstract class AbstractQuery implements Query, App } } + @Override public boolean printPlan(Appendable app) throws IOException { return printPlan(app, 0); } diff --git a/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java b/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java index e9431f3..873b8a7 100644 --- a/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java +++ b/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java @@ -81,6 +81,7 @@ public final class EmptyQuery extends AbstractQuery { mOrdering = OrderingList.emptyList(); } + @Override public Class getStorableType() { return mFactory.getStorableType(); } @@ -88,6 +89,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always returns a {@link com.amazon.carbonado.filter.ClosedFilter ClosedFilter}. */ + @Override public Filter getFilter() { return Filter.getClosedFilter(getStorableType()); } @@ -95,6 +97,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always returns null. */ + @Override public FilterValues getFilterValues() { return null; } @@ -102,6 +105,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always returns zero. */ + @Override public int getBlankParameterCount() { return 0; } @@ -109,6 +113,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query with(int value) { throw error(); } @@ -116,6 +121,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query with(long value) { throw error(); } @@ -123,6 +129,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query with(float value) { throw error(); } @@ -130,6 +137,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query with(double value) { throw error(); } @@ -137,6 +145,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query with(boolean value) { throw error(); } @@ -144,6 +153,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query with(char value) { throw error(); } @@ -151,6 +161,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query with(byte value) { throw error(); } @@ -158,6 +169,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query with(short value) { throw error(); } @@ -165,6 +177,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query with(Object value) { throw error(); } @@ -172,6 +185,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Throws an IllegalStateException unless no values passed in. */ + @Override public Query withValues(Object... values) { if (values == null || values.length == 0) { return this; @@ -182,10 +196,12 @@ public final class EmptyQuery extends AbstractQuery { /** * Always throws an IllegalStateException. */ + @Override public Query and(Filter filter) { throw new IllegalStateException("Query is already guaranteed to fetch nothing"); } + @Override public Query or(Filter filter) throws FetchException { return mFactory.query(filter, null, mOrdering, null); } @@ -193,25 +209,30 @@ public final class EmptyQuery extends AbstractQuery { /** * Returns a query that fetches everything, possibly in a specified order. */ + @Override public Query not() throws FetchException { return mFactory.query(null, null, mOrdering, null); } + @Override public Query orderBy(String property) throws FetchException { return new EmptyQuery(mFactory, property); } + @Override public Query orderBy(String... properties) throws FetchException { return new EmptyQuery(mFactory, properties); } - public Query after(S start) { + @Override + public Query after(T start) { return this; } /** * Always returns an {@link EmptyCursor}. */ + @Override public Cursor fetch() { return EmptyCursor.the(); } @@ -219,6 +240,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always returns an {@link EmptyCursor}. */ + @Override public Cursor fetchSlice(long from, Long to) { checkSliceArguments(from, to); return EmptyCursor.the(); @@ -235,6 +257,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always returns false. */ + @Override public boolean tryDeleteOne() { return false; } @@ -242,12 +265,14 @@ public final class EmptyQuery extends AbstractQuery { /** * Does nothing. */ + @Override public void deleteAll() { } /** * Always returns zero. */ + @Override public long count() { return 0; } @@ -255,10 +280,12 @@ public final class EmptyQuery extends AbstractQuery { /** * Always returns false. */ + @Override public boolean exists() { return false; } + @Override public void appendTo(Appendable app) throws IOException { app.append("Query {type="); app.append(getStorableType().getName()); @@ -282,6 +309,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always returns false. */ + @Override public boolean printNative(Appendable app, int indentLevel) { return false; } @@ -289,6 +317,7 @@ public final class EmptyQuery extends AbstractQuery { /** * Always returns false. */ + @Override public boolean printPlan(Appendable app, int indentLevel) { return false; } diff --git a/src/main/java/com/amazon/carbonado/qe/StandardQuery.java b/src/main/java/com/amazon/carbonado/qe/StandardQuery.java index 736f31f..08cd27a 100644 --- a/src/main/java/com/amazon/carbonado/qe/StandardQuery.java +++ b/src/main/java/com/amazon/carbonado/qe/StandardQuery.java @@ -90,10 +90,12 @@ public abstract class StandardQuery extends AbstractQuery mHints = hints; } + @Override public Class getStorableType() { return queryFactory().getStorableType(); } + @Override public Filter getFilter() { Filter filter = mFilter; if (filter == null) { @@ -102,50 +104,62 @@ public abstract class StandardQuery extends AbstractQuery return filter; } + @Override public FilterValues getFilterValues() { return mValues; } + @Override public int getBlankParameterCount() { return mValues == null ? 0 : mValues.getBlankParameterCount(); } + @Override public Query with(int value) { return newInstance(requireValues().with(value)); } + @Override public Query with(long value) { return newInstance(requireValues().with(value)); } + @Override public Query with(float value) { return newInstance(requireValues().with(value)); } + @Override public Query with(double value) { return newInstance(requireValues().with(value)); } + @Override public Query with(boolean value) { return newInstance(requireValues().with(value)); } + @Override public Query with(char value) { return newInstance(requireValues().with(value)); } + @Override public Query with(byte value) { return newInstance(requireValues().with(value)); } + @Override public Query with(short value) { return newInstance(requireValues().with(value)); } + @Override public Query with(Object value) { return newInstance(requireValues().with(value)); } + @Override public Query withValues(Object... values) { if (values == null || values.length == 0) { return this; @@ -153,6 +167,7 @@ public abstract class StandardQuery extends AbstractQuery return newInstance(requireValues().withValues(values)); } + @Override public Query and(Filter filter) throws FetchException { Filter newFilter; FilterValues newValues; @@ -172,6 +187,7 @@ public abstract class StandardQuery extends AbstractQuery return createQuery(newFilter, newValues, mOrdering, mHints); } + @Override public Query or(Filter filter) throws FetchException { if (mFilter == null) { throw new IllegalStateException("Query is already guaranteed to fetch everything"); @@ -187,6 +203,7 @@ public abstract class StandardQuery extends AbstractQuery return createQuery(newFilter, newValues, mOrdering, mHints); } + @Override public Query not() throws FetchException { if (mFilter == null) { return new EmptyQuery(queryFactory(), mOrdering); @@ -199,17 +216,20 @@ public abstract class StandardQuery extends AbstractQuery return createQuery(newFilter, newValues, mOrdering, mHints); } + @Override public Query orderBy(String property) throws FetchException { return createQuery(mFilter, mValues, OrderingList.get(getStorableType(), property), mHints); } + @Override public Query orderBy(String... properties) throws FetchException { return createQuery(mFilter, mValues, OrderingList.get(getStorableType(), properties), mHints); } - public Query after(S start) throws FetchException { + @Override + public Query after(T start) throws FetchException { OrderingList orderings; if (start == null || (orderings = mOrdering).size() == 0) { return this; @@ -217,7 +237,9 @@ public abstract class StandardQuery extends AbstractQuery return buildAfter(start, orderings); } - private Query buildAfter(S start, OrderingList orderings) throws FetchException { + private Query buildAfter(T start, OrderingList orderings) + throws FetchException + { Class storableType = getStorableType(); Filter orderFilter = Filter.getClosedFilter(storableType); Filter openFilter = Filter.getOpenFilter(storableType); @@ -256,6 +278,7 @@ public abstract class StandardQuery extends AbstractQuery return query; } + @Override public Cursor fetch() throws FetchException { try { return executor().fetch(mValues); @@ -264,6 +287,7 @@ public abstract class StandardQuery extends AbstractQuery } } + @Override public Cursor fetchSlice(long from, Long to) throws FetchException { if (!checkSliceArguments(from, to)) { return fetch(); @@ -277,6 +301,7 @@ public abstract class StandardQuery extends AbstractQuery } } + @Override public boolean tryDeleteOne() throws PersistException { Transaction txn = enterTransaction(IsolationLevel.READ_COMMITTED); try { @@ -308,6 +333,7 @@ public abstract class StandardQuery extends AbstractQuery } } + @Override public void deleteAll() throws PersistException { Transaction txn = enterTransaction(IsolationLevel.READ_COMMITTED); try { @@ -331,6 +357,7 @@ public abstract class StandardQuery extends AbstractQuery } } + @Override public long count() throws FetchException { try { return executor().count(mValues); @@ -339,6 +366,7 @@ public abstract class StandardQuery extends AbstractQuery } } + @Override public boolean exists() throws FetchException { Cursor cursor = fetchSlice(0L, 1L); try { @@ -348,6 +376,7 @@ public abstract class StandardQuery extends AbstractQuery } } + @Override public boolean printNative(Appendable app, int indentLevel) throws IOException { try { return executor().printNative(app, indentLevel, mValues); @@ -356,6 +385,7 @@ public abstract class StandardQuery extends AbstractQuery } } + @Override public boolean printPlan(Appendable app, int indentLevel) throws IOException { try { return executor().printPlan(app, indentLevel, mValues); @@ -394,6 +424,7 @@ public abstract class StandardQuery extends AbstractQuery return false; } + @Override public void appendTo(Appendable app) throws IOException { app.append("Query {type="); app.append(getStorableType().getName()); 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 c4c8414..eb18933 100644 --- a/src/main/java/com/amazon/carbonado/repo/logging/LoggingQuery.java +++ b/src/main/java/com/amazon/carbonado/repo/logging/LoggingQuery.java @@ -43,96 +43,119 @@ class LoggingQuery implements Query { mQuery = query; } + @Override public Class getStorableType() { return mQuery.getStorableType(); } + @Override public Filter getFilter() { return mQuery.getFilter(); } + @Override public FilterValues getFilterValues() { return mQuery.getFilterValues(); } + @Override public int getBlankParameterCount() { return mQuery.getBlankParameterCount(); } + @Override public Query with(int value) { return newInstance(mQuery.with(value)); } + @Override public Query with(long value) { return newInstance(mQuery.with(value)); } + @Override public Query with(float value) { return newInstance(mQuery.with(value)); } + @Override public Query with(double value) { return newInstance(mQuery.with(value)); } + @Override public Query with(boolean value) { return newInstance(mQuery.with(value)); } + @Override public Query with(char value) { return newInstance(mQuery.with(value)); } + @Override public Query with(byte value) { return newInstance(mQuery.with(value)); } + @Override public Query with(short value) { return newInstance(mQuery.with(value)); } + @Override public Query with(Object value) { return newInstance(mQuery.with(value)); } + @Override public Query withValues(Object... objects) { return newInstance(mQuery.withValues(objects)); } + @Override public Query and(String filter) throws FetchException { return newInstance(mQuery.and(filter)); } + @Override public Query and(Filter filter) throws FetchException { return newInstance(mQuery.and(filter)); } + @Override public Query or(String filter) throws FetchException { return newInstance(mQuery.or(filter)); } + @Override public Query or(Filter filter) throws FetchException { return newInstance(mQuery.or(filter)); } + @Override public Query not() throws FetchException { return newInstance(mQuery.not()); } + @Override public Query orderBy(String property) throws FetchException, UnsupportedOperationException { return newInstance(mQuery.orderBy(property)); } + @Override public Query orderBy(String... strings) throws FetchException, UnsupportedOperationException { return newInstance(mQuery.orderBy(strings)); } - public Query after(S start) throws FetchException { + @Override + public Query after(T start) throws FetchException { return newInstance(mQuery.after(start)); } + @Override public Cursor fetch() throws FetchException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -141,6 +164,7 @@ class LoggingQuery implements Query { return mQuery.fetch(); } + @Override public Cursor fetchSlice(long from, Long to) throws FetchException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -150,7 +174,8 @@ class LoggingQuery implements Query { return mQuery.fetchSlice(from, to); } - public Cursor fetchAfter(S start) throws FetchException { + @Override + public Cursor fetchAfter(T start) throws FetchException { Log log = mStorage.mLog; if (log.isEnabled()) { log.write("Query.fetchAfter(start) on " + this + ", start: " + start); @@ -158,6 +183,7 @@ class LoggingQuery implements Query { return mQuery.fetchAfter(start); } + @Override public S loadOne() throws FetchException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -166,6 +192,7 @@ class LoggingQuery implements Query { return mQuery.loadOne(); } + @Override public S tryLoadOne() throws FetchException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -174,6 +201,7 @@ class LoggingQuery implements Query { return mQuery.tryLoadOne(); } + @Override public void deleteOne() throws PersistException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -182,6 +210,7 @@ class LoggingQuery implements Query { mQuery.deleteOne(); } + @Override public boolean tryDeleteOne() throws PersistException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -190,6 +219,7 @@ class LoggingQuery implements Query { return mQuery.tryDeleteOne(); } + @Override public void deleteAll() throws PersistException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -198,6 +228,7 @@ class LoggingQuery implements Query { mQuery.deleteAll(); } + @Override public long count() throws FetchException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -206,6 +237,7 @@ class LoggingQuery implements Query { return mQuery.count(); } + @Override public boolean exists() throws FetchException { Log log = mStorage.mLog; if (log.isEnabled()) { @@ -214,34 +246,36 @@ class LoggingQuery implements Query { return mQuery.exists(); } + @Override public boolean printNative() { return mQuery.printNative(); } + @Override public boolean printNative(Appendable app) throws IOException { return mQuery.printNative(app); } + @Override public boolean printNative(Appendable app, int indentLevel) throws IOException { return mQuery.printNative(app, indentLevel); } + @Override public boolean printPlan() { return mQuery.printPlan(); } + @Override public boolean printPlan(Appendable app) throws IOException { return mQuery.printPlan(app); } + @Override public boolean printPlan(Appendable app, int indentLevel) throws IOException { return mQuery.printPlan(app, indentLevel); } - public void appendTo(Appendable appendable) throws IOException { - appendable.append(mQuery.toString()); - } - @Override public String toString() { return mQuery.toString(); -- cgit v1.2.3