Skip to content

Commit f2cc286

Browse files
committed
Check checksums partially
1 parent 8f4c914 commit f2cc286

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

crates/core/src/bucket_priority.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ impl TryFrom<i32> for BucketPriority {
2424
const VALID: RangeInclusive<i32> = (BucketPriority::HIGHEST.0)..=(BucketPriority::LOWEST.0);
2525

2626
if !VALID.contains(&value) {
27-
return Err(SQLiteError::from(ResultCode::MISUSE));
27+
return Err(SQLiteError(
28+
ResultCode::MISUSE,
29+
Some("Invalid bucket priority".into()),
30+
));
2831
}
2932

3033
return Ok(BucketPriority(value));

crates/core/src/checkpoint.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ bucket_list(bucket, checksum) AS (
4040
json_extract(json_each.value, '$.bucket') as bucket,
4141
json_extract(json_each.value, '$.checksum') as checksum
4242
FROM json_each(json_extract(?1, '$.buckets'))
43+
WHERE IFNULL(json_extract(json_each.value, '$.priority'), 1) <=
44+
IFNULL(json_extract(?1, '$.priority'), 3)
4345
)
4446
SELECT
4547
bucket_list.bucket as bucket,

crates/core/src/sync_types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ pub struct Checkpoint {
1818
pub struct BucketChecksum {
1919
pub bucket: String,
2020
pub checksum: i32,
21+
pub priority: Option<i32>,
2122
}

dart/test/sync_test.dart

+20-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ void main() {
5858
'last_op_id': lastOpId,
5959
'write_checkpoint': writeCheckpoint,
6060
'buckets': checksums,
61+
'priority': priority,
6162
})
6263
]);
6364

@@ -88,7 +89,9 @@ void main() {
8889
pushSyncData('prio1', '1', 'row-0', 'PUT', {'col': 'hi'});
8990
expect(fetchRows(), isEmpty);
9091

91-
expect(pushCheckpointComplete('1', null, [_bucketChecksum('prio1', 0)]),
92+
expect(
93+
pushCheckpointComplete(
94+
'1', null, [_bucketChecksum('prio1', 1, checksum: 0)]),
9295
isTrue);
9396
expect(fetchRows(), [
9497
{'id': 'row-0', 'col': 'hi'}
@@ -101,7 +104,9 @@ void main() {
101104
expect(fetchRows(), isNotEmpty);
102105

103106
pushSyncData('prio1', '1', 'row-0', 'PUT', {'col': 'hi'});
104-
expect(pushCheckpointComplete('1', null, [_bucketChecksum('prio1', 0)]),
107+
expect(
108+
pushCheckpointComplete(
109+
'1', null, [_bucketChecksum('prio1', 1, checksum: 0)]),
105110
isFalse);
106111
expect(fetchRows(), [
107112
{'id': 'local', 'col': 'data'}
@@ -118,7 +123,7 @@ void main() {
118123
pushCheckpointComplete(
119124
'1',
120125
null,
121-
[_bucketChecksum('prio0', 0)],
126+
[_bucketChecksum('prio0', 0, checksum: 0)],
122127
priority: 0,
123128
),
124129
isTrue,
@@ -141,7 +146,16 @@ void main() {
141146
pushCheckpointComplete(
142147
'1',
143148
null,
144-
[_bucketChecksum('prio$i', 0)],
149+
[
150+
for (var j = 0; j <= 4; j++)
151+
_bucketChecksum(
152+
'prio$j',
153+
j,
154+
// Give buckets outside of the current priority a wrong
155+
// checksum. They should not be validated yet.
156+
checksum: j <= i ? 0 : 1234,
157+
),
158+
],
145159
priority: i,
146160
),
147161
isTrue,
@@ -155,8 +169,8 @@ void main() {
155169
});
156170
}
157171

158-
Object? _bucketChecksum(String bucket, int checksum) {
159-
return {'bucket': bucket, 'checksum': checksum};
172+
Object? _bucketChecksum(String bucket, int prio, {int checksum = 0}) {
173+
return {'bucket': bucket, 'priority': prio, 'checksum': checksum};
160174
}
161175

162176
const _schema = {

0 commit comments

Comments
 (0)