Skip to main content

reifydb_core/interface/catalog/
ringbuffer.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use serde::{Deserialize, Serialize};
5
6use crate::interface::catalog::{
7	column::ColumnDef,
8	id::{NamespaceId, RingBufferId},
9	key::PrimaryKeyDef,
10};
11
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct RingBufferDef {
14	pub id: RingBufferId,
15	pub namespace: NamespaceId,
16	pub name: String,
17	pub columns: Vec<ColumnDef>,
18	pub capacity: u64,
19	pub primary_key: Option<PrimaryKeyDef>,
20}
21
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23pub struct RingBufferMetadata {
24	pub id: RingBufferId,
25	pub capacity: u64,
26	pub count: u64,
27	pub head: u64, // Position of oldest entry
28	pub tail: u64, // Position for next insert
29}
30
31impl RingBufferMetadata {
32	pub fn new(buffer_id: RingBufferId, capacity: u64) -> Self {
33		Self {
34			id: buffer_id,
35			capacity,
36			count: 0,
37			head: 1,
38			tail: 1,
39		}
40	}
41
42	pub fn is_full(&self) -> bool {
43		self.count >= self.capacity
44	}
45
46	pub fn is_empty(&self) -> bool {
47		self.count == 0
48	}
49}