1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
use crate::core::index::index_writer::Inner;
use crate::core::index::merge_policy::MergeContext;
use crate::core::index::segment_commit_info::SegmentCommitInfo;
use crate::core::store::directory::Directory;
use crate::core::util::error::lucene_error::Result;
use crate::core::util::info_stream::InfoStreamMT;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
/// A wrapper around the IndexWriter [`MergeContext`](crate::core::index::merge_policy::MergeContext).
///
/// Attempts to cache the result of [`MergeContext::num_deletes_to_merge`], in order to avoid
/// duplicate calculations during the merge phase.
///
/// This helps prevent repeated computation of delete counts for the same
/// [`SegmentCommitInfo`](crate::core::index::segment_commit_info::SegmentCommitInfo).
pub(crate) struct CachingMergeContext<'a, T> {
merge_context: &'a T,
pub(crate) cached_num_deletes_to_merge: RefCell<HashMap<Arc<str>, i32>>,
}
impl<'a, T> CachingMergeContext<'a, T> {
pub fn new(merge_context: &'a T) -> Self {
CachingMergeContext {
merge_context,
cached_num_deletes_to_merge: RefCell::new(HashMap::new()),
}
}
}
impl<T, D> MergeContext<D> for CachingMergeContext<'_, T>
where
T: MergeContext<D>,
D: Directory,
{
fn num_deletes_to_merge(
&self,
info: &SegmentCommitInfo<D>,
) -> crate::core::util::error::lucene_error::Result<i32> {
let key = info.info.get_id_key();
if let Some(v) = self.cached_num_deletes_to_merge.borrow().get(key).copied() {
return Ok(v);
}
let v = self.merge_context.num_deletes_to_merge(info)?;
self
.cached_num_deletes_to_merge
.borrow_mut()
.insert(info.info.id_key.clone(), v);
Ok(v)
}
fn num_deleted_docs(&self, info: &SegmentCommitInfo<D>) -> Result<i32> {
self.merge_context.num_deleted_docs(info)
}
fn get_info_stream(&self) -> InfoStreamMT {
self.merge_context.get_info_stream()
}
fn get_merging_segments(&self, inner: Option<&Inner<D>>) -> HashSet<String> {
self.merge_context.get_merging_segments(inner)
}
}