Skip to content

Commit 0583385

Browse files
committed
Update local on first write
1 parent 8b20c4b commit 0583385

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

crates/core/src/crud_vtab.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl VirtualTable {
160160
set_updated_rows.bind_text(1, row_type, sqlite::Destructor::STATIC)?;
161161
set_updated_rows.bind_text(2, id, sqlite::Destructor::STATIC)?;
162162
set_updated_rows.exec()?;
163-
simple.had_writes = true;
163+
simple.record_local_write(db)?;
164164
}
165165
}
166166

@@ -191,26 +191,7 @@ impl VirtualTable {
191191
Ok(())
192192
}
193193

194-
fn end_transaction(&mut self) -> Result<(), SQLiteError> {
195-
let tx = self.current_tx.take();
196-
if let Some(tx) = tx {
197-
let needs_local_bucket_update = match tx.mode {
198-
CrudTransactionMode::Manual { .. } => {
199-
// In manual mode, users need to update the $local bucket themselves.
200-
false
201-
}
202-
CrudTransactionMode::Simple(simple) => simple.had_writes,
203-
};
204-
205-
if needs_local_bucket_update {
206-
self.db.exec_safe(formatcp!("INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('$local', 0, {MAX_OP_ID})"))?;
207-
}
208-
}
209-
210-
Ok(())
211-
}
212-
213-
fn clear_transaction_state(&mut self) {
194+
fn end_transaction(&mut self) {
214195
self.current_tx = None;
215196
}
216197
}
@@ -252,6 +233,15 @@ impl SimpleCrudTransactionMode {
252233
)
253234
})
254235
}
236+
237+
fn record_local_write(&mut self, db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
238+
if !self.had_writes {
239+
db.exec_safe(formatcp!("INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('$local', 0, {MAX_OP_ID})"))?;
240+
self.had_writes = true;
241+
}
242+
243+
Ok(())
244+
}
255245
}
256246

257247
/// A variant of `Option.get_or_insert` that handles insertions returning errors.
@@ -324,12 +314,13 @@ extern "C" fn begin(vtab: *mut sqlite::vtab) -> c_int {
324314

325315
extern "C" fn commit(vtab: *mut sqlite::vtab) -> c_int {
326316
let tab = unsafe { &mut *(vtab.cast::<VirtualTable>()) };
327-
vtab_result(vtab, tab.end_transaction())
317+
tab.end_transaction();
318+
ResultCode::OK as c_int
328319
}
329320

330321
extern "C" fn rollback(vtab: *mut sqlite::vtab) -> c_int {
331322
let tab = unsafe { &mut *(vtab.cast::<VirtualTable>()) };
332-
tab.clear_transaction_state();
323+
tab.end_transaction();
333324
// ps_tx will be rolled back automatically
334325
ResultCode::OK as c_int
335326
}

0 commit comments

Comments
 (0)