Crate syncless

Crate syncless 

Source
Expand description

syncless: ordered, atomic storage without durability guarantees.

Many times you don’t want to pay the cost of continuous fsyncs, and are ok with losing the latest updates if an OS crash/power outage were to happen and the user is unlucky - but it’s not acceptable to corrupt older data. Think of cases like “browser bookmarks” or “history”: synchronous requirements are overkill for these (and stressful on the entire system).

Once syncless has opened a file (except for hardware errors or bugs) writes are atomic and consistently ordered: if you see a write you will see all of it, and all prior writes.

Writes are not isolated (a single reader/writer is assumed) and not durable: data may remain buffered in memory and be lost on crash or power failure. However, crash recovery will never expose torn writes or corrupt earlier data.

§Guarantees

  • Atomic visibility of individual writes
  • Ordered visibility of writes
  • Crash-safe recovery of previously visible data

§Non-guarantees

  • Durability (recent writes may be lost)
  • Isolation (single writer assumed)
  • Multi-process coordination

§Example: atomically storing a JSON file

This saves a JSON blob (perhaps your program’s config?) using syncless so it either gets the old or new one, never a corrupted version. In practice you would probably keep the Store object around, as reloading it can be expensive if it has many changes.

use syncless::{open, Store, Writable, Error};

pub fn save_config(
    store: &mut Store<syncless::Writable>,
    json: &str,
) -> Result<(), Error> {
    store.write(0, json.as_bytes())
}

pub fn load_config(
    store: &mut Store<syncless::ReadOnly>
) -> Result<String, Error> {
    let mut buf = vec![0u8; store.size() as usize];

    store.read(0, &mut buf)?;
    let s = std::str::from_utf8(&buf)
        .map_err(|e| Error::CorruptRecord)?;
    Ok(s.to_owned())
}

Structs§

ReadOnly
Phantom data to make a Readonly store
Store
Store comes in two flavors: ReadOnly and Writable.
Writable
Phantom data to make a Writable store

Enums§

Error
Errors from our functions.
WriteOpenMode
How to open the Syncless store file:

Functions§

open
Opens an existing syncless store for reading and writing.
open_readonly
Opens an existing syncless store readonly.