Git Product home page Git Product logo

Comments (3)

ordian avatar ordian commented on July 17, 2024

I think the main problem is that kvdb exposes sync API like fn get(&self, col: Option<u32>, key: &[u8]) -> io::Result<Option<DBValue>>, whereas IndexedDB provides an async API with js-style callbacks.

From https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStoreSync:

Important: The synchronous version of the IndexedDB API was originally intended for use only with Web Workers, and was eventually removed from the spec because its need was questionable. It may however be reintroduced in the future if there is enough demand from web developers.

from parity-common.

tomaka avatar tomaka commented on July 17, 2024

Oh, I see.

Maybe a better approach is simply to keep the database in memory (like kvdb-memory), and save a serialized version in the local storage from time to time and on shutdown.

The use case is a light client whose database is probably very small. I imagine that the light client will somehow warp-sync in the future, and storing the database is mostly an optimization.

from parity-common.

ordian avatar ordian commented on July 17, 2024

While trying to make this work, I've encountered a bunch of issues:

Some problems of localStorage:

  • The API allows you to use only JS string (UTF-16) as keys and values
  • It usually has a limit of 10MB (https://arty.name/localstorage.html)
  • localStorage API is not available in WebWorkers, meaning it's not possible to save a serialized version in a background thread.

IndexDB API is not ready yet

  • one can access low-level API via web_sys, but it's really ugly
  • Hopefully this will be resolved by rustwasm/gloo#68

I'm not sure what is the expected size of of the substrate light client db, but we could implement it with localStorage first and migrate to IndexDB later (I imagine having an in-memory hashmap in both cases, reading the whole db into the memory on startup and writing async in case of indexdb).

wasm-bindgen limitations

  • it doesn't (and probably won't) support lifetimes in returned values (rustwasm/wasm-bindgen#423) (we have iter and iter_from_prefix methods on KeyValueDB)
  • only supports c-style enums (we have a rusty enum DBOp in kvdb)
  • [minor] wasm-bindgen provides a proc macro to generate all the js shims, which means it can't be used as an optional dependency on kvdb due to rust-lang/rust#29642, unless I'm missing something?

This means that kvdb KeyValueDB interface would need some breaking changes. Alternatively, we could create another (async?) interface just for the browser use-case.

EDIT: the wasm-bindgen limitations are only relevant if we want to expose the KeyValueDB interface to JS, but looks like we don't.

from parity-common.

Related Issues (20)

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.