-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: refactor insert to separate file
- Loading branch information
Showing
7 changed files
with
335 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use blake3::Hash; | ||
use redb::{Database, ReadableTable, Table, TableDefinition, WriteTransaction}; | ||
|
||
#[derive(Debug)] | ||
enum RefCountDiff { | ||
Increment, | ||
Decrement, | ||
} | ||
|
||
pub(crate) fn increment_ref_count(node: Option<Hash>, table: &mut Table<&[u8], (u64, &[u8])>) { | ||
update_ref_count(node, RefCountDiff::Increment, table); | ||
} | ||
|
||
pub(crate) fn decrement_ref_count(node: Option<Hash>, table: &mut Table<&[u8], (u64, &[u8])>) { | ||
update_ref_count(node, RefCountDiff::Decrement, table); | ||
} | ||
|
||
fn update_ref_count( | ||
node: Option<Hash>, | ||
ref_diff: RefCountDiff, | ||
table: &mut Table<&[u8], (u64, &[u8])>, | ||
) { | ||
if let Some(hash) = node { | ||
let mut existing = table | ||
.get(hash.as_bytes().as_slice()) | ||
.unwrap() | ||
.expect("node shouldn't be messing!"); | ||
|
||
let (ref_count, bytes) = { | ||
let (r, v) = existing.value(); | ||
(r, v.to_vec()) | ||
}; | ||
drop(existing); | ||
|
||
let ref_count = match ref_diff { | ||
RefCountDiff::Increment => ref_count + 1, | ||
RefCountDiff::Decrement => { | ||
if ref_count > 0 { | ||
ref_count - 1 | ||
} else { | ||
ref_count | ||
} | ||
} | ||
}; | ||
|
||
match ref_count { | ||
0 => { | ||
// TODO: Confirm (read: test) this, because it is not easy to see in graphs. | ||
table.remove(hash.as_bytes().as_slice()); | ||
} | ||
_ => { | ||
table.insert(hash.as_bytes().as_slice(), (ref_count, bytes.as_slice())); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.