alopex-core 0.5.0

Core storage engine for Alopex DB - LSM-tree, columnar storage, and vector index
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! SSTable データブロック向けのバッファプール(LRU キャッシュ)。
//!
//! 目的: SSTable のデータブロックをメモリに保持して、ディスク I/O を削減する(設計書 §3.5)。

use std::collections::HashMap;
use std::hash::Hash;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

/// バッファプール設定(設計書 §3.5.3)。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BufferPoolConfig {
    /// キャッシュ容量(バイト)。
    pub capacity: usize,
    /// 最小ブロック保持時間(ミリ秒)。
    ///
    /// 容量超過でエビクションが必要な場合、LRU の末尾からこの保持時間を満たす候補を探索する。
    /// ただし候補が見つからない場合は、容量制限を優先して LRU 末尾をエビクトする(ベストエフォート)。
    pub min_block_age_ms: u64,
}

impl Default for BufferPoolConfig {
    fn default() -> Self {
        Self {
            capacity: 128 * 1024 * 1024,
            min_block_age_ms: 0,
        }
    }
}

/// SSTable ブロックを一意に識別する ID(設計書 §3.5.2)。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BlockId {
    /// SSTable ファイル ID。
    pub file_id: u64,
    /// ブロックのファイル内オフセット。
    pub block_offset: u64,
}

/// キャッシュ対象のデータブロック(バイト列)。
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataBlock {
    bytes: Vec<u8>,
}

impl DataBlock {
    /// 新しいデータブロックを作成する。
    pub fn new(bytes: Vec<u8>) -> Self {
        Self { bytes }
    }

    /// ブロックの長さ(バイト)。
    pub fn len(&self) -> usize {
        self.bytes.len()
    }

    /// ブロックが空かどうか。
    pub fn is_empty(&self) -> bool {
        self.bytes.is_empty()
    }

    /// ブロックのバイト列を参照する。
    pub fn as_slice(&self) -> &[u8] {
        &self.bytes
    }
}

#[derive(Debug)]
struct CacheEntry {
    block: Arc<DataBlock>,
    size: usize,
    inserted_at: Instant,
}

/// バッファプール統計(スレッドセーフ)。
#[derive(Debug, Default)]
pub struct BufferPoolStats {
    hits: AtomicU64,
    misses: AtomicU64,
    puts: AtomicU64,
    evictions: AtomicU64,
    oversized_puts: AtomicU64,
}

/// `BufferPoolStats` のスナップショット。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BufferPoolStatsSnapshot {
    /// キャッシュヒット数。
    pub hits: u64,
    /// キャッシュミス数。
    pub misses: u64,
    /// put 呼び出し回数(キャッシュ可能なサイズに限らない)。
    pub puts: u64,
    /// エビクション回数。
    pub evictions: u64,
    /// 容量を超えるためキャッシュしなかった put 回数。
    pub oversized_puts: u64,
}

impl BufferPoolStats {
    fn snapshot(&self) -> BufferPoolStatsSnapshot {
        BufferPoolStatsSnapshot {
            hits: self.hits.load(Ordering::Relaxed),
            misses: self.misses.load(Ordering::Relaxed),
            puts: self.puts.load(Ordering::Relaxed),
            evictions: self.evictions.load(Ordering::Relaxed),
            oversized_puts: self.oversized_puts.load(Ordering::Relaxed),
        }
    }
}

/// SSTable データブロック用の LRU バッファプール。
#[derive(Debug)]
pub struct BufferPool {
    cache: RwLock<LruCache<BlockId, CacheEntry>>,
    capacity: usize,
    min_block_age: Duration,
    current_size: AtomicUsize,
    stats: BufferPoolStats,
}

impl BufferPool {
    /// 新しい `BufferPool` を作成する。
    pub fn new(config: BufferPoolConfig) -> Self {
        Self {
            cache: RwLock::new(LruCache::new()),
            capacity: config.capacity,
            min_block_age: Duration::from_millis(config.min_block_age_ms),
            current_size: AtomicUsize::new(0),
            stats: BufferPoolStats::default(),
        }
    }

    /// 現在の使用量(バイト、概算ではなく実測)。
    pub fn current_size_bytes(&self) -> usize {
        self.current_size.load(Ordering::Relaxed)
    }

    /// 容量(バイト)。
    pub fn capacity_bytes(&self) -> usize {
        self.capacity
    }

    /// 統計を取得する。
    pub fn stats(&self) -> BufferPoolStatsSnapshot {
        self.stats.snapshot()
    }

    /// ブロックを取得する(ヒット時は LRU を更新)。
    pub fn get(&self, id: &BlockId) -> Option<Arc<DataBlock>> {
        let mut cache = self.cache.write().expect("poisoned BufferPool lock");
        match cache.get(id) {
            Some(entry) => {
                self.stats.hits.fetch_add(1, Ordering::Relaxed);
                Some(Arc::clone(&entry.block))
            }
            None => {
                self.stats.misses.fetch_add(1, Ordering::Relaxed);
                None
            }
        }
    }

    /// ブロックを追加する(容量超過時は LRU でエビクション)。
    ///
    /// 戻り値:
    /// - `true`: キャッシュに格納した
    /// - `false`: ブロックが容量より大きく、キャッシュしなかった
    pub fn put(&self, id: BlockId, block: Arc<DataBlock>) -> bool {
        self.stats.puts.fetch_add(1, Ordering::Relaxed);

        let size = block.len();
        if size > self.capacity {
            self.stats.oversized_puts.fetch_add(1, Ordering::Relaxed);
            return false;
        }

        let mut cache = self.cache.write().expect("poisoned BufferPool lock");
        let now = Instant::now();

        if let Some(old) = cache.put(
            id,
            CacheEntry {
                block,
                size,
                inserted_at: now,
            },
        ) {
            let old_size = old.size;
            match size.cmp(&old_size) {
                std::cmp::Ordering::Greater => {
                    self.current_size
                        .fetch_add(size - old_size, Ordering::Relaxed);
                }
                std::cmp::Ordering::Less => {
                    self.current_size
                        .fetch_sub(old_size - size, Ordering::Relaxed);
                }
                std::cmp::Ordering::Equal => {}
            }
        } else {
            self.current_size.fetch_add(size, Ordering::Relaxed);
        }

        self.evict_while_over_capacity_locked(&mut cache, now);
        true
    }

    /// 明示的に LRU で 1 件エビクトする(エビクトできなければ `None`)。
    pub fn evict_one(&self) -> Option<(BlockId, Arc<DataBlock>)> {
        let mut cache = self.cache.write().expect("poisoned BufferPool lock");
        let now = Instant::now();
        let evicted = self.evict_one_locked(&mut cache, now, self.min_block_age);
        if let Some((id, entry)) = evicted {
            self.current_size.fetch_sub(entry.size, Ordering::Relaxed);
            self.stats.evictions.fetch_add(1, Ordering::Relaxed);
            return Some((id, entry.block));
        }
        None
    }

    fn evict_while_over_capacity_locked(
        &self,
        cache: &mut LruCache<BlockId, CacheEntry>,
        now: Instant,
    ) {
        while self.current_size.load(Ordering::Relaxed) > self.capacity {
            let Some((_, entry)) = self.evict_one_locked(cache, now, self.min_block_age) else {
                break;
            };
            self.current_size.fetch_sub(entry.size, Ordering::Relaxed);
            self.stats.evictions.fetch_add(1, Ordering::Relaxed);
        }
    }

    fn evict_one_locked(
        &self,
        cache: &mut LruCache<BlockId, CacheEntry>,
        now: Instant,
        min_age: Duration,
    ) -> Option<(BlockId, CacheEntry)> {
        if cache.len() == 0 {
            return None;
        }

        if min_age == Duration::from_millis(0) {
            return cache.pop_lru();
        }

        let mut candidate = cache.tail_index();
        while let Some(index) = candidate {
            let entry = cache.value_at(index)?;
            if now.saturating_duration_since(entry.inserted_at) >= min_age {
                return cache.remove_at(index);
            }
            candidate = cache.prev_index(index);
        }

        // 候補が見つからなければ、容量制限を優先して LRU 末尾をエビクトする(ベストエフォート)。
        cache.pop_lru()
    }
}

#[derive(Debug)]
struct Node<K, V> {
    key: K,
    value: V,
    prev: Option<usize>,
    next: Option<usize>,
}

#[derive(Debug)]
struct LruCache<K, V> {
    map: HashMap<K, usize>,
    nodes: Vec<Option<Node<K, V>>>,
    free: Vec<usize>,
    head: Option<usize>,
    tail: Option<usize>,
    len: usize,
}

impl<K, V> LruCache<K, V>
where
    K: Eq + Hash + Copy,
{
    fn new() -> Self {
        Self {
            map: HashMap::new(),
            nodes: Vec::new(),
            free: Vec::new(),
            head: None,
            tail: None,
            len: 0,
        }
    }

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

    fn tail_index(&self) -> Option<usize> {
        self.tail
    }

    fn prev_index(&self, index: usize) -> Option<usize> {
        self.nodes
            .get(index)
            .and_then(|n| n.as_ref().and_then(|n| n.prev))
    }

    fn value_at(&self, index: usize) -> Option<&V> {
        self.nodes
            .get(index)
            .and_then(|n| n.as_ref().map(|n| &n.value))
    }

    fn get(&mut self, key: &K) -> Option<&V> {
        let index = *self.map.get(key)?;
        self.move_to_front(index);
        self.value_at(index)
    }

    fn put(&mut self, key: K, value: V) -> Option<V> {
        if let Some(&index) = self.map.get(&key) {
            let old = self
                .nodes
                .get_mut(index)
                .and_then(|n| n.as_mut())
                .map(|n| std::mem::replace(&mut n.value, value));
            self.move_to_front(index);
            return old;
        }

        let index = self.alloc_index();
        let node = Node {
            key,
            value,
            prev: None,
            next: None,
        };
        self.nodes[index] = Some(node);
        self.map.insert(key, index);
        self.len += 1;
        self.attach_front(index);
        None
    }

    fn pop_lru(&mut self) -> Option<(K, V)> {
        let index = self.tail?;
        self.remove_at(index)
    }

    fn remove_at(&mut self, index: usize) -> Option<(K, V)> {
        let node = self.nodes.get_mut(index)?.take()?;
        self.detach(index, node.prev, node.next);
        self.map.remove(&node.key);
        self.free.push(index);
        self.len -= 1;
        Some((node.key, node.value))
    }

    fn alloc_index(&mut self) -> usize {
        if let Some(index) = self.free.pop() {
            return index;
        }
        let index = self.nodes.len();
        self.nodes.push(None);
        index
    }

    fn move_to_front(&mut self, index: usize) {
        if Some(index) == self.head {
            return;
        }
        let (prev, next) = match self.nodes.get(index).and_then(|n| n.as_ref()) {
            Some(node) => (node.prev, node.next),
            None => return,
        };
        self.detach(index, prev, next);
        self.attach_front(index);
    }

    fn detach(&mut self, index: usize, prev: Option<usize>, next: Option<usize>) {
        if let Some(prev) = prev {
            if let Some(Some(node)) = self.nodes.get_mut(prev) {
                node.next = next;
            }
        } else {
            self.head = next;
        }

        if let Some(next) = next {
            if let Some(Some(node)) = self.nodes.get_mut(next) {
                node.prev = prev;
            }
        } else {
            self.tail = prev;
        }

        if let Some(Some(node)) = self.nodes.get_mut(index) {
            node.prev = None;
            node.next = None;
        }
    }

    fn attach_front(&mut self, index: usize) {
        let old_head = self.head;
        self.head = Some(index);
        if let Some(old_head) = old_head {
            if let Some(Some(node)) = self.nodes.get_mut(old_head) {
                node.prev = Some(index);
            }
            if let Some(Some(node)) = self.nodes.get_mut(index) {
                node.next = Some(old_head);
                node.prev = None;
            }
        } else {
            self.tail = Some(index);
            if let Some(Some(node)) = self.nodes.get_mut(index) {
                node.prev = None;
                node.next = None;
            }
        }
    }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;

    fn block(n: u8, len: usize) -> Arc<DataBlock> {
        Arc::new(DataBlock::new(vec![n; len]))
    }

    #[test]
    fn put_get_updates_stats() {
        let pool = BufferPool::new(BufferPoolConfig {
            capacity: 1024,
            min_block_age_ms: 0,
        });

        let id = BlockId {
            file_id: 1,
            block_offset: 0,
        };
        assert!(pool.put(id, block(7, 10)));

        assert!(pool.get(&id).is_some());
        assert!(pool
            .get(&BlockId {
                file_id: 1,
                block_offset: 999,
            })
            .is_none());

        let s = pool.stats();
        assert_eq!(s.hits, 1);
        assert_eq!(s.misses, 1);
        assert_eq!(s.puts, 1);
    }

    #[test]
    fn lru_evicts_least_recent() {
        let pool = BufferPool::new(BufferPoolConfig {
            capacity: 8,
            min_block_age_ms: 0,
        });

        let a = BlockId {
            file_id: 1,
            block_offset: 0,
        };
        let b = BlockId {
            file_id: 1,
            block_offset: 1,
        };
        let c = BlockId {
            file_id: 1,
            block_offset: 2,
        };

        assert!(pool.put(a, block(1, 4)));
        assert!(pool.put(b, block(2, 4)));

        // A を最近使ったことにする → 次の put では B が追い出される。
        assert!(pool.get(&a).is_some());
        assert!(pool.put(c, block(3, 4)));

        assert!(pool.get(&a).is_some());
        assert!(pool.get(&b).is_none());
        assert!(pool.get(&c).is_some());
    }

    #[test]
    fn oversized_block_is_not_cached() {
        let pool = BufferPool::new(BufferPoolConfig {
            capacity: 4,
            min_block_age_ms: 0,
        });

        let id = BlockId {
            file_id: 1,
            block_offset: 0,
        };
        assert!(!pool.put(id, block(1, 5)));
        assert_eq!(pool.current_size_bytes(), 0);

        let s = pool.stats();
        assert_eq!(s.puts, 1);
        assert_eq!(s.oversized_puts, 1);
    }
}