-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.rs
1381 lines (1223 loc) · 44.6 KB
/
file.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::api::error::{Error, Result};
use crate::api::page::Page;
use crate::api::tree::Tree;
use crate::api::Store;
use crate::util::cache::{Cache, LruCache};
use crate::util::hex::hex;
use bytes::{Buf, BufMut, BytesMut};
use log::{debug, error, trace};
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashSet};
use std::fs::{self, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::mem::size_of;
use std::ops::Deref;
use std::path::Path;
use std::sync::Arc;
use parking_lot::{
MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
};
pub struct File<P: Page> {
/// Underlying file reference where all data is physically stored.
file: Arc<RwLock<fs::File>>,
head: Head,
/// In-memory page cache. All page access happens only through cached page representation.
cache: Arc<RwLock<LruCache<u32, P>>>,
dirty: Arc<RwLock<HashSet<u32>>>,
/// Min-heap of available page identifiers (this helps avoid "gaps": empty pages inside file).
empty: Arc<RwLock<BinaryHeap<Reverse<u32>>>>,
}
const MAGIC: &[u8] = b"YAKVDB42";
const HEAD: usize = MAGIC.len() + size_of::<Head>();
const ROOT: u32 = 1;
const SPLIT_THRESHOLD: u8 = 80;
const MERGE_THRESHOLD: u8 = 20;
#[derive(Debug)]
#[repr(C)]
struct Head {
page_bytes: u32,
page_count: u32,
}
impl<P: Page> File<P> {
pub fn make(path: &Path, page_bytes: u32) -> io::Result<Self> {
if path.exists() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("File exists: {:?}", path),
));
}
let mut file = OpenOptions::new()
.create(true)
.write(true)
.read(true)
.open(path)?;
let head = Head {
page_bytes,
page_count: 1,
};
let mut buf = BytesMut::with_capacity(HEAD + page_bytes as usize);
buf.put_slice(MAGIC);
buf.put_u32(head.page_bytes);
buf.put_u32(head.page_count);
let root = P::create(ROOT, head.page_bytes);
buf.put_slice(root.as_ref());
file.write_all(buf.as_ref())?;
file.flush()?;
Ok(Self {
file: Arc::new(RwLock::new(file)),
head,
cache: Arc::new(RwLock::new(LruCache::new(32))),
dirty: Arc::new(RwLock::new(HashSet::with_capacity(32))),
empty: Arc::new(RwLock::new(BinaryHeap::with_capacity(32))),
})
}
pub fn open(path: &Path) -> io::Result<Self> {
let mut file = OpenOptions::new()
.create(true)
.read(true)
.write(true)
.open(path)?;
let len = file.metadata()?.len() as usize;
if len < HEAD {
return Err(io::Error::new(io::ErrorKind::Other, "File too short"));
}
let mut buf = BytesMut::with_capacity(HEAD);
buf.extend_from_slice(&[0u8; HEAD]);
file.read_exact(&mut buf[..])?;
let mut magic = [0u8; 8];
buf.copy_to_slice(&mut magic);
if magic != MAGIC {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("MAGIC mismatch: {:?}", magic),
));
}
let head = Head {
page_bytes: buf.get_u32(),
page_count: buf.get_u32(),
};
if head.page_bytes > u16::MAX as u32 {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Page size too large: {}", head.page_bytes),
));
}
if len < HEAD + head.page_bytes as usize {
return Err(io::Error::new(
io::ErrorKind::Other,
"File does not contain one full page".to_string(),
));
}
let mut root = P::reserve(head.page_bytes);
file.read_exact(root.as_mut())?;
let this = Self {
file: Arc::new(RwLock::new(file)),
head,
cache: Arc::new(RwLock::new(LruCache::new(32))),
dirty: Arc::new(RwLock::new(HashSet::with_capacity(32))),
empty: Arc::new(RwLock::new(BinaryHeap::with_capacity(16))),
};
this.cache.write().put(ROOT, root);
let total_pages = (len - HEAD) as u32 / this.head.page_bytes;
debug!("Processing pages for compaction: {}", total_pages);
if this.head.page_count < total_pages {
for id in 2..=total_pages {
// skipping the root page (id=1)
if let Ok(page) = this.load(this.offset(id), this.head.page_bytes) {
if page.len() == 0 {
debug!("Page id={} is empty", id);
this.empty.write().push(Reverse(id));
}
} else {
error!("Page failed to load: id={}", id);
}
}
}
Ok(this)
}
fn load(&self, offset: usize, length: u32) -> io::Result<P> {
let mut page = P::reserve(length);
{
let mut file = self.file.write();
file.seek(SeekFrom::Start(offset as u64))?;
file.read_exact(page.as_mut())?;
}
debug!("Loading page {}", page.id());
Ok(page)
}
fn save(&self, page: &P) -> io::Result<()> {
debug!("Saving page {}", page.id());
let offset = self.offset(page.id()) as u64;
{
let mut file = self.file.write();
file.seek(SeekFrom::Start(offset))?;
file.write_all(page.as_ref())
}
}
fn offset(&self, id: u32) -> usize {
HEAD + (id - 1) as usize * self.head.page_bytes as usize
}
}
impl<P: Page> Store for File<P> {
fn lookup(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
debug!("lookup: {}", hex(key));
let mut seen = HashSet::with_capacity(8);
let mut page = self.root();
loop {
let idx_opt = page.ceil(key);
if idx_opt.is_none() {
return Ok(None);
}
let idx = idx_opt.unwrap();
let slot_opt = page.slot(idx);
if slot_opt.is_none() {
return Err(Error::Tree(page.id(), format!("Slot not found: {}", idx)));
}
let slot = slot_opt.unwrap();
if slot.page == 0 {
// Log how deep the lookup went into the tree depth: seen.len()
return if key == page.key(idx) {
Ok(Some(page.val(idx).to_vec()))
} else {
Ok(None)
};
} else {
let id = page.id();
drop(page);
if seen.contains(&slot.page) {
return Err(Error::Tree(id, "Cyclic reference detected".to_string()));
}
seen.insert(id);
let page_opt = self.page(slot.page);
if page_opt.is_none() {
return Err(Error::Tree(id, format!("Page not found: {}", slot.page)));
}
page = page_opt.unwrap();
}
}
}
fn insert(&self, key: &[u8], val: &[u8]) -> Result<()> {
debug!("insert: {} -> {}", hex(key), hex(val));
let mut page = self.root_mut();
let mut seen = HashSet::with_capacity(8);
let mut path = Vec::with_capacity(8);
loop {
let id = page.id();
let parent_id = path.last().cloned().map(|(id, _)| id).unwrap_or_default();
if page.len() == 0 {
// TODO handle keys/values larger than (half-) page size
let len = (key.len() + val.len()) as u32;
if !page.fits(len) {
return Err(Error::Tree(
page.id(),
format!(
"Entry does not fit into the page: size={} free={}",
len,
page.free()
),
));
}
page.put_val(key, val);
drop(page);
return Ok(());
}
let idx = page.ceil(key).unwrap_or_else(|| page.len() - 1);
drop(page);
if let Some((parent_id, parent_idx)) = path.last().cloned() {
let mut parent_page = self.page_mut(parent_id).unwrap();
let parent_key = parent_page.key(parent_idx);
if key > parent_key {
parent_page.remove(parent_idx);
parent_page.put_ref(key, id);
drop(parent_page);
}
}
page = self.page_mut(id).unwrap();
let slot_opt = page.slot(idx);
if slot_opt.is_none() {
return Err(Error::Tree(page.id(), format!("Slot not found: {}", idx)));
}
let slot = slot_opt.unwrap();
if slot.page == 0 {
let len = (key.len() + val.len()) as u32;
if !page.fits(len) {
// TODO handle keys/values larger than (half-) page size
return Err(Error::Tree(
page.id(),
format!(
"Entry does not fit into the page: size={} free={}",
len,
page.free()
),
));
}
page.put_val(key, val);
let full = page.full();
drop(page);
if full > SPLIT_THRESHOLD {
self.split(id, parent_id)?;
}
while !path.is_empty() {
let (page_id, _) = path.pop().unwrap();
let (parent_id, _) = path.last().cloned().unwrap_or_default();
let full = {
let page = self.page(page_id).unwrap();
page.full()
};
if full > SPLIT_THRESHOLD {
self.split(page_id, parent_id)?;
}
}
self.flush()?;
return Ok(());
} else {
path.push((id, idx));
seen.insert(id);
if seen.contains(&slot.page) {
return Err(Error::Tree(
id,
format!("Cyclic reference detected: {:?}", path),
));
}
drop(page);
let page_opt = self.page_mut(slot.page);
if page_opt.is_none() {
return Err(Error::Tree(
slot.page,
format!("Page not found: {}", slot.page),
));
}
page = page_opt.unwrap();
}
}
}
fn remove(&self, key: &[u8]) -> Result<()> {
debug!("remove: {}", hex(key));
let mut page = self.root_mut();
let mut seen = HashSet::with_capacity(8);
let mut path = Vec::with_capacity(8);
loop {
let idx_opt = page.ceil(key);
if idx_opt.is_none() {
return Ok(());
}
let idx = idx_opt.unwrap();
let slot_opt = page.slot(idx);
if slot_opt.is_none() {
return Err(Error::Tree(page.id(), format!("Slot not found: {}", idx)));
}
let slot = slot_opt.unwrap();
let id = page.id();
if slot.page == 0 {
debug!("remove: key={} page={} idx={}", hex(key), id, idx);
page.remove(idx);
drop(page);
// Navigate up-tree and remove/update references if needed
let mut page_id = id;
for (parent_id, mut idx) in path.iter().cloned().rev() {
let full = self.page(page_id).unwrap().full();
if full < MERGE_THRESHOLD {
let peer_id = {
let parent = self.page(parent_id).unwrap();
let mut peers = Vec::with_capacity(2);
if idx > 0 {
let peer = parent.slot(idx - 1).unwrap().page;
peers.push(peer);
}
if idx < parent.len() - 1 {
let peer = parent.slot(idx + 1).unwrap().page;
peers.push(peer);
}
drop(parent);
peers
.into_iter()
.filter_map(|peer_id| {
let peer = self.page(peer_id).unwrap();
let full = peer.full();
if peer.len() > 0 && full < MERGE_THRESHOLD {
Some((peer_id, full))
} else {
None
}
})
.min_by_key(|(_, full)| *full)
.map(|(peer_id, _)| peer_id)
};
if let Some(peer_id) = peer_id {
trace!(
"merge: found peer_id={} to merge page_id={} (parent_id={})",
peer_id,
page_id,
parent_id
);
let peer_max = {
let peer = self.page(peer_id).unwrap();
peer.max().to_vec()
};
trace!("\t merge: peer_max={}", hex(&peer_max));
let mut parent = self.page_mut(parent_id).unwrap();
parent.remove(idx);
let peer_idx = parent.ceil(&peer_max).unwrap();
trace!("\t merge: parent remove: peer_idx={} idx={}", peer_idx, idx);
parent.remove(peer_idx);
drop(parent);
self.merge(page_id, peer_id)?;
let page_max = {
let peer = self.page(peer_id).unwrap();
peer.max().to_vec()
};
trace!("\t merge: page_max={}", hex(&page_max));
let mut parent = self.page_mut(parent_id).unwrap();
trace!(
"\t merge: parent insert: page_max={}, peer_id={}",
hex(&page_max),
peer_id
);
parent.put_ref(&page_max, peer_id);
idx = parent.ceil(&page_max).unwrap();
page_id = peer_id;
}
}
let max_opt = {
let page = self.page(page_id).unwrap();
if page.len() > 0 {
Some(page.max().to_vec())
} else {
None
}
};
let mut parent = self.page_mut(parent_id).unwrap();
if let Some(max) = max_opt {
if max < parent.key(idx).to_vec() {
parent.remove(idx);
parent.put_ref(&max, page_id);
}
} else {
parent.remove(idx);
}
drop(parent);
page_id = parent_id;
}
self.flush()?;
return Ok(());
} else {
path.push((id, idx));
seen.insert(id);
if seen.contains(&slot.page) {
return Err(Error::Tree(id, "Cyclic reference detected".to_string()));
}
drop(page);
let page_opt = self.page_mut(slot.page);
if page_opt.is_none() {
return Err(Error::Tree(id, format!("Page not found: {}", slot.page)));
}
page = page_opt.unwrap();
}
}
}
fn is_empty(&self) -> bool {
self.root().len() == 0
}
fn min(&self) -> Result<Option<Vec<u8>>> {
let mut page = self.root();
if page.len() == 0 {
return Ok(None);
}
loop {
let slot = page.slot(0).unwrap();
if slot.page == 0 {
return Ok(Some(page.min().to_vec()));
} else {
let id = slot.page;
drop(page);
if let Some(next) = self.page(id) {
page = next;
} else {
return Err(Error::Tree(id, "Page not found".to_string()));
}
}
}
}
fn max(&self) -> Result<Option<Vec<u8>>> {
let mut page = self.root();
if page.len() == 0 {
return Ok(None);
}
loop {
let last = page.len() - 1;
let slot = page.slot(last).unwrap();
if slot.page == 0 {
return Ok(Some(page.max().to_vec()));
} else {
let id = slot.page;
drop(page);
if let Some(next) = self.page(id) {
page = next;
} else {
return Err(Error::Tree(id, "Page not found".to_string()));
}
}
}
}
fn above(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
debug!("above: {}", hex(key));
let mut path = Vec::with_capacity(8);
let mut page = self.root();
if page.len() == 0 {
return Ok(None);
}
if page.max() < key {
return Ok(None);
}
loop {
let idx = page.ceil(key).unwrap();
let slot = page.slot(idx).unwrap();
if slot.page == 0 {
return if key < page.key(idx) {
Ok(Some(page.key(idx).to_vec()))
} else if key == page.key(idx) && idx < page.len() - 1 {
Ok(Some(page.key(idx + 1).to_vec()))
} else {
// ceil == key, need to take min value from parent's next adjacent subtree
for (parent_id, parent_idx) in path.iter().rev().cloned() {
page = self.page(parent_id).unwrap();
if parent_idx < page.len() - 1 {
let id = page.slot(parent_idx + 1).unwrap().page;
drop(page);
page = self.page(id).unwrap();
loop {
let slot = page.slot(0).unwrap();
if slot.page == 0 {
return Ok(Some(page.min().to_vec()));
} else {
drop(page);
page = self.page(slot.page).unwrap();
}
}
}
}
// the key appears to be the maximum value stored in the tree
Ok(None)
};
} else {
path.push((page.id(), idx));
let id = slot.page;
drop(page);
if let Some(next) = self.page(id) {
page = next;
} else {
return Err(Error::Tree(id, "Page not found".to_string()));
}
}
}
}
fn below(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
debug!("below: {}", hex(key));
let mut path = Vec::with_capacity(8);
let mut page = self.root();
if page.len() == 0 {
return Ok(None);
}
if page.max() < key {
return Ok(Some(page.max().to_vec()));
}
loop {
let idx = page.ceil(key).unwrap();
let slot = page.slot(idx).unwrap();
if slot.page == 0 {
return if idx > 0 && key > page.key(idx - 1) {
Ok(Some(page.key(idx - 1).to_vec()))
} else {
// ceil == key, need to take max value from parent's previous adjacent page
for (parent_id, parent_idx) in path.iter().rev().cloned() {
drop(page);
page = self.page(parent_id).unwrap();
if parent_idx > 0 {
let idx = parent_idx - 1;
let id = page.slot(idx).unwrap().page;
drop(page);
page = self.page(id).unwrap();
return Ok(Some(page.max().to_vec()));
}
}
// the key seems to be the minimum value stored in the tree
Ok(None)
};
} else {
path.push((page.id(), idx));
let id = slot.page;
drop(page);
if let Some(next) = self.page(id) {
page = next;
} else {
return Err(Error::Tree(id, "Page not found".to_string()));
}
}
}
}
}
impl<P: Page> Tree<P> for File<P> {
fn root(&self) -> MappedRwLockReadGuard<'_, P> {
self.page(ROOT).unwrap()
}
fn page(&self, id: u32) -> Option<MappedRwLockReadGuard<'_, P>> {
self.cache(id).ok()?;
let page = RwLockReadGuard::map(self.cache.read(), |cache| cache.get(&id).unwrap());
Some(page)
}
fn root_mut(&self) -> MappedRwLockWriteGuard<'_, P> {
self.mark(ROOT);
self.page_mut(ROOT).unwrap()
}
fn page_mut(&self, id: u32) -> Option<MappedRwLockWriteGuard<'_, P>> {
self.cache(id).ok()?;
self.mark(id);
let page = RwLockWriteGuard::map(self.cache.write(), |cache| cache.get_mut(&id).unwrap());
Some(page)
}
fn cache(&self, id: u32) -> io::Result<()> {
let has_id = self.cache.read().has(&id);
if !has_id {
let page = self.load(self.offset(id), self.head.page_bytes)?;
self.cache.write().put(id, page);
}
Ok(())
}
fn mark(&self, id: u32) {
self.dirty.write().insert(id);
}
fn flush(&self) -> crate::api::error::Result<()> {
let pages = self.dirty.read().iter().cloned().collect::<Vec<_>>();
self.dirty.write().clear();
let mut failed = vec![];
for id in pages {
if let Some(page) = self.page(id) {
self.save(page.deref())?;
debug!("flush: page={}", id);
} else {
failed.push(id);
error!("flush: no such page={}", id);
}
}
if failed.is_empty() {
Ok(())
} else {
let pages = failed
.into_iter()
.map(|id| format!("{}", id))
.collect::<Vec<_>>()
.join(", ");
Err(Error::Other(format!("Missing pages: {}", pages)))
}
}
fn next_id(&self) -> Result<u32> {
let is_empty = self.empty.read().is_empty();
if !is_empty {
let id = self.empty.write().pop().unwrap().0;
let temp = P::create(id, self.head.page_bytes);
let mut page = self.page_mut(id).unwrap();
page.as_mut().copy_from_slice(temp.as_ref());
return Ok(id);
}
let len = self.file.write().metadata().unwrap().len();
let id = 1 + ((len - HEAD as u64) / self.head.page_bytes as u64) as u32;
let page = P::create(id, self.head.page_bytes);
{
let mut f = self.file.write();
f.seek(SeekFrom::End(0))?;
f.write_all(page.as_ref())?;
}
Ok(id)
}
fn free_id(&self, id: u32) {
self.empty.write().push(Reverse(id))
}
fn split(&self, id: u32, parent_id: u32) -> Result<()> {
if id == ROOT {
let lo_id = self.next_id()?;
let hi_id = self.next_id()?;
debug!(
"split: root={} into lo={} and hi={} (parent={})",
id, lo_id, hi_id, parent_id
);
let (copy, lo_max, hi_max) = {
let page = self.page(id).unwrap();
let copy = page.copy();
let half = page.len() as usize / 2;
let lo_max = copy.get(half - 1).map(|(k, _, _)| k).cloned().unwrap();
let hi_max = copy.last().map(|(k, _, _)| k).cloned().unwrap();
(copy, lo_max, hi_max)
};
let half = copy.len() / 2;
{
let mut lo = self.page_mut(lo_id).unwrap();
copy.iter().take(half).for_each(|(key, val, page)| {
trace!(
"split: move k={} v={} p={} from {} to {}",
hex(key),
hex(val),
*page,
id,
lo_id
);
if *page == 0 {
lo.put_val(key, val);
} else {
lo.put_ref(key, *page);
}
});
}
{
let mut hi = self.page_mut(hi_id).unwrap();
copy.iter().skip(half).for_each(|(key, val, page)| {
trace!(
"split: move k={} v={} p={} from {} to {}",
hex(key),
hex(val),
*page,
id,
hi_id
);
if *page == 0 {
hi.put_val(key, val);
} else {
hi.put_ref(key, *page);
}
});
}
{
let mut page = self.page_mut(id).unwrap();
page.clear();
page.put_ref(&lo_max, lo_id);
page.put_ref(&hi_max, hi_id);
}
Ok(())
} else {
let (copy, max) = {
let page = self.page(id).unwrap();
(page.copy(), page.max().to_vec())
};
let half = copy.len() / 2;
let peer_id = self.next_id()?;
debug!(
"split: page={} into peer={} (parent={})",
id, peer_id, parent_id
);
let page_max = {
let mut page = self.page_mut(id).unwrap();
copy.iter().skip(half).for_each(|(key, _, _)| {
let idx = page.find(key).unwrap();
page.remove(idx);
});
page.max().to_vec()
};
let peer_max = {
let mut peer = self.page_mut(peer_id).unwrap();
copy.iter().skip(half).for_each(|(key, val, p)| {
trace!(
"split: move k={} v={} p={} from {} to {}",
hex(key),
hex(val),
*p,
id,
peer_id
);
if *p == 0 {
peer.put_val(key, val);
} else {
peer.put_ref(key, *p);
}
});
peer.max().to_vec()
};
{
let mut parent = self.page_mut(parent_id).unwrap();
let idx = parent.find(&max).unwrap();
parent.remove(idx);
parent.put_ref(&page_max, id);
parent.put_ref(&peer_max, peer_id);
}
self.check(parent_id, id)?;
self.check(parent_id, peer_id)?;
Ok(())
}
}
fn check(&self, parent_id: u32, page_id: u32) -> Result<()> {
let page_max = {
let page = self.page(page_id).unwrap();
page.max().to_vec()
};
let (parent_key, parent_ref) = {
let parent = self.page(parent_id).unwrap();
let page_idx = parent.find(&page_max).unwrap();
let parent_key = parent.key(page_idx).to_vec();
let parent_ref = parent
.slot(page_idx)
.map(|slot| slot.page)
.unwrap_or_default();
(parent_key, parent_ref)
};
if parent_key != page_max {
log::error!(
"parent_key != page_max: parent_key={} page_max={}",
hex(&parent_key),
hex(&page_max)
);
return Err(Error::Tree(
parent_id,
"Parent entry key does not match child page".to_string(),
));
}
if parent_ref != page_id {
log::error!(
"parent_ref != page_id: parent_ref={} page_id={}",
parent_ref,
page_id,
);
return Err(Error::Tree(
parent_id,
"Parent entry ref does not match child page".to_string(),
));
}
Ok(())
}
fn merge(&self, src_id: u32, dst_id: u32) -> Result<()> {
debug!("merge: src={} into dst={}", src_id, dst_id);
let src_copy = {
let page = self.page(src_id).unwrap();
page.copy()
};
{
let mut page = self.page_mut(dst_id).unwrap();
for (key, val, p) in src_copy {
trace!(
"merge: move k={} v={} p={} from {} to {}",
hex(&key),
hex(&val),
p,
src_id,
dst_id
);
if p == 0 {
page.put_val(&key, &val);
} else {
page.put_ref(&key, p);
}
}
page.max().to_vec()
};
{
let mut page = self.page_mut(src_id).unwrap();
page.clear();
}
self.free_id(src_id);
Ok(())
}
fn dump(&self) -> String {
fn dump_page<P: Page>(
file: &File<P>,
page_id: u32,
parent_id: u32,
acc: &mut String,
prefix: String,
tab: String,
) {
if page_id == 0 {
return;
}
let page = file.page(page_id).unwrap();
let copy = page.copy();
let full = page.full();
acc.push_str(&if copy.is_empty() {
format!("{}page={}: empty", prefix, page_id)
} else {
let entries = copy
.iter()
.map(|(k, v, p)| format!("{}{}, {}, {}", prefix, hex(k), hex(v), p))
.collect::<Vec<_>>()
.join("\n");
format!(
"{}page={}: (parent={}) {}% full\n{}",
prefix, page_id, parent_id, full, entries
)
});
acc.push('\n');
let links = copy.iter().map(|(_, _, p)| p).cloned();
links.for_each(|id| {
let mut p = prefix.clone();
p.push_str(&tab);
dump_page(file, id, page_id, acc, p, tab.clone());
});
}
let mut acc = String::with_capacity(1024);
dump_page(self, ROOT, 0, &mut acc, "".to_string(), "\t".to_string());
acc
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::disk::block::Block;
use crate::util;
use crate::util::hex::hex;
use rand::prelude::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
use std::ops::Deref;
fn get<P: Page>(page: &P, key: &[u8]) -> Option<(Vec<u8>, u32)> {
page.find(key)
.map(|idx| (page.val(idx).to_vec(), page.slot(idx).unwrap().page))
}
#[test]
fn test_page() {
let path = Path::new("target/test_page.tmp");
if path.exists() {
fs::remove_file(path).unwrap();
}
let size: u32 = 256;
let data = vec![
(b"aaa".to_vec(), b"zxczxczxc".to_vec(), 0),
(b"bbb".to_vec(), b"asdasdasd".to_vec(), 0),
(b"ccc".to_vec(), b"qweqweqwe".to_vec(), 0),
(b"ddd".to_vec(), b"123123123".to_vec(), 0),
(b"xxx".to_vec(), vec![], 3333),
(b"yyy".to_vec(), vec![], 2222),
(b"zzz".to_vec(), vec![], 1111),
];
{
let file: File<Block> = File::make(path, size).unwrap();
{
let mut page = file.root_mut();
for (k, v, p) in data.iter() {
if *p == 0 {
page.put_val(k, v);
} else {
page.put_ref(k, *p);