Skip to content

Commit

Permalink
Init nostr-sdk-redb crate
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 25, 2023
1 parent 539a9e0 commit 3eced0e
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 2 deletions.
62 changes: 61 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/nostr-sdk-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

use std::collections::HashSet;

use async_trait::async_trait;
pub use async_trait::async_trait;
pub use nostr;
use nostr::{Event, EventId, Filter, Timestamp, Url};

mod error;
Expand Down
18 changes: 18 additions & 0 deletions crates/nostr-sdk-redb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "nostr-sdk-redb"
version = "0.1.0"
edition = "2021"
description = "TODO"
authors = ["Yuki Kishimoto <[email protected]>"]
homepage.workspace = true
repository.workspace = true
license.workspace = true
readme = "README.md"
rust-version.workspace = true
keywords = ["nostr", "sdk", "db", "redb"]

[dependencies]
nostr-sdk-db = { version = "0.1", path = "../nostr-sdk-db" }
nostr-sdk-fbs = { version = "0.1", path = "../nostr-sdk-fbs" }
redb = "1.3"
thiserror = { workspace = true }
155 changes: 155 additions & 0 deletions crates/nostr-sdk-redb/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Distributed under the MIT software license

use std::collections::HashSet;
use std::path::Path;

use nostr_sdk_db::nostr::{Event, EventId, Filter, Timestamp, Url};
use nostr_sdk_db::{async_trait, Backend, DatabaseError, DatabaseOptions, NostrDatabase};
use nostr_sdk_fbs::FlatBufferBuilder;
use redb::{Database, TableDefinition};
use thiserror::Error;

const EVENTS: TableDefinition<&[u8], &[u8]> = TableDefinition::new("events");

/// Redb Database Error
#[derive(Debug, Error)]
pub enum Error {
/// Redb opening related errors
#[error(transparent)]
RedbOpen(#[from] redb::DatabaseError),
/// Redb transaction errors
#[error(transparent)]
RedbTx(#[from] redb::TransactionError),
/// Redb table errors
#[error(transparent)]
RedbTable(#[from] redb::TableError),
/// Redb storage errors
#[error(transparent)]
RedbStorage(#[from] redb::StorageError),
/// Redb commit errors
#[error(transparent)]
RedbCommit(#[from] redb::CommitError),
/// Redb errors
#[error(transparent)]
Redb(#[from] redb::Error),
}

impl From<Error> for DatabaseError {
fn from(e: Error) -> Self {
DatabaseError::backend(e)
}
}

/// Redb Database (LMDB-like)
#[derive(Debug)]
pub struct RedbDatabase {
db: Database,
}

impl RedbDatabase {
pub fn new<P>(path: P) -> Result<Self, Error>
where
P: AsRef<Path>,
{
Ok(Self {
db: Database::create(path)?,
})
}
}

#[async_trait]
impl NostrDatabase for RedbDatabase {
type Err = DatabaseError;

fn backend(&self) -> Backend {
Backend::Custom(String::from("redb"))
}

fn opts(&self) -> DatabaseOptions {
DatabaseOptions::default()
}

async fn save_event(&self, event: &Event) -> Result<bool, Self::Err> {
let write_txn = self.db.begin_write().map_err(DatabaseError::backend)?;
{
// Open table
let mut table = write_txn
.open_table(EVENTS)
.map_err(DatabaseError::backend)?;

// Serialize event
let mut fbb = FlatBufferBuilder::new();
let ser = nostr_sdk_fbs::serialize_event(&mut fbb, event);

// Insert
table
.insert(event.id.as_bytes(), ser)
.map_err(DatabaseError::backend)?;
}

// Save
write_txn.commit().map_err(DatabaseError::backend)?;

Ok(true)
}

async fn has_event_already_been_seen(&self, _event_id: EventId) -> Result<bool, Self::Err> {
todo!()
}

async fn event_id_seen(
&self,
_event_id: EventId,
_relay_url: Option<Url>,
) -> Result<(), Self::Err> {
todo!()
}

async fn event_ids_seen(
&self,
_event_ids: Vec<EventId>,
_relay_url: Option<Url>,
) -> Result<(), Self::Err> {
todo!()
}

async fn event_recently_seen_on_relays(
&self,
_event_id: EventId,
) -> Result<Option<HashSet<Url>>, Self::Err> {
todo!()
}

async fn event_by_id(&self, _event_id: EventId) -> Result<Event, Self::Err> {
/* let read_txn = self.db.begin_read().map_err(DatabaseError::backend)?;
let table = read_txn
.open_table(EVENTS)
.map_err(DatabaseError::backend)?;
let value = table
.get(b"my_key".as_slice())
.map_err(DatabaseError::backend)?
.unwrap()
.value(); */
todo!()
}

async fn query(&self, _filters: Vec<Filter>) -> Result<Vec<Event>, Self::Err> {
todo!()
}

async fn event_ids_by_filters(&self, _filters: Vec<Filter>) -> Result<Vec<EventId>, Self::Err> {
todo!()
}

async fn negentropy_items(
&self,
_filter: &Filter,
) -> Result<Vec<(EventId, Timestamp)>, Self::Err> {
todo!()
}

async fn wipe(&self) -> Result<(), Self::Err> {
todo!()
}
}

0 comments on commit 3eced0e

Please sign in to comment.