@@ -13,7 +13,10 @@ use crate::{
13
13
error:: SQLiteError ,
14
14
ext:: SafeManagedStmt ,
15
15
operations:: delete_bucket,
16
- sync:: Checksum ,
16
+ sync:: {
17
+ checkpoint:: { validate_checkpoint, ChecksumMismatch } ,
18
+ Checksum ,
19
+ } ,
17
20
sync_local:: { PartialSyncOperation , SyncOperation } ,
18
21
} ;
19
22
@@ -145,69 +148,20 @@ impl StorageAdapter {
145
148
} ) ;
146
149
}
147
150
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
-
196
151
pub fn sync_local (
197
152
& self ,
198
153
checkpoint : & OwnedCheckpoint ,
199
154
priority : Option < BucketPriority > ,
200
155
) -> 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
+ } ) ) ;
211
165
}
212
166
213
167
let update_bucket = self
@@ -266,8 +220,6 @@ FROM ps_buckets WHERE name = ?;",
266
220
} ?;
267
221
268
222
if sync_result == 1 {
269
- // TODO: Force compact
270
-
271
223
if priority. is_none ( ) {
272
224
// Reset progress counters. We only do this for a complete sync, as we want a
273
225
// download progress to always cover a complete checkpoint instead of resetting for
@@ -310,13 +262,6 @@ pub struct CheckpointResult {
310
262
failed_buckets : Vec < ChecksumMismatch > ,
311
263
}
312
264
313
- pub struct ChecksumMismatch {
314
- bucket_name : String ,
315
- expected_checksum : Checksum ,
316
- actual_op_checksum : Checksum ,
317
- actual_add_checksum : Checksum ,
318
- }
319
-
320
265
impl CheckpointResult {
321
266
pub fn is_valid ( & self ) -> bool {
322
267
self . failed_buckets . is_empty ( )
0 commit comments