Skip to content

Commit

Permalink
Implement incrementing seq in wal
Browse files Browse the repository at this point in the history
  • Loading branch information
mikerhodes committed Apr 30, 2024
1 parent 41fceaf commit 8120b38
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
20 changes: 20 additions & 0 deletions examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ fn main() -> Result<(), ToyKVError> {
// assert_eq!(writes as u64, db.metrics.writes);
// assert_eq!(0, db.metrics.reads);

let now = Instant::now();
for n in 1..(writes + 1) {
let got = db.get(n.to_be_bytes().as_slice())?;
assert_eq!(
got.unwrap(),
n.to_le_bytes().as_slice(),
"Did not read back what we put in"
);
}
let elapsed_time = now.elapsed();
println!(
"Running read() {} times took {}ms ({}ms per read).",
writes,
elapsed_time.as_millis(),
((elapsed_time / writes).as_micros()) as f64 / 1000.0
);
db.shutdown();

// See how read time is affected if we open a new database
let mut db = toykv::open(tmp_dir.path())?;
let now = Instant::now();
for n in 1..(writes + 1) {
let got = db.get(n.to_be_bytes().as_slice())?;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum ToyKVError {
DataDirMissing,
FileError(std::io::Error),
BadWALState,
BadWALSeq { expected: u32, actual: u32 },
KeyTooLarge,
ValueTooLarge,
}
Expand Down
23 changes: 17 additions & 6 deletions src/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ Valid `op` values:
const WAL_MAGIC: u8 = b'w';
const OP_SET: u8 = 1u8;

// TODO this shouldn't exist when we implement seq in records
const DEFAULT_SEQ: u32 = 1;

pub(crate) struct WAL {
wal_path: PathBuf,
f: Option<File>,
sync: WALSync,
nextseq: u32,

/// Number of writes to the WAL since it created
pub(crate) wal_writes: u32,
Expand All @@ -56,6 +54,7 @@ pub(crate) fn new(d: &Path, sync: WALSync) -> WAL {
wal_path: d.join("db.wal"),
f: None,
sync,
nextseq: 0,
wal_writes: 0,
}
}
Expand Down Expand Up @@ -93,17 +92,26 @@ impl WAL {
let rec = WALRecord::read_one(&mut bytes)?;
match rec {
Some(wr) => {
assert_eq!(wr.seq, DEFAULT_SEQ, "Unexpected seq code");
if wr.seq != self.nextseq {
return Err(ToyKVError::BadWALSeq {
expected: self.nextseq,
actual: wr.seq,
});
}
assert_eq!(wr.op, OP_SET, "Unexpected op code");
memtable.insert(wr.key, wr.value);
self.nextseq = wr.seq + 1;
self.wal_writes += 1;
cnt += 1;
}
None => break, // assume we hit the end of the WAL file
};
}

println!("Replayed {} records from WAL.", cnt);
println!(
"Replayed {} records from WAL. nextseq={}.",
cnt, self.nextseq
);

self.f = Some(file);

Expand All @@ -116,7 +124,7 @@ impl WAL {
return Err(ToyKVError::BadWALState);
}

let seq = DEFAULT_SEQ; // TODO implement sequence numbers for records
let seq = self.nextseq;

let file = self.f.as_mut().unwrap();
WALRecord::write_one(file, seq, key, value)?;
Expand All @@ -126,6 +134,8 @@ impl WAL {
file.sync_all()?;
}

self.nextseq += 1;

Ok(())
}

Expand Down Expand Up @@ -154,6 +164,7 @@ impl WAL {
.create(true)
.open(self.wal_path.as_path())?;
self.f = Some(file);
self.nextseq = 0;
self.wal_writes = 0;
Ok(())
}
Expand Down

0 comments on commit 8120b38

Please sign in to comment.