-
Notifications
You must be signed in to change notification settings - Fork 243
/
mod.rs
154 lines (123 loc) · 4.27 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
mod keyed_state;
mod memory_state;
mod mk_key;
mod persistent_state;
mod single_state;
use std::borrow::Cow;
use std::ops::Deref;
use std::rc::Rc;
use std::vec;
use crate::prelude::*;
use ahash::RandomState;
use common::SizeOf;
use hashbag::HashBag;
pub(crate) use self::memory_state::MemoryState;
pub(crate) use self::persistent_state::PersistentState;
pub(crate) trait State: SizeOf + Send {
/// Add an index keyed by the given columns and replayed to by the given partial tags.
fn add_key(&mut self, columns: &[usize], partial: Option<Vec<Tag>>);
/// Returns whether this state is currently keyed on anything. If not, then it cannot store any
/// infromation and is thus "not useful".
fn is_useful(&self) -> bool;
fn is_partial(&self) -> bool;
// Inserts or removes each record into State. Records that miss all indices in partial state
// are removed from `records` (thus the mutable reference).
fn process_records(&mut self, records: &mut Records, partial_tag: Option<Tag>);
fn mark_hole(&mut self, key: &[DataType], tag: Tag);
fn mark_filled(&mut self, key: Vec<DataType>, tag: Tag);
fn lookup<'a>(&'a self, columns: &[usize], key: &KeyType) -> LookupResult<'a>;
fn rows(&self) -> usize;
fn keys(&self) -> Vec<Vec<usize>>;
/// Return a copy of all records. Panics if the state is only partially materialized.
fn cloned_records(&self) -> Vec<Vec<DataType>>;
/// Evict `count` randomly selected keys, returning key colunms of the index chosen to evict
/// from along with the keys evicted and the number of bytes evicted.
fn evict_random_keys(&mut self, count: usize) -> (&[usize], Vec<Vec<DataType>>, u64);
/// Evict the listed keys from the materialization targeted by `tag`, returning the key columns
/// of the index that was evicted from and the number of bytes evicted.
fn evict_keys(&mut self, tag: Tag, keys: &[Vec<DataType>]) -> Option<(&[usize], u64)>;
fn clear(&mut self);
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub(crate) struct Row(Rc<Vec<DataType>>);
pub(crate) type Rows = HashBag<Row, RandomState>;
unsafe impl Send for Row {}
impl From<Rc<Vec<DataType>>> for Row {
fn from(r: Rc<Vec<DataType>>) -> Self {
Self(r)
}
}
impl AsRef<[DataType]> for Row {
fn as_ref(&self) -> &[DataType] {
&**self.0
}
}
impl std::borrow::Borrow<[DataType]> for Row {
fn borrow(&self) -> &[DataType] {
&**self.0
}
}
impl Deref for Row {
type Target = Vec<DataType>;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl SizeOf for Row {
fn size_of(&self) -> u64 {
use std::mem::size_of;
size_of::<Self>() as u64
}
fn deep_size_of(&self) -> u64 {
(*self.0).deep_size_of()
}
fn is_empty(&self) -> bool {
false
}
}
/// An std::borrow::Cow-like wrapper around a collection of rows.
pub(crate) enum RecordResult<'a> {
Borrowed(&'a HashBag<Row, RandomState>),
Owned(Vec<Vec<DataType>>),
}
impl<'a> RecordResult<'a> {
pub(crate) fn len(&self) -> usize {
match *self {
RecordResult::Borrowed(rs) => rs.len(),
RecordResult::Owned(ref rs) => rs.len(),
}
}
pub(crate) fn is_empty(&self) -> bool {
match *self {
RecordResult::Borrowed(rs) => rs.is_empty(),
RecordResult::Owned(ref rs) => rs.is_empty(),
}
}
}
impl<'a> IntoIterator for RecordResult<'a> {
type Item = Cow<'a, [DataType]>;
type IntoIter = RecordResultIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
match self {
RecordResult::Borrowed(rs) => RecordResultIterator::Borrowed(rs.iter()),
RecordResult::Owned(rs) => RecordResultIterator::Owned(rs.into_iter()),
}
}
}
pub(crate) enum RecordResultIterator<'a> {
Owned(vec::IntoIter<Vec<DataType>>),
Borrowed(hashbag::Iter<'a, Row>),
}
impl<'a> Iterator for RecordResultIterator<'a> {
type Item = Cow<'a, [DataType]>;
fn next(&mut self) -> Option<Self::Item> {
match self {
RecordResultIterator::Borrowed(iter) => iter.next().map(|r| Cow::from(&r[..])),
RecordResultIterator::Owned(iter) => iter.next().map(Cow::from),
}
}
}
pub(crate) enum LookupResult<'a> {
Some(RecordResult<'a>),
Missing,
}