From 1e947afa4b660a23a2dcb57463dd810fb73e6030 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 3 Sep 2006 21:48:14 +0000 Subject: Manage ordering properties with lists. --- .../carbonado/qe/TestIndexedQueryExecutor.java | 8 +- .../com/amazon/carbonado/qe/TestOrderingList.java | 110 +++++++++++++++++++++ .../com/amazon/carbonado/qe/TestOrderingScore.java | 26 +++-- .../com/amazon/carbonado/repo/toy/ToyStorage.java | 17 ++-- 4 files changed, 141 insertions(+), 20 deletions(-) create mode 100644 src/test/java/com/amazon/carbonado/qe/TestOrderingList.java (limited to 'src/test/java/com/amazon') diff --git a/src/test/java/com/amazon/carbonado/qe/TestIndexedQueryExecutor.java b/src/test/java/com/amazon/carbonado/qe/TestIndexedQueryExecutor.java index 3f865e6..b3eef91 100644 --- a/src/test/java/com/amazon/carbonado/qe/TestIndexedQueryExecutor.java +++ b/src/test/java/com/amazon/carbonado/qe/TestIndexedQueryExecutor.java @@ -21,6 +21,7 @@ package com.amazon.carbonado.qe; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.List; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -56,7 +57,8 @@ public class TestIndexedQueryExecutor extends TestCase { return TestOrderingScore.makeIndex(type, props); } - static OrderedProperty[] makeOrderings(Class type, String... props) + static List> makeOrderings(Class type, + String... props) { return TestOrderingScore.makeOrderings(type, props); } @@ -679,9 +681,9 @@ public class TestIndexedQueryExecutor extends TestCase { assertTrue(executor.mReverseOrder); assertEquals(values.getFilter(), executor.getFilter()); - OrderedProperty[] expectedOrdering = + List> expectedOrdering = makeOrderings(StorableTestBasic.class, "intProp", "-doubleProp", "stringProp"); - assertEquals(Arrays.asList(expectedOrdering), executor.getOrdering()); + assertEquals(expectedOrdering, executor.getOrdering()); } /** diff --git a/src/test/java/com/amazon/carbonado/qe/TestOrderingList.java b/src/test/java/com/amazon/carbonado/qe/TestOrderingList.java new file mode 100644 index 0000000..b696984 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/qe/TestOrderingList.java @@ -0,0 +1,110 @@ +/* + * Copyright 2006 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * Licensed 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 com.amazon.carbonado.qe; + +import java.util.List; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.amazon.carbonado.info.OrderedProperty; + +import com.amazon.carbonado.stored.StorableTestBasic; + +/** + * + * + * @author Brian S O'Neill + */ +public class TestOrderingList extends TestCase { + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static TestSuite suite() { + return new TestSuite(TestOrderingList.class); + } + + public TestOrderingList(String name) { + super(name); + } + + public void testEmpty() throws Exception { + assertEquals(0, OrderingList.get(StorableTestBasic.class).size()); + } + + public void testSingle() throws Exception { + List> list_1 = + OrderingList.get(StorableTestBasic.class, "date"); + + assertEquals(1, list_1.size()); + assertEquals("+date", list_1.get(0).toString()); + + List> list_2 = + OrderingList.get(StorableTestBasic.class, "+date"); + + assertEquals(1, list_2.size()); + assertEquals("+date", list_2.get(0).toString()); + assertEquals(list_1, list_2); + assertTrue(list_1 == list_2); + + List> list_3 = + OrderingList.get(StorableTestBasic.class, "-date"); + + assertEquals(1, list_3.size()); + assertEquals("-date", list_3.get(0).toString()); + assertFalse(list_2.equals(list_3)); + assertFalse(list_2 == list_3); + } + + public void testDouble() throws Exception { + List> list_1 = + OrderingList.get(StorableTestBasic.class, "date", "intProp"); + + assertEquals(2, list_1.size()); + assertEquals("+date", list_1.get(0).toString()); + assertEquals("+intProp", list_1.get(1).toString()); + + List> list_2 = + OrderingList.get(StorableTestBasic.class, "+date", "+intProp"); + + assertEquals(2, list_2.size()); + assertEquals("+date", list_2.get(0).toString()); + assertEquals("+intProp", list_2.get(1).toString()); + assertEquals(list_1, list_2); + assertTrue(list_1 == list_2); + + List> list_3 = + OrderingList.get(StorableTestBasic.class, "-date", "-intProp"); + + assertEquals(2, list_3.size()); + assertEquals("-date", list_3.get(0).toString()); + assertEquals("-intProp", list_3.get(1).toString()); + assertFalse(list_2.equals(list_3)); + assertFalse(list_2 == list_3); + } + + public void testIllegal() throws Exception { + try { + OrderingList.get(StorableTestBasic.class, "foo"); + fail(); + } catch (IllegalArgumentException e) { + } + } +} diff --git a/src/test/java/com/amazon/carbonado/qe/TestOrderingScore.java b/src/test/java/com/amazon/carbonado/qe/TestOrderingScore.java index 3929f59..1545b70 100644 --- a/src/test/java/com/amazon/carbonado/qe/TestOrderingScore.java +++ b/src/test/java/com/amazon/carbonado/qe/TestOrderingScore.java @@ -18,6 +18,8 @@ package com.amazon.carbonado.qe; +import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -53,15 +55,19 @@ public class TestOrderingScore extends TestCase { } static StorableIndex makeIndex(Class type, String... props) { - return new StorableIndex(makeOrderings(type, props), UNSPECIFIED); + List> list = makeOrderings(type, props); + OrderedProperty[] array = list.toArray(new OrderedProperty[0]); + + return new StorableIndex(array, UNSPECIFIED); } - static OrderedProperty[] makeOrderings(Class type, String... props) + static List> makeOrderings(Class type, + String... props) { StorableInfo info = StorableIntrospector.examine(type); - OrderedProperty[] ops = new OrderedProperty[props.length]; + List> ops = new ArrayList>(props.length); for (int i=0; i ix; - OrderedProperty[] ops; + List> ops; OrderingScore score; ///////////// @@ -180,7 +186,7 @@ public class TestOrderingScore extends TestCase { public void testMultipleProps() throws Exception { final StorableIndex ix; - OrderedProperty[] ops; + List> ops; OrderingScore score; ix = makeIndex(StorableTestBasic.class, "id", "intProp"); @@ -317,7 +323,7 @@ public class TestOrderingScore extends TestCase { public void testMidGap() throws Exception { final StorableIndex ix; - OrderedProperty[] ops; + List> ops; OrderingScore score; Filter filter; @@ -401,7 +407,7 @@ public class TestOrderingScore extends TestCase { public void testComparator() throws Exception { StorableIndex ix_1, ix_2; - OrderedProperty[] ops; + List> ops; OrderingScore score_1, score_2; Filter filter; Comparator> comp = OrderingScore.fullComparator(); @@ -466,7 +472,7 @@ public class TestOrderingScore extends TestCase { // properties are filtered out. Thus the index is not needed. final StorableIndex ix; - OrderedProperty[] ops; + List> ops; OrderingScore score; Filter filter; @@ -501,7 +507,7 @@ public class TestOrderingScore extends TestCase { // Test a unique index which has been fully specified. Ordering is not // needed at all. final StorableIndex ix; - OrderedProperty[] ops; + List> ops; OrderingScore score; Filter filter; diff --git a/src/test/java/com/amazon/carbonado/repo/toy/ToyStorage.java b/src/test/java/com/amazon/carbonado/repo/toy/ToyStorage.java index dd29e4b..01f9fbf 100644 --- a/src/test/java/com/amazon/carbonado/repo/toy/ToyStorage.java +++ b/src/test/java/com/amazon/carbonado/repo/toy/ToyStorage.java @@ -48,10 +48,11 @@ import com.amazon.carbonado.filter.FilterValues; import com.amazon.carbonado.info.OrderedProperty; import com.amazon.carbonado.info.StorableIntrospector; +import com.amazon.carbonado.qe.ArraySortedQueryExecutor; import com.amazon.carbonado.qe.FilteredQueryExecutor; import com.amazon.carbonado.qe.IterableQueryExecutor; +import com.amazon.carbonado.qe.OrderingList; import com.amazon.carbonado.qe.QueryExecutor; -import com.amazon.carbonado.qe.SortedQueryExecutor; import com.amazon.carbonado.qe.StandardQuery; /** @@ -89,7 +90,7 @@ public class ToyStorage implements Storage, MasterSupport } public Query query() throws FetchException { - return new ToyQuery(null); + return new ToyQuery(null, null); } public Query query(String filter) throws FetchException { @@ -97,7 +98,7 @@ public class ToyStorage implements Storage, MasterSupport } public Query query(Filter filter) throws FetchException { - return new ToyQuery(filter.initialFilterValues()); + return new ToyQuery(filter.initialFilterValues(), null); } public boolean addTrigger(Trigger trigger) { @@ -211,7 +212,7 @@ public class ToyStorage implements Storage, MasterSupport } private class ToyQuery extends StandardQuery { - ToyQuery(FilterValues values, String... orderings) { + ToyQuery(FilterValues values, OrderingList orderings) { super(values, orderings); } @@ -223,19 +224,21 @@ public class ToyStorage implements Storage, MasterSupport return mRepo.enterTransaction(level); } - protected QueryExecutor getExecutor(FilterValues values, String... orderings) { + protected QueryExecutor getExecutor(FilterValues values, OrderingList orderings) { QueryExecutor executor = new IterableQueryExecutor(mType, mData, mDataLock); if (values != null) { executor = new FilteredQueryExecutor(executor, values.getFilter()); } - // FIXME: sorting + if (orderings.size() > 0) { + executor = new ArraySortedQueryExecutor(executor, null, orderings); + } return executor; } - protected StandardQuery newInstance(FilterValues values, String... orderings) { + protected StandardQuery newInstance(FilterValues values, OrderingList orderings) { return new ToyQuery(values, orderings); } } -- cgit v1.2.3