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();
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();
}
#[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();
{
let lock = arc.clone();
lock.write(100, b"original_data").unwrap();
}
{
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;
{
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
);
{
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();
}
#[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();
{
let lock = arc.clone();
lock.write(100, b"to_be_deleted").unwrap();
}
{
let lock = arc.clone();
let result = lock.read(100).unwrap();
assert!(result.is_some());
assert_eq!(result.unwrap().1, b"to_be_deleted");
}
{
let lock = arc.clone();
lock.delete(100).unwrap();
}
{
let lock = arc.clone();
let result = lock.read(100).unwrap();
assert!(result.is_none(), "deleted record should return None");
}
store.close().unwrap();
}
#[test]
fn t31_3_cache_retention_reclaim() {
use timslite::{Store, StoreConfig};
let dir = temp_dir();
let mut store = Store::open(&dir, StoreConfig::default()).unwrap();
store
.create_dataset(
"cache_ret",
"data",
64 * 1024 * 1024,
4 * 1024 * 1024,
6,
0,
100, )
.unwrap();
let ds = store.open_dataset("cache_ret", "data").unwrap();
let arc = ds.clone();
{
let lock = arc.clone();
lock.write(50, b"old_data").unwrap();
}
{
let lock = arc.clone();
let result = lock.read(50).unwrap();
assert!(result.is_some());
}
{
let lock = arc.clone();
lock.write(200, b"new_data").unwrap();
}
{
let lock = arc.clone();
let _reclaimed = lock.reclaim_expired_segments().unwrap();
}
{
let lock = arc.clone();
let result = lock.read(50).unwrap();
if result.is_some() {
}
}
{
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();
}
#[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();
{
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();
}
{
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");
}
{
let lock = arc.clone();
lock.write(200, b"new_data_200").unwrap();
}
{
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");
}
{
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();
}
#[test]
fn t31_5_cache_lru_eviction() {
use timslite::{Store, StoreConfig};
let dir = temp_dir();
let config = StoreConfig::builder()
.cache_max_memory(1024 * 1024) .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();
{
let lock = arc.clone();
for i in 0..200i64 {
let data = vec![0xABu8; 8192]; lock.write(i * 10 + 1, &data).unwrap();
}
}
{
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);
}
}
{
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();
}