diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-07-03 20:42:19 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-07-03 20:42:19 +0000 |
commit | 62bd002c38c0d70233c1de3c35ffb17bb687ad8f (patch) | |
tree | 1f93662ab9e1bc282278840db81b0c1568e10ce1 /searchcore | |
parent | 86bd836d889e6648f459701fe8fec7e5a364afd6 (diff) |
Move code only used from searchcore to searchcore to reduce wiring and api leakage.
Diffstat (limited to 'searchcore')
4 files changed, 76 insertions, 39 deletions
diff --git a/searchcore/src/vespa/searchcore/grouping/groupingcontext.cpp b/searchcore/src/vespa/searchcore/grouping/groupingcontext.cpp index 30a1f6074f1..08bbf422674 100644 --- a/searchcore/src/vespa/searchcore/grouping/groupingcontext.cpp +++ b/searchcore/src/vespa/searchcore/grouping/groupingcontext.cpp @@ -3,6 +3,8 @@ #include "groupingcontext.h" #include <vespa/searchlib/aggregation/predicates.h> #include <vespa/searchlib/aggregation/modifiers.h> +#include <vespa/searchlib/aggregation/hitsaggregationresult.h> +#include <vespa/searchlib/common/bitvector.h> namespace search::grouping { @@ -17,12 +19,11 @@ GroupingContext::deserialize(const char *groupSpec, uint32_t groupSpecLen) vespalib::NBOSerializer nis(is); uint32_t numGroupings = 0; nis >> numGroupings; + _groupingList.reserve(numGroupings); for (size_t i = 0; i < numGroupings; i++) { auto grouping = std::make_shared<search::aggregation::Grouping>(); grouping->deserialize(nis); - grouping->setClock(&_clock); - grouping->setTimeOfDoom(_timeOfDoom); - _groupingList.push_back(grouping); + _groupingList.push_back(std::move(grouping)); } } } @@ -65,8 +66,7 @@ GroupingContext::GroupingContext(const vespalib::Clock & clock, vespalib::steady _os(), _groupingList(), _enableNestedMultivalueGrouping(true) -{ -} +{ } GroupingContext::GroupingContext(const GroupingContext & rhs) : _clock(rhs._clock), @@ -74,8 +74,7 @@ GroupingContext::GroupingContext(const GroupingContext & rhs) : _os(), _groupingList(), _enableNestedMultivalueGrouping(rhs._enableNestedMultivalueGrouping) -{ -} +{ } void GroupingContext::addGrouping(const GroupingPtr & g) @@ -104,4 +103,43 @@ GroupingContext::needRanking() const return true; } +using DocId = uint32_t; + +void +GroupingContext::aggregate(Grouping & grouping, const RankedHit * rankedHit, unsigned int len, const BitVector * bVec) const +{ + grouping.preAggregate(false); + + for(unsigned int i(0), m(grouping.getMaxN(len)); (i < m) && !hasExpired(); i++) { + grouping.aggregate(rankedHit[i].getDocId(), rankedHit[i].getRank()); + } + if (bVec != nullptr) { + unsigned int sz(bVec->size()); + int64_t topN = grouping.getTopN(); + if (topN > 0) { + for(DocId d(bVec->getFirstTrueBit()), i(0), m(grouping.getMaxN(sz)); (d < sz) && (i < m) && !hasExpired(); d = bVec->getNextTrueBit(d+1), i++) { + grouping.aggregate(d, 0.0); + } + } else { + for(DocId d(bVec->getFirstTrueBit()); (d < sz) && !hasExpired(); d = bVec->getNextTrueBit(d+1)) { + grouping.aggregate(d, 0.0); + } + } + } + grouping.postProcess(); +} + +void +GroupingContext::aggregate(Grouping & grouping, const RankedHit * rankedHit, unsigned int len) const +{ + bool isOrdered(! grouping.needResort()); + grouping.preAggregate(isOrdered); + search::aggregation::HitsAggregationResult::SetOrdered pred; + grouping.select(pred, pred); + for(unsigned int i(0), m(grouping.getMaxN(len)); (i < m) && !hasExpired(); i++) { + grouping.aggregate(rankedHit[i].getDocId(), rankedHit[i].getRank()); + } + grouping.postProcess(); +} + } diff --git a/searchcore/src/vespa/searchcore/grouping/groupingcontext.h b/searchcore/src/vespa/searchcore/grouping/groupingcontext.h index af71cf30017..cbbe78a1019 100644 --- a/searchcore/src/vespa/searchcore/grouping/groupingcontext.h +++ b/searchcore/src/vespa/searchcore/grouping/groupingcontext.h @@ -4,6 +4,7 @@ #include <vespa/searchlib/aggregation/grouping.h> #include <vespa/vespalib/objects/nbostream.h> #include <vespa/vespalib/util/time.h> +#include <vespa/vespalib/util/clock.h> #include <vector> #include <memory> @@ -18,17 +19,10 @@ class GroupingContext { public: using UP = std::unique_ptr<GroupingContext>; - using GroupingPtr = std::shared_ptr<search::aggregation::Grouping>; + using Grouping = search::aggregation::Grouping; + using GroupingPtr = std::shared_ptr<Grouping>; using GroupingList = std::vector<GroupingPtr>; -private: - const vespalib::Clock & _clock; - vespalib::steady_time _timeOfDoom; - vespalib::nbostream _os; - GroupingList _groupingList; - bool _enableNestedMultivalueGrouping; -public: - /** * Deserialize a grouping spec into this context. * @@ -74,7 +68,7 @@ public: * Return the internal list of grouping expressions in this context. * @return a list of groupings. **/ - GroupingList &getGroupingList() { return _groupingList; } + GroupingList &getGroupingList() noexcept { return _groupingList; } /** * Serialize the grouping expressions in this context. @@ -84,7 +78,7 @@ public: /** * Check whether this context contains any groupings. **/ - bool empty() const { return _groupingList.empty(); } + bool empty() const noexcept { return _groupingList.empty(); } /** * Obtain the grouping result. @@ -108,13 +102,23 @@ public: /** * Obtain the time of doom. */ - vespalib::steady_time getTimeOfDoom() const { return _timeOfDoom; } + vespalib::steady_time getTimeOfDoom() const noexcept { return _timeOfDoom; } + bool hasExpired() const noexcept { return _clock.getTimeNS() > _timeOfDoom; } /** * Figure out if ranking is necessary for any of the grouping requests here. * @return true if ranking is required. */ bool needRanking() const; - bool enableNestedMultivalueGrouping() const { return _enableNestedMultivalueGrouping; } + bool enableNestedMultivalueGrouping() const noexcept { return _enableNestedMultivalueGrouping; } + + void aggregate(Grouping & grouping, const RankedHit * rankedHit, unsigned int len, const BitVector * bVec) const; + void aggregate(Grouping & grouping, const RankedHit * rankedHit, unsigned int len) const; +private: + const vespalib::Clock & _clock; + vespalib::steady_time _timeOfDoom; + vespalib::nbostream _os; + GroupingList _groupingList; + bool _enableNestedMultivalueGrouping; }; } diff --git a/searchcore/src/vespa/searchcore/grouping/groupingmanager.cpp b/searchcore/src/vespa/searchcore/grouping/groupingmanager.cpp index e907f4a7c76..3eb410ad2c2 100644 --- a/searchcore/src/vespa/searchcore/grouping/groupingmanager.cpp +++ b/searchcore/src/vespa/searchcore/grouping/groupingmanager.cpp @@ -69,14 +69,12 @@ GroupingManager::init(const IAttributeContext &attrCtx) void GroupingManager::groupInRelevanceOrder(const RankedHit *searchResults, uint32_t binSize) { - GroupingContext::GroupingList &groupingList(_groupingContext.getGroupingList()); - for (size_t i = 0; i < groupingList.size(); ++i) { - Grouping & g = *groupingList[i]; - if ( ! g.needResort() ) { - g.aggregate(searchResults, binSize); - LOG(debug, "groupInRelevanceOrder: %s", g.asString().c_str()); - g.cleanTemporary(); - g.cleanupAttributeReferences(); + for (const auto & g : _groupingContext.getGroupingList()) { + if ( ! g->needResort() ) { + _groupingContext.aggregate(*g, searchResults, binSize); + LOG(debug, "groupInRelevanceOrder: %s", g->asString().c_str()); + g->cleanTemporary(); + g->cleanupAttributeReferences(); } } } @@ -84,14 +82,12 @@ GroupingManager::groupInRelevanceOrder(const RankedHit *searchResults, uint32_t void GroupingManager::groupUnordered(const RankedHit *searchResults, uint32_t binSize, const search::BitVector * overflow) { - GroupingContext::GroupingList &groupingList(_groupingContext.getGroupingList()); - for (size_t i = 0; i < groupingList.size(); ++i) { - Grouping & g = *groupingList[i]; - if ( g.needResort() ) { - g.aggregate(searchResults, binSize, overflow); - LOG(debug, "groupUnordered: %s", g.asString().c_str()); - g.cleanTemporary(); - g.cleanupAttributeReferences(); + for (const auto & g : _groupingContext.getGroupingList()) { + if ( g->needResort() ) { + _groupingContext.aggregate(*g, searchResults, binSize, overflow); + LOG(debug, "groupUnordered: %s", g->asString().c_str()); + g->cleanTemporary(); + g->cleanupAttributeReferences(); } } } diff --git a/searchcore/src/vespa/searchcore/proton/matching/result_processor.cpp b/searchcore/src/vespa/searchcore/proton/matching/result_processor.cpp index 9b85ddbdde8..1608c633124 100644 --- a/searchcore/src/vespa/searchcore/proton/matching/result_processor.cpp +++ b/searchcore/src/vespa/searchcore/proton/matching/result_processor.cpp @@ -119,7 +119,6 @@ ResultProcessor::Result::UP ResultProcessor::makeReply(PartialResultUP full_result) { auto reply = std::make_unique<search::engine::SearchReply>(); - const search::IDocumentMetaStore &metaStore = _metaStore; search::engine::SearchReply &r = *reply; PartialResult &result = *full_result; size_t numFs4Hits(0); @@ -127,7 +126,7 @@ ResultProcessor::makeReply(PartialResultUP full_result) if (_wasMerged) { _groupingSession->getGroupingManager().prune(); } - _groupingSession->getGroupingManager().convertToGlobalId(metaStore); + _groupingSession->getGroupingManager().convertToGlobalId(_metaStore); _groupingSession->continueExecution(_groupingContext); numFs4Hits = _groupingContext.countFS4Hits(); _groupingContext.getResult().swap(r.groupResult); @@ -144,7 +143,7 @@ ResultProcessor::makeReply(PartialResultUP full_result) search::engine::SearchReply::Hit &dst = r.hits[i]; const search::RankedHit &src = result.hit(hitOffset + i); uint32_t docId = src.getDocId(); - if (metaStore.getGidEvenIfMoved(docId, gid)) { + if (_metaStore.getGidEvenIfMoved(docId, gid)) { dst.gid = gid; } dst.metric = src.getRank(); |