pub struct CdbWriter<W: Write + Seek, H: Hasher + Default = CdbHash> { /* private fields */ }Implementations§
Source§impl<W: Write + Seek, H: Hasher + Default> CdbWriter<W, H>
impl<W: Write + Seek, H: Hasher + Default> CdbWriter<W, H>
pub fn new(writer: W) -> Result<Self, Error>
Sourcepub fn put(&mut self, key: &[u8], value: &[u8]) -> Result<(), Error>
pub fn put(&mut self, key: &[u8], value: &[u8]) -> Result<(), Error>
Inserts a key-value pair into the CDB database.
§Arguments
key- A byte slice representing the keyvalue- A byte slice representing the value
§Behavior
- Duplicate Keys: This method allows inserting duplicate keys. If the same key is inserted
multiple times, all values will be stored. However,
Cdb::get()will only return the first matching value it encounters during the hash table probe. Use iteration to retrieve all values for duplicate keys. - Empty Keys/Values: Both empty keys (
b"") and empty values are valid and supported.
§Errors
Returns Error::WriterFinalized if called after finalize().
Returns Error::Io if an I/O error occurs during writing.
§Examples
use cdb64::{CdbWriter, CdbHash};
use std::io::Cursor;
let mut writer = CdbWriter::<_, CdbHash>::new(Cursor::new(Vec::new())).unwrap();
writer.put(b"key1", b"value1").unwrap();
writer.put(b"key1", b"value2").unwrap(); // Duplicate key is allowed
writer.put(b"", b"empty_key_value").unwrap(); // Empty key is allowed
writer.finalize().unwrap();pub fn finalize(&mut self) -> Result<(), Error>
Sourcepub fn into_inner(self) -> Result<W, Error>
pub fn into_inner(self) -> Result<W, Error>
Consumes the CdbWriter and returns the underlying writer.
This is useful for retrieving the written data, for example, when using Cursor<Vec<u8>>.
The writer is flushed before being returned.
§Errors
Returns Error::WriterNotFinalized if finalize() has not been called yet.
You must call finalize() before into_inner() to ensure the database is complete.
§Examples
use cdb64::{CdbWriter, CdbHash};
use std::io::Cursor;
let mut writer = CdbWriter::<_, CdbHash>::new(Cursor::new(Vec::new())).unwrap();
writer.put(b"key", b"value").unwrap();
writer.finalize().unwrap();
let cursor = writer.into_inner().unwrap();
let data = cursor.into_inner();
assert!(data.len() > 0);Source§impl<H: Hasher + Default> CdbWriter<File, H>
impl<H: Hasher + Default> CdbWriter<File, H>
Sourcepub fn freeze(self, path_to_reopen: &Path) -> Result<Cdb<File, H>, Error>
pub fn freeze(self, path_to_reopen: &Path) -> Result<Cdb<File, H>, Error>
Freezes the writer by finalizing it, flushing to disk, and reopening it as a Cdb reader.
This method consumes the writer and provides a convenient way to transition from writing to reading without manually managing files.
§Arguments
path_to_reopen- The path to reopen the file as a reader. This should be the same path originally used to create the writer.
§Errors
Returns an error if finalization fails or if the file cannot be reopened.
§Comparison with finalize() + into_inner()
- Use
freeze()when you want to immediately read from the same file after writing. - Use
finalize()+into_inner()when you need access to the underlying writer (e.g., to extract data from aCursor<Vec<u8>>).
§Examples
use cdb64::{CdbWriter, CdbHash};
use std::path::Path;
let path = Path::new("my.cdb");
let mut writer = CdbWriter::<_, CdbHash>::create(path).unwrap();
writer.put(b"key", b"value").unwrap();
// Freeze consumes the writer and returns a reader
let cdb = writer.freeze(path).unwrap();
assert_eq!(cdb.get(b"key").unwrap().unwrap(), b"value");