Skip to content

Commit

Permalink
Switch page cache to approximate LRU
Browse files Browse the repository at this point in the history
This resolves an edge case where values stored near the beginning of the
database file could become uncacheable. It also simplifies the caching
code
  • Loading branch information
cberner committed Oct 11, 2024
1 parent db458e9 commit 3d184aa
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 254 deletions.
6 changes: 3 additions & 3 deletions src/multimap_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::sealed::Sealed;
use crate::table::{ReadableTableMetadata, TableStats};
use crate::tree_store::{
btree_stats, AllPageNumbersBtreeIter, BranchAccessor, BranchMutator, Btree, BtreeHeader,
BtreeMut, BtreeRangeIter, BtreeStats, CachePriority, Checksum, LeafAccessor, LeafMutator, Page,
PageHint, PageNumber, PagePath, RawBtree, RawLeafBuilder, TransactionalMemory, UntypedBtree,
BtreeMut, BtreeRangeIter, BtreeStats, Checksum, LeafAccessor, LeafMutator, Page, PageHint,
PageNumber, PagePath, RawBtree, RawLeafBuilder, TransactionalMemory, UntypedBtree,
UntypedBtreeMut, BRANCH, DEFERRED, LEAF, MAX_PAIR_LENGTH, MAX_VALUE_LENGTH,
};
use crate::types::{Key, TypeName, Value};
Expand Down Expand Up @@ -1057,7 +1057,7 @@ impl<'txn, K: Key + 'static, V: Key + 'static> MultimapTable<'txn, K, V> {
.insert(key.borrow(), &DynamicCollection::new(&inline_data))?;
} else {
// convert into a subtree
let mut page = self.mem.allocate(leaf_data.len(), CachePriority::Low)?;
let mut page = self.mem.allocate(leaf_data.len())?;
page.memory_mut()[..leaf_data.len()].copy_from_slice(leaf_data);
let page_number = page.get_page_number();
drop(page);
Expand Down
16 changes: 5 additions & 11 deletions src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::sealed::Sealed;
use crate::table::ReadOnlyUntypedTable;
use crate::transaction_tracker::{SavepointId, TransactionId, TransactionTracker};
use crate::tree_store::{
Btree, BtreeHeader, BtreeMut, CachePriority, FreedPageList, FreedTableKey,
InternalTableDefinition, Page, PageHint, PageNumber, SerializedSavepoint, TableTree,
TableTreeMut, TableType, TransactionalMemory, MAX_PAIR_LENGTH, MAX_VALUE_LENGTH,
Btree, BtreeHeader, BtreeMut, FreedPageList, FreedTableKey, InternalTableDefinition, Page,
PageHint, PageNumber, SerializedSavepoint, TableTree, TableTreeMut, TableType,
TransactionalMemory, MAX_PAIR_LENGTH, MAX_VALUE_LENGTH,
};
use crate::types::{Key, Value};
use crate::{
Expand Down Expand Up @@ -1177,10 +1177,7 @@ impl WriteTransaction {
continue;
}
let old_page = self.mem.get_page(path.page_number())?;
let mut new_page = self.mem.allocate_lowest(
old_page.memory().len(),
CachePriority::default_btree(old_page.memory()),
)?;
let mut new_page = self.mem.allocate_lowest(old_page.memory().len())?;
let new_page_number = new_page.get_page_number();
// We have to copy at least the page type into the new page.
// Otherwise its cache priority will be calculated incorrectly
Expand All @@ -1194,10 +1191,7 @@ impl WriteTransaction {
continue;
}
let old_parent = self.mem.get_page(*parent)?;
let mut new_page = self.mem.allocate_lowest(
old_parent.memory().len(),
CachePriority::default_btree(old_parent.memory()),
)?;
let mut new_page = self.mem.allocate_lowest(old_parent.memory().len())?;
let new_page_number = new_page.get_page_number();
// We have to copy at least the page type into the new page.
// Otherwise its cache priority will be calculated incorrectly
Expand Down
16 changes: 7 additions & 9 deletions src/tree_store/btree_base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::tree_store::page_store::{
xxh3_checksum, CachePriority, Page, PageImpl, PageMut, TransactionalMemory,
};
use crate::tree_store::page_store::{xxh3_checksum, Page, PageImpl, PageMut, TransactionalMemory};
use crate::tree_store::PageNumber;
use crate::types::{Key, MutInPlaceValue, Value};
use crate::{Result, StorageError};
Expand Down Expand Up @@ -555,7 +553,7 @@ impl<'a, 'b> LeafBuilder<'a, 'b> {

let required_size =
self.required_bytes(division, first_split_key_bytes + first_split_value_bytes);
let mut page1 = self.mem.allocate(required_size, CachePriority::Low)?;
let mut page1 = self.mem.allocate(required_size)?;
let mut builder = RawLeafBuilder::new(
page1.memory_mut(),
division,
Expand All @@ -574,7 +572,7 @@ impl<'a, 'b> LeafBuilder<'a, 'b> {
- first_split_key_bytes
- first_split_value_bytes,
);
let mut page2 = self.mem.allocate(required_size, CachePriority::Low)?;
let mut page2 = self.mem.allocate(required_size)?;
let mut builder = RawLeafBuilder::new(
page2.memory_mut(),
self.pairs.len() - division,
Expand All @@ -595,7 +593,7 @@ impl<'a, 'b> LeafBuilder<'a, 'b> {
self.pairs.len(),
self.total_key_bytes + self.total_value_bytes,
);
let mut page = self.mem.allocate(required_size, CachePriority::Low)?;
let mut page = self.mem.allocate(required_size)?;
let mut builder = RawLeafBuilder::new(
page.memory_mut(),
self.pairs.len(),
Expand Down Expand Up @@ -1306,7 +1304,7 @@ impl<'a, 'b> BranchBuilder<'a, 'b> {
self.total_key_bytes,
self.fixed_key_size,
);
let mut page = self.mem.allocate(size, CachePriority::High)?;
let mut page = self.mem.allocate(size)?;
let mut builder = RawBranchBuilder::new(&mut page, self.keys.len(), self.fixed_key_size);
builder.write_first_page(self.children[0].0, self.children[0].1);
for i in 1..self.children.len() {
Expand Down Expand Up @@ -1337,7 +1335,7 @@ impl<'a, 'b> BranchBuilder<'a, 'b> {

let size =
RawBranchBuilder::required_bytes(division, first_split_key_len, self.fixed_key_size);
let mut page1 = self.mem.allocate(size, CachePriority::High)?;
let mut page1 = self.mem.allocate(size)?;
let mut builder = RawBranchBuilder::new(&mut page1, division, self.fixed_key_size);
builder.write_first_page(self.children[0].0, self.children[0].1);
for i in 0..division {
Expand All @@ -1356,7 +1354,7 @@ impl<'a, 'b> BranchBuilder<'a, 'b> {
second_split_key_len,
self.fixed_key_size,
);
let mut page2 = self.mem.allocate(size, CachePriority::High)?;
let mut page2 = self.mem.allocate(size)?;
let mut builder = RawBranchBuilder::new(
&mut page2,
self.keys.len() - division - 1,
Expand Down
4 changes: 2 additions & 2 deletions src/tree_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub(crate) use btree_base::{
pub(crate) use btree_iters::{AllPageNumbersBtreeIter, BtreeExtractIf, BtreeRangeIter};
pub use page_store::{file_backend, InMemoryBackend, Savepoint};
pub(crate) use page_store::{
CachePriority, Page, PageHint, PageNumber, SerializedSavepoint, TransactionalMemory,
FILE_FORMAT_VERSION2, MAX_PAIR_LENGTH, MAX_VALUE_LENGTH, PAGE_SIZE,
Page, PageHint, PageNumber, SerializedSavepoint, TransactionalMemory, FILE_FORMAT_VERSION2,
MAX_PAIR_LENGTH, MAX_VALUE_LENGTH, PAGE_SIZE,
};
pub(crate) use table_tree::{FreedPageList, FreedTableKey, TableTree, TableTreeMut};
pub(crate) use table_tree_base::{InternalTableDefinition, TableType};
Loading

0 comments on commit 3d184aa

Please sign in to comment.