Skip to content

Commit 7a9de72

Browse files
rename to target_checkpoint_request_id
1 parent a6d73fa commit 7a9de72

13 files changed

Lines changed: 110 additions & 111 deletions

crates/core/src/crud_vtab.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ impl SimpleCrudTransactionMode {
252252
// restart. Keeping them around could open the apply gate for a newly allocated target
253253
// id that compares below a stale seen value.
254254
db.exec_safe(formatcp!(
255-
"INSERT OR REPLACE INTO ps_kv(key, value) VALUES('local_target_op', {MAX_OP_ID});
255+
"INSERT OR REPLACE INTO ps_kv(key, value) VALUES('target_checkpoint_request_id', {MAX_OP_ID});
256256
DELETE FROM ps_kv WHERE key IN ('last_seen_checkpoint_request_id', 'last_applied_checkpoint_request_id')"
257257
))?;
258258
self.had_writes = true;

crates/core/src/migrations.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ DROP TABLE ps_sync_state_old;
469469
// so it becomes the last seen checkpoint request.
470470
//
471471
// `$local.target_op` can either be a concrete legacy write checkpoint id or a sentinel such
472-
// as i64::MAX while local writes are pending. Store it separately as `local_target_op`.
472+
// as i64::MAX while local writes are pending. Store it separately as `target_checkpoint_request_id`.
473473
// Seeding `last_requested_checkpoint_request_id` from a concrete target would be possible,
474474
// but should be redundant because SDKs reconcile the request counter with service state on
475475
// connect before advancing it through `next_checkpoint_request_id`.
@@ -496,7 +496,7 @@ DELETE FROM ps_kv
496496
WHERE key IN (
497497
'last_applied_checkpoint_request_id',
498498
'last_seen_checkpoint_request_id',
499-
'local_target_op'
499+
'target_checkpoint_request_id'
500500
);
501501
502502
INSERT INTO ps_kv(key, value)
@@ -512,7 +512,7 @@ SELECT 'last_seen_checkpoint_request_id', last_op
512512
AND last_op > 0;
513513
514514
INSERT INTO ps_kv(key, value)
515-
SELECT 'local_target_op', target_op
515+
SELECT 'target_checkpoint_request_id', target_op
516516
FROM ps_buckets
517517
WHERE name = '$local'
518518
AND target_op > 0;
@@ -531,7 +531,7 @@ ALTER TABLE ps_buckets DROP COLUMN target_op;
531531
// `$local.pending_delete = 1` marked this as a synthetic local-only bucket instead of a
532532
// normal service bucket. Restore each old progress column from its matching ps_kv key.
533533
// The 0 defaults cover a local target that exists before any checkpoint has been seen or
534-
// applied. If `local_target_op` is absent, don't create a `$local` row: the old
534+
// applied. If `target_checkpoint_request_id` is absent, don't create a `$local` row: the old
535535
// implementation also didn't have a `$local` bucket unless there was local target state to
536536
// track.
537537
const DOWN_STATEMENTS: &[&str] = &[
@@ -582,10 +582,10 @@ SELECT '$local', 1, seen, applied, target
582582
SELECT
583583
IFNULL((SELECT CAST(value AS INTEGER) FROM ps_kv WHERE key = 'last_seen_checkpoint_request_id'), 0) AS seen,
584584
IFNULL((SELECT CAST(value AS INTEGER) FROM ps_kv WHERE key = 'last_applied_checkpoint_request_id'), 0) AS applied,
585-
(SELECT CAST(value AS INTEGER) FROM ps_kv WHERE key = 'local_target_op') AS target
585+
(SELECT CAST(value AS INTEGER) FROM ps_kv WHERE key = 'target_checkpoint_request_id') AS target
586586
)
587587
WHERE EXISTS (
588-
SELECT 1 FROM ps_kv WHERE key = 'local_target_op'
588+
SELECT 1 FROM ps_kv WHERE key = 'target_checkpoint_request_id'
589589
)
590590
ON CONFLICT(name) DO UPDATE SET
591591
pending_delete = excluded.pending_delete,

crates/core/src/sync/interface.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,17 @@ pub fn register(db: *mut sqlite::sqlite3, state: Rc<DatabaseState>) -> Result<()
273273
ctx.result_int64(request_id);
274274
return Ok(());
275275
}
276-
"local_target_op" => {
277-
let target_op = parse_optional_i64_payload(
276+
"target_checkpoint_request_id" => {
277+
let target = parse_optional_i64_payload(
278278
*payload,
279-
"local target op",
280-
"local target op must be an integer, integer string, or null",
279+
"target checkpoint request id",
280+
"target checkpoint request id must be an integer, integer string, or null",
281281
)?;
282282
let adapter = state.storage_adapter(db)?;
283-
let previous_target_op = adapter.probe_local_target_op(target_op)?;
283+
let previous_target = adapter.probe_target_checkpoint_request_id(target)?;
284284

285-
match previous_target_op {
286-
Some(target_op) => ctx.result_int64(target_op),
285+
match previous_target {
286+
Some(target) => ctx.result_int64(target),
287287
None => ctx.result_null(),
288288
}
289289

crates/core/src/sync/storage_adapter.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const LAST_REQUESTED_CHECKPOINT_REQUEST_ID_KEY: &str = "last_requested_checkpoin
3030
const LAST_SEEN_CHECKPOINT_REQUEST_ID_KEY: &str = "last_seen_checkpoint_request_id";
3131
const LAST_APPLIED_CHECKPOINT_REQUEST_ID_KEY: &str = "last_applied_checkpoint_request_id";
3232

33-
// Tracks the local target used to block applying downloaded rows while local writes are
34-
// outstanding. When present, this is normally either the max-op sentinel for pending local writes or
35-
// a concrete checkpoint request id also stored in LAST_REQUESTED_CHECKPOINT_REQUEST_ID_KEY.
36-
const LOCAL_TARGET_OP_KEY: &str = "local_target_op";
33+
// Tracks the target used to block applying downloaded rows while local writes are outstanding.
34+
// When present, this is normally either the max-op sentinel for pending local writes or a concrete
35+
// checkpoint request id also stored in LAST_REQUESTED_CHECKPOINT_REQUEST_ID_KEY.
36+
const TARGET_CHECKPOINT_REQUEST_ID_KEY: &str = "target_checkpoint_request_id";
3737

3838
/// An adapter for storing sync state.
3939
///
@@ -509,51 +509,49 @@ WHERE bucket = ?1",
509509
Ok(())
510510
}
511511

512-
pub fn local_state(&self) -> Result<Option<LocalState>, PowerSyncError> {
513-
Ok(self
514-
.read_i64_kv(LOCAL_TARGET_OP_KEY)?
515-
.map(|target_op| LocalState { target_op }))
512+
pub fn target_checkpoint_request_id(&self) -> Result<Option<i64>, PowerSyncError> {
513+
self.read_i64_kv(TARGET_CHECKPOINT_REQUEST_ID_KEY)
516514
}
517515

518-
/// Probes and optionally updates the local target op used to block applying downloaded rows
516+
/// Probes and optionally updates the target checkpoint request id used to block applying downloaded rows
519517
/// while local writes are outstanding.
520518
///
521519
/// In the write-checkpoint flow, callers allocate a checkpoint request id, post it to the
522520
/// service, and then update this from the max-op sentinel to the concrete checkpoint request id
523521
/// once the request succeeds. This is also used for older services where the SDK cannot create
524522
/// checkpoint requests explicitly.
525523
///
526-
/// The target op can also be used internally as a sentinel value such as max op id while local
524+
/// The target can also be used internally as a sentinel value such as max op id while local
527525
/// writes are pending, so it must not always be interpreted as a checkpoint request id.
528526
///
529527
/// This only updates the apply gate. It does not allocate, seed or overwrite
530528
/// `last_requested_checkpoint_request_id`, which is managed by `seed_checkpoint_request_id` and
531529
/// `next_checkpoint_request_id`.
532530
///
533-
/// Returns the target op value from before this call. When `target_op` is `None`, this only
534-
/// reads the current value. A `target_op` of zero clears the stored target, removing the apply
531+
/// Returns the target value from before this call. When `target` is `None`, this only
532+
/// reads the current value. A `target` of zero clears the stored target, removing the apply
535533
/// gate entirely; any other value overwrites it.
536534
///
537535
/// Negative values are rejected when parsing the `powersync_control` payload, before this is
538536
/// called.
539-
pub fn probe_local_target_op(
537+
pub fn probe_target_checkpoint_request_id(
540538
&self,
541-
target_op: Option<i64>,
539+
target: Option<i64>,
542540
) -> Result<Option<i64>, PowerSyncError> {
543-
let previous_target_op = self.local_state()?.map(|state| state.target_op);
541+
let previous_target = self.target_checkpoint_request_id()?;
544542

545-
let Some(target_op) = target_op else {
546-
return Ok(previous_target_op);
543+
let Some(target) = target else {
544+
return Ok(previous_target);
547545
};
548546

549-
if target_op == 0 {
550-
self.delete_kv(LOCAL_TARGET_OP_KEY)?;
551-
return Ok(previous_target_op);
547+
if target == 0 {
548+
self.delete_kv(TARGET_CHECKPOINT_REQUEST_ID_KEY)?;
549+
return Ok(previous_target);
552550
}
553551

554-
self.write_i64_kv(LOCAL_TARGET_OP_KEY, target_op)?;
552+
self.write_i64_kv(TARGET_CHECKPOINT_REQUEST_ID_KEY, target)?;
555553

556-
Ok(previous_target_op)
554+
Ok(previous_target)
557555
}
558556

559557
/// Persists the checkpoint request id observed in a complete sync checkpoint.
@@ -653,10 +651,6 @@ ON CONFLICT(key) DO UPDATE SET value = excluded.value",
653651
}
654652
}
655653

656-
pub struct LocalState {
657-
pub target_op: i64,
658-
}
659-
660654
pub struct BucketInfo {
661655
pub id: i64,
662656
pub last_applied_op: i64,

crates/core/src/sync/streaming_sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl StreamingSyncIteration {
651651
return Ok(());
652652
};
653653

654-
let target_write = self.adapter.local_state()?.map(|e| e.target_op);
654+
let target_write = self.adapter.target_checkpoint_request_id()?;
655655
if checkpoint.write_checkpoint < target_write {
656656
// Note: None < Some(x). The pending checkpoint does not contain the write
657657
// checkpoint created during the upload, so we don't have to try applying it, it's

crates/core/src/sync/sync_local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<'a> SyncOperation<'a> {
7070
"SELECT 1
7171
FROM ps_kv AS target
7272
LEFT JOIN ps_kv AS seen ON seen.key = 'last_seen_checkpoint_request_id'
73-
WHERE target.key = 'local_target_op'
73+
WHERE target.key = 'target_checkpoint_request_id'
7474
AND CAST(target.value AS INTEGER) > COALESCE(CAST(seen.value AS INTEGER), 0)",
7575
)?;
7676

dart/test/crud_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void main() {
249249
});
250250
});
251251

252-
test('updates local target op and updated rows', () {
252+
test('updates target checkpoint request id and updated rows', () {
253253
// Stale high-water marks (e.g. from before a request counter restart) must be cleared
254254
// by a local write, so they can't open the apply gate for a smaller new target id.
255255
db.execute('''
@@ -274,10 +274,10 @@ INSERT INTO ps_kv(key, value) VALUES
274274
isEmpty);
275275
expect(
276276
db.select(
277-
"SELECT key, value FROM ps_kv WHERE key LIKE '%checkpoint_request_id' OR key = 'local_target_op'"),
277+
"SELECT key, value FROM ps_kv WHERE key LIKE '%checkpoint_request_id'"),
278278
[
279279
{
280-
'key': 'local_target_op',
280+
'key': 'target_checkpoint_request_id',
281281
'value': 9223372036854775807,
282282
}
283283
]);

dart/test/migration_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ VALUES(1, '$local', 5, 6, 7, 0, 0, 1, 0, 0, 0);
109109
expect(db.select('SELECT key, value FROM ps_kv ORDER BY key'), [
110110
{'key': 'last_applied_checkpoint_request_id', 'value': 5},
111111
{'key': 'last_seen_checkpoint_request_id', 'value': 6},
112-
{'key': 'local_target_op', 'value': 7},
112+
{'key': 'target_checkpoint_request_id', 'value': 7},
113113
]);
114114
expect(db.select(r"SELECT * FROM ps_buckets WHERE name = '$local'"),
115115
isEmpty);
@@ -142,7 +142,7 @@ VALUES(1, '$local', 5, 6, 9223372036854775807, 0, 0, 1, 0, 0, 0);
142142
expect(db.select('SELECT key, value FROM ps_kv ORDER BY key'), [
143143
{'key': 'last_applied_checkpoint_request_id', 'value': 5},
144144
{'key': 'last_seen_checkpoint_request_id', 'value': 6},
145-
{'key': 'local_target_op', 'value': 9223372036854775807},
145+
{'key': 'target_checkpoint_request_id', 'value': 9223372036854775807},
146146
]);
147147
});
148148

@@ -159,7 +159,7 @@ VALUES(1, '$local', 0, 0, 9223372036854775807, 0, 0, 1, 0, 0, 0);
159159
// The max-op sentinel is valid local target state, but target ops no longer seed
160160
// last_requested_checkpoint_request_id.
161161
expect(db.select('SELECT key, value FROM ps_kv ORDER BY key'), [
162-
{'key': 'local_target_op', 'value': 9223372036854775807},
162+
{'key': 'target_checkpoint_request_id', 'value': 9223372036854775807},
163163
]);
164164
});
165165

@@ -170,7 +170,7 @@ INSERT INTO ps_kv(key, value) VALUES
170170
('last_requested_checkpoint_request_id', 7),
171171
('last_seen_checkpoint_request_id', 6),
172172
('last_applied_checkpoint_request_id', 5),
173-
('local_target_op', 7);
173+
('target_checkpoint_request_id', 7);
174174
''');
175175

176176
db.executeInTx('select powersync_test_migration(13)');
@@ -196,7 +196,7 @@ INSERT INTO ps_kv(key, value) VALUES
196196
('last_requested_checkpoint_request_id', 7),
197197
('last_seen_checkpoint_request_id', 6),
198198
('last_applied_checkpoint_request_id', 5),
199-
('local_target_op', 7);
199+
('target_checkpoint_request_id', 7);
200200
''');
201201

202202
db.executeInTx('select powersync_test_migration(13)');
@@ -216,7 +216,7 @@ UPDATE ps_buckets
216216
{'key': 'last_applied_checkpoint_request_id', 'value': 8},
217217
{'key': 'last_requested_checkpoint_request_id', 'value': 7},
218218
{'key': 'last_seen_checkpoint_request_id', 'value': 8},
219-
{'key': 'local_target_op', 'value': 9},
219+
{'key': 'target_checkpoint_request_id', 'value': 9},
220220
]);
221221
expect(db.select(r"SELECT * FROM ps_buckets WHERE name = '$local'"),
222222
isEmpty);

0 commit comments

Comments
 (0)