Skip to content

Commit 79b4062

Browse files
committed
Fix dirt calculation across restarts
1 parent 61d68d9 commit 79b4062

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ reports changes here.
1010

1111
- Add function `CubDB.writes_since_compaction/1` to get the number of writes since the last successful compaction
1212

13+
Bug fixes:
14+
15+
- Fix dirt calculation upon restart of `CubDB`
16+
1317
## v2.0.2 (2023-01-01)
1418

1519
Bug fixes:

lib/cubdb/btree.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ defmodule CubDB.Btree do
244244
# updates won't be committed to the database and will be lost in case of a
245245
# restart.
246246
def commit(tree = %Btree{store: store, size: size, root_loc: root_loc, dirt: dirt}) do
247-
Store.put_header(store, header(size: size, location: root_loc, dirt: dirt + 1))
247+
Store.put_header(store, header(size: size, location: root_loc, dirt: dirt))
248248
tree
249249
end
250250

test/cubdb_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,24 @@ defmodule CubDBTest do
10031003
refute_received :compaction_started
10041004
end
10051005

1006+
test "dirt calculation is consistent after restart", %{tmp_dir: tmp_dir} do
1007+
{:ok, db} = CubDB.start_link(data_dir: tmp_dir, auto_compact: false)
1008+
1009+
CubDB.put(db, "foo", 123)
1010+
CubDB.put(db, "bar", 234)
1011+
CubDB.delete(db, "foo")
1012+
CubDB.clear(db)
1013+
1014+
original_dirt_factor = CubDB.dirt_factor(db)
1015+
original_writes_since_compaction = CubDB.dirt_factor(db)
1016+
1017+
CubDB.stop(db)
1018+
1019+
{:ok, db} = CubDB.start_link(data_dir: tmp_dir, auto_compact: false)
1020+
assert original_dirt_factor == CubDB.dirt_factor(db)
1021+
assert original_writes_since_compaction == CubDB.dirt_factor(db)
1022+
end
1023+
10061024
test "auto compaction is active by default", %{tmp_dir: tmp_dir} do
10071025
{:ok, db} = CubDB.start_link(tmp_dir)
10081026

0 commit comments

Comments
 (0)