Skip to main content

reifydb_store_commit/
entry.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::{
5	collections::{BTreeSet, VecDeque},
6	mem::size_of,
7	sync::{Arc, atomic::AtomicU64},
8};
9
10use reifydb_codec::key::encoded::EncodedKey;
11use reifydb_core::{common::CommitVersion, interface::store::EntryKind, metrics::heap::HeapSize};
12use reifydb_runtime::sync::{
13	map::Map,
14	mutex::Mutex,
15	rwlock::{RwLock, RwLockWriteGuard},
16};
17use reifydb_value::util::cowvec::CowVec;
18use tracing::instrument;
19
20use crate::rows::{ActiveRows, ClosedRows};
21
22pub(super) type Value = Option<CowVec<u8>>;
23
24pub(super) const ENTRY_OVERHEAD: usize = size_of::<EncodedKey>() + size_of::<CommitVersion>() + size_of::<Value>();
25
26pub(super) fn entry_bytes(key: &EncodedKey, value: &Value) -> u64 {
27	entry_bytes_with(key.heap_size(), value)
28}
29
30pub(super) fn entry_bytes_with(key_heap: usize, value: &Value) -> u64 {
31	(ENTRY_OVERHEAD + key_heap + value.as_ref().map_or(0, |bytes| bytes.len())) as u64
32}
33
34pub(super) struct Entry {
35	pub active: RwLock<ActiveRows>,
36
37	pub closed: RwLock<VecDeque<Arc<ClosedRows>>>,
38
39	pub pending: Mutex<BTreeSet<EncodedKey>>,
40
41	pub retained: Mutex<BTreeSet<EncodedKey>>,
42
43	pub key_count: AtomicU64,
44}
45
46impl Entry {
47	pub fn new() -> Self {
48		Self {
49			active: RwLock::new(ActiveRows::new()),
50			closed: RwLock::new(VecDeque::new()),
51			pending: Mutex::new(BTreeSet::new()),
52			retained: Mutex::new(BTreeSet::new()),
53			key_count: AtomicU64::new(0),
54		}
55	}
56
57	#[instrument(name = "store::multi::memory::write_acquire", level = "debug", skip_all)]
58	pub fn active_write(&self) -> RwLockWriteGuard<'_, ActiveRows> {
59		self.active.write()
60	}
61
62	pub fn closed_snapshot(&self) -> Vec<Arc<ClosedRows>> {
63		self.closed.read().iter().cloned().collect()
64	}
65}
66
67pub(super) struct Entries {
68	pub(super) data: Map<EntryKind, Arc<Entry>>,
69}
70
71impl Default for Entries {
72	fn default() -> Self {
73		Self {
74			data: Map::new(),
75		}
76	}
77}