GithubHelp home page GithubHelp logo

cryptozenci / atomicstore Goto Github PK

View Code? Open in Web Editor NEW

This project forked from espressosystems/atomicstore

0.0 0.0 0.0 2.16 MB

Home Page: https://atomicstore.docs.espressosys.com/

License: GNU General Public License v3.0

Rust 98.58% Makefile 1.42%

atomicstore's Introduction

AtomicStore: synchronized persistence library

Provides a per-entity coordinated persistence index, AtomicStore, that tracks multiple resource persistence handlers (which use a VersionSyncHandle to coordinate) and records a entity-wide atomic recovery point each time the global state is committed.

This insures that recovery after a failure will always reflect a consistent logical point in time for the entire entity.

Usage

Each logical component with a persistable state must initialize an instance of AtomicStore, and a store for each element of its state that can be updated independently. There are two fields used to define the domain of the logical component: a storage_path: &Path, and a component_tag: &str. Note that the first field contains an assumption of storage in a file system; this may change in the future.

At the time of logical component initialization, a temporary AtomicStoreLoader must be used to load the prior state indexes, or clear them if restoring the initial global state. This must then be used to initialize each associated stateful element. Once all elements are initialized, the global AtomicStore instance can be initialized, and should be kept in scope until the logical component terminates.

Each element that is persisted should specify its stateful representation in terms of one or more log types. The atomic_store crate currently provides three types of log: AppendLog, FixedAppendLog, and RollingLog, but it will work with custom logs as well.

AppendLog provides an iterable append log, with random access support. The entire history can be loaded with an iterator, or a specific index can be loaded. FixedAppendLog is a more efficient version of the same concept where the serialization of the type being stored is always a consistent size. RollingLog only keeps a fixed number of the persisted element, and is suitable for snapshots or transient fields.

Each time the state of a element has meaningfully changed, it can persist this change with its log representation, using log.store_resource(value);, and when the element's changes are ready for inclusion in the global state, it can syncronize it to the logical compenent state using log.commit_version();. The logical component state can then update the persisted state with atomic_store.commit_version();, and this will guarantee an atomically consistent persisted state.

If all stateful data can be accessed in the same place, this can be simplified with the following pattern:

struct ComponentPersistence {
    atomic_store: AtomicStore,
    type_x_array_history: AppendLog<BincodeLoadStore<TypeX>>,
    type_y_snapshots: RollingLog<BincodeLoadStore<TypeY>>,
}

impl ComponentPersistence {
    pub fn new(store_path: &Path, key_tag: &str) -> Result<ComponentPersistence, PersistenceError> {
        let mut loader = AtomicStoreLoader::load(&transform_path(store_path), key_tag)?;
        let type_x_tag = format!("{}_type_x", key_tag);
        let type_y_tag = format!("{}_type_y", key_tag);
        let type_x_array_history = AppendLog::load(&mut loader, Default::default(), &type_x_tag, 1024)?;
        let type_y_snapshots = RollingLog::load(&mut loader, Default::default(), &type_y_tag, 1024)?;
        let atomic_store = AtomicStore::open(loader)?;
        Ok(ComponentPersistence {
            atomic_store,
            type_x_array_history,
            type_y_snapshots,
        })
    }
    pub fn store_x(&mut self, x_element: &TypeX) {
        self.type_x_array_history.store_resource(x_element).unwrap();
    }

    pub fn store_y(&mut self, y_element: &TypeY) {
        self.type_y_snapshots.store_resource(y_element).unwrap();
    }

    pub fn fn commit(&mut self) {
        self.type_x_array_history.commit_version().unwrap();
        self.type_y_snapshots.commit_version().unwrap();
        self.atomic_store.commit_version().unwrap();
    }

    pub fn x_iter(&self) -> Iter<BincodeLoadStore<TypeX>> {
        self.type_x_array_history.iter()
    }

    pub fn last_y(&self) -> Option<TypeY> {
        self.type_y_snapshots.load_latest().ok()
    }
}

atomicstore's People

Contributors

nyospe avatar tri-joe avatar ancient123 avatar d8zy avatar dependabot[bot] avatar victorkoenders avatar clu8 avatar pictographer avatar jbearer avatar sveitser avatar qm3ster avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.