Skip to main content

reifydb_core/interface/catalog/
ringbuffer.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_type::value::Value;
5use serde::{Deserialize, Serialize};
6
7use crate::interface::catalog::{
8	column::Column,
9	id::{NamespaceId, RingBufferId},
10	key::PrimaryKey,
11};
12
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14pub struct RingBuffer {
15	pub id: RingBufferId,
16	pub namespace: NamespaceId,
17	pub name: String,
18	pub columns: Vec<Column>,
19	pub capacity: u64,
20	pub primary_key: Option<PrimaryKey>,
21	pub partition_by: Vec<String>,
22}
23
24impl RingBuffer {
25	pub fn name(&self) -> &str {
26		&self.name
27	}
28}
29
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub struct RingBufferMetadata {
32	pub id: RingBufferId,
33	pub capacity: u64,
34	pub count: u64,
35	pub head: u64, // Position of oldest entry
36	pub tail: u64, // Position for next insert
37}
38
39#[derive(Debug, Clone, PartialEq)]
40pub struct PartitionedMetadata {
41	pub metadata: RingBufferMetadata,
42	pub partition_values: Vec<Value>,
43}
44
45impl RingBufferMetadata {
46	pub fn new(buffer_id: RingBufferId, capacity: u64) -> Self {
47		Self {
48			id: buffer_id,
49			capacity,
50			count: 0,
51			head: 1,
52			tail: 1,
53		}
54	}
55
56	pub fn is_full(&self) -> bool {
57		self.count >= self.capacity
58	}
59
60	pub fn is_empty(&self) -> bool {
61		self.count == 0
62	}
63}