reifydb-core 0.10.3

Core database interfaces and data structures for ReifyDB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

use std::{
	collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
	mem,
	ops::Add,
	sync::Arc,
};

use reifydb_codec::{
	key::encoded::EncodedKey,
	row::{bytes::EncodedBytes, shape::fingerprint::RowShapeFingerprint},
};
pub use reifydb_macro::HeapSize;
use reifydb_value::{
	byte_size::ByteSize,
	count::Count,
	util::hash::Hash128,
	value::{
		Value,
		column_view::{ColumnView, ViewData},
		container::{
			bool_array, decimal_array::DecimalView, digest_array, fixed_array, primitive, varlen_array,
		},
		date::Date,
		datetime::DateTime,
		decimal::Decimal,
		dictionary::DictionaryEntryId,
		digest::Digest,
		duration::Duration,
		identity::IdentityId,
		ordered_f32::OrderedF32,
		ordered_f64::OrderedF64,
		partition::Partition,
		row_number::RowNumber,
		time::Time,
		uuid::{Uuid4, Uuid7},
	},
};

use crate::{
	key::any::TaggedKey,
	state::{join::ContentVersion, timer::TimerKind},
	value::index::encoded::EncodedIndexKey,
};

pub trait HeapSize {
	fn heap_size(&self) -> usize;
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct StateMemory {
	pub entries: Count,
	pub bytes: ByteSize,
}

impl StateMemory {
	pub const ZERO: Self = Self {
		entries: Count::ZERO,
		bytes: ByteSize::ZERO,
	};

	pub fn new(entries: Count, bytes: ByteSize) -> Self {
		Self {
			entries,
			bytes,
		}
	}
}

impl Add for StateMemory {
	type Output = Self;

	fn add(self, rhs: Self) -> Self {
		Self {
			entries: self.entries + rhs.entries,
			bytes: self.bytes + rhs.bytes,
		}
	}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StateCompleteness {
	pub values_complete: bool,
	pub membership_complete: bool,
	pub absences_served: Count,
	pub false_positives: Count,
	pub revocations: Count,
}

impl StateCompleteness {
	pub fn merge(self, rhs: Self) -> Self {
		Self {
			values_complete: self.values_complete && rhs.values_complete,
			membership_complete: self.membership_complete && rhs.membership_complete,
			absences_served: self.absences_served + rhs.absences_served,
			false_positives: self.false_positives + rhs.false_positives,
			revocations: self.revocations + rhs.revocations,
		}
	}
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct OperatorSample {
	pub memory: Option<StateMemory>,
	pub row_number_cache: Option<StateMemory>,
}

impl OperatorSample {
	pub fn with_memory(memory: StateMemory) -> Self {
		Self {
			memory: Some(memory),
			..Self::default()
		}
	}

	pub fn with_row_number_cache(mut self, memory: StateMemory) -> Self {
		self.row_number_cache = Some(memory);
		self
	}
}

macro_rules! zero_heap {
	($($ty:ty),* $(,)?) => {
		$(impl HeapSize for $ty {
			fn heap_size(&self) -> usize {
				0
			}
		})*
	};
}

zero_heap!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64, bool, char, ());
zero_heap!(OrderedF32, OrderedF64, Date, DateTime, Time, Duration, IdentityId, Uuid4, Uuid7, RowNumber, Hash128);
zero_heap!(Partition, RowShapeFingerprint, TimerKind, ContentVersion, DictionaryEntryId);

const BIGNUM_APPROX_HEAP: usize = 32;

impl HeapSize for Decimal {
	fn heap_size(&self) -> usize {
		BIGNUM_APPROX_HEAP
	}
}

impl HeapSize for String {
	fn heap_size(&self) -> usize {
		self.capacity()
	}
}

impl HeapSize for EncodedBytes {
	fn heap_size(&self) -> usize {
		self.as_slice().len()
	}
}

impl HeapSize for EncodedKey {
	fn heap_size(&self) -> usize {
		match self {
			EncodedKey::Inline {
				..
			} => 0,
			EncodedKey::Shared(bytes) => bytes.len() + 2 * mem::size_of::<usize>(),
		}
	}
}

impl HeapSize for TaggedKey {
	fn heap_size(&self) -> usize {
		match self {
			TaggedKey::SortedViewRow(key) => key.run.len(),
			TaggedKey::PartitionedSortedViewRow(key) => key.run.len(),
			TaggedKey::RingBufferMetadata(key) => key.encoded_partition.capacity(),
			TaggedKey::QueueDeduplication(key) => key.tail.heap_size(),
			TaggedKey::IndexEntry(key) => key.key.heap_size(),
			TaggedKey::OperatorState(key) => key.suffix.capacity(),
			_ => 0,
		}
	}
}

impl HeapSize for EncodedIndexKey {
	fn heap_size(&self) -> usize {
		match self {
			EncodedIndexKey::Inline {
				..
			} => 0,
			EncodedIndexKey::Heap(bytes) => bytes.capacity(),
		}
	}
}

impl<T: HeapSize> HeapSize for Option<T> {
	fn heap_size(&self) -> usize {
		self.as_ref().map_or(0, HeapSize::heap_size)
	}
}

impl<T: HeapSize> HeapSize for Vec<T> {
	fn heap_size(&self) -> usize {
		self.capacity() * mem::size_of::<T>() + self.iter().map(HeapSize::heap_size).sum::<usize>()
	}
}

impl<T: HeapSize, const N: usize> HeapSize for [T; N] {
	fn heap_size(&self) -> usize {
		self.iter().map(HeapSize::heap_size).sum::<usize>()
	}
}

impl<T: HeapSize> HeapSize for VecDeque<T> {
	fn heap_size(&self) -> usize {
		self.capacity() * mem::size_of::<T>() + self.iter().map(HeapSize::heap_size).sum::<usize>()
	}
}

impl HeapSize for Digest {
	fn heap_size(&self) -> usize {
		self.bucket_count() * (mem::size_of::<i32>() + mem::size_of::<u64>())
	}
}

impl<T: HeapSize> HeapSize for Box<T> {
	fn heap_size(&self) -> usize {
		mem::size_of::<T>() + (**self).heap_size()
	}
}

impl<T: HeapSize> HeapSize for Arc<T> {
	fn heap_size(&self) -> usize {
		mem::size_of::<usize>() * 2 + mem::size_of::<T>() + (**self).heap_size()
	}
}

impl HeapSize for Arc<str> {
	fn heap_size(&self) -> usize {
		mem::size_of::<usize>() * 2 + self.len()
	}
}

impl<T: HeapSize> HeapSize for Arc<[T]> {
	fn heap_size(&self) -> usize {
		mem::size_of::<usize>() * 2
			+ self.len() * mem::size_of::<T>()
			+ self.iter().map(HeapSize::heap_size).sum::<usize>()
	}
}

impl<T: HeapSize> HeapSize for Box<[T]> {
	fn heap_size(&self) -> usize {
		self.len() * mem::size_of::<T>() + self.iter().map(HeapSize::heap_size).sum::<usize>()
	}
}

impl HeapSize for Box<str> {
	fn heap_size(&self) -> usize {
		self.len()
	}
}

impl<K: HeapSize, V: HeapSize> HeapSize for BTreeMap<K, V> {
	fn heap_size(&self) -> usize {
		self.len() * (mem::size_of::<K>() + mem::size_of::<V>())
			+ self.iter().map(|(k, v)| k.heap_size() + v.heap_size()).sum::<usize>()
	}
}

impl<T: HeapSize> HeapSize for BTreeSet<T> {
	fn heap_size(&self) -> usize {
		self.len() * mem::size_of::<T>() + self.iter().map(HeapSize::heap_size).sum::<usize>()
	}
}

impl<K: HeapSize, V: HeapSize, S> HeapSize for HashMap<K, V, S> {
	fn heap_size(&self) -> usize {
		self.capacity() * (mem::size_of::<K>() + mem::size_of::<V>() + 1)
			+ self.iter().map(|(k, v)| k.heap_size() + v.heap_size()).sum::<usize>()
	}
}

impl<T: HeapSize, S> HeapSize for HashSet<T, S> {
	fn heap_size(&self) -> usize {
		self.capacity() * (mem::size_of::<T>() + 1) + self.iter().map(HeapSize::heap_size).sum::<usize>()
	}
}

impl<A: HeapSize, B: HeapSize> HeapSize for (A, B) {
	fn heap_size(&self) -> usize {
		self.0.heap_size() + self.1.heap_size()
	}
}

impl<A: HeapSize, B: HeapSize, C: HeapSize> HeapSize for (A, B, C) {
	fn heap_size(&self) -> usize {
		self.0.heap_size() + self.1.heap_size() + self.2.heap_size()
	}
}

impl HeapSize for Value {
	fn heap_size(&self) -> usize {
		match self {
			Value::Utf8(text) => text.capacity(),
			Value::Blob(blob) => blob.as_bytes().len(),
			Value::Decimal(_) => BIGNUM_APPROX_HEAP,
			Value::Any(inner) => mem::size_of::<Value>() + inner.heap_size(),
			Value::Digest(digest) => digest.heap_size(),
			Value::List(items) | Value::Tuple(items) => items.heap_size(),
			Value::Record(fields) => {
				fields.capacity() * mem::size_of::<(String, Value)>()
					+ fields.iter()
						.map(|(name, value)| name.capacity() + value.heap_size())
						.sum::<usize>()
			}
			_ => 0,
		}
	}
}

impl HeapSize for ColumnView<'_> {
	fn heap_size(&self) -> usize {
		let nulls = self.logical_nulls().map_or(0, |nulls| nulls.len().div_ceil(8));
		nulls + match &self.data {
			ViewData::Digest {
				container,
				..
			} => {
				varlen_array::heap_size(*container)
					+ digest_array::iter(container)
						.flatten()
						.map(|digest| mem::size_of::<Digest>() + digest.heap_size())
						.sum::<usize>()
			}
			ViewData::Bool(a) => bool_array::heap_size(a),
			ViewData::Decimal(DecimalView::Decimal128(d)) => primitive::heap_size(*d),
			ViewData::Decimal(DecimalView::Decimal256(d)) => primitive::heap_size(*d),
			ViewData::None {
				..
			} => 0,
			ViewData::Float4(a) => primitive::heap_size(*a),
			ViewData::Float8(a) => primitive::heap_size(*a),
			ViewData::Int1(a) => primitive::heap_size(*a),
			ViewData::Int2(a) => primitive::heap_size(*a),
			ViewData::Int4(a) => primitive::heap_size(*a),
			ViewData::Int8(a) => primitive::heap_size(*a),
			ViewData::Uint1(a) => primitive::heap_size(*a),
			ViewData::Uint2(a) => primitive::heap_size(*a),
			ViewData::Uint4(a) => primitive::heap_size(*a),
			ViewData::Uint8(a) => primitive::heap_size(*a),
			ViewData::Date(t) => primitive::heap_size(*t),
			ViewData::DateTime(t) => primitive::heap_size(*t),
			ViewData::Time(t) => primitive::heap_size(*t),
			ViewData::Duration(t) => primitive::heap_size(*t),
			ViewData::IdentityId(u)
			| ViewData::Uuid4(u)
			| ViewData::Uuid7(u)
			| ViewData::Int16(u)
			| ViewData::Uint16(u)
			| ViewData::DictionaryId {
				container: u,
				..
			} => fixed_array::heap_size(u),
			ViewData::Utf8 {
				container: v,
				..
			}
			| ViewData::Enum {
				container: v,
				..
			} => varlen_array::heap_size(*v),
			ViewData::Blob {
				container: v,
				..
			}
			| ViewData::Any {
				container: v,
				..
			} => varlen_array::heap_size(*v),
		}
	}
}

#[cfg(test)]
mod tests {
	use super::HeapSize;

	#[derive(HeapSize)]
	struct DerivedSample {
		name: String,
		values: Vec<u64>,
		count: u64,
	}

	#[test]
	fn derived_heap_size_sums_all_fields() {
		// The derive must sum every field so a heap-owning field added later is picked up
		// automatically, and scalar fields must contribute zero.
		let sample = DerivedSample {
			name: String::with_capacity(32),
			values: Vec::with_capacity(4),
			count: 7,
		};
		assert_eq!(sample.count, 7);
		assert_eq!(sample.heap_size(), 32 + 4 * 8);
	}
}