Skip to content

Commit 63c23c4

Browse files
committed
Clean up duplicate checkpoint validation
1 parent 4fe042d commit 63c23c4

File tree

1 file changed

+13
-68
lines changed

1 file changed

+13
-68
lines changed

crates/core/src/sync/storage_adapter.rs

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use crate::{
1313
error::SQLiteError,
1414
ext::SafeManagedStmt,
1515
operations::delete_bucket,
16-
sync::Checksum,
16+
sync::{
17+
checkpoint::{validate_checkpoint, ChecksumMismatch},
18+
Checksum,
19+
},
1720
sync_local::{PartialSyncOperation, SyncOperation},
1821
};
1922

@@ -145,69 +148,20 @@ impl StorageAdapter {
145148
});
146149
}
147150

148-
fn validate_checkpoint(
149-
&self,
150-
checkpoint: &OwnedCheckpoint,
151-
priority: Option<BucketPriority>,
152-
) -> Result<CheckpointResult, SQLiteError> {
153-
// language=SQLite
154-
let statement = self.db.prepare_v2(
155-
"
156-
SELECT
157-
ps_buckets.add_checksum as add_checksum,
158-
ps_buckets.op_checksum as oplog_checksum
159-
FROM ps_buckets WHERE name = ?;",
160-
)?;
161-
162-
let mut failures: Vec<ChecksumMismatch> = Vec::new();
163-
for bucket in checkpoint.buckets.values() {
164-
if bucket.is_in_priority(priority) {
165-
statement.bind_text(1, &bucket.bucket, sqlite_nostd::Destructor::STATIC)?;
166-
167-
let (add_checksum, oplog_checksum) = match statement.step()? {
168-
ResultCode::ROW => {
169-
let add_checksum = Checksum::from_i32(statement.column_int(0));
170-
let oplog_checksum = Checksum::from_i32(statement.column_int(1));
171-
(add_checksum, oplog_checksum)
172-
}
173-
_ => (Checksum::zero(), Checksum::zero()),
174-
};
175-
176-
let actual = add_checksum + oplog_checksum;
177-
178-
if actual != bucket.checksum {
179-
failures.push(ChecksumMismatch {
180-
bucket_name: bucket.bucket.clone(),
181-
expected_checksum: bucket.checksum,
182-
actual_add_checksum: add_checksum,
183-
actual_op_checksum: oplog_checksum,
184-
});
185-
}
186-
187-
statement.reset()?;
188-
}
189-
}
190-
191-
Ok(CheckpointResult {
192-
failed_buckets: failures,
193-
})
194-
}
195-
196151
pub fn sync_local(
197152
&self,
198153
checkpoint: &OwnedCheckpoint,
199154
priority: Option<BucketPriority>,
200155
) -> Result<SyncLocalResult, SQLiteError> {
201-
let checksums = self.validate_checkpoint(checkpoint, priority)?;
202-
203-
if !checksums.is_valid() {
204-
self.delete_buckets(
205-
checksums
206-
.failed_buckets
207-
.iter()
208-
.map(|i| i.bucket_name.as_str()),
209-
)?;
210-
return Ok(SyncLocalResult::ChecksumFailure(checksums));
156+
let mismatched_checksums =
157+
validate_checkpoint(checkpoint.buckets.values(), priority, self.db)?;
158+
159+
if !mismatched_checksums.is_empty() {
160+
self.delete_buckets(mismatched_checksums.iter().map(|i| i.bucket_name.as_str()))?;
161+
162+
return Ok(SyncLocalResult::ChecksumFailure(CheckpointResult {
163+
failed_buckets: mismatched_checksums,
164+
}));
211165
}
212166

213167
let update_bucket = self
@@ -266,8 +220,6 @@ FROM ps_buckets WHERE name = ?;",
266220
}?;
267221

268222
if sync_result == 1 {
269-
// TODO: Force compact
270-
271223
if priority.is_none() {
272224
// Reset progress counters. We only do this for a complete sync, as we want a
273225
// download progress to always cover a complete checkpoint instead of resetting for
@@ -310,13 +262,6 @@ pub struct CheckpointResult {
310262
failed_buckets: Vec<ChecksumMismatch>,
311263
}
312264

313-
pub struct ChecksumMismatch {
314-
bucket_name: String,
315-
expected_checksum: Checksum,
316-
actual_op_checksum: Checksum,
317-
actual_add_checksum: Checksum,
318-
}
319-
320265
impl CheckpointResult {
321266
pub fn is_valid(&self) -> bool {
322267
self.failed_buckets.is_empty()

0 commit comments

Comments
 (0)