forked from databendlabs/databend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.rs
More file actions
84 lines (66 loc) · 2.88 KB
/
Copy pathcache.rs
File metadata and controls
84 lines (66 loc) · 2.88 KB
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
// Copyright 2021 Datafuse 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.
pub mod lru;
use std::borrow::Borrow;
use std::hash::Hash;
use crate::mem_sized::MemSized;
/// A trait for a cache.
pub trait Cache<K: Eq + Hash + MemSized, V: MemSized> {
/// Returns a reference to the value corresponding to the given key in the cache, if
/// any.
fn get<Q>(&mut self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized;
/// Returns a reference to the value corresponding to the key in the cache or `None` if it is
/// not present in the cache. Unlike `get`, `peek` does not update the Cache state so the key's
/// position will be unchanged.
fn peek<Q>(&self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized;
/// Returns the value corresponding to the item by policy or `None` if the
/// cache is empty. Like `peek`, `peek_by_policy` does not update the Cache state so the item's
/// position will be unchanged.
// TODO: change to fn peek_by_policy<'a>(&self) -> Option<(&'a K, &'a V)>;
fn peek_by_policy(&self) -> Option<(&K, &V)>;
/// Inserts a key-value pair into the cache. If the key already existed, the old value is
/// returned.
fn insert(&mut self, k: K, v: V) -> Option<V>;
/// Removes the given key from the cache and returns its corresponding value.
fn pop<Q>(&mut self, k: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized;
/// Removes and returns the key-value pair as a tuple by policy (Lru, Lfu, etc.).
fn pop_by_policy(&mut self) -> Option<(K, V)>;
/// Checks if the map contains the given key.
fn contains<Q>(&self, k: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized;
/// Returns the number of key-value pairs in the cache.
fn len(&self) -> usize;
/// Returns `true` if the cache contains no key-value pairs.
fn is_empty(&self) -> bool;
/// Returns the maximum bytes size of the key-value pairs the cache can hold.
fn bytes_capacity(&self) -> u64;
fn items_capacity(&self) -> u64;
fn set_bytes_capacity(&mut self, capacity: usize);
fn set_items_capacity(&mut self, capacity: usize);
/// Returns the bytes size of all the key-value pairs in the cache.
fn bytes_size(&self) -> u64;
/// Removes all key-value pairs from the cache.
fn clear(&mut self);
}