From 094f2d2d8f472f092a03b396d4cf7cff8e302946 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Mon, 21 Oct 2013 17:46:45 +0000 Subject: Fixed resync logic to consider index when a filter is provided. --- .../repo/replicated/ReplicatedRepository.java | 52 ++++++++++++++++++---- 1 file changed, 44 insertions(+), 8 deletions(-) (limited to 'src/main/java/com') diff --git a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java index 1b46073..6cf0d6c 100644 --- a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java @@ -19,6 +19,7 @@ package com.amazon.carbonado.repo.replicated; import java.util.Comparator; import java.util.LinkedHashSet; +import java.util.List; import java.util.Set; import org.apache.commons.logging.Log; @@ -49,10 +50,20 @@ import com.amazon.carbonado.capability.StorableInfoCapability; import com.amazon.carbonado.cursor.SortedCursor; +import com.amazon.carbonado.filter.Filter; + import com.amazon.carbonado.info.Direction; +import com.amazon.carbonado.info.StorableIndex; import com.amazon.carbonado.info.StorableInfo; import com.amazon.carbonado.info.StorableIntrospector; +import com.amazon.carbonado.qe.IndexedQueryAnalyzer; +import com.amazon.carbonado.qe.OrderingList; +import com.amazon.carbonado.qe.QueryHint; +import com.amazon.carbonado.qe.QueryHints; +import com.amazon.carbonado.qe.RepositoryAccess; +import com.amazon.carbonado.qe.UnionQueryAnalyzer; + import com.amazon.carbonado.repo.indexed.IndexEntryAccessCapability; import com.amazon.carbonado.spi.StoragePool; @@ -82,15 +93,40 @@ class ReplicatedRepository private static final int RESYNC_WATERMARK = 100; /** - * Utility method to select the natural ordering of a storage, by looking - * for a clustered index on the primary key. Returns null if no clustered - * index was found. - * - * TODO: Try to incorporate this into standard storage interface somehow. + * Utility method to select the natural ordering of a storage, by looking for a clustered + * index on the primary key. Returns null if no clustered index was found. If a filter is + * provided, the ordering which utilizes the best index is used. */ - private static String[] selectNaturalOrder(Repository repo, Class type) + private static String[] selectNaturalOrder(Repository repo, + Class type, + Filter filter) throws RepositoryException { + if (!filter.isOpen() && repo instanceof RepositoryAccess) { + UnionQueryAnalyzer.Result result = + new UnionQueryAnalyzer(type, (RepositoryAccess) repo) + .analyze(filter, null, QueryHints.emptyHints().with(QueryHint.CONSUME_SLICE)); + OrderingList ordering = result.getTotalOrdering(); + + if (ordering == null) { + List list = result.getSubResults(); + if (!list.isEmpty()) { + StorableIndex index = list.get(0).getLocalIndex(); + if (index != null) { + ordering = OrderingList.get(index.getOrderedProperties()); + } + } + } + + if (ordering != null) { + String[] props = new String[ordering.size()]; + for (int i=0; i pkSet = StorableIntrospector.examine(type).getPrimaryKeyProperties().keySet(); -- cgit v1.2.3