Skip to content

Commit 59f3ac2

Browse files
committed
Compact based on remove operation count.
1 parent 7aef64e commit 59f3ac2

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

crates/core/src/operations.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ INSERT INTO ps_oplog(bucket, op_id, op, key, row_type, row_id, data, hash, super
9898
let mut last_op: Option<i64> = None;
9999
let mut add_checksum: i32 = 0;
100100
let mut op_checksum: i32 = 0;
101-
let mut pending_compact: i32 = 0;
101+
let mut remove_operations: i32 = 0;
102102

103103
while iterate_statement.step()? == ResultCode::ROW {
104104
let op_id = iterate_statement.column_int64(0)?;
@@ -181,7 +181,7 @@ INSERT INTO ps_oplog(bucket, op_id, op, key, row_type, row_id, data, hash, super
181181
if opi == 4 {
182182
// We persisted a REMOVE statement, so the bucket needs
183183
// to be compacted at some point.
184-
pending_compact = 1;
184+
remove_operations += 1;
185185
}
186186
} else if op == "MOVE" {
187187
add_checksum = add_checksum.wrapping_add(checksum);
@@ -215,14 +215,14 @@ INSERT INTO ps_oplog(bucket, op_id, op, key, row_type, row_id, data, hash, super
215215
SET last_op = ?2,
216216
add_checksum = (add_checksum + ?3) & 0xffffffff,
217217
op_checksum = (op_checksum + ?4) & 0xffffffff,
218-
pending_compact = (pending_compact OR ?5)
218+
remove_operations = (remove_operations + ?5)
219219
WHERE name = ?1",
220220
)?;
221221
statement.bind_text(1, bucket, sqlite::Destructor::STATIC)?;
222222
statement.bind_int64(2, *last_op)?;
223223
statement.bind_int(3, add_checksum)?;
224224
statement.bind_int(4, op_checksum)?;
225-
statement.bind_int(5, pending_compact)?;
225+
statement.bind_int(5, remove_operations)?;
226226

227227
statement.exec()?;
228228
}
@@ -244,15 +244,22 @@ SELECT
244244
AND (oplog.superseded = 1 OR oplog.op != 3)
245245
) as checksum
246246
FROM ps_buckets
247-
WHERE ps_buckets.pending_delete = 0 AND ps_buckets.pending_compact = 1",
247+
WHERE ps_buckets.pending_delete = 0 AND
248+
ps_buckets.remove_operations >= CASE
249+
WHEN ?1 = '' THEN 1
250+
ELSE IFNULL(?1 ->> 'threshold', 1)
251+
END",
248252
)?;
253+
// Compact bucket if there are 50 or more operations
254+
statement.bind_text(1, _data, sqlite::Destructor::STATIC);
249255

250256
// language=SQLite
251257
let update_statement = db.prepare_v2(
252258
"
253259
UPDATE ps_buckets
254260
SET add_checksum = (add_checksum + ?2) & 0xffffffff,
255-
op_checksum = (op_checksum - ?2) & 0xffffffff
261+
op_checksum = (op_checksum - ?2) & 0xffffffff,
262+
remove_operations = 0
256263
WHERE ps_buckets.name = ?1",
257264
)?;
258265

crates/core/src/view_admin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ INSERT INTO ps_migration(id, down_migrations) VALUES(3, json_array(json_object('
252252
// language=SQLite
253253
local_db.exec_safe("\
254254
ALTER TABLE ps_buckets ADD COLUMN op_checksum INTEGER NOT NULL DEFAULT 0;
255-
ALTER TABLE ps_buckets ADD COLUMN pending_compact INTEGER NOT NULL DEFAULT 0;
255+
ALTER TABLE ps_buckets ADD COLUMN remove_operations INTEGER NOT NULL DEFAULT 0;
256256
257257
UPDATE ps_buckets SET op_checksum = (
258258
SELECT IFNULL(SUM(ps_oplog.hash), 0) & 0xffffffff FROM ps_oplog WHERE ps_oplog.bucket = ps_buckets.name
@@ -263,7 +263,7 @@ INSERT INTO ps_migration(id, down_migrations)
263263
json_array(
264264
json_object('sql', 'DELETE FROM ps_migration WHERE id >= 4'),
265265
json_object('sql', 'ALTER TABLE ps_buckets DROP COLUMN op_checksum'),
266-
json_object('sql', 'ALTER TABLE ps_buckets DROP COLUMN pending_compact')
266+
json_object('sql', 'ALTER TABLE ps_buckets DROP COLUMN remove_operations')
267267
));
268268
").into_db_result(local_db)?;
269269
}

0 commit comments

Comments
 (0)