generated from al8n/template-rs
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzero_cost.rs
199 lines (170 loc) · 4.74 KB
/
zero_cost.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::{cmp, sync::Arc, thread::spawn};
use dbutils::leb128::{decode_u64_varint, encode_u64_varint, encoded_u64_varint_len};
use orderwal::{
base::{OrderWal, Reader, Writer},
types::{KeyRef, Type, TypeRef},
Builder, Comparable, Equivalent,
};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Person {
id: u64,
name: String,
}
impl Person {
fn random() -> Self {
Self {
id: rand::random(),
name: names::Generator::default().next().unwrap(),
}
}
}
#[derive(Debug, Clone, Copy)]
struct PersonRef<'a> {
id: u64,
name: &'a str,
}
impl PartialEq for PersonRef<'_> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.name == other.name
}
}
impl Eq for PersonRef<'_> {}
impl PartialOrd for PersonRef<'_> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PersonRef<'_> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self
.id
.cmp(&other.id)
.then_with(|| self.name.cmp(other.name))
}
}
impl Equivalent<Person> for PersonRef<'_> {
fn equivalent(&self, key: &Person) -> bool {
self.id == key.id && self.name == key.name
}
}
impl Comparable<Person> for PersonRef<'_> {
fn compare(&self, key: &Person) -> core::cmp::Ordering {
self.id.cmp(&key.id).then_with(|| self.name.cmp(&key.name))
}
}
impl Equivalent<PersonRef<'_>> for Person {
fn equivalent(&self, key: &PersonRef<'_>) -> bool {
self.id == key.id && self.name == key.name
}
}
impl Comparable<PersonRef<'_>> for Person {
fn compare(&self, key: &PersonRef<'_>) -> core::cmp::Ordering {
self
.id
.cmp(&key.id)
.then_with(|| self.name.as_str().cmp(key.name))
}
}
impl<'a> KeyRef<'a, Person> for PersonRef<'a> {
fn compare<Q>(&self, a: &Q) -> cmp::Ordering
where
Q: ?Sized + Comparable<Self>,
{
Comparable::compare(a, self).reverse()
}
unsafe fn compare_binary(this: &[u8], other: &[u8]) -> cmp::Ordering {
let (this_id_size, this_id) = decode_u64_varint(this).unwrap();
let (other_id_size, other_id) = decode_u64_varint(other).unwrap();
PersonRef {
id: this_id,
name: std::str::from_utf8(&this[this_id_size..]).unwrap(),
}
.cmp(&PersonRef {
id: other_id,
name: std::str::from_utf8(&other[other_id_size..]).unwrap(),
})
}
}
impl Type for Person {
type Ref<'a> = PersonRef<'a>;
type Error = dbutils::error::InsufficientBuffer;
fn encoded_len(&self) -> usize {
encoded_u64_varint_len(self.id) + self.name.len()
}
#[inline]
fn encode(&self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let id_size = encode_u64_varint(self.id, buf)?;
buf[id_size..].copy_from_slice(self.name.as_bytes());
Ok(id_size + self.name.len())
}
#[inline]
fn encode_to_buffer(
&self,
buf: &mut orderwal::types::VacantBuffer<'_>,
) -> Result<usize, Self::Error> {
let id_size = buf.put_u64_varint(self.id)?;
buf.put_slice_unchecked(self.name.as_bytes());
Ok(id_size + self.name.len())
}
}
impl<'a> TypeRef<'a> for PersonRef<'a> {
unsafe fn from_slice(src: &'a [u8]) -> Self {
let (id_size, id) = decode_u64_varint(src).unwrap();
let name = std::str::from_utf8(&src[id_size..]).unwrap();
PersonRef { id, name }
}
}
fn main() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("zero_copy.wal");
let people = (0..100)
.map(|_| {
let p = Person::random();
let v = std::format!("My name is {}", p.name);
(p, v)
})
.collect::<Vec<_>>();
let mut wal = unsafe {
Builder::new()
.with_capacity(1024 * 1024)
.with_create_new(true)
.with_read(true)
.with_write(true)
.map_mut::<OrderWal<Person, String>, _>(&path)
.unwrap()
};
// Create 100 readers
let readers = (0..100).map(|_| wal.reader()).collect::<Vec<_>>();
let people = Arc::new(people);
// Spawn 100 threads to read from the wal
let handles = readers.into_iter().enumerate().map(|(i, reader)| {
let people = people.clone();
spawn(move || loop {
let (person, hello) = &people[i];
let person_ref = PersonRef {
id: person.id,
name: &person.name,
};
if let Some(p) = reader.get(person) {
assert_eq!(p.key().id, person.id);
assert_eq!(p.key().name, person.name);
assert_eq!(p.value(), hello);
break;
}
if let Some(p) = reader.get(&person_ref) {
assert_eq!(p.key().id, person.id);
assert_eq!(p.key().name, person.name);
assert_eq!(p.value(), hello);
break;
};
})
});
// Insert 100 people into the wal
for (p, h) in people.iter() {
wal.insert(p, h).unwrap();
}
// Wait for all threads to finish
for handle in handles {
handle.join().unwrap();
}
}