minikv 0.7.0

A production-grade distributed KV store with Raft consensus in ~1,500 lines
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
//! 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 supports snapshotting for fast recovery after a crash.
//! TTL (Time-To-Live) support enables automatic key expiration.

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

/// Blob location metadata
/// Describes the physical location of a value in the log-structured storage engine.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlobLocation {
    pub shard: u64,
    pub offset: u64,
    pub size: u64,
    pub blake3: String,
    /// Optional expiration timestamp in milliseconds since Unix epoch.
    /// If None, the key never expires.
    #[serde(default)]
    pub expires_at: Option<u64>,
}

/// In-memory index
/// HashMap-based index for fast key lookups.
/// Supports saving/loading snapshots for recovery.
#[derive(Debug, Default)]
pub struct Index {
    map: HashMap<String, BlobLocation>,
}

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

    /// Insert or update a key in the index.
    pub fn insert(&mut self, key: String, location: BlobLocation) {
        self.map.insert(key, location);
    }

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

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

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

    /// Number of keys
    pub fn len(&self) -> usize {
        self.map.len()
    }

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

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

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

    /// Clear all entries
    pub fn clear(&mut self) {
        self.map.clear();
    }

    /// Check if a key is expired.
    /// Returns true if the key exists and has expired.
    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
    }

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

    /// Remove all expired keys and return the number of keys removed.
    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
    }

    /// Get all keys that have TTL set (for monitoring)
    pub fn keys_with_ttl(&self) -> Vec<(&String, u64)> {
        self.map
            .iter()
            .filter_map(|(k, loc)| loc.expires_at.map(|exp| (k, exp)))
            .collect()
    }

    /// Save the current index as a snapshot file.
    /// Used for fast recovery after restart.
    pub fn save_snapshot(&self, path: impl AsRef<Path>) -> Result<()> {
        let file = File::create(path)?;
        let mut writer = BufWriter::new(file);

        // Write magic
        writer.write_all(SNAPSHOT_MAGIC)?;

        // Write number of entries
        writer.write_all(&(self.map.len() as u64).to_le_bytes())?;

        // Write each entry
        for (key, loc) in &self.map {
            // Key length + key
            let key_bytes = key.as_bytes();
            writer.write_all(&(key_bytes.len() as u32).to_le_bytes())?;
            writer.write_all(key_bytes)?;

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

            // BLAKE3 hash length + hash
            let hash_bytes = loc.blake3.as_bytes();
            writer.write_all(&(hash_bytes.len() as u32).to_le_bytes())?;
            writer.write_all(hash_bytes)?;

            // TTL: expires_at (0 = no expiration, >0 = timestamp)
            let expires_at = loc.expires_at.unwrap_or(0);
            writer.write_all(&expires_at.to_le_bytes())?;
        }

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

    /// Load an index snapshot from file.
    /// Returns a new Index instance populated from the snapshot.
    pub fn load_snapshot(path: impl AsRef<Path>) -> Result<Self> {
        let file = File::open(path)?;
        let mut reader = BufReader::new(file);

        // Read and verify magic
        let mut magic = [0u8; 8];
        reader.read_exact(&mut magic)?;
        // Support both v2 (KVINDEX2) and v3 (KVINDEX3) formats
        let has_ttl = &magic == b"KVINDEX3";
        if &magic != b"KVINDEX2" && !has_ttl {
            return Err(crate::Error::Corrupted("Invalid snapshot magic".into()));
        }

        // Read number of entries
        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();

        // Read each entry
        for _ in 0..num_entries {
            // Read key
            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()))?;

            // Read location
            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);

            // Read BLAKE3 hash
            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()))?;

            // Read TTL if v3 format
            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
            },
        );

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

        // Load
        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();

        // Key with past expiration (already expired)
        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),
            },
        );

        // Key with future expiration (not expired)
        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),
            },
        );

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

        // Check expiration status
        assert!(index.is_expired("expired_key"));
        assert!(!index.is_expired("valid_key"));
        assert!(!index.is_expired("permanent_key"));

        // get_if_valid should not return expired keys
        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());

        // Cleanup should remove only expired keys
        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);
    }
}