Skip to content

Commit

Permalink
redb: add wasm32 support
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Dec 13, 2024
1 parent 499f523 commit 495cbb2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 6 additions & 2 deletions crates/nostr-redb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![allow(clippy::mutable_key_type)]

use std::collections::HashSet;
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;

use nostr_database::prelude::*;
Expand All @@ -31,6 +32,7 @@ pub struct NostrRedb {
impl NostrRedb {
/// Persistent database
#[inline]
#[cfg(not(target_arch = "wasm32"))]
pub fn persistent<P>(path: P) -> Result<Self, DatabaseError>
where
P: AsRef<Path>,
Expand All @@ -55,7 +57,8 @@ impl NostrRedb {
}
}

#[async_trait]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl NostrDatabase for NostrRedb {
#[inline]
fn backend(&self) -> Backend {
Expand All @@ -73,7 +76,8 @@ impl NostrDatabase for NostrRedb {
}
}

#[async_trait]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl NostrEventsDatabase for NostrRedb {
#[inline]
async fn save_event(&self, event: &Event) -> Result<SaveEventStatus, DatabaseError> {
Expand Down
7 changes: 6 additions & 1 deletion crates/nostr-redb/src/store/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
// Distributed under the MIT software license

use std::collections::BTreeSet;
#[cfg(not(target_arch = "wasm32"))]
use std::fs;
use std::iter;
use std::ops::Bound;
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;
use std::sync::Arc;
use std::{fs, iter};

use nostr::prelude::*;
use nostr_database::flatbuffers::FlatBufferDecodeBorrowed;
Expand Down Expand Up @@ -58,6 +61,7 @@ impl Db {
Ok(Self { env })
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn persistent<P>(path: P) -> Result<Self, Error>
where
P: AsRef<Path>,
Expand All @@ -72,6 +76,7 @@ impl Db {
Self::new(env)
}

// TODO: add support to in-memory with limited capacity?
pub(crate) fn in_memory() -> Result<Self, Error> {
let backend = InMemoryBackend::new();
let env = Arc::new(Database::builder().create_with_backend(backend)?);
Expand Down
2 changes: 2 additions & 0 deletions crates/nostr-redb/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Distributed under the MIT software license

use std::collections::BTreeSet;
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;
use std::sync::Arc;

Expand All @@ -30,6 +31,7 @@ pub struct Store {
}

impl Store {
#[cfg(not(target_arch = "wasm32"))]
pub fn persistent<P>(path: P) -> Result<Self, Error>
where
P: AsRef<Path>,
Expand Down

0 comments on commit 495cbb2

Please sign in to comment.