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
use std::cell::RefCell; use std::io; use std::io::{Cursor, SeekFrom}; use std::io::prelude::*; use byteorder::{LE, ReadBytesExt}; use super::*; pub trait CDBAccess where Self::Output : AsRef<[u8]> { type Output; fn read_pair(&self, pos: u64) -> io::Result<(usize, usize)> { let data = self.get_data(pos, PAIR_SIZE)?; let mut cur = Cursor::new(data); Ok((cur.read_u32::<LE>()? as usize, cur.read_u32::<LE>()? as usize)) } fn get_data(&self, pos: u64, len: usize) -> io::Result<Self::Output>; fn len(&self) -> u64; } impl <'c> CDBAccess for &'c[u8] { type Output = &'c [u8]; fn get_data(&self, pos: u64, len: usize) -> io::Result<Self::Output> { let pos = pos as usize; Ok(&self[pos..pos+len]) } fn len(&self) -> u64 { (*self).len() as u64 } } struct CDBFile<T> { file: T, pos: u64, size: u64, } impl <T: Read + Seek> CDBFile<T> { fn new(mut file: T) -> io::Result<Self> { let size = file.seek(SeekFrom::End(0))?; Ok(CDBFile{file, pos: size, size}) } fn read(&mut self, pos: u64, out: &mut [u8]) -> io::Result<()> { if pos != self.pos { self.file.seek(SeekFrom::Start(pos as u64))?; self.pos = pos; } match self.file.read_exact(out) { Ok(_) => { self.pos += out.len() as u64; Ok(()) } Err(err) => Err(err), } } } pub struct CDBFileAccess<T> ( RefCell<CDBFile<T>> ); impl <T: Read + Seek> CDBFileAccess<T> { pub fn new(file: T) -> io::Result<Self> { Ok(CDBFileAccess(RefCell::new(CDBFile::new(file)?))) } } impl <T: Read + Seek> CDBAccess for CDBFileAccess<T> { type Output = Vec<u8>; fn get_data(&self, pos: u64, len: usize) -> io::Result<Self::Output> { let mut out = Vec::with_capacity(len); out.resize(len, 0); let mut file = self.0.borrow_mut(); file.read(pos, &mut out).map(|_| out) } fn len(&self) -> u64 { self.0.borrow().size } } pub struct CDBReader<A> { pub access: A, tables: [PosLen; ENTRIES], } pub struct FileIter<'c, A: 'c> { pub cdb: &'c CDBReader<A>, pos: usize, } pub struct LookupIter<'c, 'k, A: 'c> { pub cdb: &'c CDBReader<A>, table: &'c PosLen, key: &'k [u8], khash: CDBHash, start_pos: usize, nlookups: usize, done: bool, } impl<'c, A: CDBAccess> Iterator for FileIter<'c, A> { type Item = io::Result<(A::Output, A::Output)>; fn next(&mut self) -> Option<Self::Item> { if self.pos < self.cdb.tables[0].pos { match self.cdb.get_data(self.pos) { Ok((k, v, newpos)) => { self.pos = newpos; Some(Ok((k, v))) } Err(e) => { self.pos = self.cdb.tables[0].pos; Some(Err(e)) } } } else { None } } } impl<'c, 'k, A: CDBAccess> LookupIter<'c, 'k, A> { fn new(cdb: &'c CDBReader<A>, key: &'k [u8]) -> Self { let khash = CDBHash::new(key); let table = &cdb.tables[khash.table()]; let start_pos = if table.len != 0 { khash.slot(table.len) } else { 0 }; LookupIter{cdb, table, key, khash, start_pos, nlookups: 0, done: false} } } impl<'c, 'k, A: CDBAccess> Iterator for LookupIter<'c, 'k, A> { type Item = io::Result<A::Output>; fn next(&mut self) -> Option<Self::Item> { if self.done { return None; } if self.table.len == 0 { self.done = true; return None; } loop { let tableidx = (self.start_pos + self.nlookups) % self.table.len; let pos = self.table.pos + tableidx*PAIR_SIZE; if tableidx == self.start_pos && self.nlookups != 0 { self.done = true; return None; } self.nlookups += 1; let (hash, ptr) = match self.cdb.access.read_pair(pos as u64) { Ok(v) => v, Err(e) => { self.done = true; return Some(Err(e)) } }; if ptr == 0 { self.done = true; return None; } if hash != self.khash.0 as usize { continue; } let (k, v, _) = match self.cdb.get_data(ptr as usize) { Ok(v) => v, Err(e) => { self.done = true; return Some(Err(e)) } }; if k.as_ref() == self.key { return Some(Ok(v)); } } } } impl<'c, A: CDBAccess> IntoIterator for &'c CDBReader<A> { type IntoIter = FileIter<'c, A>; type Item = <FileIter<'c, A> as Iterator>::Item; fn into_iter(self) -> Self::IntoIter { self.iter() } } type KeyValueNext<O> = (O, O, usize); impl<A: CDBAccess> CDBReader<A> { pub fn new(access: A) -> io::Result<CDBReader<A>> { let mut tables: [PosLen; ENTRIES] = [PosLen{pos: 0, len: 0}; ENTRIES]; for (x, table) in tables.iter_mut().enumerate() { let (pos, len) = access.read_pair(x as u64*PAIR_SIZE as u64)?; *table = PosLen{pos, len}; if !table.valid(access.len()) { return Err(io::Error::new(io::ErrorKind::InvalidData, "")); } } Ok(CDBReader{access, tables}) } fn get_data(&self, pos: usize) -> io::Result<KeyValueNext<A::Output>> { let (klen, vlen) = self.access.read_pair(pos as u64)?; let keystart = pos + PAIR_SIZE; let keyend = keystart + klen; let valend = keyend + vlen; Ok((self.access.get_data(keystart as u64, klen)?, self.access.get_data(keyend as u64, vlen)?, valend)) } pub fn iter(&self) -> FileIter<A> { FileIter{cdb: self, pos: ENTRIES*PAIR_SIZE} } pub fn lookup<'c, 'k>(&'c self, key: &'k [u8]) -> LookupIter<'c, 'k, A> { LookupIter::new(self, key) } pub fn get<'c, 'k>(&'c self, key: &'k [u8]) -> Option<io::Result<A::Output>> { self.lookup(key).nth(0) } } impl <B: Read + Seek> CDBReader<CDBFileAccess<B>> { pub fn from_file(file: B) -> io::Result<Self> { CDBReader::new(CDBFileAccess::new(file)?) } }