turso_sync_sdk_kit 0.4.3-pre.2

Turso sync SDK kit
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use std::sync::Arc;

use parking_lot::Mutex;
use turso_core::{MemoryIO, IO};
use turso_sdk_kit::rsapi::{str_from_c_str, TursoError};
use turso_sync_engine::{
    database_sync_engine::{self, DatabaseSyncEngine},
    database_sync_engine_io::SyncEngineIo,
    database_sync_operations::SyncEngineIoStats,
};

use crate::{
    capi,
    sync_engine_io::{self, SyncEngineIoQueue},
    turso_async_operation::{TursoAsyncOperationResult, TursoDatabaseAsyncOperation},
};

#[derive(Clone)]
pub struct TursoDatabaseSyncConfig {
    pub remote_url: Option<String>,
    pub path: String,
    pub client_name: String,
    pub long_poll_timeout_ms: Option<u32>,
    pub bootstrap_if_empty: bool,
    pub reserved_bytes: Option<usize>,
    pub partial_sync_opts: Option<turso_sync_engine::types::PartialSyncOpts>,
}

pub type PartialSyncOpts = turso_sync_engine::types::PartialSyncOpts;
pub type PartialBootstrapStrategy = turso_sync_engine::types::PartialBootstrapStrategy;
pub type DatabaseSyncStats = turso_sync_engine::types::SyncEngineStats;

impl TursoDatabaseSyncConfig {
    /// helper method to restore [TursoDatabaseSyncConfig] instance from C representation
    /// this method is used in the capi wrappers
    ///
    /// # Safety
    /// [capi::c::turso_sync_database_config_t::path] field must be valid C-string pointer
    /// [capi::c::turso_sync_database_config_t::client_name] field must be valid C-string pointer
    /// [capi::c::turso_sync_database_config_t::partial_bootstrap_strategy_query] field must be valid C-string pointer or null
    pub unsafe fn from_capi(
        config: *const capi::c::turso_sync_database_config_t,
    ) -> Result<Self, turso_sdk_kit::rsapi::TursoError> {
        if config.is_null() {
            return Err(TursoError::Misuse(
                "config pointer must be not null".to_string(),
            ));
        }
        let config = *config;
        Ok(Self {
            path: str_from_c_str(config.path)?.to_string(),
            remote_url: if config.remote_url.is_null() {
                None
            } else {
                Some(str_from_c_str(config.remote_url)?.to_string())
            },
            client_name: str_from_c_str(config.client_name)?.to_string(),
            long_poll_timeout_ms: if config.long_poll_timeout_ms == 0 {
                None
            } else {
                Some(config.long_poll_timeout_ms as u32)
            },
            bootstrap_if_empty: config.bootstrap_if_empty,
            reserved_bytes: if config.reserved_bytes == 0 {
                None
            } else {
                Some(config.reserved_bytes as usize)
            },
            partial_sync_opts: if config.partial_bootstrap_strategy_prefix != 0 {
                Some(turso_sync_engine::types::PartialSyncOpts {
                    bootstrap_strategy: Some(
                        turso_sync_engine::types::PartialBootstrapStrategy::Prefix {
                            length: config.partial_bootstrap_strategy_prefix as usize,
                        },
                    ),
                    segment_size: config.partial_bootstrap_segment_size,
                    prefetch: config.partial_bootstrap_prefetch,
                })
            } else if !config.partial_bootstrap_strategy_query.is_null() {
                let query = str_from_c_str(config.partial_bootstrap_strategy_query)?;
                Some(turso_sync_engine::types::PartialSyncOpts {
                    bootstrap_strategy: Some(
                        turso_sync_engine::types::PartialBootstrapStrategy::Query {
                            query: query.to_string(),
                        },
                    ),
                    segment_size: config.partial_bootstrap_segment_size,
                    prefetch: config.partial_bootstrap_prefetch,
                })
            } else {
                None
            },
        })
    }
}

pub struct TursoDatabaseSyncChanges {
    changes: turso_sync_engine::types::DbChangesStatus,
}

impl TursoDatabaseSyncChanges {
    pub fn empty(&self) -> bool {
        self.changes.file_slot.is_none()
    }
    pub fn to_capi(self: Box<Self>) -> *mut capi::c::turso_sync_changes_t {
        Box::into_raw(self) as *mut capi::c::turso_sync_changes_t
    }
    /// helper method to restore [TursoDatabaseSyncChanges] ref from C raw container
    /// this method is used in the capi wrappers
    ///
    /// # Safety
    /// value must be a pointer returned from [Self::to_capi] method
    pub unsafe fn ref_from_capi<'a>(
        value: *mut capi::c::turso_sync_changes_t,
    ) -> Result<&'a Self, TursoError> {
        if value.is_null() {
            Err(TursoError::Misuse("got null pointer".to_string()))
        } else {
            Ok(&*(value as *const Self))
        }
    }
    /// helper method to restore [TursoDatabaseSyncChanges] instance from C raw container
    /// this method is used in the capi wrappers
    ///
    /// # Safety
    /// value must be a pointer returned from [Self::to_capi] method
    pub unsafe fn box_from_capi(value: *const capi::c::turso_sync_changes_t) -> Box<Self> {
        Box::from_raw(value as *mut Self)
    }
}

pub struct TursoDatabaseSync<TBytes: AsRef<[u8]> + Send + Sync + 'static> {
    db_config: turso_sdk_kit::rsapi::TursoDatabaseConfig,
    sync_config: TursoDatabaseSyncConfig,
    sync_engine_opts: turso_sync_engine::database_sync_engine::DatabaseSyncEngineOpts,
    sync_engine_io_queue: SyncEngineIoStats<SyncEngineIoQueue<TBytes>>,
    sync_engine: Arc<Mutex<Option<DatabaseSyncEngine<SyncEngineIoQueue<TBytes>>>>>,
    db_io: Option<Arc<dyn IO>>,
}

fn persistent_io(partial: bool) -> Result<Arc<dyn IO>, turso_sync_engine::errors::Error> {
    #[cfg(target_os = "linux")]
    {
        if !partial {
            Ok(Arc::new(turso_core::PlatformIO::new().map_err(|e| {
                turso_sync_engine::errors::Error::DatabaseSyncEngineError(format!(
                    "Failed to create platform IO: {e}"
                ))
            })?))
        } else {
            use turso_sync_engine::sparse_io::SparseLinuxIo;

            Ok(Arc::new(SparseLinuxIo::new().map_err(|e| {
                turso_sync_engine::errors::Error::DatabaseSyncEngineError(format!(
                    "Failed to create sparse IO: {e}"
                ))
            })?))
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        Ok(Arc::new(turso_core::PlatformIO::new().map_err(|e| {
            turso_sync_engine::errors::Error::DatabaseSyncEngineError(format!(
                "Failed to create platform IO: {e}"
            ))
        })?))
    }
}

impl<TBytes: AsRef<[u8]> + Send + Sync + 'static> TursoDatabaseSync<TBytes> {
    /// create database sync holder struct but do not initialize it yet
    /// this can be useful for some environments, where IO operations must be executed in certain fashion (and open do IO under the hood)
    pub fn new(
        db_config: turso_sdk_kit::rsapi::TursoDatabaseConfig,
        sync_config: TursoDatabaseSyncConfig,
    ) -> Result<Arc<Self>, turso_sdk_kit::rsapi::TursoError> {
        let sync_engine_opts = turso_sync_engine::database_sync_engine::DatabaseSyncEngineOpts {
            remote_url: sync_config.remote_url.clone(),
            client_name: sync_config.client_name.clone(),
            tables_ignore: vec![],
            use_transform: false,
            wal_pull_batch_size: 0,
            long_poll_timeout: sync_config
                .long_poll_timeout_ms
                .map(|t| std::time::Duration::from_millis(t as u64)),
            protocol_version_hint: turso_sync_engine::types::DatabaseSyncEngineProtocolVersion::V1,
            bootstrap_if_empty: sync_config.bootstrap_if_empty,
            reserved_bytes: sync_config.reserved_bytes.unwrap_or(0),
            partial_sync_opts: sync_config.partial_sync_opts.clone(),
        };
        let is_memory = db_config.path == ":memory:";
        let db_io: Option<Arc<dyn IO>> = if is_memory {
            Some(Arc::new(MemoryIO::new()))
        } else {
            // persitent IO initialized later in order to read metadata first and decide if we need partial DB IO
            None
        };
        let sync_engine_io_queue = SyncEngineIoStats::new(SyncEngineIoQueue::new());
        Ok(Arc::new(Self {
            db_config,
            sync_config,
            sync_engine_opts,
            sync_engine_io_queue,
            sync_engine: Arc::new(Mutex::new(None)),
            db_io,
        }))
    }
    /// open the database which must be created earlier (e.g. through [Self::init])
    pub fn open(&self) -> Box<TursoDatabaseAsyncOperation> {
        let io = self.db_io.clone();
        let sync_engine_io = self.sync_engine_io_queue.clone();
        let main_db_path = self.sync_config.path.clone();
        let db_config = self.db_config.clone();
        let sync_engine_opts = self.sync_engine_opts.clone();
        let sync_engine = self.sync_engine.clone();
        Box::new(TursoDatabaseAsyncOperation::new(Box::new(move |coro| {
            Box::pin(async move {
                let metadata = database_sync_engine::DatabaseSyncEngine::read_db_meta(
                    &coro,
                    io.clone(),
                    sync_engine_io.clone(),
                    &main_db_path,
                )
                .await?;
                let Some(metadata) = metadata else {
                    return Err(turso_sync_engine::errors::Error::DatabaseSyncEngineError(
                        "metadata not found".to_string(),
                    ));
                };
                let io = match io {
                    Some(io) => io,
                    None => persistent_io(metadata.partial_bootstrap_server_revision.is_some())?,
                };
                let db_file = database_sync_engine::DatabaseSyncEngine::init_db_storage(
                    io.clone(),
                    sync_engine_io.clone(),
                    &metadata,
                    &main_db_path,
                )?;
                let main_db = turso_sdk_kit::rsapi::TursoDatabase::new(
                    turso_sdk_kit::rsapi::TursoDatabaseConfig {
                        db_file: Some(db_file),
                        io: Some(io.clone()),
                        ..db_config
                    },
                );
                main_db.open().map_err(|e| {
                    turso_sync_engine::errors::Error::DatabaseSyncEngineError(format!(
                        "unable to open database file: {e}"
                    ))
                })?;
                let main_db_core = main_db.db_core().map_err(|e| {
                    turso_sync_engine::errors::Error::DatabaseSyncEngineError(format!(
                        "unable to get core database instance: {e}",
                    ))
                })?;
                let sync_engine_opened = database_sync_engine::DatabaseSyncEngine::open_db(
                    &coro,
                    io,
                    sync_engine_io,
                    main_db_core,
                    sync_engine_opts,
                )
                .await?;
                *sync_engine.lock() = Some(sync_engine_opened);
                Ok(None)
            })
        })))
    }
    /// initialize and open the database
    pub fn create(&self) -> Box<TursoDatabaseAsyncOperation> {
        let io = self.db_io.clone();
        let sync_engine_io = self.sync_engine_io_queue.clone();
        let main_db_path = self.sync_config.path.clone();
        let db_config = self.db_config.clone();
        let sync_engine_opts = self.sync_engine_opts.clone();
        let sync_engine = self.sync_engine.clone();
        Box::new(TursoDatabaseAsyncOperation::new(Box::new(move |coro| {
            Box::pin(async move {
                let metadata = database_sync_engine::DatabaseSyncEngine::read_db_meta(
                    &coro,
                    io.clone(),
                    sync_engine_io.clone(),
                    &main_db_path,
                )
                .await?;
                let io = match io {
                    Some(io) => io,
                    None => persistent_io(if let Some(metadata) = &metadata {
                        metadata.partial_sync_opts().is_some()
                    } else {
                        sync_engine_opts.partial_sync_opts.is_some()
                    })?,
                };
                let metadata = database_sync_engine::DatabaseSyncEngine::bootstrap_db(
                    &coro,
                    io.clone(),
                    sync_engine_io.clone(),
                    &main_db_path,
                    &sync_engine_opts,
                    metadata,
                )
                .await?;
                let db_file = database_sync_engine::DatabaseSyncEngine::init_db_storage(
                    io.clone(),
                    sync_engine_io.clone(),
                    &metadata,
                    &main_db_path,
                )?;
                let main_db = turso_sdk_kit::rsapi::TursoDatabase::new(
                    turso_sdk_kit::rsapi::TursoDatabaseConfig {
                        db_file: Some(db_file),
                        io: Some(io.clone()),
                        ..db_config
                    },
                );
                main_db.open().map_err(|e| {
                    turso_sync_engine::errors::Error::DatabaseSyncEngineError(format!(
                        "unable to open database file: {e}"
                    ))
                })?;
                let main_db_core = main_db.db_core().map_err(|e| {
                    turso_sync_engine::errors::Error::DatabaseSyncEngineError(format!(
                        "unable to get core database instance: {e}",
                    ))
                })?;
                let sync_engine_opened = database_sync_engine::DatabaseSyncEngine::open_db(
                    &coro,
                    io,
                    sync_engine_io,
                    main_db_core,
                    sync_engine_opts,
                )
                .await?;
                *sync_engine.lock() = Some(sync_engine_opened);
                Ok(None)
            })
        })))
    }

    /// create tursodb connection for already opened database (with [Self::open] or [Self::create] methods)
    pub fn connect(&self) -> Box<TursoDatabaseAsyncOperation> {
        let db_config = self.db_config.clone();
        let sync_engine = self.sync_engine.clone();
        Box::new(TursoDatabaseAsyncOperation::new(Box::new(move |coro| {
            Box::pin(async move {
                let sync_engine = sync_engine.lock_arc();
                let Some(sync_engine) = &*sync_engine else {
                    return Err(turso_sync_engine::errors::Error::DatabaseSyncEngineError(
                        "sync engine must be initialized".to_string(),
                    ));
                };
                let connection = sync_engine.connect_rw(&coro).await?;
                Ok(Some(TursoAsyncOperationResult::Connection {
                    connection: turso_sdk_kit::rsapi::TursoConnection::new(&db_config, connection),
                }))
            })
        })))
    }

    /// get stats of synced database
    pub fn stats(&self) -> Box<TursoDatabaseAsyncOperation> {
        let sync_engine = self.sync_engine.clone();
        Box::new(TursoDatabaseAsyncOperation::new(Box::new(move |coro| {
            Box::pin(async move {
                let sync_engine = sync_engine.lock_arc();
                let Some(sync_engine) = &*sync_engine else {
                    return Err(turso_sync_engine::errors::Error::DatabaseSyncEngineError(
                        "sync engine must be initialized".to_string(),
                    ));
                };
                let stats = sync_engine.stats(&coro).await?;
                Ok(Some(TursoAsyncOperationResult::Stats { stats }))
            })
        })))
    }
    /// checkpoint WAL of synced database
    pub fn checkpoint(&self) -> Box<TursoDatabaseAsyncOperation> {
        let sync_engine = self.sync_engine.clone();
        Box::new(TursoDatabaseAsyncOperation::new(Box::new(move |coro| {
            Box::pin(async move {
                let sync_engine = sync_engine.lock_arc();
                let Some(sync_engine) = &*sync_engine else {
                    return Err(turso_sync_engine::errors::Error::DatabaseSyncEngineError(
                        "sync engine must be initialized".to_string(),
                    ));
                };
                sync_engine.checkpoint(&coro).await?;
                Ok(None)
            })
        })))
    }
    /// push local changes to remote for synced database
    pub fn push_changes(&self) -> Box<TursoDatabaseAsyncOperation> {
        let sync_engine = self.sync_engine.clone();
        Box::new(TursoDatabaseAsyncOperation::new(Box::new(move |coro| {
            Box::pin(async move {
                let sync_engine = sync_engine.lock_arc();
                let Some(sync_engine) = &*sync_engine else {
                    return Err(turso_sync_engine::errors::Error::DatabaseSyncEngineError(
                        "sync engine must be initialized".to_string(),
                    ));
                };
                sync_engine.push_changes_to_remote(&coro).await?;
                Ok(None)
            })
        })))
    }
    /// wait changes from remote to apply them later with [Self::apply_changes] methods
    pub fn wait_changes(&self) -> Box<TursoDatabaseAsyncOperation> {
        let sync_engine = self.sync_engine.clone();
        Box::new(TursoDatabaseAsyncOperation::new(Box::new(move |coro| {
            Box::pin(async move {
                let sync_engine = sync_engine.lock_arc();
                let Some(sync_engine) = &*sync_engine else {
                    return Err(turso_sync_engine::errors::Error::DatabaseSyncEngineError(
                        "sync engine must be initialized".to_string(),
                    ));
                };
                let changes = sync_engine.wait_changes_from_remote(&coro).await?;
                Ok(Some(TursoAsyncOperationResult::Changes {
                    changes: Box::new(TursoDatabaseSyncChanges { changes }),
                }))
            })
        })))
    }
    /// apply changes from remote locally fetched with [Self::wait_changes] method
    pub fn apply_changes(
        &self,
        changes: Box<TursoDatabaseSyncChanges>,
    ) -> Box<TursoDatabaseAsyncOperation> {
        let sync_engine = self.sync_engine.clone();
        Box::new(TursoDatabaseAsyncOperation::new(Box::new(move |coro| {
            Box::pin(async move {
                let sync_engine = sync_engine.lock_arc();
                let Some(sync_engine) = &*sync_engine else {
                    return Err(turso_sync_engine::errors::Error::DatabaseSyncEngineError(
                        "sync engine must be initialized".to_string(),
                    ));
                };
                let changes = changes.changes;
                sync_engine
                    .apply_changes_from_remote(&coro, changes)
                    .await?;
                Ok(None)
            })
        })))
    }

    /// take sync engine IO item to process
    /// note, that sync engine extends IO operation from tursodatabase with atomic file operations and HTTP
    /// that's why there is another flow to process sync-engine specific IO operations
    pub fn take_io_item(&self) -> Option<Box<sync_engine_io::SyncEngineIoQueueItem<TBytes>>> {
        self.sync_engine_io_queue.pop_front()
    }

    /// run synced database extra callbacks after execution of IO operation on the caller side
    pub fn step_io_callbacks(&self) {
        self.sync_engine_io_queue.step_io_callbacks();
    }

    /// helper method to get C raw container to the TursoDatabaseSync instance
    /// this method is used in the capi wrappers
    pub fn to_capi(self: Arc<Self>) -> *mut capi::c::turso_sync_database_t {
        Arc::into_raw(self.clone()) as *mut capi::c::turso_sync_database_t
    }

    /// helper method to restore [TursoDatabaseSync] ref from C raw container
    /// this method is used in the capi wrappers
    ///
    /// # Safety
    /// value must be a pointer returned from [Self::to_capi] method
    pub unsafe fn ref_from_capi<'a>(
        value: *const capi::c::turso_sync_database_t,
    ) -> Result<&'a Self, TursoError> {
        if value.is_null() {
            Err(TursoError::Misuse("got null pointer".to_string()))
        } else {
            Ok(&*(value as *const Self))
        }
    }

    /// helper method to restore [TursoDatabaseSync] instance from C raw container
    /// this method is used in the capi wrappers
    ///
    /// # Safety
    /// value must be a pointer returned from [Self::to_capi] method
    pub unsafe fn arc_from_capi(value: *const capi::c::turso_sync_database_t) -> Arc<Self> {
        Arc::from_raw(value as *const Self)
    }
}