oxedyne/fe2o3/fe2o3_o3db_sync/src/file/floc.rs
3.2 KiB, 9 runs
created by r1870400018:793, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::prelude::*; |
| 2 | |
| 3 | use oxedyne_fe2o3_core::{ |
| 4 | byte::{ |
| 5 | FromBytes, |
| 6 | ToBytes, |
| 7 | }, |
| 8 | mem::Extract, |
| 9 | }; |
| 10 | use oxedyne_fe2o3_jdat::{ |
| 11 | tup3dat, |
| 12 | try_extract_dat_as, |
| 13 | try_extract_tup3dat, |
| 14 | daticle::Dat, |
| 15 | }; |
| 16 | use oxedyne_fe2o3_iop_hash::csum::Checksummer; |
| 17 | |
| 18 | use std::io; |
| 19 | |
| 20 | pub type FileNum = u32; |
| 21 | |
| 22 | #[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)] |
| 23 | pub struct DataLocation { |
| 24 | pub start: u64, |
| 25 | pub len: u64, |
| 26 | } |
| 27 | |
| 28 | #[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)] |
| 29 | pub struct FileLocation { |
| 30 | pub fnum: FileNum, |
| 31 | pub start: u64, |
| 32 | pub klen: u64, // for deleting kv pair |
| 33 | pub vlen: u64, // for reading value |
| 34 | } |
| 35 | |
| 36 | impl FileLocation { |
| 37 | |
| 38 | pub fn file_number(&self) -> FileNum { self.fnum } |
| 39 | |
| 40 | pub fn keyval(&self) -> DataLocation { |
| 41 | DataLocation { |
| 42 | start: self.start, |
| 43 | len: self.klen + self.vlen, |
| 44 | } |
| 45 | } |
| 46 | pub fn val(&self) -> DataLocation { |
| 47 | DataLocation { |
| 48 | start: self.start + self.klen, |
| 49 | len: self.vlen, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | } |
| 54 | |
| 55 | #[derive(Clone, Debug, Default)] |
| 56 | pub struct StoredFileLocation { |
| 57 | pub floc: FileLocation, |
| 58 | pub buf: Vec<u8>, // encoding including checksum |
| 59 | } |
| 60 | |
| 61 | impl StoredFileLocation { |
| 62 | |
| 63 | /// The encoding process is built into the creation of a new `FileLocation`, to avoid |
| 64 | /// extra work calculating the encoded size when fbots keep track of index file sizes. |
| 65 | pub fn new< |
| 66 | C: Checksummer, |
| 67 | >( |
| 68 | fnum: FileNum, |
| 69 | start: u64, |
| 70 | klen: u64, |
| 71 | vlen: u64, |
| 72 | csummer: C, |
| 73 | ) |
| 74 | -> Outcome<Self> |
| 75 | { |
| 76 | let mut buf = Vec::new(); |
| 77 | let list = tup3dat![ |
| 78 | Dat::u64dat(start), |
| 79 | Dat::u64dat(klen), |
| 80 | Dat::u64dat(vlen), |
| 81 | ]; |
| 82 | buf = res!(list.to_bytes(buf)); |
| 83 | (buf, _) = res!(csummer.append(buf)); |
| 84 | |
| 85 | Ok(Self { |
| 86 | floc: FileLocation { |
| 87 | fnum, |
| 88 | start, |
| 89 | klen, |
| 90 | vlen, |
| 91 | }, |
| 92 | buf, |
| 93 | }) |
| 94 | } |
| 95 | |
| 96 | pub fn ref_file_location(&self) -> &FileLocation { &self.floc } |
| 97 | pub fn own_file_location(self) -> FileLocation { self.floc } |
| 98 | |
| 99 | /// Read `FileLocation` bytes, calculating but not reading the checksum. |
| 100 | pub fn read_bytes< |
| 101 | C: Checksummer, |
| 102 | R: io::Read, |
| 103 | >( |
| 104 | r: &mut R, |
| 105 | fnum: FileNum, |
| 106 | csummer: C, |
| 107 | ) |
| 108 | -> Outcome<(Self, usize, Vec<u8>)> |
| 109 | { |
| 110 | let buf = res!(Dat::load_bytes(r)); |
| 111 | let (dat, n) = res!(Dat::from_bytes(&buf)); |
| 112 | let (buf, csum) = res!(csummer.append(buf)); |
| 113 | let mut v = try_extract_tup3dat!(dat); |
| 114 | let start = try_extract_dat_as!(v[0].extract(), u64, U8, U16, U32, U64); |
| 115 | let klen = try_extract_dat_as!(v[1].extract(), u64, U8, U16, U32, U64); |
| 116 | let vlen = try_extract_dat_as!(v[2].extract(), u64, U8, U16, U32, U64); |
| 117 | |
| 118 | Ok(( |
| 119 | Self { |
| 120 | floc: FileLocation { |
| 121 | fnum, |
| 122 | start, |
| 123 | klen, |
| 124 | vlen, |
| 125 | }, |
| 126 | buf, |
| 127 | }, |
| 128 | n, |
| 129 | csum, |
| 130 | )) |
| 131 | } |
| 132 | } |