Skip to content

Commit

Permalink
Implement RedbKey and RedbValue for CompleteEntry and PartialEntryData
Browse files Browse the repository at this point in the history
also implement scanning from disk to db.
  • Loading branch information
rklaehn committed Jan 18, 2024
1 parent 4d715bd commit 55604c8
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 175 deletions.
5 changes: 3 additions & 2 deletions 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 iroh-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ data-encoding = { version = "2.3.3", optional = true }
hex = "0.4.3"
multibase = { version = "0.9.1", optional = true }
postcard = { version = "1", default-features = false, features = ["alloc", "use-std", "experimental-derive"], optional = true }
redb = { version = "1.5.0", optional = true }
serde = { version = "1", features = ["derive"] }
serde-error = "0.1.2"
thiserror = "1"
Expand All @@ -32,6 +33,6 @@ serde_json = "1.0.107"
serde_test = "1.0.176"

[features]
default = ["hash", "base32"]
default = ["hash", "base32", "redb"]
hash = ["bao-tree", "multibase", "data-encoding", "postcard"]
base32 = ["data-encoding"]
54 changes: 53 additions & 1 deletion iroh-base/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! The blake3 hash used in Iroh.
use std::fmt;
use std::str::FromStr;
use std::{borrow::Borrow, fmt};

use bao_tree::blake3;
use postcard::experimental::max_size::MaxSize;
use redb::RedbKey;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use crate::base32::{parse_array_hex_or_base32, HexOrBase32ParseError};
Expand All @@ -13,6 +14,45 @@ use crate::base32::{parse_array_hex_or_base32, HexOrBase32ParseError};
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
pub struct Hash(blake3::Hash);

#[cfg(feature = "redb")]
impl redb::RedbValue for Hash {
type SelfType<'a> = Self;

type AsBytes<'a> = &'a [u8; 32];

fn fixed_width() -> Option<usize> {
Some(32)
}

fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
where
Self: 'a,
{
let contents: &'a [u8; 32] = data.try_into().unwrap();
(*contents).into()
}

fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
where
Self: 'a,
Self: 'b,
{
value.as_bytes()
}

fn type_name() -> redb::TypeName {
redb::TypeName::new(std::any::type_name::<Self>())
}
}

impl RedbKey for Hash {
fn compare(data1: &[u8], data2: &[u8]) -> std::cmp::Ordering {
let hash1 = Self::from_bytes(data1.try_into().unwrap());
let hash2 = Self::from_bytes(data2.try_into().unwrap());
hash1.cmp(&hash2)
}
}

impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Hash").field(&DD(self.to_hex())).finish()
Expand Down Expand Up @@ -62,6 +102,18 @@ impl AsRef<[u8]> for Hash {
}
}

impl Borrow<[u8]> for Hash {
fn borrow(&self) -> &[u8] {
self.0.as_bytes()
}
}

impl Borrow<[u8; 32]> for Hash {
fn borrow(&self) -> &[u8; 32] {
self.0.as_bytes()
}
}

impl From<Hash> for blake3::Hash {
fn from(value: Hash) -> Self {
value.0
Expand Down
Loading

0 comments on commit 55604c8

Please sign in to comment.