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
//  Copyright 2024 Foyer Project Authors
//
//  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 std::{
    fmt::Debug,
    time::{Duration, Instant},
};

use ahash::RandomState;
use foyer_common::code::{HashBuilder, StorageKey, StorageValue};
use foyer_memory::CacheContext;

use crate::{HybridCache, HybridCacheEntry};

/// Writer for hybrid cache to support more flexible write APIs.
pub struct HybridCacheWriter<K, V, S = RandomState>
where
    K: StorageKey,
    V: StorageValue,
    S: HashBuilder + Debug,
{
    hybrid: HybridCache<K, V, S>,
    key: K,
}

impl<K, V, S> HybridCacheWriter<K, V, S>
where
    K: StorageKey,
    V: StorageValue,
    S: HashBuilder + Debug,
{
    pub(crate) fn new(hybrid: HybridCache<K, V, S>, key: K) -> Self {
        Self { hybrid, key }
    }

    /// Insert the entry to the hybrid cache.
    pub fn insert(self, value: V) -> HybridCacheEntry<K, V, S> {
        self.hybrid.insert(self.key, value)
    }

    /// Insert the entry with context to the hybrid cache.
    pub fn insert_with_context(self, value: V, context: CacheContext) -> HybridCacheEntry<K, V, S> {
        self.hybrid.insert_with_context(self.key, value, context)
    }

    /// Convert [`HybridCacheWriter`] to [`HybridCacheStorageWriter`].
    pub fn storage(self) -> HybridCacheStorageWriter<K, V, S> {
        HybridCacheStorageWriter::new(self.hybrid, self.key)
    }
}

/// Writer for disk cache of a hybrid cache to support more flexible write APIs.
pub struct HybridCacheStorageWriter<K, V, S = RandomState>
where
    K: StorageKey,
    V: StorageValue,
    S: HashBuilder + Debug,
{
    hybrid: HybridCache<K, V, S>,
    key: K,

    picked: Option<bool>,
    pick_duration: Duration,
}

impl<K, V, S> HybridCacheStorageWriter<K, V, S>
where
    K: StorageKey,
    V: StorageValue,
    S: HashBuilder + Debug,
{
    pub(crate) fn new(hybrid: HybridCache<K, V, S>, key: K) -> Self {
        Self {
            hybrid,
            key,
            picked: None,
            pick_duration: Duration::default(),
        }
    }

    /// Check if the entry can be admitted by the admission picker of the disk cache.
    ///
    /// `pick` is idempotent (unless `force` is called). Which means the disk cache only check its admission at most
    /// once.
    pub fn pick(&mut self) -> bool {
        if let Some(picked) = self.picked {
            return picked;
        }
        let now = Instant::now();

        let picked = self.hybrid.storage().pick(&self.key);
        self.picked = Some(picked);

        self.pick_duration = now.elapsed();

        picked
    }

    /// Force the disk cache to admit the writer.
    pub fn force(mut self) -> Self {
        self.picked = Some(true);
        self
    }

    fn insert_inner(mut self, value: V, context: Option<CacheContext>) -> Option<HybridCacheEntry<K, V, S>> {
        let now = Instant::now();

        if !self.pick() {
            return None;
        }

        let entry = match context {
            Some(context) => self.hybrid.memory().deposit_with_context(self.key, value, context),
            None => self.hybrid.memory().deposit(self.key, value),
        };
        self.hybrid.storage().enqueue(entry.clone(), true);

        self.hybrid.metrics().hybrid_insert.increment(1);
        self.hybrid
            .metrics()
            .hybrid_insert_duration
            .record(now.elapsed() + self.pick_duration);

        Some(entry)
    }

    /// Insert the entry to the disk cache only.
    pub fn insert(self, value: V) -> Option<HybridCacheEntry<K, V, S>> {
        self.insert_inner(value, None)
    }

    /// Insert the entry with context to the disk cache only.
    pub fn insert_with_context(self, value: V, context: CacheContext) -> Option<HybridCacheEntry<K, V, S>> {
        self.insert_inner(value, Some(context))
    }
}