diff --git a/ourdb/src/backend.rs b/ourdb/src/backend.rs index 9b8ed29..15f1a3d 100644 --- a/ourdb/src/backend.rs +++ b/ourdb/src/backend.rs @@ -14,9 +14,7 @@ pub const HEADER_SIZE: usize = 12; impl OurDB { /// Selects and opens a database file for read/write operations pub(crate) fn db_file_select(&mut self, file_nr: u16) -> Result<(), Error> { - if file_nr > 65535 { - return Err(Error::InvalidOperation("File number needs to be < 65536".to_string())); - } + // No need to check if file_nr > 65535 as u16 can't exceed that value let path = self.path.join(format!("{}.db", file_nr)); @@ -53,6 +51,18 @@ impl OurDB { /// Gets the file number to use for the next write operation pub(crate) fn get_file_nr(&mut self) -> Result { + // For keysize 2, 3, or 4, we can only use file_nr 0 + if self.lookup.keysize() <= 4 { + let path = self.path.join("0.db"); + + if !path.exists() { + self.create_new_db_file(0)?; + } + + return Ok(0); + } + + // For keysize 6, we can use multiple files let path = self.path.join(format!("{}.db", self.last_used_file_nr)); if !path.exists() { @@ -254,7 +264,7 @@ impl OurDB { // Read and process records let mut buffer = vec![0u8; 1024]; // Read in chunks - let mut position = 0; + let mut _position = 0; while let Ok(bytes_read) = src_file.read(&mut buffer) { if bytes_read == 0 { @@ -265,7 +275,7 @@ impl OurDB { // This is a simplified version - in a real implementation, // you would need to handle records that span chunk boundaries - position += bytes_read; + _position += bytes_read; } // TODO: Implement proper record copying and position updating diff --git a/ourdb/src/lib.rs b/ourdb/src/lib.rs index b89463d..7ba9bd2 100644 --- a/ourdb/src/lib.rs +++ b/ourdb/src/lib.rs @@ -37,7 +37,11 @@ pub struct OurDBConfig { pub incremental_mode: bool, /// Maximum file size (default: 500MB) pub file_size: Option, - /// Lookup table key size + /// Lookup table key size (default: 4) + /// - 2: For databases with < 65,536 records (single file) + /// - 3: For databases with < 16,777,216 records (single file) + /// - 4: For databases with < 4,294,967,296 records (single file) + /// - 6: For large databases requiring multiple files (default) pub keysize: Option, } diff --git a/ourdb/src/location.rs b/ourdb/src/location.rs index 9284fab..83af1e5 100644 --- a/ourdb/src/location.rs +++ b/ourdb/src/location.rs @@ -27,10 +27,10 @@ impl Location { // Create padded bytes let mut padded = vec![0u8; keysize as usize]; - let start_idx = keysize as usize - bytes.len(); - if start_idx < 0 { + if bytes.len() > keysize as usize { return Err(Error::InvalidOperation("Input bytes exceed keysize".to_string())); } + let start_idx = keysize as usize - bytes.len(); for (i, &b) in bytes.iter().enumerate() { if i + start_idx < padded.len() { diff --git a/ourdb/src/lookup.rs b/ourdb/src/lookup.rs index da5c610..a636fb0 100644 --- a/ourdb/src/lookup.rs +++ b/ourdb/src/lookup.rs @@ -13,10 +13,10 @@ pub struct LookupConfig { /// Size of the lookup table pub size: u32, /// Size of each entry in bytes (2-6) - /// - 2: For databases with < 65,536 records - /// - 3: For databases with < 16,777,216 records - /// - 4: For databases with < 4,294,967,296 records - /// - 6: For large databases requiring multiple files + /// - 2: For databases with < 65,536 records (single file) + /// - 3: For databases with < 16,777,216 records (single file) + /// - 4: For databases with < 4,294,967,296 records (single file) + /// - 6: For large databases requiring multiple files pub keysize: u8, /// Path for disk-based lookup pub lookuppath: String, @@ -37,6 +37,11 @@ pub struct LookupTable { } impl LookupTable { + /// Returns the keysize of this lookup table + pub fn keysize(&self) -> u8 { + self.keysize + } + /// Creates a new lookup table with the given configuration pub fn new(config: LookupConfig) -> Result { // Verify keysize is valid