Compare f126ad1 ... +1 ... e42e838


@@ -122,6 +122,13 @@
Loading
122 122
    fn is_free(&self) -> PRes<bool> {
123 123
        Ok((self.buff[1] & 0b1000_0000) != 0)
124 124
    }
125 +
    fn cursor_pos(&self) -> usize {
126 +
        if self.pos >= 2 {
127 +
            self.pos - 2
128 +
        } else {
129 +
            0
130 +
        }
131 +
    }
125 132
}
126 133
127 134
#[derive(Clone)]
@@ -139,6 +146,7 @@
Loading
139 146
    fn clone_read(&self) -> ReadPage;
140 147
    fn clone_write(&self) -> Page;
141 148
    fn is_free(&self) -> PRes<bool>;
149 +
    fn cursor_pos(&self) -> usize;
142 150
}
143 151
144 152
fn free_flag_set(mut cur: u8, free: bool) -> u8 {
@@ -226,6 +234,13 @@
Loading
226 234
    fn is_free(&self) -> PRes<bool> {
227 235
        Ok(is_free(self.buff[1]))
228 236
    }
237 +
    fn cursor_pos(&self) -> usize {
238 +
        if self.pos >= 2 {
239 +
            self.pos - 2
240 +
        } else {
241 +
            0
242 +
        }
243 +
    }
229 244
}
230 245
231 246
impl Page {

@@ -33,53 +33,53 @@
Loading
33 33
34 34
pub(crate) trait InfallibleReadVarInt: InfallibleRead {
35 35
    #[inline]
36 -
    fn read_u8_varint(&mut self) -> u8 {
36 +
    fn read_varint_u8(&mut self) -> u8 {
37 37
        io::read_u8(InfallibleReadWrapper::new(self)).expect("infallible")
38 38
    }
39 39
40 40
    #[inline]
41 -
    fn read_u16_varint(&mut self) -> u16 {
41 +
    fn read_varint_u16(&mut self) -> u16 {
42 42
        io::read_u16(InfallibleReadWrapper::new(self)).expect("infallible")
43 43
    }
44 44
45 45
    #[inline]
46 -
    fn read_u32_varint(&mut self) -> u32 {
46 +
    fn read_varint_u32(&mut self) -> u32 {
47 47
        io::read_u32(InfallibleReadWrapper::new(self)).expect("infallible")
48 48
    }
49 49
50 50
    #[inline]
51 -
    fn read_u64_varint(&mut self) -> u64 {
51 +
    fn read_varint_u64(&mut self) -> u64 {
52 52
        io::read_u64(InfallibleReadWrapper::new(self)).expect("infallible")
53 53
    }
54 54
55 55
    #[inline]
56 -
    fn read_u128_varint(&mut self) -> u128 {
56 +
    fn read_varint_u128(&mut self) -> u128 {
57 57
        io::read_u128(InfallibleReadWrapper::new(self)).expect("infallible")
58 58
    }
59 59
60 60
    #[inline]
61 -
    fn read_i8_varint(&mut self) -> i8 {
62 -
        ZigZag::decode(self.read_u8_varint())
61 +
    fn read_varint_i8(&mut self) -> i8 {
62 +
        ZigZag::decode(self.read_varint_u8())
63 63
    }
64 64
65 65
    #[inline]
66 -
    fn read_i16_varint(&mut self) -> i16 {
67 -
        ZigZag::decode(self.read_u16_varint())
66 +
    fn read_varint_i16(&mut self) -> i16 {
67 +
        ZigZag::decode(self.read_varint_u16())
68 68
    }
69 69
70 70
    #[inline]
71 -
    fn read_i32_varint(&mut self) -> i32 {
72 -
        ZigZag::decode(self.read_u32_varint())
71 +
    fn read_varint_i32(&mut self) -> i32 {
72 +
        ZigZag::decode(self.read_varint_u32())
73 73
    }
74 74
75 75
    #[inline]
76 -
    fn read_i64_varint(&mut self) -> i64 {
77 -
        ZigZag::decode(self.read_u64_varint())
76 +
    fn read_varint_i64(&mut self) -> i64 {
77 +
        ZigZag::decode(self.read_varint_u64())
78 78
    }
79 79
80 80
    #[inline]
81 -
    fn read_i128_varint(&mut self) -> i128 {
82 -
        ZigZag::decode(self.read_u128_varint())
81 +
    fn read_varint_i128(&mut self) -> i128 {
82 +
        ZigZag::decode(self.read_varint_u128())
83 83
    }
84 84
}
85 85
impl<R: InfallibleRead + ?Sized> InfallibleReadVarInt for R {}

@@ -8,7 +8,10 @@
Loading
8 8
    discref::{Page, PageOps, ReadPage, PAGE_METADATA_SIZE},
9 9
    error::PRes,
10 10
    flush_checksum::{double_buffer_check, prepare_buffer_flush},
11 -
    io::{read_u64, write_u64, InfallibleRead, InfallibleReadFormat, InfallibleWrite, InfallibleWriteFormat},
11 +
    io::{
12 +
        read_u64, write_u64, InfallibleRead, InfallibleReadFormat, InfallibleReadVarInt, InfallibleWrite,
13 +
        InfallibleWriteFormat, InfallibleWriteVarInt,
14 +
    },
12 15
    persy::RecoverStatus,
13 16
};
14 17
use std::{
@@ -102,7 +105,7 @@
Loading
102 105
pub(crate) trait JournalEntry {
103 106
    fn get_type(&self) -> u8;
104 107
    fn write(&self, buffer: &mut dyn InfallibleWrite) -> PRes<()>;
105 -
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<u32>;
108 +
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<()>;
106 109
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus>;
107 110
}
108 111
@@ -114,13 +117,13 @@
Loading
114 117
    pos: u32,
115 118
}
116 119
117 -
fn recover_entry<T>(entry: &mut dyn JournalEntry, page: &mut ReadPage, found: &mut T, id: &JournalId) -> PRes<u32>
120 +
fn recover_entry<T>(entry: &mut dyn JournalEntry, page: &mut ReadPage, found: &mut T, id: &JournalId) -> PRes<()>
118 121
where
119 122
    T: FnMut(&dyn JournalEntry, &JournalId),
120 123
{
121 -
    let read_size = entry.read(page)?;
124 +
    entry.read(page)?;
122 125
    found(entry, id);
123 -
    Ok(read_size)
126 +
    Ok(())
124 127
}
125 128
126 129
impl Journal {
@@ -316,20 +319,21 @@
Loading
316 319
        let mut cur_page = jr.first_page;
317 320
        jr.last_page = jr.first_page;
318 321
        journal_pages.push(cur_page);
319 -
        let mut cur_cursor = JOURNAL_PAGE_CONTENT_OFFSET;
322 +
        let mut page = self.allocator.load_page(cur_page)?;
323 +
        page.seek(JOURNAL_PAGE_CONTENT_OFFSET)?;
320 324
        loop {
321 -
            let mut page = self.allocator.load_page(cur_page)?;
322 -
            page.seek(cur_cursor)?;
323 325
            let tp = page.read_u8();
324 326
            if tp == 0 {
327 +
                let last_pos = page.cursor_pos() as u32;
325 328
                page.seek(JOURNAL_PAGE_NEXT_OFFSET)?;
326 329
                cur_page = page.read_u64();
327 330
                if cur_page == 0 {
328 -
                    jr.last_pos = cur_cursor;
331 +
                    jr.last_pos = last_pos - 1;
329 332
                    break;
330 333
                }
331 334
                journal_pages.push(cur_page);
332 -
                cur_cursor = JOURNAL_PAGE_CONTENT_OFFSET;
335 +
                page = self.allocator.load_page(cur_page)?;
336 +
                page.seek(JOURNAL_PAGE_CONTENT_OFFSET)?;
333 337
                jr.last_page = cur_page;
334 338
            } else {
335 339
                let page_id = page.read_u64();
@@ -337,7 +341,7 @@
Loading
337 341
                let id = JournalId::new(page_id, pos);
338 342
                let ref_page = &mut page;
339 343
                let ref_found = &mut found;
340 -
                let read_size = match tp {
344 +
                match tp {
341 345
                    //The Start entry has no valid id, should not be recovered
342 346
                    1 => Start::new().read(&mut page)?,
343 347
                    2 => recover_entry(&mut InsertRecord::default(), ref_page, ref_found, &id)?,
@@ -355,7 +359,6 @@
Loading
355 359
                    14 => recover_entry(&mut Cleanup::default(), ref_page, ref_found, &id)?,
356 360
                    _ => panic!(" wrong log entry {} ", tp),
357 361
                };
358 -
                cur_cursor = read_size + cur_cursor + 13;
359 362
            }
360 363
        }
361 364
        jr.current = self.allocator.write_page(jr.last_page)?;
@@ -393,19 +396,19 @@
Loading
393 396
    }
394 397
395 398
    fn write(&self, buffer: &mut dyn InfallibleWrite) -> PRes<()> {
396 -
        buffer.write_u32(self.segment);
397 -
        buffer.write_u64(self.recref.page);
398 -
        buffer.write_u32(self.recref.pos);
399 -
        buffer.write_u16(self.version);
399 +
        buffer.write_varint_u32(self.segment);
400 +
        buffer.write_varint_u64(self.recref.page);
401 +
        buffer.write_varint_u32(self.recref.pos);
402 +
        buffer.write_varint_u16(self.version);
400 403
        Ok(())
401 404
    }
402 405
403 -
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<u32> {
404 -
        self.segment = buffer.read_u32();
405 -
        self.recref.page = buffer.read_u64();
406 -
        self.recref.pos = buffer.read_u32();
407 -
        self.version = buffer.read_u16();
408 -
        Ok(18)
406 +
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<()> {
407 +
        self.segment = buffer.read_varint_u32();
408 +
        self.recref.page = buffer.read_varint_u64();
409 +
        self.recref.pos = buffer.read_varint_u32();
410 +
        self.version = buffer.read_varint_u16();
411 +
        Ok(())
409 412
    }
410 413
411 414
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus> {
@@ -420,19 +423,19 @@
Loading
420 423
    }
421 424
422 425
    fn write(&self, buffer: &mut dyn InfallibleWrite) -> PRes<()> {
423 -
        buffer.write_u32(self.segment);
424 -
        buffer.write_u64(self.recref.page);
425 -
        buffer.write_u32(self.recref.pos);
426 -
        buffer.write_u16(self.version);
426 +
        buffer.write_varint_u32(self.segment);
427 +
        buffer.write_varint_u64(self.recref.page);
428 +
        buffer.write_varint_u32(self.recref.pos);
429 +
        buffer.write_varint_u16(self.version);
427 430
        Ok(())
428 431
    }
429 432
430 -
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<u32> {
431 -
        self.segment = buffer.read_u32();
432 -
        self.recref.page = buffer.read_u64();
433 -
        self.recref.pos = buffer.read_u32();
434 -
        self.version = buffer.read_u16();
435 -
        Ok(18)
433 +
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<()> {
434 +
        self.segment = buffer.read_varint_u32();
435 +
        self.recref.page = buffer.read_varint_u64();
436 +
        self.recref.pos = buffer.read_varint_u32();
437 +
        self.version = buffer.read_varint_u16();
438 +
        Ok(())
436 439
    }
437 440
438 441
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus> {
@@ -446,21 +449,21 @@
Loading
446 449
    }
447 450
448 451
    fn write(&self, buffer: &mut dyn InfallibleWrite) -> PRes<()> {
449 -
        buffer.write_u32(self.segment);
450 -
        buffer.write_u64(self.recref.page);
451 -
        buffer.write_u32(self.recref.pos);
452 -
        buffer.write_u64(self.record_page);
453 -
        buffer.write_u16(self.version);
452 +
        buffer.write_varint_u32(self.segment);
453 +
        buffer.write_varint_u64(self.recref.page);
454 +
        buffer.write_varint_u32(self.recref.pos);
455 +
        buffer.write_varint_u64(self.record_page);
456 +
        buffer.write_varint_u16(self.version);
454 457
        Ok(())
455 458
    }
456 459
457 -
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<u32> {
458 -
        self.segment = buffer.read_u32();
459 -
        self.recref.page = buffer.read_u64();
460 -
        self.recref.pos = buffer.read_u32();
461 -
        self.record_page = buffer.read_u64();
462 -
        self.version = buffer.read_u16();
463 -
        Ok(26)
460 +
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<()> {
461 +
        self.segment = buffer.read_varint_u32();
462 +
        self.recref.page = buffer.read_varint_u64();
463 +
        self.recref.pos = buffer.read_varint_u32();
464 +
        self.record_page = buffer.read_varint_u64();
465 +
        self.version = buffer.read_varint_u16();
466 +
        Ok(())
464 467
    }
465 468
466 469
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus> {
@@ -475,19 +478,19 @@
Loading
475 478
    }
476 479
477 480
    fn write(&self, buffer: &mut dyn InfallibleWrite) -> PRes<()> {
478 -
        buffer.write_u32(self.segment);
479 -
        buffer.write_u64(self.recref.page);
480 -
        buffer.write_u32(self.recref.pos);
481 -
        buffer.write_u64(self.record_page);
481 +
        buffer.write_varint_u32(self.segment);
482 +
        buffer.write_varint_u64(self.recref.page);
483 +
        buffer.write_varint_u32(self.recref.pos);
484 +
        buffer.write_varint_u64(self.record_page);
482 485
        Ok(())
483 486
    }
484 487
485 -
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<u32> {
486 -
        self.segment = buffer.read_u32();
487 -
        self.recref.page = buffer.read_u64();
488 -
        self.recref.pos = buffer.read_u32();
489 -
        self.record_page = buffer.read_u64();
490 -
        Ok(24)
488 +
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<()> {
489 +
        self.segment = buffer.read_varint_u32();
490 +
        self.recref.page = buffer.read_varint_u64();
491 +
        self.recref.pos = buffer.read_varint_u32();
492 +
        self.record_page = buffer.read_varint_u64();
493 +
        Ok(())
491 494
    }
492 495
493 496
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus> {
@@ -505,8 +508,8 @@
Loading
505 508
        Ok(())
506 509
    }
507 510
508 -
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<u32> {
509 -
        Ok(0)
511 +
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<()> {
512 +
        Ok(())
510 513
    }
511 514
512 515
    fn recover(&self, _: &mut Transaction) -> PRes<RecoverStatus> {
@@ -523,8 +526,8 @@
Loading
523 526
        Ok(())
524 527
    }
525 528
526 -
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<u32> {
527 -
        Ok(0)
529 +
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<()> {
530 +
        Ok(())
528 531
    }
529 532
530 533
    fn recover(&self, _: &mut Transaction) -> PRes<RecoverStatus> {
@@ -541,8 +544,8 @@
Loading
541 544
        Ok(())
542 545
    }
543 546
544 -
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<u32> {
545 -
        Ok(0)
547 +
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<()> {
548 +
        Ok(())
546 549
    }
547 550
548 551
    fn recover(&self, _: &mut Transaction) -> PRes<RecoverStatus> {
@@ -556,20 +559,20 @@
Loading
556 559
    }
557 560
558 561
    fn write(&self, write: &mut dyn InfallibleWrite) -> PRes<()> {
559 -
        write.write_u8(self.strategy.value());
562 +
        write.write_varint_u8(self.strategy.value());
560 563
        let len = self.meta_id.len();
561 -
        write.write_u16(len as u16);
564 +
        write.write_varint_u16(len as u16);
562 565
        write.write_all(&self.meta_id);
563 566
        Ok(())
564 567
    }
565 568
566 -
    fn read(&mut self, read: &mut dyn InfallibleRead) -> PRes<u32> {
567 -
        self.strategy = TxStrategy::from_value(read.read_u8());
568 -
        let len = read.read_u16();
569 +
    fn read(&mut self, read: &mut dyn InfallibleRead) -> PRes<()> {
570 +
        self.strategy = TxStrategy::from_value(read.read_varint_u8());
571 +
        let len = read.read_varint_u16();
569 572
        let mut slice: Vec<u8> = vec![0; len as usize];
570 573
        read.read_exact(&mut slice[0..len as usize]);
571 574
        self.meta_id = slice;
572 -
        Ok(u32::from(1 + 2 + len))
575 +
        Ok(())
573 576
    }
574 577
575 578
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus> {
@@ -587,8 +590,8 @@
Loading
587 590
        Ok(())
588 591
    }
589 592
590 -
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<u32> {
591 -
        Ok(0)
593 +
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<()> {
594 +
        Ok(())
592 595
    }
593 596
594 597
    fn recover(&self, _: &mut Transaction) -> PRes<RecoverStatus> {
@@ -605,8 +608,8 @@
Loading
605 608
        Ok(())
606 609
    }
607 610
608 -
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<u32> {
609 -
        Ok(0)
611 +
    fn read(&mut self, _: &mut dyn InfallibleRead) -> PRes<()> {
612 +
        Ok(())
610 613
    }
611 614
    fn recover(&self, _: &mut Transaction) -> PRes<RecoverStatus> {
612 615
        Ok(RecoverStatus::Rollback)
@@ -619,21 +622,21 @@
Loading
619 622
    }
620 623
621 624
    fn write(&self, buffer: &mut dyn InfallibleWrite) -> PRes<()> {
622 -
        buffer.write_u32(self.segment_id);
623 -
        buffer.write_u64(self.first_page);
624 -
        buffer.write_u16(self.name.len() as u16);
625 +
        buffer.write_varint_u32(self.segment_id);
626 +
        buffer.write_varint_u64(self.first_page);
627 +
        buffer.write_varint_u16(self.name.len() as u16);
625 628
        buffer.write_all(self.name.as_bytes());
626 629
        Ok(())
627 630
    }
628 631
629 -
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<u32> {
630 -
        self.segment_id = buffer.read_u32();
631 -
        self.first_page = buffer.read_u64();
632 -
        let string_size = buffer.read_u16();
632 +
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<()> {
633 +
        self.segment_id = buffer.read_varint_u32();
634 +
        self.first_page = buffer.read_varint_u64();
635 +
        let string_size = buffer.read_varint_u16();
633 636
        let mut slice: Vec<u8> = vec![0; string_size as usize];
634 637
        buffer.read_exact(&mut slice[0..string_size as usize]);
635 638
        self.name = str::from_utf8(&slice[0..string_size as usize])?.into();
636 -
        Ok(u32::from(4 + string_size + 2 + 8))
639 +
        Ok(())
637 640
    }
638 641
639 642
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus> {
@@ -648,19 +651,19 @@
Loading
648 651
    }
649 652
650 653
    fn write(&self, buffer: &mut dyn InfallibleWrite) -> PRes<()> {
651 -
        buffer.write_u32(self.segment_id);
652 -
        buffer.write_u16(self.name.len() as u16);
654 +
        buffer.write_varint_u32(self.segment_id);
655 +
        buffer.write_varint_u16(self.name.len() as u16);
653 656
        buffer.write_all(self.name.as_bytes());
654 657
        Ok(())
655 658
    }
656 659
657 -
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<u32> {
658 -
        self.segment_id = buffer.read_u32();
659 -
        let string_size = buffer.read_u16();
660 +
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<()> {
661 +
        self.segment_id = buffer.read_varint_u32();
662 +
        let string_size = buffer.read_varint_u16();
660 663
        let mut slice: Vec<u8> = vec![0; string_size as usize];
661 664
        buffer.read_exact(&mut slice[0..string_size as usize]);
662 665
        self.name = str::from_utf8(&slice[0..string_size as usize])?.into();
663 -
        Ok(u32::from(4 + string_size + 2))
666 +
        Ok(())
664 667
    }
665 668
666 669
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus> {
@@ -679,9 +682,9 @@
Loading
679 682
        Ok(())
680 683
    }
681 684
682 -
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<u32> {
685 +
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<()> {
683 686
        self.page = buffer.read_u64();
684 -
        Ok((8) as u32)
687 +
        Ok(())
685 688
    }
686 689
687 690
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus> {
@@ -696,17 +699,17 @@
Loading
696 699
    }
697 700
698 701
    fn write(&self, buffer: &mut dyn InfallibleWrite) -> PRes<()> {
699 -
        buffer.write_u32(self.segment);
700 -
        buffer.write_u64(self.page);
701 -
        buffer.write_u64(self.previous);
702 +
        buffer.write_varint_u32(self.segment);
703 +
        buffer.write_varint_u64(self.page);
704 +
        buffer.write_varint_u64(self.previous);
702 705
        Ok(())
703 706
    }
704 707
705 -
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<u32> {
706 -
        self.segment = buffer.read_u32();
707 -
        self.page = buffer.read_u64();
708 -
        self.previous = buffer.read_u64();
709 -
        Ok((4 + 8 + 8) as u32)
708 +
    fn read(&mut self, buffer: &mut dyn InfallibleRead) -> PRes<()> {
709 +
        self.segment = buffer.read_varint_u32();
710 +
        self.page = buffer.read_varint_u64();
711 +
        self.previous = buffer.read_varint_u64();
712 +
        Ok(())
710 713
    }
711 714
712 715
    fn recover(&self, tx: &mut Transaction) -> PRes<RecoverStatus> {

Learn more Showing 1 files with coverage changes found.

Changes in src/io.rs
+19
Loading file...
Files Coverage
src 0.01% 93.15%
tests 98.56%
Project Totals (29 files) 94.14%
Loading