timslite 0.1.7

High-performance mmap-backed time-series storage engine with queues and journal.
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
//! Cache invalidation tests: correction, delete, retention, out-of-order write.
use std::fs;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};

static COUNTER: AtomicU64 = AtomicU64::new(0);

fn temp_dir() -> PathBuf {
    let d = std::env::temp_dir().join("timslite_integration");
    fs::create_dir_all(&d).unwrap();
    let id = COUNTER.fetch_add(1, Ordering::Relaxed);
    d.join(format!(
        "test_{:?}_{id}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ))
}

#[test]
fn global_block_cache_is_isolated_by_dataset_identifier() {
    use timslite::{Store, StoreConfig};

    let dir = temp_dir();
    let config = StoreConfig::builder().enable_journal(false).build();
    let mut store = Store::open(&dir, config).unwrap();

    store
        .create_dataset(
            "cache_scope_a",
            "data",
            64 * 1024 * 1024,
            4 * 1024 * 1024,
            6,
            0,
            0,
        )
        .unwrap();
    store
        .create_dataset(
            "cache_scope_b",
            "data",
            64 * 1024 * 1024,
            4 * 1024 * 1024,
            6,
            0,
            0,
        )
        .unwrap();

    let ds_a = store.open_dataset("cache_scope_a", "data").unwrap();
    let ds_b = store.open_dataset("cache_scope_b", "data").unwrap();
    // > 256 KiB payload cap → exclusive single-record compressed block, which
    // is the only kind that may enter the global cache.
    let payload_a = vec![b'a'; 300 * 1024];
    let payload_b = vec![b'b'; 300 * 1024];

    ds_a.write(100, &payload_a).unwrap();
    ds_b.write(100, &payload_b).unwrap();

    let read_a = ds_a.read(100).unwrap().unwrap();
    assert_eq!(read_a.1, payload_a);
    assert!(
        store.block_cache().stats().entry_count > 0,
        "single-record compressed block should populate the global cache"
    );

    let read_b = ds_b.read(100).unwrap().unwrap();
    assert_eq!(
        read_b.1, payload_b,
        "dataset B must not read dataset A's cached block at the same offset"
    );

    store.close().unwrap();
}

/// P0-A-1: Cache invalidation after correction write
///
/// Write data, read to cache, then correction write.
/// Verify the cache is invalidated and new data is returned.
#[test]
fn t31_1_cache_correction_write() {
    use timslite::{Store, StoreConfig};

    let dir = temp_dir();
    let mut store = Store::open(&dir, StoreConfig::default()).unwrap();

    store
        .create_dataset(
            "cache_corr",
            "data",
            64 * 1024 * 1024,
            4 * 1024 * 1024,
            6,
            0,
            0,
        )
        .unwrap();

    let ds = store.open_dataset("cache_corr", "data").unwrap();
    let arc = ds.clone();

    // Write original data
    {
        let lock = arc.clone();
        lock.write(100, b"original_data").unwrap();
    }

    // Read to populate cache (if cache is enabled)
    {
        let lock = arc.clone();
        let result = lock.read(100).unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().1, b"original_data");
    }

    let cache = store.block_cache();
    let count_before = cache.stats().entry_count;

    // Correction write: overwrite with new data
    {
        let lock = arc.clone();
        lock.write(100, b"corrected_data").unwrap();
    }

    let count_after = cache.stats().entry_count;
    assert!(
        count_after <= count_before,
        "cache entries should not increase after correction write: before={}, after={}",
        count_before,
        count_after
    );

    // Read again - should get corrected data (not cached old data)
    {
        let lock = arc.clone();
        let result = lock.read(100).unwrap();
        assert!(result.is_some());
        let (ts, data) = result.unwrap();
        assert_eq!(ts, 100);
        assert_eq!(data, b"corrected_data");
    }

    store.close().unwrap();
}

/// P0-A-2: Cache invalidation after delete
///
/// Write data, read to cache, then delete.
/// Verify the cache is invalidated and read returns None.
#[test]
fn t31_2_cache_delete() {
    use timslite::{Store, StoreConfig};

    let dir = temp_dir();
    let mut store = Store::open(&dir, StoreConfig::default()).unwrap();

    store
        .create_dataset(
            "cache_del",
            "data",
            64 * 1024 * 1024,
            4 * 1024 * 1024,
            6,
            0,
            0,
        )
        .unwrap();

    let ds = store.open_dataset("cache_del", "data").unwrap();
    let arc = ds.clone();

    // Write data
    {
        let lock = arc.clone();
        lock.write(100, b"to_be_deleted").unwrap();
    }

    // Read to populate cache
    {
        let lock = arc.clone();
        let result = lock.read(100).unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().1, b"to_be_deleted");
    }

    // Delete the record
    {
        let lock = arc.clone();
        lock.delete(100).unwrap();
    }

    // Read again - should return None (deleted)
    {
        let lock = arc.clone();
        let result = lock.read(100).unwrap();
        assert!(result.is_none(), "deleted record should return None");
    }

    store.close().unwrap();
}

/// P0-A-3: Cache invalidation after retention reclaim
///
/// Write data with retention window, wait for expiry, trigger reclaim.
/// Verify cache entries for expired data are invalidated.
#[test]
fn t31_3_cache_retention_reclaim() {
    use timslite::{Store, StoreConfig};

    let dir = temp_dir();
    let mut store = Store::open(&dir, StoreConfig::default()).unwrap();

    // Create dataset with small retention window
    store
        .create_dataset(
            "cache_ret",
            "data",
            64 * 1024 * 1024,
            4 * 1024 * 1024,
            6,
            0,
            100, // retention_window = 100 timestamp units
        )
        .unwrap();

    let ds = store.open_dataset("cache_ret", "data").unwrap();
    let arc = ds.clone();

    // Write data at timestamp 50 (within retention window)
    {
        let lock = arc.clone();
        lock.write(50, b"old_data").unwrap();
    }

    // Read to populate cache
    {
        let lock = arc.clone();
        let result = lock.read(50).unwrap();
        assert!(result.is_some());
    }

    // Write data at timestamp 200 (outside retention window)
    {
        let lock = arc.clone();
        lock.write(200, b"new_data").unwrap();
    }

    // Trigger retention reclaim
    {
        let lock = arc.clone();
        let _reclaimed = lock.reclaim_expired_segments().unwrap();
        // May or may not reclaim segments depending on segment boundaries
    }

    // Read old timestamp - should return None if expired
    {
        let lock = arc.clone();
        let result = lock.read(50).unwrap();
        // If retention window is 100 and latest_written_timestamp is 200,
        // then timestamp 50 is expired (200 - 50 = 150 > 100)
        if result.is_some() {
            // If still readable, it's because the segment wasn't reclaimed yet
            // This is acceptable behavior
        }
    }

    // Read new timestamp - should always be readable
    {
        let lock = arc.clone();
        let result = lock.read(200).unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().1, b"new_data");
    }

    store.close().unwrap();
}

/// P0-A-4: Cache invalidation after out-of-order write
///
/// Write data, read to cache, then out-of-order write (correction).
/// Verify the cache is invalidated.
#[test]
fn t31_4_cache_out_of_order_write() {
    use timslite::{Store, StoreConfig};

    let dir = temp_dir();
    let mut store = Store::open(&dir, StoreConfig::default()).unwrap();

    store
        .create_dataset(
            "cache_ooo",
            "data",
            64 * 1024 * 1024,
            4 * 1024 * 1024,
            6,
            0,
            0,
        )
        .unwrap();

    let ds = store.open_dataset("cache_ooo", "data").unwrap();
    let arc = ds.clone();

    // Write data at timestamps 100, 200, 300
    {
        let lock = arc.clone();
        lock.write(100, b"data_100").unwrap();
        lock.write(200, b"data_200").unwrap();
        lock.write(300, b"data_300").unwrap();
    }

    // Read all to populate cache
    {
        let lock = arc.clone();
        assert_eq!(lock.read(100).unwrap().unwrap().1, b"data_100");
        assert_eq!(lock.read(200).unwrap().unwrap().1, b"data_200");
        assert_eq!(lock.read(300).unwrap().unwrap().1, b"data_300");
    }

    // Out-of-order write: overwrite timestamp 200 with new data
    {
        let lock = arc.clone();
        lock.write(200, b"new_data_200").unwrap();
    }

    // Read again - should get new data (not cached old data)
    {
        let lock = arc.clone();
        let result = lock.read(200).unwrap();
        assert!(result.is_some());
        let (ts, data) = result.unwrap();
        assert_eq!(ts, 200);
        assert_eq!(data, b"new_data_200");
    }

    // Other timestamps should be unaffected
    {
        let lock = arc.clone();
        assert_eq!(lock.read(100).unwrap().unwrap().1, b"data_100");
        assert_eq!(lock.read(300).unwrap().unwrap().1, b"data_300");
    }

    store.close().unwrap();
}

/// P1-A-5: Cache LRU eviction behavior
///
/// Fill cache beyond watermark and verify LRU eviction works.
#[test]
fn t31_5_cache_lru_eviction() {
    use timslite::{Store, StoreConfig};

    let dir = temp_dir();
    // Use small cache to trigger eviction easily
    let config = StoreConfig::builder()
        .cache_max_memory(1024 * 1024) // 1MB cache
        .build();
    let mut store = Store::open(&dir, config).unwrap();

    store
        .create_dataset(
            "cache_lru",
            "data",
            64 * 1024 * 1024,
            4 * 1024 * 1024,
            6,
            0,
            0,
        )
        .unwrap();

    let ds = store.open_dataset("cache_lru", "data").unwrap();
    let arc = ds.clone();

    // Write enough data to exceed cache size
    // Each record ~8KB, cache is 1MB, so ~128 records to fill
    {
        let lock = arc.clone();
        for i in 0..200i64 {
            let data = vec![0xABu8; 8192]; // 8KB per record
            lock.write(i * 10 + 1, &data).unwrap();
        }
    }

    // Read all records to populate cache
    {
        let lock = arc.clone();
        for i in 0..200i64 {
            let result = lock.read(i * 10 + 1).unwrap();
            assert!(result.is_some(), "record {} should exist", i);
        }
    }

    // Cache should have evicted some entries due to LRU
    // We can't directly inspect cache state, but we can verify data is still readable
    {
        let lock = arc.clone();
        for i in 0..200i64 {
            let result = lock.read(i * 10 + 1).unwrap();
            assert!(result.is_some(), "record {} should still be readable", i);
            let (ts, data) = result.unwrap();
            assert_eq!(ts, i * 10 + 1);
            assert_eq!(data.len(), 8192);
        }
    }

    store.close().unwrap();
}