Skip to main content

reifydb_store_column/
memory.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::sync::Arc;
5
6use crossbeam_skiplist::SkipMap;
7use reifydb_core::{
8	common::CommitVersion,
9	value::column::{compressed::CompressedColumn, data::ColumnData},
10};
11use reifydb_type::Result;
12
13use crate::{ColumnStatistics, ColumnStore, Partition, PartitionKey, statistics::merge};
14
15#[derive(Clone)]
16pub struct MemoryColumnStore {
17	partitions: Arc<SkipMap<PartitionKey, Partition>>,
18}
19
20impl MemoryColumnStore {
21	pub fn new() -> Self {
22		Self {
23			partitions: Arc::new(SkipMap::new()),
24		}
25	}
26}
27
28impl Default for MemoryColumnStore {
29	fn default() -> Self {
30		Self::new()
31	}
32}
33
34impl ColumnStore for MemoryColumnStore {
35	fn insert(&self, version: CommitVersion, columns: Vec<CompressedColumn>) -> Result<()> {
36		let key = PartitionKey::new(0, version);
37		let partition = Partition::new(key.clone(), columns);
38
39		self.partitions.insert(key, partition);
40		Ok(())
41	}
42
43	fn scan(&self, version: CommitVersion, _column_indices: &[usize]) -> Result<Vec<ColumnData>> {
44		let key = PartitionKey::new(0, version);
45
46		if let Some(_entry) = self.partitions.get(&key) {
47			todo!("decompress columns")
48		} else {
49			// No data for this version
50			Ok(vec![])
51		}
52	}
53
54	fn statistics(&self, column_index: usize) -> Option<ColumnStatistics> {
55		let stats: Vec<_> = self
56			.partitions
57			.iter()
58			.filter_map(|entry| entry.value().statistics.get(column_index).cloned())
59			.collect();
60
61		merge(&stats)
62	}
63
64	fn partition_count(&self) -> usize {
65		self.partitions.len()
66	}
67
68	fn compressed_size(&self) -> usize {
69		self.partitions.iter().map(|entry| entry.value().compressed_size).sum()
70	}
71
72	fn uncompressed_size(&self) -> usize {
73		self.partitions.iter().map(|entry| entry.value().uncompressed_size).sum()
74	}
75}