timslite 0.1.0

High-performance mmap-backed time-series storage engine with queues, journal, C ABI, and Python bindings.
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
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::RwLock;
use std::time::{Duration, Instant};

use crate::error::{Result, TmslError};
use crate::util::{read_i64_le, read_u32_le};

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct CacheKey {
    pub segment_file_offset: u64,
    pub block_offset: u64,
}
impl CacheKey {
    pub fn new(s: u64, b: u64) -> Self {
        Self {
            segment_file_offset: s,
            block_offset: b,
        }
    }
}

const ENTRY_OVERHEAD: usize = 96;
fn entry_footprint(data_len: usize) -> usize {
    data_len.saturating_add(ENTRY_OVERHEAD)
}

struct CacheEntry {
    data: Vec<u8>,
    last_access_at: Instant,
    access_count: u64,
    footprint: usize,
}

#[derive(Clone, Debug)]
pub struct CacheStats {
    pub entry_count: usize,
    pub used_memory: usize,
    pub hit_count: u64,
    pub miss_count: u64,
}

pub struct BlockCache {
    max_memory: usize,
    used_memory: AtomicUsize,
    entries: RwLock<HashMap<CacheKey, CacheEntry>>,
    cache_hit_count: AtomicU64,
    cache_miss_count: AtomicU64,
}

impl BlockCache {
    pub fn new(max_memory: usize) -> Self {
        Self {
            max_memory,
            used_memory: AtomicUsize::new(0),
            entries: RwLock::new(HashMap::new()),
            cache_hit_count: AtomicU64::new(0),
            cache_miss_count: AtomicU64::new(0),
        }
    }
    pub fn is_enabled(&self) -> bool {
        self.max_memory > 0
    }
    pub fn max_memory(&self) -> usize {
        self.max_memory
    }

    pub fn get(&self, key: &CacheKey) -> Option<Vec<u8>> {
        if !self.is_enabled() {
            return None;
        }
        let mut guard = self.entries.write().unwrap();
        match guard.get_mut(key) {
            Some(entry) => {
                entry.last_access_at = Instant::now();
                entry.access_count += 1;
                self.cache_hit_count.fetch_add(1, Ordering::Relaxed);
                Some(entry.data.clone())
            }
            None => {
                self.cache_miss_count.fetch_add(1, Ordering::Relaxed);
                None
            }
        }
    }

    pub fn put(&self, key: CacheKey, data: Vec<u8>) {
        if !self.is_enabled() {
            return;
        }
        let footprint = entry_footprint(data.len());
        let mut guard = self.entries.write().unwrap();
        if guard.contains_key(&key) {
            return;
        }
        let target = (self.max_memory as f64 * 0.85) as usize;
        let current: usize = guard.values().map(|v| v.footprint).sum();
        if current + footprint > self.max_memory {
            let needed = target.saturating_sub(footprint);
            let freed = Self::evict_lru(&mut guard, needed);
            self.used_memory.fetch_sub(freed, Ordering::Relaxed);
        }
        guard.insert(
            key,
            CacheEntry {
                data,
                last_access_at: Instant::now(),
                access_count: 0,
                footprint,
            },
        );
        self.used_memory.fetch_add(footprint, Ordering::Relaxed);
    }

    pub fn invalidate(&self, key: &CacheKey) -> bool {
        if !self.is_enabled() {
            return false;
        }
        let mut guard = self.entries.write().unwrap();
        if let Some(entry) = guard.remove(key) {
            self.used_memory
                .fetch_sub(entry.footprint, Ordering::Relaxed);
            true
        } else {
            false
        }
    }

    fn evict_lru(guard: &mut HashMap<CacheKey, CacheEntry>, target_used: usize) -> usize {
        let current: usize = guard.values().map(|v| v.footprint).sum();
        if current <= target_used {
            return 0;
        }
        let need = current - target_used;
        let mut sorted: Vec<_> = guard
            .iter()
            .map(|(k, v)| (k.clone(), v.last_access_at, v.footprint))
            .collect();
        sorted.sort_by_key(|a| a.1);
        let mut freed = 0usize;
        for (k, _ts, _fp) in sorted.iter() {
            if freed >= need {
                break;
            }
            if let Some(entry) = guard.remove(k) {
                freed += entry.footprint;
            }
        }
        freed
    }

    pub fn evict_idle(&self, idle_timeout: Duration) -> usize {
        if !self.is_enabled() {
            return 0;
        }
        let mut guard = self.entries.write().unwrap();
        let now = Instant::now();
        let mut count = 0;
        let mut freed = 0usize;
        guard.retain(|_key, entry| {
            if now.duration_since(entry.last_access_at) >= idle_timeout {
                freed += entry.footprint;
                count += 1;
                false
            } else {
                true
            }
        });
        if freed > 0 {
            self.used_memory.fetch_sub(freed, Ordering::Relaxed);
        }
        count
    }

    pub fn clear(&self) {
        let mut guard = self.entries.write().unwrap();
        let freed = guard.values().map(|e| e.footprint).sum::<usize>();
        guard.clear();
        self.used_memory.fetch_sub(freed, Ordering::Relaxed);
    }

    pub fn stats(&self) -> CacheStats {
        let guard = self.entries.read().unwrap();
        CacheStats {
            entry_count: guard.len(),
            used_memory: self.used_memory.load(Ordering::Relaxed),
            hit_count: self.cache_hit_count.load(Ordering::Relaxed),
            miss_count: self.cache_miss_count.load(Ordering::Relaxed),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_cache_disabled_when_zero() {
        let c = BlockCache::new(0);
        assert!(!c.is_enabled());
        let k = CacheKey::new(0, 0);
        c.put(k.clone(), vec![1, 2, 3]);
        assert!(c.get(&k).is_none());
        assert_eq!(c.stats().entry_count, 0);
    }
    #[test]
    fn test_put_get_roundtrip() {
        let c = BlockCache::new(1024);
        let k = CacheKey::new(0, 0);
        let d = vec![10u8; 100];
        c.put(k.clone(), d.clone());
        assert_eq!(c.get(&k).unwrap(), d);
        assert_eq!(c.stats().entry_count, 1);
        assert_eq!(c.stats().hit_count, 1);
    }
    #[test]
    fn test_cache_miss_count() {
        let c = BlockCache::new(1024);
        let k = CacheKey::new(999, 0);
        assert!(c.get(&k).is_none());
        assert_eq!(c.stats().miss_count, 1);
    }
    #[test]
    fn test_lru_eviction_at_watermark() {
        let c = BlockCache::new(1000);
        for i in 0..3 {
            c.put(CacheKey::new(i, 0), vec![0u8; 200]);
        }
        assert_eq!(c.stats().entry_count, 3);
        c.put(CacheKey::new(3, 0), vec![0u8; 200]);
        assert!(
            c.stats().used_memory <= 850,
            "used {} exceeds 850",
            c.stats().used_memory
        );
    }
    #[test]
    fn test_idle_eviction() {
        let c = BlockCache::new(10240);
        let k = CacheKey::new(0, 0);
        c.put(k.clone(), vec![1u8; 100]);
        assert_eq!(c.evict_idle(Duration::from_secs(10)), 0);
        {
            let mut g = c.entries.write().unwrap();
            if let Some(e) = g.get_mut(&k) {
                e.last_access_at = Instant::now() - Duration::from_secs(20);
            }
        }
        assert_eq!(c.evict_idle(Duration::from_secs(10)), 1);
        assert_eq!(c.stats().entry_count, 0);
    }
    #[test]
    fn test_clear() {
        let c = BlockCache::new(10240);
        for i in 0..5 {
            c.put(CacheKey::new(i, 0), vec![0u8; 50]);
        }
        assert_eq!(c.stats().entry_count, 5);
        c.clear();
        assert_eq!(c.stats().entry_count, 0);
        assert_eq!(c.stats().used_memory, 0);
    }

    #[test]
    fn test_invalidate_removes_entry_and_updates_memory() {
        let c = BlockCache::new(10240);
        let k = CacheKey::new(12, 34);
        c.put(k.clone(), vec![1u8; 100]);
        assert_eq!(c.stats().entry_count, 1);
        assert!(c.stats().used_memory > 0);

        assert!(c.invalidate(&k));
        assert_eq!(c.stats().entry_count, 0);
        assert_eq!(c.stats().used_memory, 0);
        assert!(!c.invalidate(&k));
    }

    #[test]
    fn test_skip_duplicate_insertion() {
        let c = BlockCache::new(10240);
        let k = CacheKey::new(0, 0);
        c.put(k.clone(), vec![1u8; 10]);
        c.put(k.clone(), vec![2u8; 10]);
        assert_eq!(c.get(&k).unwrap(), vec![1u8; 10]);
        assert_eq!(c.stats().entry_count, 1);
    }
}

// ─── HotBlockCache ──────────────────────────────────────────────────────────

/// Per-query local Block cache, no lock contention.
pub struct HotBlockCache {
    pub(crate) current_key: Option<CacheKey>,
    /// Decompressed block payload: [data_len:4][ts:8][data:N]...
    pub(crate) current_data: Vec<u8>,
}

const RECORD_HEADER_SIZE: usize = 12;

impl HotBlockCache {
    pub fn new() -> Self {
        Self {
            current_key: None,
            current_data: Vec::new(),
        }
    }

    #[inline]
    pub fn is_hit(&self, seg_offset: u64, block_offset: u64) -> bool {
        self.current_key.as_ref() == Some(&CacheKey::new(seg_offset, block_offset))
    }

    pub fn fill(&mut self, key: CacheKey, data: Vec<u8>) {
        self.current_key = Some(key);
        self.current_data = data;
    }

    /// Extract a single record from the cached block payload.
    pub fn extract_record(&self, in_block_offset: u16) -> Result<(i64, Vec<u8>)> {
        let pos = in_block_offset as usize;
        if pos + RECORD_HEADER_SIZE > self.current_data.len() {
            return Err(TmslError::InvalidData(
                "hot block: record index out of bounds".into(),
            ));
        }

        let data_len = read_u32_le(
            self.current_data[pos..pos + 4]
                .try_into()
                .map_err(|_| TmslError::InvalidData("hot block: cannot read data_len".into()))?,
        ) as usize;

        let timestamp = read_i64_le(
            self.current_data[pos + 4..pos + 12]
                .try_into()
                .map_err(|_| TmslError::InvalidData("hot block: cannot read timestamp".into()))?,
        );

        if pos + RECORD_HEADER_SIZE + data_len > self.current_data.len() {
            return Err(TmslError::InvalidData(
                "hot block: record data out of bounds".into(),
            ));
        }

        let data = self.current_data[pos + RECORD_HEADER_SIZE..pos + RECORD_HEADER_SIZE + data_len]
            .to_vec();
        Ok((timestamp, data))
    }

    pub fn clear(&mut self) {
        self.current_key = None;
        self.current_data.clear();
    }
}

impl Default for HotBlockCache {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod hot_block_tests {
    use super::*;

    fn make_cache() -> HotBlockCache {
        let mut data = Vec::new();
        // record 1 at offset 0: data_len=5, ts=100, data=[1,2,3,4,5]
        data.extend_from_slice(&5u32.to_le_bytes());
        data.extend_from_slice(&100i64.to_le_bytes());
        data.extend_from_slice(&[1, 2, 3, 4, 5]);
        // record 2 at offset 17: data_len=3, ts=200, data=[6,7,8]
        data.extend_from_slice(&3u32.to_le_bytes());
        data.extend_from_slice(&200i64.to_le_bytes());
        data.extend_from_slice(&[6, 7, 8]);

        let mut cache = HotBlockCache::new();
        cache.fill(CacheKey::new(0, 0), data);
        cache
    }

    #[test]
    fn test_hot_block_hit_miss() {
        let cache = make_cache();
        assert!(cache.is_hit(0, 0));
        assert!(!cache.is_hit(0, 100));
        assert!(!cache.is_hit(100, 0));
    }

    #[test]
    fn test_extract_first_record() {
        let cache = make_cache();
        let (ts, data) = cache.extract_record(0).unwrap();
        assert_eq!(ts, 100);
        assert_eq!(data, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_extract_second_record() {
        let cache = make_cache();
        let (ts, data) = cache.extract_record(17).unwrap();
        assert_eq!(ts, 200);
        assert_eq!(data, vec![6, 7, 8]);
    }

    #[test]
    fn test_extract_out_of_bounds() {
        let cache = make_cache();
        assert!(cache.extract_record(100).is_err());
    }

    #[test]
    fn test_default_is_empty() {
        let cache = HotBlockCache::new();
        assert!(cache.current_key.is_none());
        assert!(!cache.is_hit(0, 0));
    }

    #[test]
    fn test_clear() {
        let mut cache = make_cache();
        assert!(cache.is_hit(0, 0));
        cache.clear();
        assert!(!cache.is_hit(0, 0));
        assert!(cache.current_data.is_empty());
    }
}