From b115f32b904a9d1686e7e98073fd066037502f47 Mon Sep 17 00:00:00 2001 From: Matt Wheeler Date: Mon, 27 Jan 2014 11:57:24 -0800 Subject: [PATCH 1/2] BOBO-286 Make SortCollector more extendable --- .gitignore | 3 ++- .../browseengine/bobo/sort/SortCollector.java | 18 +++++++++++++++-- .../bobo/sort/SortCollectorImpl.java | 20 ++++++++++++++----- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index f0c05898..357999d6 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ TAGS cscope.* bobo-contrib/target/ bobo-contrib/MANIFEST.MF - +*.iml +bobo-browse/bobo-browse.log diff --git a/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollector.java b/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollector.java index 80f3e59a..ed63b294 100644 --- a/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollector.java +++ b/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollector.java @@ -130,6 +130,7 @@ public void clearRuntimeFacetData() { public LinkedList scorearraylist; public static int BLOCK_SIZE = 4096; + private static SortCollectorFactory _sortCollectorFactory = new DefaultSortCollectorFactory(); protected Collector _collector = null; protected final SortField[] _sortFields; @@ -232,9 +233,15 @@ private static SortField convert(Browsable browser, SortField sort) { } } + public static SortCollector buildSortCollector(Browsable browser, Query q, SortField[] sort, + int offset, int count, boolean fetchStoredFields, Set termVectorsToFetch, + String[] groupBy, int maxPerGroup, boolean collectDocIdCache) { + return buildSortCollector(browser, q, sort, offset, count, fetchStoredFields, termVectorsToFetch, groupBy, maxPerGroup, collectDocIdCache, _sortCollectorFactory); + } + public static SortCollector buildSortCollector(Browsable browser, Query q, SortField[] sort, int offset, int count, boolean fetchStoredFields, Set termVectorsToFetch, - String[] groupBy, int maxPerGroup, boolean collectDocIdCache) { + String[] groupBy, int maxPerGroup, boolean collectDocIdCache, SortCollectorFactory sortCollectorFactory) { if (sort == null || sort.length == 0) { if (q != null && !(q instanceof MatchAllDocsQuery)) { sort = new SortField[] { SortField.FIELD_SCORE }; @@ -262,10 +269,17 @@ public static SortCollector buildSortCollector(Browsable browser, Query q, SortF } compSource = new MultiDocIdComparatorSource(compSources); } - return new SortCollectorImpl(compSource, sort, browser, offset, count, doScoring, + return sortCollectorFactory.create(compSource, sort, browser, offset, count, doScoring, fetchStoredFields, termVectorsToFetch, groupBy, maxPerGroup, collectDocIdCache); } + public static void setSortCollectorFactory(SortCollectorFactory sortCollectorFactory) { + if (sortCollectorFactory == null) { + throw new IllegalArgumentException("null sortCollectorFactory is not supported."); + } + _sortCollectorFactory = sortCollectorFactory; + } + public SortCollector setCollector(Collector collector) { _collector = collector; return this; diff --git a/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollectorImpl.java b/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollectorImpl.java index 555e7fc1..d2aacc56 100644 --- a/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollectorImpl.java +++ b/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollectorImpl.java @@ -84,7 +84,7 @@ public int compare(MyScoreDoc o1, MyScoreDoc o2) { private FacetCountCollector[] _facetCountCollectorMulti = null; private final boolean _doScoring; - private Scorer _scorer; + protected Scorer _scorer; private final int _offset; private final int _count; @@ -185,6 +185,16 @@ public boolean acceptsDocsOutOfOrder() { return _collector == null ? true : _collector.acceptsDocsOutOfOrder(); } + /** + * Allows sublclasses to mess with the score. + * @param score + * @return + * @throws IOException + */ + public float score(float score) throws IOException { + return (_doScoring ? score : 0.0f); + } + @Override public void collect(int doc) throws IOException { ++_totalHits; @@ -196,7 +206,7 @@ public void collect(int doc) throws IOException { } if (_count > 0) { - final float score = (_doScoring ? _scorer.score() : 0.0f); + final float score = score(_scorer.score()); if (_collectDocIdCache) { if (_totalHits > _docIdCacheCapacity) { @@ -218,7 +228,7 @@ public void collect(int doc) throws IOException { return; } else { if (_count > 0) { - final float score = (_doScoring ? _scorer.score() : 0.0f); + final float score = score(_scorer.score()); if (_collectDocIdCache) { if (_totalHits > _docIdCacheCapacity) { @@ -271,7 +281,7 @@ public void collect(int doc) throws IOException { } } else { if (_count > 0) { - final float score = (_doScoring ? _scorer.score() : 0.0f); + final float score = score(_scorer.score()); if (_queueFull) { _tmpScoreDoc.doc = doc; @@ -369,7 +379,7 @@ public BrowseHit[] topDocs() throws IOException { PrimitiveLongArrayWrapper primitiveLongArrayWrapperTmp = new PrimitiveLongArrayWrapper(null); - Object rawGroupValue = null; + Object rawGroupValue; if (_facetAccessibleLists != null) { _groupAccessibles = new CombinedFacetAccessible[_facetAccessibleLists.length]; From e6993b2820b1ef5f31261f61cf0976b4a961f1fb Mon Sep 17 00:00:00 2001 From: Matt Wheeler Date: Mon, 27 Jan 2014 11:59:45 -0800 Subject: [PATCH 2/2] BOBO-286 Make SortCollector more extendable --- .gitignore | 3 +- .../sort/DefaultSortCollectorFactory.java | 28 +++++++++++++++ .../bobo/sort/SortCollectorFactory.java | 35 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 bobo-browse/src/main/java/com/browseengine/bobo/sort/DefaultSortCollectorFactory.java create mode 100644 bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollectorFactory.java diff --git a/.gitignore b/.gitignore index 357999d6..f0c05898 100644 --- a/.gitignore +++ b/.gitignore @@ -24,5 +24,4 @@ TAGS cscope.* bobo-contrib/target/ bobo-contrib/MANIFEST.MF -*.iml -bobo-browse/bobo-browse.log + diff --git a/bobo-browse/src/main/java/com/browseengine/bobo/sort/DefaultSortCollectorFactory.java b/bobo-browse/src/main/java/com/browseengine/bobo/sort/DefaultSortCollectorFactory.java new file mode 100644 index 00000000..cefe6ea3 --- /dev/null +++ b/bobo-browse/src/main/java/com/browseengine/bobo/sort/DefaultSortCollectorFactory.java @@ -0,0 +1,28 @@ +/* + * Copyright 2013 jonathan. + * + * 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.browseengine.bobo.sort; + +import com.browseengine.bobo.api.Browsable; +import java.util.Set; +import org.apache.lucene.search.SortField; + +public class DefaultSortCollectorFactory implements SortCollectorFactory { + + @Override + public SortCollector create(DocComparatorSource compSource, SortField[] sortFields, Browsable boboBrowser, int offset, int count, boolean doScoring, boolean fetchStoredFields, Set termVectorsToFetch, String[] groupBy, int maxPerGroup, boolean collectDocIdCache) { + return new SortCollectorImpl(compSource, sortFields, boboBrowser, offset, count, doScoring, fetchStoredFields, termVectorsToFetch, groupBy, maxPerGroup, collectDocIdCache); + } +} diff --git a/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollectorFactory.java b/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollectorFactory.java new file mode 100644 index 00000000..1b366c0b --- /dev/null +++ b/bobo-browse/src/main/java/com/browseengine/bobo/sort/SortCollectorFactory.java @@ -0,0 +1,35 @@ +/* + * Copyright 2013 jonathan. + * + * 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.browseengine.bobo.sort; + +import com.browseengine.bobo.api.Browsable; +import java.util.Set; +import org.apache.lucene.search.SortField; + +public interface SortCollectorFactory { + + SortCollector create(DocComparatorSource compSource, + SortField[] sortFields, + Browsable boboBrowser, + int offset, + int count, + boolean doScoring, + boolean fetchStoredFields, + Set termVectorsToFetch, + String[] groupBy, + int maxPerGroup, + boolean collectDocIdCache); +}