be/src/storage/compaction/collection_similarity.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <gen_cpp/Opcodes_types.h> |
21 | | |
22 | | #include "core/column/column.h" |
23 | | #include "storage/segment/common.h" |
24 | | |
25 | | namespace doris { |
26 | | #include "common/compile_check_begin.h" |
27 | | |
28 | | using ScoreMap = phmap::flat_hash_map<segment_v2::rowid_t, float>; |
29 | | using ScoreMapIterator = ScoreMap::const_iterator; |
30 | | |
31 | | enum class OrderType { |
32 | | ASC, |
33 | | DESC, |
34 | | }; |
35 | | |
36 | | struct ScoreRangeFilter { |
37 | | TExprOpcode::type op; |
38 | | double threshold; |
39 | | |
40 | 430k | bool pass(float score) const { |
41 | 430k | return (op == TExprOpcode::GT) ? (score > threshold) : (score >= threshold); |
42 | 430k | } |
43 | | }; |
44 | | using ScoreRangeFilterPtr = std::shared_ptr<ScoreRangeFilter>; |
45 | | |
46 | | class CollectionSimilarity { |
47 | | public: |
48 | 1.30k | CollectionSimilarity() { _bm25_scores.reserve(1024); } |
49 | 1.32k | ~CollectionSimilarity() = default; |
50 | | |
51 | | void collect(segment_v2::rowid_t row_id, float score); |
52 | | |
53 | | void get_bm25_scores(roaring::Roaring* row_bitmap, IColumn::MutablePtr& scores, |
54 | | std::unique_ptr<std::vector<uint64_t>>& row_ids, |
55 | | const ScoreRangeFilterPtr& filter = nullptr) const; |
56 | | |
57 | | void get_topn_bm25_scores(roaring::Roaring* row_bitmap, IColumn::MutablePtr& scores, |
58 | | std::unique_ptr<std::vector<uint64_t>>& row_ids, OrderType order_type, |
59 | | size_t top_k, const ScoreRangeFilterPtr& filter = nullptr) const; |
60 | | |
61 | | private: |
62 | | template <OrderType order> |
63 | | void find_top_k_scores(const roaring::Roaring* row_bitmap, const ScoreMap& all_scores, |
64 | | size_t top_k, std::vector<std::pair<uint32_t, float>>& top_k_results, |
65 | | const ScoreRangeFilterPtr& filter) const; |
66 | | |
67 | | ScoreMap _bm25_scores; |
68 | | }; |
69 | | using CollectionSimilarityPtr = std::shared_ptr<CollectionSimilarity>; |
70 | | |
71 | | #include "common/compile_check_end.h" |
72 | | } // namespace doris |