Skip to main content

dbx_core/
traits.rs

1//! Database Traits — 기능별 역할 분리
2//!
3//! Database 구조체의 메서드를 기능별 Trait으로 분리하여
4//! 명확한 책임 구분과 테스트 용이성을 제공합니다.
5
6use crate::error::DbxResult;
7
8// ════════════════════════════════════════════
9// Core CRUD Operations
10// ════════════════════════════════════════════
11
12/// 핵심 CRUD 작업을 제공하는 Trait
13pub trait DatabaseCore {
14    /// 데이터 삽입
15    fn insert(&self, table: &str, key: &[u8], value: &[u8]) -> DbxResult<()>;
16
17    /// 데이터 조회
18    fn get(&self, table: &str, key: &[u8]) -> DbxResult<Option<Vec<u8>>>;
19
20    /// 데이터 삭제
21    fn delete(&self, table: &str, key: &[u8]) -> DbxResult<()>;
22
23    /// 전체 스캔
24    fn scan(&self, table: &str) -> DbxResult<Vec<(Vec<u8>, Vec<u8>)>>;
25
26    /// 메모리 → 디스크 플러시
27    fn flush(&self) -> DbxResult<()>;
28
29    /// 배치 삽입
30    fn insert_batch(&self, table: &str, entries: Vec<(Vec<u8>, Vec<u8>)>) -> DbxResult<()>;
31
32    /// 값이 없을 때만 삽입 (Atomic CAS)
33    fn insert_if_not_exists(&self, table: &str, key: &[u8], value: &[u8]) -> DbxResult<bool>;
34
35    /// 기존 값과 비교하여 일치할 때만 새로운 값으로 교체 (Atomic CAS)
36    fn compare_and_swap(
37        &self,
38        table: &str,
39        key: &[u8],
40        expected: &[u8],
41        new_value: &[u8],
42    ) -> DbxResult<bool>;
43
44    /// 기존 값이 존재할 때만 업데이트 (Atomic CAS)
45    fn update_if_exists(&self, table: &str, key: &[u8], value: &[u8]) -> DbxResult<bool>;
46
47    /// 기존 값과 일치할 때만 삭제 (Atomic CAS)
48    fn delete_if_equals(&self, table: &str, key: &[u8], expected: &[u8]) -> DbxResult<bool>;
49}
50
51// ════════════════════════════════════════════
52// SQL Execution
53// ════════════════════════════════════════════
54
55/// SQL 실행 기능을 제공하는 Trait
56pub trait DatabaseSql {
57    /// SQL 문 실행 (RecordBatch는 engine에서 정의)
58    fn execute_sql(&self, sql: &str) -> DbxResult<Vec<arrow::record_batch::RecordBatch>>;
59
60    /// 테이블 등록 (Arrow RecordBatch)
61    fn register_table(&self, name: &str, batches: Vec<arrow::record_batch::RecordBatch>);
62
63    /// 배치 추가
64    fn append_batch(&self, table: &str, batch: arrow::record_batch::RecordBatch) -> DbxResult<()>;
65}
66
67// ════════════════════════════════════════════
68// Query Builder
69// ════════════════════════════════════════════
70
71/// Fluent 스타일 쿼리 빌더를 제공하는 Trait
72/// (구체적인 타입은 api/query.rs에서 정의)
73pub trait DatabaseQuery {
74    // Query Builder 메서드는 engine/database.rs에서 구현
75}
76
77// ════════════════════════════════════════════
78// Transaction Management
79// ════════════════════════════════════════════
80
81/// 트랜잭션 관리 기능을 제공하는 Trait
82pub trait DatabaseTransaction {
83    /// 트랜잭션 시작
84    fn begin(
85        &self,
86    ) -> DbxResult<crate::transaction::api::Transaction<'_, crate::transaction::api::Active>>;
87}
88
89// ════════════════════════════════════════════
90// Snapshot & Backup
91// ════════════════════════════════════════════
92
93/// 스냅샷 및 백업 기능을 제공하는 Trait
94pub trait DatabaseSnapshot {
95    /// 데이터베이스를 파일로 저장
96    fn save_to_file(&self, path: &str) -> DbxResult<()>;
97
98    /// 파일에서 데이터베이스 로드
99    fn load_from_file(path: &str) -> DbxResult<Self>
100    where
101        Self: Sized;
102}
103
104// ════════════════════════════════════════════
105// Native Serde Support
106// ════════════════════════════════════════════
107
108/// `serde` 기반의 구조체 직접 입출력을 지원하는 Trait
109pub trait DatabaseSerde {
110    /// 구조체를 직렬화하여 삽입
111    fn insert_struct<T: serde::Serialize>(
112        &self,
113        table: &str,
114        key: &[u8],
115        data: &T,
116    ) -> DbxResult<()>;
117
118    /// 데이터를 조회하여 구조체로 역직렬화
119    fn get_struct<T: serde::de::DeserializeOwned>(
120        &self,
121        table: &str,
122        key: &[u8],
123    ) -> DbxResult<Option<T>>;
124}