Skip to content

Commit

Permalink
Fix dirt calculation across restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaong committed Oct 2, 2024
1 parent 61d68d9 commit 79b4062
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ reports changes here.

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

Bug fixes:

- Fix dirt calculation upon restart of `CubDB`

## v2.0.2 (2023-01-01)

Bug fixes:
Expand Down
2 changes: 1 addition & 1 deletion lib/cubdb/btree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ defmodule CubDB.Btree do
# updates won't be committed to the database and will be lost in case of a
# restart.
def commit(tree = %Btree{store: store, size: size, root_loc: root_loc, dirt: dirt}) do
Store.put_header(store, header(size: size, location: root_loc, dirt: dirt + 1))
Store.put_header(store, header(size: size, location: root_loc, dirt: dirt))
tree
end

Expand Down
18 changes: 18 additions & 0 deletions test/cubdb_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,24 @@ defmodule CubDBTest do
refute_received :compaction_started
end

test "dirt calculation is consistent after restart", %{tmp_dir: tmp_dir} do
{:ok, db} = CubDB.start_link(data_dir: tmp_dir, auto_compact: false)

CubDB.put(db, "foo", 123)
CubDB.put(db, "bar", 234)
CubDB.delete(db, "foo")
CubDB.clear(db)

original_dirt_factor = CubDB.dirt_factor(db)
original_writes_since_compaction = CubDB.dirt_factor(db)

CubDB.stop(db)

{:ok, db} = CubDB.start_link(data_dir: tmp_dir, auto_compact: false)
assert original_dirt_factor == CubDB.dirt_factor(db)
assert original_writes_since_compaction == CubDB.dirt_factor(db)
end

test "auto compaction is active by default", %{tmp_dir: tmp_dir} do
{:ok, db} = CubDB.start_link(tmp_dir)

Expand Down

0 comments on commit 79b4062

Please sign in to comment.