CdbWriter

Struct CdbWriter 

Source
pub struct CdbWriter<W: Write + Seek, H: Hasher + Default = CdbHash> { /* private fields */ }

Implementations§

Source§

impl<H: Hasher + Default> CdbWriter<File, H>

Source

pub fn create(path: impl AsRef<Path>) -> Result<Self, Error>

Source§

impl<W: Write + Seek, H: Hasher + Default> CdbWriter<W, H>

Source

pub fn new(writer: W) -> Result<Self, Error>

Source

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 key
  • value - 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();
Source

pub fn finalize(&mut self) -> Result<(), Error>

Source

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>

Source

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 a Cursor<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");

Auto Trait Implementations§

§

impl<W, H> Freeze for CdbWriter<W, H>
where W: Freeze,

§

impl<W, H> RefUnwindSafe for CdbWriter<W, H>

§

impl<W, H> Send for CdbWriter<W, H>
where W: Send, H: Send,

§

impl<W, H> Sync for CdbWriter<W, H>
where W: Sync, H: Sync,

§

impl<W, H> Unpin for CdbWriter<W, H>
where W: Unpin, H: Unpin,

§

impl<W, H> UnwindSafe for CdbWriter<W, H>
where W: UnwindSafe, H: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.