minikv 2.0.1

Distributed key-value and object store with Raft-replicated metadata, two-phase commit to volume servers, and write-ahead logs.
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
//! In-memory index for fast key lookups
//!
//! This module provides a fast, in-memory HashMap index for key-value lookups.
//! Each key maps to a BlobLocation, which describes where the value is stored on disk.
//! The index can be saved to a snapshot file and loaded back; the volume
//! server rebuilds it from the segments and the WAL, and only takes the
//! expiries from a snapshot.
//! A key can carry an expiry: reads skip expired keys, and `cleanup_expired`
//! removes them.

use crate::common::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::Path;

const SNAPSHOT_MAGIC: &[u8; 8] = b"KVINDEX3"; // Bumped version for TTL support

/// Describes the physical location of a value in the log-structured storage engine.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlobLocation {
    /// Number of the segment file that holds the record (`seg_NNNN.blob`).
    /// Despite its name, it has nothing to do with sharding: read it with
    /// [`segment`](Self::segment). A future major version will rename it.
    pub shard: u64,
    pub offset: u64,
    /// Size of the value, before compression.
    pub size: u64,
    /// BLAKE3 of the value, in hexadecimal. Empty when the index was rebuilt
    /// from a record that could not be read.
    pub blake3: String,
    #[serde(default)]
    pub expires_at: Option<u64>,
}

impl BlobLocation {
    /// Number of the segment file that holds the record. Same value as the
    /// `shard` field, under its real meaning.
    pub fn segment(&self) -> u64 {
        self.shard
    }
}

#[derive(Debug, Default)]
pub struct Index {
    map: HashMap<String, BlobLocation>,
}

impl Index {
    pub fn new() -> Self {
        Self {
            map: HashMap::new(),
        }
    }

    pub fn insert(&mut self, key: String, location: BlobLocation) {
        self.map.insert(key, location);
    }

    pub fn get(&self, key: &str) -> Option<&BlobLocation> {
        self.map.get(key)
    }

    pub fn remove(&mut self, key: &str) -> Option<BlobLocation> {
        self.map.remove(key)
    }

    pub fn contains(&self, key: &str) -> bool {
        self.map.contains_key(key)
    }

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

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

    pub fn keys(&self) -> impl Iterator<Item = &String> {
        self.map.keys()
    }

    pub fn iter(&self) -> impl Iterator<Item = (&String, &BlobLocation)> {
        self.map.iter()
    }

    pub fn clear(&mut self) {
        self.map.clear();
    }

    pub fn is_expired(&self, key: &str) -> bool {
        if let Some(loc) = self.map.get(key) {
            if let Some(expires_at) = loc.expires_at {
                let now = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_millis() as u64;
                return now > expires_at;
            }
        }
        false
    }

    pub fn get_if_valid(&self, key: &str) -> Option<&BlobLocation> {
        if self.is_expired(key) {
            return None;
        }
        self.map.get(key)
    }

    pub fn cleanup_expired(&mut self) -> usize {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        let expired_keys: Vec<String> = self
            .map
            .iter()
            .filter(|(_, loc)| loc.expires_at.map(|exp| now > exp).unwrap_or(false))
            .map(|(k, _)| k.clone())
            .collect();

        let count = expired_keys.len();
        for key in expired_keys {
            self.map.remove(&key);
        }
        count
    }

    pub fn keys_with_ttl(&self) -> Vec<(&String, u64)> {
        self.map
            .iter()
            .filter_map(|(k, loc)| loc.expires_at.map(|exp| (k, exp)))
            .collect()
    }

    pub fn save_snapshot(&self, path: impl AsRef<Path>) -> Result<()> {
        let file = File::create(path)?;
        let mut writer = BufWriter::new(file);

        writer.write_all(SNAPSHOT_MAGIC)?;

        writer.write_all(&(self.map.len() as u64).to_le_bytes())?;

        for (key, loc) in &self.map {
            let key_bytes = key.as_bytes();
            writer.write_all(&(key_bytes.len() as u32).to_le_bytes())?;
            writer.write_all(key_bytes)?;

            writer.write_all(&loc.shard.to_le_bytes())?;
            writer.write_all(&loc.offset.to_le_bytes())?;
            writer.write_all(&loc.size.to_le_bytes())?;

            let hash_bytes = loc.blake3.as_bytes();
            writer.write_all(&(hash_bytes.len() as u32).to_le_bytes())?;
            writer.write_all(hash_bytes)?;

            let expires_at = loc.expires_at.unwrap_or(0);
            writer.write_all(&expires_at.to_le_bytes())?;
        }

        writer.flush()?;
        Ok(())
    }

    pub fn load_snapshot(path: impl AsRef<Path>) -> Result<Self> {
        let file = File::open(path)?;
        let mut reader = BufReader::new(file);

        let mut magic = [0u8; 8];
        reader.read_exact(&mut magic)?;
        let has_ttl = &magic == b"KVINDEX3";
        if &magic != b"KVINDEX2" && !has_ttl {
            return Err(crate::Error::Corrupted("Invalid snapshot magic".into()));
        }

        let mut num_entries_bytes = [0u8; 8];
        reader.read_exact(&mut num_entries_bytes)?;
        let num_entries = u64::from_le_bytes(num_entries_bytes);

        let mut index = Index::new();

        for _ in 0..num_entries {
            let mut key_len_bytes = [0u8; 4];
            reader.read_exact(&mut key_len_bytes)?;
            let key_len = u32::from_le_bytes(key_len_bytes) as usize;

            let mut key_bytes = vec![0u8; key_len];
            reader.read_exact(&mut key_bytes)?;
            let key = String::from_utf8(key_bytes)
                .map_err(|_| crate::Error::Corrupted("Invalid UTF-8 in key".into()))?;

            let mut shard_bytes = [0u8; 8];
            reader.read_exact(&mut shard_bytes)?;
            let shard = u64::from_le_bytes(shard_bytes);

            let mut offset_bytes = [0u8; 8];
            reader.read_exact(&mut offset_bytes)?;
            let offset = u64::from_le_bytes(offset_bytes);

            let mut size_bytes = [0u8; 8];
            reader.read_exact(&mut size_bytes)?;
            let size = u64::from_le_bytes(size_bytes);

            let mut hash_len_bytes = [0u8; 4];
            reader.read_exact(&mut hash_len_bytes)?;
            let hash_len = u32::from_le_bytes(hash_len_bytes) as usize;

            let mut hash_bytes = vec![0u8; hash_len];
            reader.read_exact(&mut hash_bytes)?;
            let blake3 = String::from_utf8(hash_bytes)
                .map_err(|_| crate::Error::Corrupted("Invalid UTF-8 in hash".into()))?;

            let expires_at = if has_ttl {
                let mut expires_bytes = [0u8; 8];
                reader.read_exact(&mut expires_bytes)?;
                let ts = u64::from_le_bytes(expires_bytes);
                if ts == 0 {
                    None
                } else {
                    Some(ts)
                }
            } else {
                None
            };

            index.insert(
                key,
                BlobLocation {
                    shard,
                    offset,
                    size,
                    blake3,
                    expires_at,
                },
            );
        }

        Ok(index)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::blake3_hash;
    use tempfile::tempdir;

    #[test]
    fn test_index_basic() {
        let mut index = Index::new();

        index.insert(
            "key1".to_string(),
            BlobLocation {
                shard: 0,
                offset: 100,
                size: 1024,
                blake3: "abc123".to_string(),
                expires_at: None,
            },
        );

        assert_eq!(index.len(), 1);
        assert!(index.contains("key1"));

        let loc = index.get("key1").unwrap();
        assert_eq!(loc.shard, 0);
        assert_eq!(loc.offset, 100);
        assert_eq!(loc.size, 1024);

        index.remove("key1");
        assert_eq!(index.len(), 0);
    }

    #[test]
    fn test_snapshot_roundtrip() {
        let dir = tempdir().unwrap();
        let snapshot_path = dir.path().join("index.snap");

        let mut index = Index::new();
        index.insert(
            "key1".to_string(),
            BlobLocation {
                shard: 0,
                offset: 100,
                size: 1024,
                blake3: blake3_hash(b"data1"),
                expires_at: None,
            },
        );
        index.insert(
            "key2".to_string(),
            BlobLocation {
                shard: 1,
                offset: 200,
                size: 2048,
                blake3: blake3_hash(b"data2"),
                expires_at: Some(9999999999999), // Far future expiration
            },
        );

        index.save_snapshot(&snapshot_path).unwrap();

        let loaded = Index::load_snapshot(&snapshot_path).unwrap();

        assert_eq!(loaded.len(), 2);
        assert!(loaded.contains("key1"));
        assert!(loaded.contains("key2"));

        let loc1 = loaded.get("key1").unwrap();
        assert_eq!(loc1.offset, 100);
        assert_eq!(loc1.expires_at, None);

        let loc2 = loaded.get("key2").unwrap();
        assert_eq!(loc2.expires_at, Some(9999999999999));
    }

    #[test]
    fn test_ttl_expiration() {
        let mut index = Index::new();

        let past_time = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64
            - 1000; // 1 second in the past

        index.insert(
            "expired_key".to_string(),
            BlobLocation {
                shard: 0,
                offset: 0,
                size: 100,
                blake3: "test".to_string(),
                expires_at: Some(past_time),
            },
        );

        let future_time = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64
            + 60000; // 60 seconds in the future

        index.insert(
            "valid_key".to_string(),
            BlobLocation {
                shard: 0,
                offset: 100,
                size: 100,
                blake3: "test".to_string(),
                expires_at: Some(future_time),
            },
        );

        index.insert(
            "permanent_key".to_string(),
            BlobLocation {
                shard: 0,
                offset: 200,
                size: 100,
                blake3: "test".to_string(),
                expires_at: None,
            },
        );

        assert!(index.is_expired("expired_key"));
        assert!(!index.is_expired("valid_key"));
        assert!(!index.is_expired("permanent_key"));

        assert!(index.get_if_valid("expired_key").is_none());
        assert!(index.get_if_valid("valid_key").is_some());
        assert!(index.get_if_valid("permanent_key").is_some());

        let removed = index.cleanup_expired();
        assert_eq!(removed, 1);
        assert_eq!(index.len(), 2);
        assert!(!index.contains("expired_key"));
        assert!(index.contains("valid_key"));
        assert!(index.contains("permanent_key"));
    }

    #[test]
    fn test_keys_with_ttl() {
        let mut index = Index::new();

        index.insert(
            "key_with_ttl".to_string(),
            BlobLocation {
                shard: 0,
                offset: 0,
                size: 100,
                blake3: "test".to_string(),
                expires_at: Some(12345),
            },
        );

        index.insert(
            "key_without_ttl".to_string(),
            BlobLocation {
                shard: 0,
                offset: 100,
                size: 100,
                blake3: "test".to_string(),
                expires_at: None,
            },
        );

        let keys_with_ttl = index.keys_with_ttl();
        assert_eq!(keys_with_ttl.len(), 1);
        assert_eq!(keys_with_ttl[0].0, "key_with_ttl");
        assert_eq!(keys_with_ttl[0].1, 12345);
    }
}