1pub mod batch;
70pub mod column_access;
71pub mod connection;
72pub mod context_query;
73pub mod crud;
74pub mod error;
75pub mod path_query;
76pub mod query;
77pub mod recovery;
78pub mod result;
79pub mod schema;
80pub mod storage;
81pub mod transaction;
82pub mod vectors;
83
84pub use connection::DurableConnection;
86pub type Connection = DurableConnection;
88
89pub type Database = DurableConnection;
92
93pub use connection::ToonConnection;
95pub type InMemoryConnection = ToonConnection;
97
98pub use batch::{BatchOp, BatchResult, BatchWriter};
99pub use column_access::{ColumnView, TypedColumn};
100#[cfg(feature = "embedded")]
101pub use connection::EmbeddedConnection;
102pub use connection::{ConnectionConfig, DurableStats, RecoveryResult, SyncModeClient};
103pub use context_query::{ContextQueryBuilder, ContextQueryResult, SectionBuilder, SectionContent};
104pub use crud::{DeleteResult, InsertResult, RowBuilder, UpdateResult};
105pub use path_query::PathQuery;
106pub use result::{ResultMetrics, ToonResult};
107pub use schema::{SchemaBuilder, TableDescription};
108pub use transaction::{ClientTransaction, IsolationLevel, SnapshotReader};
109pub use vectors::{SearchResult, VectorCollection};
110#[allow(deprecated)]
112pub use batch::{GroupCommitBuffer, GroupCommitConfig};
113pub use error::ClientError;
114pub use query::{QueryExecutor, QueryResult};
115pub use recovery::{CheckpointResult, RecoveryManager, RecoveryStatus, WalVerificationResult};
116
117pub use toondb_storage::ColumnarQueryResult;
119
120use std::path::Path;
121use std::sync::Arc;
122
123pub struct ToonClient {
135 connection: Arc<ToonConnection>,
136 config: ClientConfig,
137}
138
139#[derive(Debug, Clone)]
141pub struct ClientConfig {
142 pub token_budget: Option<usize>,
144 pub streaming: bool,
146 pub output_format: OutputFormat,
148 pub pool_size: usize,
150}
151
152#[derive(Debug, Clone, Copy, PartialEq, Eq)]
154pub enum OutputFormat {
155 Toon,
157 Json,
159 Columnar,
161}
162
163impl Default for ClientConfig {
164 fn default() -> Self {
165 Self {
166 token_budget: None,
167 streaming: false,
168 output_format: OutputFormat::Toon,
169 pool_size: 10,
170 }
171 }
172}
173
174impl ToonClient {
175 pub fn open(path: impl AsRef<Path>) -> Result<Self, ClientError> {
177 let connection = ToonConnection::open(path)?;
178 Ok(Self {
179 connection: Arc::new(connection),
180 config: ClientConfig::default(),
181 })
182 }
183
184 pub fn open_with_config(
186 path: impl AsRef<Path>,
187 config: ClientConfig,
188 ) -> Result<Self, ClientError> {
189 let connection = ToonConnection::open(path)?;
190 Ok(Self {
191 connection: Arc::new(connection),
192 config,
193 })
194 }
195
196 pub fn with_token_budget(mut self, budget: usize) -> Self {
198 self.config.token_budget = Some(budget);
199 self
200 }
201
202 pub fn query(&self, path: &str) -> PathQuery<'_> {
205 PathQuery::from_path(&self.connection, path)
206 }
207
208 pub fn vectors(&self, name: &str) -> Result<VectorCollection, ClientError> {
210 VectorCollection::open(&self.connection, name)
211 }
212
213 pub fn begin(&self) -> Result<ClientTransaction<'_>, ClientError> {
215 ClientTransaction::begin(&self.connection, IsolationLevel::SnapshotIsolation)
216 }
217
218 pub fn begin_with_isolation(
220 &self,
221 isolation: IsolationLevel,
222 ) -> Result<ClientTransaction<'_>, ClientError> {
223 ClientTransaction::begin(&self.connection, isolation)
224 }
225
226 pub fn snapshot(&self) -> Result<SnapshotReader<'_>, ClientError> {
228 SnapshotReader::now(&self.connection)
229 }
230
231 pub fn execute(&self, sql: &str) -> Result<QueryResult, ClientError> {
233 self.connection.query_sql(sql)
234 }
235
236 pub fn connection(&self) -> &ToonConnection {
238 &self.connection
239 }
240
241 pub fn stats(&self) -> ClientStats {
243 self.connection.stats()
244 }
245
246 pub fn token_budget(&self) -> Option<usize> {
248 self.config.token_budget
249 }
250
251 pub fn output_format(&self) -> OutputFormat {
253 self.config.output_format
254 }
255}
256
257#[cfg(feature = "embedded")]
271pub struct DurableToonClient {
272 connection: Arc<EmbeddedConnection>,
273 config: ClientConfig,
274}
275
276#[cfg(feature = "embedded")]
277impl DurableToonClient {
278 pub fn open(path: impl AsRef<Path>) -> Result<Self, ClientError> {
280 let connection = EmbeddedConnection::open(path)?;
281 Ok(Self {
282 connection: Arc::new(connection),
283 config: ClientConfig::default(),
284 })
285 }
286
287 pub fn from_connection(connection: Arc<EmbeddedConnection>) -> Self {
289 Self {
290 connection,
291 config: ClientConfig::default(),
292 }
293 }
294
295 pub fn open_with_config(
297 path: impl AsRef<Path>,
298 config: ClientConfig,
299 db_config: toondb_storage::database::DatabaseConfig,
300 ) -> Result<Self, ClientError> {
301 let connection = EmbeddedConnection::open_with_config(path, db_config)?;
302 Ok(Self {
303 connection: Arc::new(connection),
304 config,
305 })
306 }
307
308 pub fn with_token_budget(mut self, budget: usize) -> Self {
310 self.config.token_budget = Some(budget);
311 self
312 }
313
314 pub fn begin(&self) -> Result<(), ClientError> {
316 self.connection.begin()
317 }
318
319 pub fn commit(&self) -> Result<u64, ClientError> {
321 self.connection.commit()
322 }
323
324 pub fn abort(&self) -> Result<(), ClientError> {
326 self.connection.abort()
327 }
328
329 pub fn put(&self, path: &str, value: &[u8]) -> Result<(), ClientError> {
331 self.connection.put(path, value)
332 }
333
334 pub fn get(&self, path: &str) -> Result<Option<Vec<u8>>, ClientError> {
336 self.connection.get(path)
337 }
338
339 pub fn delete(&self, path: &str) -> Result<(), ClientError> {
341 self.connection.delete(path)
342 }
343
344 pub fn scan(&self, prefix: &str) -> Result<Vec<(String, Vec<u8>)>, ClientError> {
346 self.connection.scan(prefix)
347 }
348
349 pub fn stats(&self) -> ClientStats {
351 self.connection.stats()
352 }
353
354 pub fn fsync(&self) -> Result<(), ClientError> {
356 self.connection.fsync()
357 }
358
359 pub fn connection(&self) -> &EmbeddedConnection {
361 &self.connection
362 }
363
364 pub fn token_budget(&self) -> Option<usize> {
366 self.config.token_budget
367 }
368
369 pub fn output_format(&self) -> OutputFormat {
371 self.config.output_format
372 }
373}
374
375#[derive(Debug, Clone)]
377pub struct ClientStats {
378 pub queries_executed: u64,
380 pub toon_tokens_emitted: u64,
382 pub json_tokens_equivalent: u64,
384 pub token_savings_percent: f64,
386 pub cache_hit_rate: f64,
388}
389
390pub mod prelude {
392 #[cfg(feature = "embedded")]
393 pub use crate::DurableToonClient;
394 pub use crate::path_query::CompareOp;
395 pub use crate::{
396 ClientConfig,
397 ClientError,
398 ClientStats,
399 ClientTransaction,
400 Connection,
402 DeleteResult,
403 DurableConnection,
404 InMemoryConnection,
405 InsertResult,
406 IsolationLevel,
407 OutputFormat,
408 PathQuery,
409 ResultMetrics,
410 RowBuilder,
411 SchemaBuilder,
412 SearchResult,
413 SnapshotReader,
414 TableDescription,
415 ToonClient,
416 ToonResult,
417 UpdateResult,
418 VectorCollection,
419 };
420 pub use toondb_core::toon::{ToonType, ToonValue};
421}