-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from LevKruglyak/52-mvp-for-learned-components
MVP for learned components
- Loading branch information
Showing
27 changed files
with
1,994 additions
and
156 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
pub mod btree_disk; | ||
pub mod btree_memory; | ||
pub mod btree_top; | ||
pub mod disk; | ||
pub mod memory; | ||
|
||
mod node; | ||
|
||
pub use btree_disk::*; | ||
pub use btree_memory::*; | ||
pub use btree_top::*; | ||
pub use disk::*; | ||
pub use memory::*; |
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,5 @@ | ||
pub mod pgm_memory; | ||
|
||
mod node; | ||
|
||
pub use pgm_memory::*; |
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,68 @@ | ||
use learned_index_segmentation::LinearModel; | ||
|
||
use crate::{Key, KeyBounded, StaticBounded}; | ||
use gapped_array::GappedKVArray; | ||
|
||
impl<K: StaticBounded, const EPSILON: usize> KeyBounded<K> for LinearModel<K, EPSILON> { | ||
fn lower_bound(&self) -> &K { | ||
self.min_key() | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct PGMNode<K: Key, V, const EPSILON: usize> { | ||
gapped: GappedKVArray<K, V>, | ||
model: LinearModel<K, EPSILON>, | ||
} | ||
|
||
impl<K: Key, V, const EPSILON: usize> KeyBounded<K> for PGMNode<K, V, EPSILON> { | ||
fn lower_bound(&self) -> &K { | ||
self.gapped.min().unwrap_or(&K::max_ref()) | ||
} | ||
} | ||
|
||
impl<K: Key, V, const EPSILON: usize> Default for PGMNode<K, V, EPSILON> { | ||
fn default() -> Self { | ||
Self { | ||
gapped: GappedKVArray::new(0), | ||
model: LinearModel::sentinel(), | ||
} | ||
} | ||
} | ||
|
||
impl<K: Key, V, const EPSILON: usize> PGMNode<K, V, EPSILON> { | ||
pub fn from_trained(model: LinearModel<K, EPSILON>, entries: Vec<(K, V)>) -> Self { | ||
// NOTE: Filling at 0.5 utilization is just a heuristic, eventually this should be a param | ||
let mut gapped = GappedKVArray::new(entries.len() * 2); | ||
for (key, value) in entries { | ||
let hint = model.hint(&key).min(gapped.len() - 1); | ||
gapped | ||
.initial_model_based_insert((key, value), hint) | ||
.unwrap(); | ||
} | ||
Self { gapped, model } | ||
} | ||
|
||
pub fn search_exact(&self, key: &K) -> Option<&V> { | ||
let hint = self.model.hint(key); | ||
self.gapped.search_exact(key, Some(hint)) | ||
} | ||
|
||
pub fn search_pir(&self, key: &K) -> &V { | ||
let hint = self.model.hint(key); | ||
match self.gapped.search_pir(key, Some(hint)) { | ||
Some(val) => val, | ||
None => self.gapped.min_val().unwrap(), | ||
} | ||
} | ||
|
||
pub fn grow_insert(&mut self, entry: (K, V)) { | ||
if self.gapped.density() >= 0.8 { | ||
let scale_factor = 2.0; | ||
self.gapped.rescale(scale_factor).unwrap(); | ||
self.model.rescale(scale_factor as f64); | ||
} | ||
let hint = self.model.hint(&entry.0); | ||
self.gapped.upsert_with_hint(entry, hint).unwrap(); | ||
} | ||
} |
Oops, something went wrong.