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
//! A reasonably fast, parallel, in-memory key/value store with items that can
//! expire. Designed to support read-heavy workloads.
//!
//! CornerStore uses locks and shards to retain safe, shared access to
//! internal state.
//!
//! Writes (via set) are slower, so that reads (get) can be made faster.
//!
//! ## References
//!
//! - https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/sharded/index.html

#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;

use std::hash::{Hash, Hasher};
use std::{error::Error};
use std::collections::{BTreeMap, HashMap};
use std::time::{Instant};
use std::{sync::RwLock};

#[cfg(feature = "safe-input")]
use fxhash;
#[cfg(not(feature = "safe-input"))]
use std::collections::hash_map::{DefaultHasher};

const SHARDS: usize = 128;

type Bytes = Vec<u8>; // we can tolerate

/// HiddenKey is a pre-calculated hash of the key provided by
/// the user. The key is stored in multiple places, keeping it
/// as a fixed length means that memory is more manageable.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct HiddenKey(u64);

impl HiddenKey {
    #[cfg(not(feature = "safe-input"))]
    #[inline]
    fn new(key: &[u8]) -> Self {
 
        let mut hasher = DefaultHasher::new();
        key.hash(&mut hasher);
        let ident = hasher.finish();

        HiddenKey(ident)
    }

    #[cfg(feature = "safe-input")]
    #[inline]
    fn new(key: &[u8]) -> Self {
        let ident: u64 = fxhash::hash64(key);

        HiddenKey(ident)
    }

    #[inline]
    fn shard(&self) -> usize {
        // avoid high bits and low bits, which are used by
        // the hashbrown crate (used for Rust's hashmap)

        // returns a value in the range 0..=127
        ((self.0 & 0xff000) >> 13) as usize
    }
}

#[derive(Debug, Clone)]
struct KeyValuePair {
    key: Vec<u8>,
    value: Vec<u8>,
    expiry: Option<Instant>,
}

/// The "back of house" for the CornerStore.
///
/// Keys and values are untyped byte-streams of arbitrary length
#[derive(Debug)]
pub(crate) struct BoH {
    //  TODO: make lock more granular
    /// Map times to 1 or more keys. Using BTreeMap because we'll want
    /// to take ranges of values.
    expiry_times: RwLock<BTreeMap<Instant, Vec<HiddenKey>>>,

    /// Timestamp of when expired items were evicted from the cache  
    created_at: Instant,

    /// Sharded internal storage
    data: Vec<RwLock<HashMap<HiddenKey, KeyValuePair>>>,
}

/// A thread-safe store for perishable items.
///
/// Keys and values are untyped byte-streams of arbitrary length
pub struct CornerStore(std::sync::Arc<BoH>);

impl CornerStore {
    pub fn new() -> Self {
        let mut store = Vec::with_capacity(SHARDS);
        for _ in 0..SHARDS {
            store.push(RwLock::new(HashMap::new()));
        }
        let boh = BoH {
            data: store,
            created_at: Instant::now(),
            expiry_times: RwLock::new(BTreeMap::new()),
        };
        CornerStore(std::sync::Arc::new(boh))
    }

    pub fn with_capacity(cap: usize) -> Self {
        let mut store = Vec::with_capacity(SHARDS);
        for _ in 0..SHARDS {
            store.push(RwLock::new(HashMap::with_capacity(cap / SHARDS)));
        }
        let boh = BoH {
            data: store,
            created_at: Instant::now(),
            expiry_times: RwLock::new(BTreeMap::new()),
        };
        CornerStore(std::sync::Arc::new(boh))
    }

    /// Get an item, but only if it has not expired
    pub fn get(&self, key: &[u8]) -> Result<Option<Bytes>, Box<dyn Error + '_>> {
        let hidden_key = HiddenKey::new(&key);

        let shard = &self.0.data[hidden_key.shard()];
        if let Some(kv_pair) = shard.read()?.get(&hidden_key) {
            if let Some(expiry) = kv_pair.expiry {
                if expiry <= Instant::now() {
                    return Ok(None);
                }
            }
            Ok(Some(kv_pair.value.clone()))
        } else {
            Ok(None)
        }
    }

    /// Retrieve a key/value paid, but only if they have not expired
    pub fn get_key_value(&self, key: &[u8]) -> Result<Option<(Bytes, Bytes)>, Box<dyn Error + '_>> {
        let hidden_key = HiddenKey::new(&key);
        let shard = &self.0.data[hidden_key.shard()];

        if let Some(kv_pair) = shard.read()?.get(&hidden_key) {
            Ok(Some((kv_pair.key.clone(), kv_pair.value.clone())))
        } else {
            Ok(None)
        }
    }

    /// Retrieve a value, even if it is stale
    pub fn get_unchecked(&self, key: &[u8]) -> Result<Option<Bytes>, Box<dyn Error + '_>> {
        let hidden_key = HiddenKey::new(&key);
        let shard = &self.0.data[hidden_key.shard()];

        if let Some(kv_pair) = shard.read()?.get(&hidden_key) {
            Ok(Some(kv_pair.value.clone()))
        } else {
            Ok(None)
        }
    }

    /// Retrieve a key/value pair, even if the pair is stale.
    pub fn get_key_value_unchecked(
        &self,
        key: &[u8],
    ) -> Result<Option<(Bytes, Bytes)>, Box<dyn Error + '_>> {
        let hidden_key = HiddenKey::new(&key);
        let shard = &self.0.data[hidden_key.shard()];

        if let Some(kv_pair) = shard.read()?.get(&hidden_key) {
            Ok(Some((kv_pair.key.clone(), kv_pair.value.clone())))
        } else {
            Ok(None)
        }
    }

    /// Sets key to value, overwriting any previous value. Providing an optional `expiry`
    /// time treats the key/value pair as perishable.
    pub fn set(
        &mut self,
        key: &[u8],
        val: &[u8],
        expiry: Option<Instant>,
    ) -> Result<(), Box<dyn Error + '_>> {
        // willing to take the hit allocating on insertion
        let key = key.to_vec();
        let hidden_key = HiddenKey::new(&key);
        let value = val.to_vec();

        let kv_pair = KeyValuePair { key, value, expiry };

        if let Some(time) = expiry {
            self.0
                .expiry_times
                .write()?
                .entry(time)
                .or_insert_with(|| vec![hidden_key]);
        }

        {
            let shard = hidden_key.shard();
            let _ = self.0.data[shard].write()?.insert(hidden_key, kv_pair);
        }

        Ok(())
    }

    pub fn update(
        &mut self,
        key: &[u8],
        val: &[u8],
        expiry: Option<Instant>,
    ) -> Result<(), Box<dyn Error + '_>> {
        self.set(key, val, expiry)?;
        Ok(())
    }

    /// Removes the key/value pair from the store.
    pub fn remove(&mut self, key: &[u8]) -> Result<(), Box<dyn Error + '_>> {
        let hidden_key = HiddenKey::new(&key);

        let mut expiry = None;
        {
            let shard = &self.0.data[hidden_key.shard()];
            let mut lock = shard.write()?;
            if let Some(kv_pair) = lock.get_mut(&hidden_key) {
                expiry = kv_pair.expiry.clone(); // copying out of this scope to avoid deadlock
                lock.remove(&hidden_key);
            }
        }

        if let Some(expiry) = expiry {
            if let Some(keys) = self.0.expiry_times.write()?.get_mut(&expiry) {
                keys.retain(|&x| x != hidden_key);
            }
        }

        Ok(())
    }

    /// Remove any expired perishable items from the store
    pub fn evict(&mut self) -> Result<(), Box<dyn Error + '_>> {
        let now = Instant::now();

        let mut times_to_remove = vec![];
        let mut items_to_remove: Vec<HiddenKey> = vec![];

        for (expiry, items) in self.0.expiry_times.read()?.range(self.0.created_at..now) {
            // Avoid deleting things while holding the read lock - potential deadlock
            times_to_remove.push(expiry.clone());
            items_to_remove.extend(items);
        }

        for item in &items_to_remove {
            self.0.data[item.shard()].write()?.remove(&item);
        }
        for expiry in &times_to_remove {
            &mut self.0.expiry_times.write()?.remove(expiry);
        }

        Ok(())
    }
}

// /// C API.. in a pretty bad state

// /// Indicates that the function returned successfully
// pub const CNR_OK: isize = 0;

// /// Create an empty, in-process cache. Returns a pointer
// /// to the new instance.
// #[no_mangle]
// pub extern "C" fn cnr_init() -> *const CornerStore {
//     let s = CornerStore::new();
//     &s as *const _
// }

// #[no_mangle]
// pub extern "C" fn cnr_free(s: *mut CornerStore) {
//     unsafe {
//         drop_in_place(s);
//     }
// }

// /// Returns a [libc error code]
// ///
// ///
// /// - 1: success
// /// - `libc::EINVAL` (invalid argument) indicates that
// ///
// /// [libc error code]: https://www.gnu.org/software/libc/manual/html_node/Error-Codes.html
// #[no_mangle]
// pub extern "C" fn cnr_set(
//     shuttle: *mut CornerStore,
//     key: *mut u8,
//     key_len: usize,
//     val: *mut u8,
//     val_len: usize,
//     expiry: *const i64,
// ) -> isize {
//     if key.is_null() || val.is_null() || key_len == 0 || val_len == 0 {
//         return libc::EINVAL as isize;
//     }

//     let key = unsafe { std::slice::from_raw_parts(key, key_len) };

//     let val = unsafe { std::slice::from_raw_parts(val, val_len) };

//     CNR_OK
// }

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn test_can_store_data() {
        let mut store = CornerStore::new();

        let key = b"greeting";
        let expected_value = b"hello";
        store.set(key, expected_value, None).unwrap();

        let actual_value = store.get(key).unwrap();
        assert_eq!(actual_value.unwrap(), expected_value.to_vec())
    }

    #[test]
    fn test_expired_data_is_not_returned() {
        let mut store = CornerStore::new();

        let past = Instant::now() - Duration::new(1, 0);

        let key = b"greeting";
        let value = b"hello";
        let expected_value: Result<_, Box<dyn Error>> = Ok(None);
        store.set(key, value, Some(past)).unwrap();

        let actual_value = store.get(key);
        assert_eq!(actual_value.unwrap(), expected_value.unwrap());

        let expected_unchecked_value: Result<_, Box<dyn Error>> = Ok(Some(value.to_vec()));
        let actual_unchecked_value = store.get_unchecked(key);
        assert_eq!(
            expected_unchecked_value.unwrap(),
            actual_unchecked_value.unwrap()
        );

        store.evict().unwrap();

        let actual_value: Result<_, Box<dyn Error>> = store.get(key);
        assert_eq!(actual_value.unwrap(), None);
    }

    #[test]
    fn test_fresh_data_is_returned() {
        let mut store = CornerStore::new();

        let future = Instant::now() + Duration::new(1, 0);

        let key = b"greeting";
        let value = b"hello";
        let expected_value: Result<_, Box<dyn Error>> = Ok(Some(value.to_vec()));
        store.set(key, value, Some(future)).unwrap();

        let actual_value = store.get(key);
        assert_eq!(actual_value.unwrap(), expected_value.unwrap())
    }
}