tinylfu-cached 0.0.4

High performance, LFU based in-memory cache.
Documentation
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashSet};
use std::hash::Hash;
use std::sync::Arc;

use dashmap::DashMap;
use dashmap::mapref::multiple::RefMulti;
use log::info;
use parking_lot::RwLock;

use crate::cache::key_description::KeyDescription;
use crate::cache::policy::config::CacheWeightConfig;
use crate::cache::stats::ConcurrentStatsCounter;
use crate::cache::types::{FrequencyEstimate, KeyHash, KeyId, Weight};

/// WeightedKey maintains the key, its hash and its weight. It is used as a value type in the DashMap used inside `CacheWeight`
pub(crate) struct WeightedKey<Key> {
    key: Key,
    pub(crate) key_hash: KeyHash,
    weight: Weight,
}

impl<Key> WeightedKey<Key> {
    fn new(key: Key, key_hash: KeyHash, weight: Weight) -> Self {
        WeightedKey {
            key,
            key_hash,
            weight,
        }
    }
}

/// SampledKey represents a key with its id, weight and estimated frequency.
/// A collection of `SampledKey` is returned by `FrequencyCounterBasedMinHeapSamples` when a sample is requested during the admission of a key
#[derive(Copy, Clone, Debug)]
pub(crate) struct SampledKey {
    pub(crate) id: KeyId,
    pub(crate) weight: Weight,
    pub(crate) estimated_frequency: FrequencyEstimate,
}

impl Ord for SampledKey {
    fn cmp(&self, other: &Self) -> Ordering {
        (other.estimated_frequency, self.weight).cmp(&(self.estimated_frequency, other.weight))
    }
}

impl PartialOrd for SampledKey {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for SampledKey {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for SampledKey {}

impl SampledKey {
    pub(crate) fn new<Key>(frequency: FrequencyEstimate, pair: RefMulti<KeyId, WeightedKey<Key>>) -> Self <> {
        Self::using(*pair.key(), pair.weight, frequency)
    }

    fn using(id: KeyId, key_weight: Weight, frequency: FrequencyEstimate) -> Self <> {
        SampledKey {
            id,
            weight: key_weight,
            estimated_frequency: frequency,
        }
    }
}

/// FrequencyCounterBasedMinHeapSamples returns a sample to the `create_space` method of `crate::cache::policy::admission_policy::AdmissionPolicy`
/// The idea is to return a sample and allow getting the key with the smallest access frequency.
/// Internally, `FrequencyCounterBasedMinHeapSamples` uses [`std::collections::BinaryHeap`] and returns `SampledKey` that contains the key_id, its weight and its access frequency.
pub(crate) struct FrequencyCounterBasedMinHeapSamples<'a, Key, Freq>
    where Freq: Fn(KeyHash) -> FrequencyEstimate {
    source: &'a DashMap<KeyId, WeightedKey<Key>>,
    sample: BinaryHeap<SampledKey>,
    current_sample_key_ids: HashSet<KeyId>,
    sample_size: usize,
    frequency_counter: Freq,
}

impl<'a, Key, Freq> FrequencyCounterBasedMinHeapSamples<'a, Key, Freq>
    where Freq: Fn(KeyHash) -> FrequencyEstimate {
    fn new(
        source: &'a DashMap<KeyId, WeightedKey<Key>>,
        sample_size: usize,
        frequency_counter: Freq) -> Self <> {
        let (sample, current_sample_key_ids) = Self::initial_sample(source, sample_size, &frequency_counter);
        FrequencyCounterBasedMinHeapSamples {
            source,
            sample,
            current_sample_key_ids,
            sample_size,
            frequency_counter,
        }
    }

    /// There should never be a case where `min_frequency_key` is not able to return a key because sample size is always greater than 1.
    /// However, the current implementation has a sort of a race condition.
    /// Consider there is a `put` operation in progress through `AdmissionPolicy`.
    /// At the same time, `shutdown` is invoked on the instance of `Cached`. The current implementation of `shutdown` sends a `Shutdown` command to the
    /// `crate::cache::command::command_executor::CommandExecutor` and then calls `clear` on the `AdmissionPolicy`.
    /// The `clear` method of `AdmissionPolicy` clears all the entries in the `CacheWeight`.
    /// Consider that the `clear` operation is done on  `CacheWeight` and now `maybe_fill_in` is called but it would not be able to fill in the new keys
    /// There would be a stage where `self.sample` might become empty and `pop` does not return anything.
    /// Hence, at this stage, `min_frequency_key` returns an Option<SampledKey>
    pub(crate) fn min_frequency_key(&mut self) -> Option<SampledKey> {
        if let Some(key) = self.sample.pop() {
            self.current_sample_key_ids.remove(&key.id);
            return Some(key);
        }
        None
    }

    /// Everytime a key is used from the sample, an attempt is made to fill_in the sample
    /// In order to fill_in, we need to ensure that we do not add the key which is already present in the `BinaryHeap` again
    /// Rust's `BinaryHeap` does not provide a `contains` method, so we use a `HashSet` to determine the key_ids that are a part of the current sample.
    pub(crate) fn maybe_fill_in(&mut self) -> bool {
        let mut filled_in: bool = false;
        let mut iterator = self.source.iter();

        while self.sample.len() < self.sample_size {
            match iterator.next() {
                Some(pair) => {
                    if !self.current_sample_key_ids.contains(pair.key()) {
                        let frequency = (self.frequency_counter)(pair.key_hash);
                        self.current_sample_key_ids.insert(*pair.key());
                        self.sample.push(SampledKey::new(frequency, pair));
                        filled_in = true;
                    }
                }
                None => {
                    break;
                }
            }
        }
        filled_in
    }

    pub(crate) fn size(&self) -> usize {
        self.sample.len()
    }

    /// Return an initial sample with size = `sample_size`
    fn initial_sample(
        source: &DashMap<KeyId, WeightedKey<Key>>,
        sample_size: usize,
        frequency_counter: &Freq) -> (BinaryHeap<SampledKey>, HashSet<KeyId>) {
        let mut counter = 0;
        let mut sample = BinaryHeap::new();
        let mut current_sample_key_ids = HashSet::new();

        for pair in source.iter().by_ref() {
            current_sample_key_ids.insert(*pair.key());
            sample.push(SampledKey::new(frequency_counter(pair.value().key_hash), pair));
            counter += 1;

            if counter >= sample_size {
                break;
            }
        }
        (sample, current_sample_key_ids)
    }
}

/// CacheWeight maintains the weight of each key in the Cache and also manages the weight that is used in the cache.
/// Weight of each key is maintained via `WeightedKey` abstraction that contains the key, key_hash and its weight.
/// Every time a key is added in the cache, it is also added in `CacheWeight`, there by increasing the total weight of the cache.
/// Every time a key is updated, an attempt is made to update its weight, there by changing the total weight of the cache.
/// Every time a key is deleted, it is also deleted from `CacheWeight`, there by decreasing the total weight of the cache.
pub(crate) struct CacheWeight<Key>
    where Key: Hash + Eq + Send + Sync + Clone + 'static, {
    max_weight: Weight,
    weight_used: RwLock<Weight>,
    key_weights: DashMap<KeyId, WeightedKey<Key>>,
    stats_counter: Arc<ConcurrentStatsCounter>,
}

impl<Key> CacheWeight<Key>
    where Key: Hash + Eq + Send + Sync + Clone + 'static, {
    pub(crate) fn new(cache_weight_config: CacheWeightConfig, stats_counter: Arc<ConcurrentStatsCounter>) -> Self <> {
        info!("Initializing CacheWeight with a total weight {}", cache_weight_config.total_cache_weight());
        CacheWeight {
            max_weight: cache_weight_config.total_cache_weight(),
            weight_used: RwLock::new(0),
            key_weights: DashMap::with_capacity_and_shard_amount(cache_weight_config.capacity(), cache_weight_config.shards()),
            stats_counter,
        }
    }

    pub(crate) fn get_max_weight(&self) -> Weight {
        self.max_weight
    }

    pub(crate) fn get_weight_used(&self) -> Weight {
        *self.weight_used.read()
    }

    pub(crate) fn is_space_available_for(&self, weight: Weight) -> (Weight, bool) {
        let available = self.max_weight - (*self.weight_used.read());
        (available, available >= weight)
    }

    pub(crate) fn add(&self, key_description: &KeyDescription<Key>) {
        self.key_weights.insert(key_description.id, WeightedKey::new(key_description.clone_key(), key_description.hash, key_description.weight));
        let mut guard = self.weight_used.write();
        *guard += key_description.weight;

        self.stats_counter.add_weight(key_description.weight as u64);
    }

    pub(crate) fn update(&self, key_id: &KeyId, weight: Weight) -> bool {
        if let Some(mut existing) = self.key_weights.get_mut(key_id) {
            {
                let mut guard = self.weight_used.write();
                *guard += weight - existing.weight;
            }

            self.stats_counter.update_key();
            self.update_weight_stats(weight, existing.weight);

            let weighted_key = existing.value_mut();
            weighted_key.weight = weight;

            return true;
        }
        false
    }

    pub(crate) fn delete<DeleteHook>(&self, key_id: &KeyId, delete_hook: &DeleteHook)
        where DeleteHook: Fn(Key) {
        if let Some(weight_by_key_hash) = self.key_weights.remove(key_id) {
            let mut guard = self.weight_used.write();
            *guard -= weight_by_key_hash.1.weight;
            delete_hook(weight_by_key_hash.1.key);

            self.stats_counter.remove_weight(weight_by_key_hash.1.weight as u64);
        }
    }

    pub(crate) fn contains(&self, key_id: &KeyId) -> bool {
        self.key_weights.contains_key(key_id)
    }

    pub(crate) fn weight_of(&self, key_id: &KeyId) -> Option<Weight> {
        self.key_weights.get(key_id).map(|pair| pair.weight)
    }

    pub(crate) fn sample<Freq>(&self, size: usize, frequency_counter: Freq)
                               -> FrequencyCounterBasedMinHeapSamples<'_, Key, Freq>
        where Freq: Fn(KeyHash) -> FrequencyEstimate {
        FrequencyCounterBasedMinHeapSamples::new(&self.key_weights, size, frequency_counter)
    }

    pub(crate) fn clear(&self) {
        self.key_weights.clear();
        let mut guard = self.weight_used.write();
        *guard = 0;
    }

    fn update_weight_stats(&self, new_weight: Weight, existing_weight: Weight) {
        if new_weight > existing_weight {
            let difference = new_weight - existing_weight;
            self.stats_counter.add_weight(difference as u64);
        } else {
            let difference = existing_weight - new_weight;
            self.stats_counter.add_weight(!(difference - 1) as u64);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use parking_lot::RwLock;

    use crate::cache::key_description::KeyDescription;
    use crate::cache::policy::cache_weight::CacheWeight;
    use crate::cache::policy::config::CacheWeightConfig;
    use crate::cache::stats::ConcurrentStatsCounter;

    struct DeletedKeys<Key> {
        keys: RwLock<Vec<Key>>,
    }

    fn test_cache_weight_config() -> CacheWeightConfig {
        CacheWeightConfig::new(100, 4, 10)
    }

    #[test]
    fn maximum_cache_weight() {
        let cache_weight: CacheWeight<&str> = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));
        assert_eq!(10, cache_weight.get_max_weight());
    }

    #[test]
    fn space_is_available_for_new_key() {
        let cache_weight: CacheWeight<&str> = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));
        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));

        assert!(cache_weight.is_space_available_for(7).1);
    }

    #[test]
    fn space_is_not_available_for_new_key() {
        let cache_weight: CacheWeight<&str> = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));
        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));

        assert!(!cache_weight.is_space_available_for(8).1);
    }

    #[test]
    fn add_key_weight() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));
        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));

        assert_eq!(3, cache_weight.get_weight_used());
    }

    #[test]
    fn add_key_weight_and_increase_stats() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));
        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));

        assert_eq!(3, cache_weight.stats_counter.weight_added());
    }

    #[test]
    fn update_non_existing_key() {
        let cache_weight: CacheWeight<&str> = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        let result = cache_weight.update(&1, 2);
        assert!(!result);
    }

    #[test]
    fn update_an_existing_key() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));
        let result = cache_weight.update(&1, 3);

        assert!(result);
    }

    #[test]
    fn update_key_weight_given_the_updated_weight_is_less() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));
        assert_eq!(3, cache_weight.get_weight_used());

        cache_weight.update(&1, 2);
        assert_eq!(2, cache_weight.get_weight_used());
    }

    #[test]
    fn update_key_weight_given_the_updated_weight_is_less_and_increase_stats() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));
        assert_eq!(3, cache_weight.stats_counter.weight_added());

        cache_weight.update(&1, 2);
        assert_eq!(2, cache_weight.stats_counter.weight_added());
        assert_eq!(1, cache_weight.stats_counter.keys_updated());
    }

    #[test]
    fn update_key_weight_given_the_updated_weight_is_more() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 4));
        assert_eq!(4, cache_weight.get_weight_used());

        cache_weight.update(&1, 8);
        assert_eq!(8, cache_weight.get_weight_used());
    }

    #[test]
    fn update_key_weight_given_the_updated_weight_is_more_and_increase_stats() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 4));
        assert_eq!(4, cache_weight.stats_counter.weight_added());

        cache_weight.update(&1, 8);
        assert_eq!(8, cache_weight.stats_counter.weight_added());
    }

    #[test]
    fn update_key_weight_given_the_updated_weight_is_same() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 4));
        assert_eq!(4, cache_weight.get_weight_used());

        cache_weight.update(&1, 4);
        assert_eq!(4, cache_weight.get_weight_used());
    }

    #[test]
    fn update_key_weight_given_the_updated_weight_is_same_and_make_no_changes_in_stats() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 4));
        assert_eq!(4, cache_weight.stats_counter.weight_added());

        cache_weight.update(&1, 4);
        assert_eq!(4, cache_weight.stats_counter.weight_added());
    }

    #[test]
    fn delete_key_weight() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));
        assert_eq!(3, cache_weight.get_weight_used());

        let deleted_keys = DeletedKeys { keys: RwLock::new(Vec::new()) };
        let delete_hook = |key| { deleted_keys.keys.write().push(key) };
        cache_weight.delete(&1, &delete_hook);

        assert_eq!(vec!["disk"], *deleted_keys.keys.read());
        assert_eq!(0, cache_weight.get_weight_used());
        assert!(!cache_weight.contains(&1));
    }

    #[test]
    fn delete_key_weight_increase_stats() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));

        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));
        assert_eq!(3, cache_weight.get_weight_used());

        let delete_hook = |_| {};
        cache_weight.delete(&1, &delete_hook);

        assert_eq!(3, cache_weight.stats_counter.weight_removed())
    }

    #[test]
    fn clear() {
        let cache_weight = CacheWeight::new(test_cache_weight_config(), Arc::new(ConcurrentStatsCounter::new()));
        cache_weight.add(&KeyDescription::new("disk", 1, 3040, 3));

        assert_eq!(3, cache_weight.get_weight_used());
        assert!(cache_weight.contains(&1));

        cache_weight.clear();

        assert_eq!(0, cache_weight.get_weight_used());
        assert!(!cache_weight.contains(&1));
    }
}

#[cfg(test)]
mod frequency_counter_based_min_heap_samples_tests {
    use dashmap::DashMap;

    use crate::cache::policy::cache_weight::{FrequencyCounterBasedMinHeapSamples, SampledKey, WeightedKey};
    use crate::cache::types::KeyId;

    #[test]
    fn equality_of_sampled_keys() {
        let cache: DashMap<KeyId, WeightedKey<&str>> = DashMap::new();
        cache.insert(1, WeightedKey::new("disk", 3040, 3));

        let mut sampled_keys = Vec::new();
        for pair in cache.iter().by_ref() {
            sampled_keys.push(SampledKey::new(10, pair));
        }

        assert_eq!(sampled_keys[0], sampled_keys[0]);
    }

    #[test]
    fn sample_size() {
        let cache: DashMap<KeyId, WeightedKey<&str>> = DashMap::new();
        cache.insert(1, WeightedKey::new("disk", 3040, 3));
        cache.insert(2, WeightedKey::new("topic", 1090, 4));
        cache.insert(3, WeightedKey::new("SSD", 1290, 3));

        let sample = FrequencyCounterBasedMinHeapSamples::new(
            &cache,
            2,
            |_hash| { 1 },
        );

        assert_eq!(2, sample.size());
    }

    #[test]
    fn maybe_fill_in_with_empty_source() {
        let cache: DashMap<KeyId, WeightedKey<&str>> = DashMap::new();
        let mut sample = FrequencyCounterBasedMinHeapSamples::new(
            &cache,
            2,
            |_hash| { 1 },
        );

        let sample_key = sample.min_frequency_key();
        assert_eq!(None, sample_key);
    }

    #[test]
    fn maybe_fill_in_with_source_having_keys_to_fill() {
        let cache: DashMap<KeyId, WeightedKey<&str>> = DashMap::new();
        cache.insert(1, WeightedKey::new("disk", 3040, 3));
        cache.insert(2, WeightedKey::new("topic", 1090, 4));
        cache.insert(3, WeightedKey::new("SSD", 1290, 3));

        let mut sample = FrequencyCounterBasedMinHeapSamples::new(
            &cache,
            2,
            |_hash| { 1 },
        );

        assert_eq!(2, sample.size());

        let _ = sample.min_frequency_key();
        let _ = sample.maybe_fill_in();

        assert_eq!(2, sample.size());
    }

    #[test]
    fn maybe_fill_in_with_source_not_having_keys_to_fill() {
        let cache: DashMap<KeyId, WeightedKey<&str>> = DashMap::new();
        cache.insert(1, WeightedKey::new("disk", 3040, 3));
        cache.insert(2, WeightedKey::new("topic", 1090, 4));

        let mut sample = FrequencyCounterBasedMinHeapSamples::new(
            &cache,
            2,
            |_hash| { 1 },
        );

        assert_eq!(2, sample.size());
        let _ = sample.min_frequency_key();

        cache.remove(&1);
        cache.remove(&2);
        let _ = sample.maybe_fill_in();

        assert_eq!(1, sample.size());
    }

    #[test]
    fn maybe_fill_in_with_source_having_an_existing_sample_key_to_fill() {
        let cache: DashMap<KeyId, WeightedKey<&str>> = DashMap::new();
        cache.insert(1, WeightedKey::new("disk", 3040, 3));
        cache.insert(2, WeightedKey::new("topic", 1090, 4));

        let mut sample = FrequencyCounterBasedMinHeapSamples::new(
            &cache,
            2,
            |hash| match hash {
                3040 => 1,
                1090 => 2,
                _ => 0
            },
        );

        assert_eq!(2, sample.size());
        let _ = sample.min_frequency_key();

        cache.remove(&1);
        let _ = sample.maybe_fill_in();

        assert_eq!(1, sample.size());
    }

    #[test]
    fn maybe_fill_in_with_the_sample_already_containing_the_source_keys() {
        let cache: DashMap<KeyId, WeightedKey<&str>> = DashMap::new();
        cache.insert(1, WeightedKey::new("disk", 3040, 3));
        cache.insert(2, WeightedKey::new("topic", 1090, 4));

        let mut sample = FrequencyCounterBasedMinHeapSamples::new(
            &cache,
            2,
            |_hash| { 1 },
        );

        assert_eq!(2, sample.size());
        let _ = sample.maybe_fill_in();
        assert_eq!(2, sample.size());
    }

    #[test]
    fn sample_keys_with_distinct_frequencies() {
        let cache: DashMap<KeyId, WeightedKey<&str>> = DashMap::new();
        cache.insert(1, WeightedKey::new("disk", 3040, 3));
        cache.insert(2, WeightedKey::new("topic", 1090, 4));
        cache.insert(3, WeightedKey::new("SSD", 1290, 3));

        let mut sample = FrequencyCounterBasedMinHeapSamples::new(
            &cache,
            3,
            |hash| {
                match hash {
                    3040 => 1,
                    1090 => 2,
                    1290 => 3,
                    _ => 0
                }
            },
        );

        assert_eq!(1, sample.min_frequency_key().unwrap().estimated_frequency);
        assert_eq!(2, sample.min_frequency_key().unwrap().estimated_frequency);
        assert_eq!(3, sample.min_frequency_key().unwrap().estimated_frequency);
    }

    #[test]
    fn sample_keys_with_same_frequencies() {
        let cache: DashMap<KeyId, WeightedKey<&str>> = DashMap::new();
        cache.insert(10, WeightedKey::new("disk", 3040, 5));
        cache.insert(20, WeightedKey::new("topic", 1090, 2));
        cache.insert(30, WeightedKey::new("SSD", 1290, 3));

        let mut sample = FrequencyCounterBasedMinHeapSamples::new(
            &cache,
            3,
            |hash| {
                match hash {
                    3040 => 1,
                    1090 => 2,
                    1290 => 1,
                    _ => 0
                }
            },
        );

        let sampled_key = sample.min_frequency_key().unwrap();
        assert_eq!(1, sampled_key.estimated_frequency);
        assert_eq!(5, sampled_key.weight);
        assert_eq!(10, sampled_key.id);

        let sampled_key = sample.min_frequency_key().unwrap();
        assert_eq!(1, sampled_key.estimated_frequency);
        assert_eq!(3, sampled_key.weight);
        assert_eq!(30, sampled_key.id);

        let sampled_key = sample.min_frequency_key().unwrap();
        assert_eq!(2, sampled_key.estimated_frequency);
        assert_eq!(2, sampled_key.weight);
        assert_eq!(20, sampled_key.id);
    }
}