-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcheckpoint.rs
More file actions
71 lines (60 loc) · 1.75 KB
/
checkpoint.rs
File metadata and controls
71 lines (60 loc) · 1.75 KB
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
extern crate alloc;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use core::ffi::c_int;
use serde::Serialize;
use serde_json as json;
use sqlite::ResultCode;
use sqlite_nostd as sqlite;
use sqlite_nostd::{Connection, Context, Value};
use crate::create_sqlite_text_fn;
use crate::error::SQLiteError;
use crate::sync::checkpoint::{validate_checkpoint, OwnedBucketChecksum};
use crate::sync::line::Checkpoint;
#[derive(Serialize)]
struct CheckpointResult {
valid: bool,
failed_buckets: Vec<String>,
}
fn powersync_validate_checkpoint_impl(
ctx: *mut sqlite::context,
args: &[*mut sqlite::value],
) -> Result<String, SQLiteError> {
let data = args[0].text();
let checkpoint: Checkpoint = serde_json::from_str(data)?;
let db = ctx.db_handle();
let buckets: Vec<OwnedBucketChecksum> = checkpoint
.buckets
.iter()
.map(OwnedBucketChecksum::from)
.collect();
let failures = validate_checkpoint(buckets.iter(), None, db)?;
let mut failed_buckets = Vec::<String>::with_capacity(failures.len());
for failure in failures {
failed_buckets.push(failure.bucket_name);
}
let result = CheckpointResult {
valid: failed_buckets.is_empty(),
failed_buckets: failed_buckets,
};
Ok(json::to_string(&result)?)
}
create_sqlite_text_fn!(
powersync_validate_checkpoint,
powersync_validate_checkpoint_impl,
"powersync_validate_checkpoint"
);
pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
db.create_function_v2(
"powersync_validate_checkpoint",
1,
sqlite::UTF8 | sqlite::DETERMINISTIC,
None,
Some(powersync_validate_checkpoint),
None,
None,
None,
)?;
Ok(())
}