-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!() | ||
} | ||
} |