Skip to content

Commit 7676b67

Browse files
committed
Remove unused code and fix linter issues.
1 parent a8396b1 commit 7676b67

File tree

7 files changed

+37
-226
lines changed

7 files changed

+37
-226
lines changed

crates/core/src/error.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use alloc::string::{String, ToString};
22
use core::error::Error;
3-
use sqlite_nostd::{Connection, ResultCode, sqlite3};
4-
3+
use sqlite_nostd::{sqlite3, Connection, ResultCode};
54

65
#[derive(Debug)]
76
pub struct SQLiteError(pub ResultCode, pub Option<String>);
87

9-
108
impl core::fmt::Display for SQLiteError {
119
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1210
write!(f, "{:?}", self)
@@ -16,21 +14,10 @@ impl core::fmt::Display for SQLiteError {
1614
impl Error for SQLiteError {}
1715

1816
pub trait PSResult<T> {
19-
fn into_result(self) -> Result<T, SQLiteError>;
2017
fn into_db_result(self, db: *mut sqlite3) -> Result<T, SQLiteError>;
2118
}
2219

2320
impl<T> PSResult<T> for Result<T, ResultCode> {
24-
fn into_result(self) -> Result<T, SQLiteError> {
25-
if let Err(code) = self {
26-
Err(SQLiteError(code, None))
27-
} else if let Ok(r) = self {
28-
Ok(r)
29-
} else {
30-
Err(SQLiteError(ResultCode::ABORT, None))
31-
}
32-
}
33-
3421
fn into_db_result(self, db: *mut sqlite3) -> Result<T, SQLiteError> {
3522
if let Err(code) = self {
3623
let message = db.errmsg().unwrap_or(String::from("Conversion error"));

crates/core/src/kv.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ extern crate alloc;
22

33
use alloc::format;
44
use alloc::string::{String, ToString};
5-
use alloc::vec::Vec;
65
use core::ffi::c_int;
76
use core::slice;
87

9-
use serde::{Deserialize, Serialize};
10-
use serde_json as json;
118
use sqlite::ResultCode;
129
use sqlite_nostd as sqlite;
13-
use sqlite_nostd::{Connection, Context, Value};
10+
use sqlite_nostd::{Connection, Context};
1411

1512
use crate::create_sqlite_optional_text_fn;
1613
use crate::create_sqlite_text_fn;
1714
use crate::error::SQLiteError;
18-
use crate::sync_types::Checkpoint;
1915

2016
fn powersync_client_id_impl(
2117
ctx: *mut sqlite::context,
22-
args: &[*mut sqlite::value],
18+
_args: &[*mut sqlite::value],
2319
) -> Result<String, SQLiteError> {
2420
let db = ctx.db_handle();
2521

@@ -45,7 +41,7 @@ create_sqlite_text_fn!(
4541

4642
fn powersync_last_synced_at_impl(
4743
ctx: *mut sqlite::context,
48-
args: &[*mut sqlite::value],
44+
_args: &[*mut sqlite::value],
4945
) -> Result<Option<String>, SQLiteError> {
5046
let db = ctx.db_handle();
5147

crates/core/src/operations.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
use alloc::format;
22
use alloc::string::{String, ToString};
3-
use alloc::vec::Vec;
4-
use serde::{Deserialize, Deserializer, Serialize};
5-
use serde_json as json;
63

74
use crate::error::{PSResult, SQLiteError};
85
use sqlite_nostd as sqlite;
96
use sqlite_nostd::{Connection, ResultCode};
107

118
use crate::ext::SafeManagedStmt;
12-
use crate::sync_types::{BucketChecksum, Checkpoint, StreamingSyncLine};
139
use crate::util::*;
1410

1511
// Run inside a transaction
@@ -142,7 +138,7 @@ INSERT INTO ps_oplog(bucket, op_id, op, key, row_type, row_id, data, hash, super
142138
supersede_statement.reset()?;
143139

144140
let should_skip_remove = !superseded && op == "REMOVE";
145-
if (should_skip_remove) {
141+
if should_skip_remove {
146142
// If a REMOVE statement did not replace (supersede) any previous
147143
// operations, we do not need to persist it.
148144
// The same applies if the bucket was not synced to the local db yet,
@@ -307,9 +303,3 @@ pub fn delete_bucket(db: *mut sqlite::sqlite3, name: &str) -> Result<(), SQLiteE
307303

308304
Ok(())
309305
}
310-
311-
pub fn stream_operation(db: *mut sqlite::sqlite3, data: &str) -> Result<(), SQLiteError> {
312-
let line: StreamingSyncLine = serde_json::from_str(data)?;
313-
314-
Ok(())
315-
}

crates/core/src/operations_vtab.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@ use core::slice;
77
use sqlite::{Connection, ResultCode, Value};
88
use sqlite_nostd as sqlite;
99

10-
use crate::operations::{clear_remove_ops, delete_bucket, delete_pending_buckets, insert_operation, stream_operation};
10+
use crate::operations::{
11+
clear_remove_ops, delete_bucket, delete_pending_buckets, insert_operation,
12+
};
1113
use crate::sync_local::sync_local;
12-
use crate::sync_types::Checkpoint;
1314
use crate::vtab_util::*;
1415

1516
#[repr(C)]
1617
struct VirtualTable {
1718
base: sqlite::vtab,
1819
db: *mut sqlite::sqlite3,
1920

20-
target_checkpoint: Option<Checkpoint>,
2121
target_applied: bool,
22-
target_validated: bool
22+
target_validated: bool,
2323
}
2424

25-
2625
extern "C" fn connect(
2726
db: *mut sqlite::sqlite3,
2827
_aux: *mut c_void,
@@ -31,7 +30,8 @@ extern "C" fn connect(
3130
vtab: *mut *mut sqlite::vtab,
3231
_err: *mut *mut c_char,
3332
) -> c_int {
34-
if let Err(rc) = sqlite::declare_vtab(db, "CREATE TABLE powersync_operations(op TEXT, data TEXT);")
33+
if let Err(rc) =
34+
sqlite::declare_vtab(db, "CREATE TABLE powersync_operations(op TEXT, data TEXT);")
3535
{
3636
return rc as c_int;
3737
}
@@ -44,9 +44,8 @@ extern "C" fn connect(
4444
zErrMsg: core::ptr::null_mut(),
4545
},
4646
db,
47-
target_checkpoint: None,
4847
target_validated: false,
49-
target_applied: false
48+
target_applied: false,
5049
}));
5150
*vtab = tab.cast::<sqlite::vtab>();
5251
let _ = sqlite::vtab_config(db, 0);
@@ -102,10 +101,7 @@ extern "C" fn update(
102101
} else if op == "delete_bucket" {
103102
let result = delete_bucket(db, data);
104103
vtab_result(vtab, result)
105-
} else if op == "stream" {
106-
let result = stream_operation(db, data);
107-
vtab_result(vtab, result)
108-
} else {
104+
} else {
109105
ResultCode::MISUSE as c_int
110106
}
111107
} else {

crates/core/src/sync_types.rs

+2-165
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
use alloc::string::String;
22
use alloc::vec::Vec;
3-
use serde::{de, Deserialize, Deserializer, Serialize};
4-
use serde_json as json;
3+
use serde::{Deserialize, Serialize};
54

6-
use crate::util::{deserialize_string_to_i64, deserialize_optional_string_to_i64};
7-
use alloc::format;
8-
use alloc::string::{ToString};
9-
use core::fmt;
10-
use serde::de::{MapAccess, Visitor};
11-
12-
use sqlite_nostd as sqlite;
13-
use sqlite_nostd::{Connection, ResultCode};
14-
use uuid::Uuid;
15-
use crate::error::{SQLiteError, PSResult};
16-
17-
use crate::ext::SafeManagedStmt;
5+
use crate::util::{deserialize_optional_string_to_i64, deserialize_string_to_i64};
186

197
#[derive(Serialize, Deserialize, Debug)]
208
pub struct Checkpoint {
@@ -31,154 +19,3 @@ pub struct BucketChecksum {
3119
pub bucket: String,
3220
pub checksum: i32,
3321
}
34-
35-
36-
#[derive(Serialize, Deserialize, Debug)]
37-
pub struct CheckpointComplete {
38-
#[serde(deserialize_with = "deserialize_string_to_i64")]
39-
last_op_id: i64
40-
}
41-
42-
#[derive(Serialize, Deserialize, Debug)]
43-
pub struct SyncBucketData {
44-
// TODO: complete this
45-
bucket: String
46-
}
47-
48-
#[derive(Serialize, Deserialize, Debug)]
49-
pub struct Keepalive {
50-
token_expires_in: i32
51-
}
52-
53-
#[derive(Serialize, Deserialize, Debug)]
54-
pub struct CheckpointDiff {
55-
#[serde(deserialize_with = "deserialize_string_to_i64")]
56-
last_op_id: i64,
57-
updated_buckets: Vec<BucketChecksum>,
58-
removed_buckets: Vec<String>,
59-
#[serde(default)]
60-
#[serde(deserialize_with = "deserialize_optional_string_to_i64")]
61-
write_checkpoint: Option<i64>
62-
}
63-
64-
65-
66-
#[derive(Debug)]
67-
pub enum StreamingSyncLine {
68-
CheckpointLine(Checkpoint),
69-
CheckpointDiffLine(CheckpointDiff),
70-
CheckpointCompleteLine(CheckpointComplete),
71-
SyncBucketDataLine(SyncBucketData),
72-
KeepaliveLine(i32),
73-
Unknown
74-
}
75-
76-
// Serde does not supporting ignoring unknown fields in externally-tagged enums, so we use our own
77-
// serializer.
78-
79-
struct StreamingSyncLineVisitor;
80-
81-
impl<'de> Visitor<'de> for StreamingSyncLineVisitor {
82-
type Value = StreamingSyncLine;
83-
84-
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
85-
formatter.write_str("sync data")
86-
}
87-
88-
fn visit_map<A>(self, mut access: A) -> Result<Self::Value, A::Error>
89-
where
90-
A: MapAccess<'de>,
91-
{
92-
let mut r = StreamingSyncLine::Unknown;
93-
while let Some((key, value)) = access.next_entry::<String, json::Value>()? {
94-
if !matches!(r, StreamingSyncLine::Unknown) {
95-
// Generally, we don't expect to receive multiple in one line.
96-
// But if it does happen, we keep the first one.
97-
continue;
98-
}
99-
match key.as_str() {
100-
"checkpoint" => {
101-
r = StreamingSyncLine::CheckpointLine(
102-
serde_json::from_value(value).map_err(de::Error::custom)?,
103-
);
104-
}
105-
"checkpoint_diff" => {
106-
r = StreamingSyncLine::CheckpointDiffLine(
107-
serde_json::from_value(value).map_err(de::Error::custom)?,
108-
);
109-
}
110-
"checkpoint_complete" => {
111-
r = StreamingSyncLine::CheckpointCompleteLine(
112-
serde_json::from_value(value).map_err(de::Error::custom)?,
113-
);
114-
}
115-
"data" => {
116-
r = StreamingSyncLine::SyncBucketDataLine(
117-
serde_json::from_value(value).map_err(de::Error::custom)?,
118-
);
119-
}
120-
"token_expires_in" => {
121-
r = StreamingSyncLine::KeepaliveLine(
122-
serde_json::from_value(value).map_err(de::Error::custom)?,
123-
);
124-
}
125-
_ => {}
126-
}
127-
}
128-
129-
Ok(r)
130-
}
131-
}
132-
133-
impl<'de> Deserialize<'de> for StreamingSyncLine {
134-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
135-
where
136-
D: Deserializer<'de>,
137-
{
138-
deserializer.deserialize_map(StreamingSyncLineVisitor)
139-
}
140-
}
141-
142-
143-
#[cfg(test)]
144-
mod tests {
145-
use core::assert_matches::assert_matches;
146-
use super::*;
147-
148-
#[test]
149-
fn json_parsing_test() {
150-
let line: StreamingSyncLine = serde_json::from_str(r#"{"token_expires_in": 42}"#).unwrap();
151-
assert_matches!(line, StreamingSyncLine::KeepaliveLine(42));
152-
153-
let line: StreamingSyncLine = serde_json::from_str(r#"{"checkpoint_complete": {"last_op_id": "123"}}"#).unwrap();
154-
assert_matches!(line, StreamingSyncLine::CheckpointCompleteLine(CheckpointComplete { last_op_id: 123 }));
155-
156-
let line: StreamingSyncLine = serde_json::from_str(r#"{"checkpoint_complete": {"last_op_id": "123", "other": "foo"}}"#).unwrap();
157-
assert_matches!(line, StreamingSyncLine::CheckpointCompleteLine(CheckpointComplete { last_op_id: 123 }));
158-
159-
let line: StreamingSyncLine = serde_json::from_str(r#"{"checkpoint": {"last_op_id": "123", "buckets": []}}"#).unwrap();
160-
assert_matches!(line, StreamingSyncLine::CheckpointLine(Checkpoint { last_op_id: 123, .. }));
161-
162-
let line: StreamingSyncLine = serde_json::from_str(r#"{"checkpoint": {"last_op_id": "123", "write_checkpoint": "42", "buckets": []}}"#).unwrap();
163-
assert_matches!(line, StreamingSyncLine::CheckpointLine(Checkpoint { last_op_id: 123, write_checkpoint: Some(42), .. }));
164-
165-
let line: StreamingSyncLine = serde_json::from_str(r#"{"checkpoint_diff": {"last_op_id": "123", "updated_buckets": [], "removed_buckets": []}}"#).unwrap();
166-
assert_matches!(line, StreamingSyncLine::CheckpointDiffLine(CheckpointDiff { last_op_id: 123, .. }));
167-
168-
// Additional/unknown fields
169-
let line: StreamingSyncLine = serde_json::from_str(r#"{"token_expires_in": 42, "foo": 1}"#).unwrap();
170-
assert_matches!(line, StreamingSyncLine::KeepaliveLine(42));
171-
let line: StreamingSyncLine = serde_json::from_str(r#"{}"#).unwrap();
172-
assert_matches!(line, StreamingSyncLine::Unknown);
173-
let line: StreamingSyncLine = serde_json::from_str(r#"{"other":"test"}"#).unwrap();
174-
assert_matches!(line, StreamingSyncLine::Unknown);
175-
176-
// Multiple - keep the first one
177-
let line: StreamingSyncLine = serde_json::from_str(r#"{"token_expires_in": 42, "checkpoint_complete": {"last_op_id": "123"}}"#).unwrap();
178-
assert_matches!(line, StreamingSyncLine::KeepaliveLine(42));
179-
180-
// Test error handling
181-
let line: Result<StreamingSyncLine, _> = serde_json::from_str(r#"{"token_expires_in": "42"}"#);
182-
assert!(line.is_err());
183-
}
184-
}

0 commit comments

Comments
 (0)