finish 2.7

Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
Alex Chi
2024-01-28 14:08:08 +08:00
parent b96479384c
commit b4485f49c3
15 changed files with 165 additions and 36 deletions

View File

@@ -146,7 +146,7 @@ impl SsTable {
let raw_bloom_offset = file.read(len - 4, 4)?;
let bloom_offset = (&raw_bloom_offset[..]).get_u32() as u64;
let raw_bloom = file.read(bloom_offset, len - 4 - bloom_offset)?;
let bloom_filter = Bloom::decode(&raw_bloom);
let bloom_filter = Bloom::decode(&raw_bloom)?;
let raw_meta_offset = file.read(bloom_offset - 4, 4)?;
let block_meta_offset = (&raw_meta_offset[..]).get_u32() as u64;
let raw_meta = file.read(block_meta_offset, bloom_offset - 4 - block_meta_offset)?;

View File

@@ -1,6 +1,7 @@
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.
use bytes::{BufMut, Bytes, BytesMut};
use anyhow::{bail, Result};
use bytes::{Buf, BufMut, Bytes, BytesMut};
/// Implements a bloom filter
pub struct Bloom {
@@ -45,19 +46,26 @@ impl<T: AsMut<[u8]>> BitSliceMut for T {
impl Bloom {
/// Decode a bloom filter
pub fn decode(buf: &[u8]) -> Self {
let filter = &buf[..buf.len() - 1];
let k = buf[buf.len() - 1];
Self {
pub fn decode(buf: &[u8]) -> Result<Self> {
let checksum = (&buf[buf.len() - 4..buf.len()]).get_u32();
if checksum != crc32fast::hash(&buf[..buf.len() - 4]) {
bail!("checksum mismatched for bloom filters");
}
let filter = &buf[..buf.len() - 5];
let k = buf[buf.len() - 5];
Ok(Self {
filter: filter.to_vec().into(),
k,
}
})
}
/// Encode a bloom filter
pub fn encode(&self, buf: &mut Vec<u8>) {
let offset = buf.len();
buf.extend(&self.filter);
buf.put_u8(self.k);
let checksum = crc32fast::hash(&buf[offset..]);
buf.put_u32(checksum);
}
/// Get bloom filter bits per key from entries count and FPR

View File

@@ -1,6 +1,6 @@
use std::fs::{File, OpenOptions};
use std::hash::Hasher;
use std::io::{Read, Write};
use std::io::{BufWriter, Read, Write};
use std::path::Path;
use std::sync::Arc;
@@ -10,20 +10,20 @@ use crossbeam_skiplist::SkipMap;
use parking_lot::Mutex;
pub struct Wal {
file: Arc<Mutex<File>>,
file: Arc<Mutex<BufWriter<File>>>,
}
impl Wal {
pub fn create(path: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
file: Arc::new(Mutex::new(
file: Arc::new(Mutex::new(BufWriter::new(
OpenOptions::new()
.read(true)
.create_new(true)
.write(true)
.open(path)
.context("failed to create WAL")?,
)),
))),
})
}
@@ -56,7 +56,7 @@ impl Wal {
skiplist.insert(key, value);
}
Ok(Self {
file: Arc::new(Mutex::new(file)),
file: Arc::new(Mutex::new(BufWriter::new(file))),
})
}
@@ -80,8 +80,9 @@ impl Wal {
}
pub fn sync(&self) -> Result<()> {
let file = self.file.lock();
file.sync_all()?;
let mut file = self.file.lock();
file.flush()?;
file.get_mut().sync_all()?;
Ok(())
}
}