Skip to content

Commit 2eb12e0

Browse files
committed
Add basic tests around priorities
1 parent b2c081f commit 2eb12e0

File tree

5 files changed

+315
-96
lines changed

5 files changed

+315
-96
lines changed

crates/core/src/bucket_priority.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::ops::RangeInclusive;
2+
13
use sqlite_nostd::ResultCode;
24

35
use crate::error::SQLiteError;
@@ -19,7 +21,9 @@ impl TryFrom<i32> for BucketPriority {
1921
type Error = SQLiteError;
2022

2123
fn try_from(value: i32) -> Result<Self, Self::Error> {
22-
if value < BucketPriority::LOWEST.0 || value > BucketPriority::HIGHEST.0 {
24+
const VALID: RangeInclusive<i32> = (BucketPriority::HIGHEST.0)..=(BucketPriority::LOWEST.0);
25+
26+
if !VALID.contains(&value) {
2327
return Err(SQLiteError::from(ResultCode::MISUSE));
2428
}
2529

crates/core/src/operations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SELECT
1919
json_extract(e.value, '$.has_more') as has_more,
2020
json_extract(e.value, '$.after') as after,
2121
json_extract(e.value, '$.next_after') as next_after,
22-
json_extract(d.value, '$.priority') as priority,
22+
json_extract(d.value, '$.priority') as priority
2323
FROM json_each(json_extract(?1, '$.buckets')) e
2424
LEFT OUTER JOIN json_each(json_extract(?1, '$.descriptions')) d
2525
ON json_extract(e.value, '$.bucket') == d.key",

crates/core/src/sync_local.rs

+31-15
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,16 @@ pub fn sync_local<V: Value>(db: *mut sqlite::sqlite3, data: &V) -> Result<i64, S
7979
-- 1. Filter oplog by the ops added but not applied yet (oplog b).
8080
-- SELECT DISTINCT / UNION is important for cases with many duplicate ids.
8181
WITH updated_rows AS (
82-
SELECT DISTINCT b.row_type, b.row_id FROM ps_buckets AS buckets
82+
SELECT DISTINCT FALSE as local, b.row_type, b.row_id FROM ps_buckets AS buckets
8383
CROSS JOIN ps_oplog AS b ON b.bucket = buckets.id AND (b.op_id > buckets.last_applied_op)
84-
WHERE buckets.priority <= ?
85-
UNION SELECT row_type, row_id FROM ps_updated_rows
84+
WHERE buckets.priority <= ?1
85+
UNION SELECT TRUE, row_type, row_id FROM ps_updated_rows
8686
)
8787
8888
-- 3. Group the objects from different buckets together into a single one (ops).
8989
SELECT b.row_type as type,
9090
b.row_id as id,
91+
b.local as local,
9192
r.data as data,
9293
count(r.bucket) as buckets,
9394
/* max() affects which row is used for 'data' */
@@ -97,6 +98,7 @@ FROM updated_rows b
9798
LEFT OUTER JOIN ps_oplog AS r
9899
ON r.row_type = b.row_type
99100
AND r.row_id = b.row_id
101+
AND (SELECT priority FROM ps_buckets WHERE id = r.bucket) <= ?1
100102
-- Group for (3)
101103
GROUP BY b.row_type, b.row_id",
102104
)
@@ -108,11 +110,18 @@ GROUP BY b.row_type, b.row_id",
108110
while statement.step().into_db_result(db)? == ResultCode::ROW {
109111
let type_name = statement.column_text(0)?;
110112
let id = statement.column_text(1)?;
111-
let buckets = statement.column_int(3)?;
112-
let data = statement.column_text(2);
113+
let local = statement.column_int(2)? == 1;
114+
let buckets = statement.column_int(4)?;
115+
let data = statement.column_text(3);
113116

114117
let table_name = internal_table_name(type_name);
115118

119+
if local && buckets == 0 && priority == BucketPriority::HIGHEST {
120+
// These rows are still local and they haven't been uploaded yet (which we allow for
121+
// buckets with priority=0 completing). We should just keep them around.
122+
continue;
123+
}
124+
116125
if tables.contains(&table_name) {
117126
let quoted = quote_internal_name(type_name, false);
118127

@@ -157,20 +166,27 @@ GROUP BY b.row_type, b.row_id",
157166
}
158167

159168
// language=SQLite
160-
db.exec_safe(
161-
"UPDATE ps_buckets
169+
let updated = db
170+
.prepare_v2(
171+
"UPDATE ps_buckets
162172
SET last_applied_op = last_op
163-
WHERE last_applied_op != last_op",
164-
)
165-
.into_db_result(db)?;
166-
167-
// language=SQLite
168-
db.exec_safe("DELETE FROM ps_updated_rows")
173+
WHERE last_applied_op != last_op AND priority <= ?",
174+
)
169175
.into_db_result(db)?;
176+
updated.bind_int(1, priority.into())?;
177+
updated.exec()?;
170178

171-
// language=SQLite
172-
db.exec_safe("insert or replace into ps_kv(key, value) values('last_synced_at', datetime())")
179+
if priority == BucketPriority::LOWEST {
180+
// language=SQLite
181+
db.exec_safe("DELETE FROM ps_updated_rows")
182+
.into_db_result(db)?;
183+
184+
// language=SQLite
185+
db.exec_safe(
186+
"insert or replace into ps_kv(key, value) values('last_synced_at', datetime())",
187+
)
173188
.into_db_result(db)?;
189+
}
174190

175191
Ok(1)
176192
}

0 commit comments

Comments
 (0)