Skip to content

Commit

Permalink
Enforce max key and value sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
mikerhodes committed Apr 29, 2024
1 parent 47697c6 commit e6536a2
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum ToyKVError {
DataDirMissing,
FileError(std::io::Error),
BadWALState,
KeyTooLarge,
ValueTooLarge,
}

impl From<std::io::Error> for ToyKVError {
Expand Down Expand Up @@ -66,10 +68,18 @@ pub fn with_sync(d: &Path, sync: WALSync) -> Result<ToyKV, ToyKVError> {

/// How many writes to a WAL before we SSTable it.
const WAL_WRITE_THRESHOLD: u32 = 1000;
const MAX_KEY_SIZE: usize = 10_240; // 10kb
const MAX_VALUE_SIZE: usize = 102_400; // 100kb

impl ToyKV {
/// Set key k to v.
pub fn set(&mut self, k: Vec<u8>, v: Vec<u8>) -> Result<(), ToyKVError> {
if k.len() > MAX_KEY_SIZE {
return Err(ToyKVError::KeyTooLarge);
}
if v.len() > MAX_VALUE_SIZE {
return Err(ToyKVError::ValueTooLarge);
}
self.wal.write(&k, &v)?;
self.memtable.insert(k, v);
if self.wal.wal_writes >= WAL_WRITE_THRESHOLD {
Expand Down

0 comments on commit e6536a2

Please sign in to comment.