Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom storage backends #707

Merged
merged 19 commits into from
Oct 23, 2023
Merged

Add custom storage backends #707

merged 19 commits into from
Oct 23, 2023

Conversation

DouglasDwyer
Copy link
Contributor

I've been looking for a pure-Rust, embedded key value store that supports pluggable storage backends or a virtual file system interface (my use case involves storing data to the Javascript OPFS via WASM, which requires using Javascript file system APIs rather than std::fs). Unfortunately, there don't seem to be any available - but redb looks like a great project, and it was easy to add this feature to it. This PR abstracts the file system code with a StorageBackend trait, which the user can implement in order to customize how redb interacts with the file system.

Please let me know what you think, and if there are any other improvements that I should add!

@cberner
Copy link
Owner

cberner commented Oct 21, 2023

btw, if you run just in the root directory. It will run all the checks that the CI enforces

@DouglasDwyer
Copy link
Contributor Author

My apologies for the failed CI runs. They were a bit stricter than I expected, and I'm developing on Windows (also do not have just installed at present) so it's a bit difficult to check for issues on other targets lol
I appreciate you re-running them for me; sorry for any inconvenience.

@DouglasDwyer
Copy link
Contributor Author

Given the repeated issues, I switched to running the CI pipelines on my fork of redb. I managed to get all of them to pass, so the latest set of commits should hopefully pass in this repository too :)

src/db.rs Outdated Show resolved Hide resolved
src/db.rs Outdated Show resolved Hide resolved
src/db.rs Outdated Show resolved Hide resolved
Copy link
Owner

@cberner cberner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor change requests. Otherwise this lgtm :)

src/db.rs Outdated Show resolved Hide resolved
src/db.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
src/tree_store/page_store/memory_backend.rs Outdated Show resolved Hide resolved
src/tree_store/page_store/memory_backend.rs Outdated Show resolved Hide resolved
src/tree_store/page_store/file_backend/unix.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
src/tree_store/page_store/memory_backend.rs Outdated Show resolved Hide resolved
@cberner cberner merged commit c8d98b7 into cberner:master Oct 23, 2023
3 checks passed
@C1710
Copy link

C1710 commented Jul 13, 2024

Sorry for asking this maybe very simple question:
Does this Trait actually directly allow an OPFS-based implementation? After all, all types from web-sys are !Send and !Sync, which is incompatible with the requirements of StorageBackend. Am I missing something?

@DouglasDwyer
Copy link
Contributor Author

DouglasDwyer commented Jul 13, 2024

Does this Trait actually directly allow an OPFS-based implementation? After all, all types from web-sys are !Send and !Sync, which is incompatible with the requirements of StorageBackend. Am I missing something?

That's a good question. The approach I have in mind (although I haven't gotten a chance to implement this yet) would look as follows:

  • A background thread is started (using the wasm_thread crate) which opens a connection to the OPFS using web-sys types. The background thread has two channels (one for reading data and one for writing data) or something similar.
  • The main thread (or other thread) creates a Database with a custom StorageBackend. The StorageBackend ferries all read/write requests over the channels to the worker thread. It then waits for the results, blocking with the wasm_sync synchronization primitives.
  • The worker thread uses the OPFS API to perform the reads/writes, and sends the data back to the main thread over the channels.

This approach, while roundabout, works around the web's annoying restrictions; the StorageBackend would be usable from multiple threads and synchronous, consistent with the current API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants