risingwave_common_estimate_size/collections/
btreemap.rs

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2024 RisingWave Labs
//
// 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.

use core::fmt;
use std::collections::BTreeMap;
use std::ops::{Bound, RangeInclusive};

use crate::{EstimateSize, KvSize};

#[derive(Clone)]
pub struct EstimatedBTreeMap<K, V> {
    inner: BTreeMap<K, V>,
    heap_size: KvSize,
}

impl<K, V> EstimatedBTreeMap<K, V> {
    pub fn new() -> Self {
        Self {
            inner: BTreeMap::new(),
            heap_size: KvSize::new(),
        }
    }

    pub fn inner(&self) -> &BTreeMap<K, V> {
        &self.inner
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    pub fn iter(&self) -> impl DoubleEndedIterator<Item = (&K, &V)> {
        self.inner.iter()
    }

    pub fn range<R>(&self, range: R) -> std::collections::btree_map::Range<'_, K, V>
    where
        K: Ord,
        R: std::ops::RangeBounds<K>,
    {
        self.inner.range(range)
    }

    pub fn values(&self) -> impl Iterator<Item = &V> {
        self.inner.values()
    }
}

impl<K, V> EstimatedBTreeMap<K, V>
where
    K: Ord,
{
    pub fn first_key_value(&self) -> Option<(&K, &V)> {
        self.inner.first_key_value()
    }

    pub fn first_key(&self) -> Option<&K> {
        self.first_key_value().map(|(k, _)| k)
    }

    pub fn first_value(&self) -> Option<&V> {
        self.first_key_value().map(|(_, v)| v)
    }

    pub fn last_key_value(&self) -> Option<(&K, &V)> {
        self.inner.last_key_value()
    }

    pub fn last_key(&self) -> Option<&K> {
        self.last_key_value().map(|(k, _)| k)
    }

    pub fn last_value(&self) -> Option<&V> {
        self.last_key_value().map(|(_, v)| v)
    }
}

impl<K, V> Default for EstimatedBTreeMap<K, V> {
    fn default() -> Self {
        Self::new()
    }
}

impl<K, V> EstimatedBTreeMap<K, V>
where
    K: EstimateSize + Ord,
    V: EstimateSize,
{
    pub fn insert(&mut self, key: K, value: V) -> Option<V> {
        let key_size = self.heap_size.add_val(&key);
        self.heap_size.add_val(&value);
        let old_value = self.inner.insert(key, value);
        if let Some(old_value) = &old_value {
            self.heap_size.sub_size(key_size);
            self.heap_size.sub_val(old_value);
        }
        old_value
    }

    pub fn remove(&mut self, key: &K) -> Option<V> {
        let old_value = self.inner.remove(key);
        if let Some(old_value) = &old_value {
            self.heap_size.sub(key, old_value);
        }
        old_value
    }

    pub fn clear(&mut self) {
        self.inner.clear();
        self.heap_size.set(0);
    }

    pub fn pop_first(&mut self) -> Option<(K, V)> {
        let (key, value) = self.inner.pop_first()?;
        self.heap_size.sub(&key, &value);
        Some((key, value))
    }

    pub fn pop_last(&mut self) -> Option<(K, V)> {
        let (key, value) = self.inner.pop_last()?;
        self.heap_size.sub(&key, &value);
        Some((key, value))
    }

    pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
        self.inner.last_entry().map(|inner| OccupiedEntry {
            inner,
            heap_size: &mut self.heap_size,
        })
    }

    /// Retain the given range of entries in the map, removing others.
    pub fn retain_range(&mut self, range: RangeInclusive<&K>) -> (BTreeMap<K, V>, BTreeMap<K, V>)
    where
        K: Clone,
    {
        let start = *range.start();
        let end = *range.end();

        // [ left, [mid], right ]
        let mut mid_right = self.inner.split_off(start);
        let mid_right_split_key = mid_right
            .lower_bound(Bound::Excluded(end))
            .peek_next()
            .map(|(k, _)| k)
            .cloned();
        let right = if let Some(ref mid_right_split_key) = mid_right_split_key {
            mid_right.split_off(mid_right_split_key)
        } else {
            Default::default()
        };
        let mid = mid_right;
        let left = std::mem::replace(&mut self.inner, mid);

        for (k, v) in &left {
            self.heap_size.sub(k, v);
        }
        for (k, v) in &right {
            self.heap_size.sub(k, v);
        }

        (left, right)
    }

    pub fn extract_if<'a, F>(
        &'a mut self,
        mut pred: F,
    ) -> ExtractIf<'a, K, V, impl FnMut(&K, &mut V) -> bool>
    where
        F: 'a + FnMut(&K, &V) -> bool,
    {
        let pred_immut = move |key: &K, value: &mut V| pred(key, value);
        ExtractIf {
            inner: self.inner.extract_if(pred_immut),
            heap_size: &mut self.heap_size,
        }
    }

    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&K, &V) -> bool,
    {
        self.extract_if(|k, v| !f(k, v)).for_each(drop);
    }
}

impl<K, V> EstimateSize for EstimatedBTreeMap<K, V>
where
    K: EstimateSize,
    V: EstimateSize,
{
    fn estimated_heap_size(&self) -> usize {
        self.heap_size.size()
    }
}

impl<K, V> fmt::Debug for EstimatedBTreeMap<K, V>
where
    K: fmt::Debug,
    V: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.inner.fmt(f)
    }
}

pub struct OccupiedEntry<'a, K, V> {
    inner: std::collections::btree_map::OccupiedEntry<'a, K, V>,
    heap_size: &'a mut KvSize,
}

impl<K, V> OccupiedEntry<'_, K, V>
where
    K: EstimateSize + Ord,
    V: EstimateSize,
{
    pub fn key(&self) -> &K {
        self.inner.key()
    }

    pub fn remove_entry(self) -> (K, V) {
        let (key, value) = self.inner.remove_entry();
        self.heap_size.sub(&key, &value);
        (key, value)
    }
}

pub struct ExtractIf<'a, K, V, F>
where
    F: FnMut(&K, &mut V) -> bool,
{
    inner: std::collections::btree_map::ExtractIf<'a, K, V, F>,
    heap_size: &'a mut KvSize,
}

impl<K, V, F> Iterator for ExtractIf<'_, K, V, F>
where
    K: EstimateSize,
    V: EstimateSize,
    F: FnMut(&K, &mut V) -> bool,
{
    type Item = (K, V);

    fn next(&mut self) -> Option<Self::Item> {
        let (key, value) = self.inner.next()?;
        self.heap_size.sub(&key, &value);
        Some((key, value))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

#[cfg(test)]
mod tests {
    use super::EstimatedBTreeMap;

    #[test]
    fn test_retain_range() {
        let mut map = EstimatedBTreeMap::new();

        let (left, right) = map.retain_range(&1..=&10);
        assert!(left.is_empty());
        assert!(right.is_empty());

        map.insert(1, "hello".to_string());
        map.insert(6, "world".to_string());
        let (left, right) = map.retain_range(&6..=&6);
        assert_eq!(map.len(), 1);
        assert_eq!(map.inner[&6], "world".to_string());
        assert_eq!(left.len(), 1);
        assert_eq!(left[&1], "hello".to_string());
        assert!(right.is_empty());

        map.insert(8, "risingwave".to_string());
        map.insert(3, "great".to_string());
        map.insert(0, "wooow".to_string());
        let (left, right) = map.retain_range(&2..=&7);
        assert_eq!(map.len(), 2);
        assert_eq!(map.inner[&3], "great".to_string());
        assert_eq!(map.inner[&6], "world".to_string());
        assert_eq!(left.len(), 1);
        assert_eq!(left[&0], "wooow".to_string());
        assert_eq!(right.len(), 1);
        assert_eq!(right[&8], "risingwave".to_string());
    }
}