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

Alternative to rocksdb: Added jammdb as another lightweight option for wallet storage #1004

Closed
wants to merge 56 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
1794e10
added jammdb impl
mighty840 Aug 4, 2023
f810b43
adjusted features
mighty840 Aug 4, 2023
70bcf7a
adjusted features
mighty840 Aug 4, 2023
ab775c9
adjusted features
mighty840 Aug 4, 2023
c4eba2d
adjusted features and added impl
mighty840 Aug 4, 2023
cc434a0
added from impl
mighty840 Aug 4, 2023
2ee4870
added deps and feature
mighty840 Aug 4, 2023
120aaa0
added feature
mighty840 Aug 4, 2023
a83a99c
updated lock file
mighty840 Aug 4, 2023
4938e4f
reverted example deps
mighty840 Aug 4, 2023
7624ac5
Merge branch 'develop' into develop
mighty840 Aug 11, 2023
340d434
sorted dependencies
mighty840 Aug 13, 2023
5cbb595
corrected year in license
mighty840 Aug 13, 2023
61b5cf3
fixed case (supposedly)
mighty840 Aug 13, 2023
44ca624
fixed case (supposedly)
mighty840 Aug 13, 2023
de7d6ec
improved impl
mighty840 Aug 13, 2023
975be80
feature flag fixed
mighty840 Aug 13, 2023
2290f76
feature flag fixed
mighty840 Aug 13, 2023
c8ae9a3
feature flag fixed
mighty840 Aug 13, 2023
2031f24
feature flag fixed
mighty840 Aug 13, 2023
f9f9b3c
deps sorted
mighty840 Aug 13, 2023
61dc915
deps sorted
mighty840 Aug 13, 2023
cc2bee5
reverted changes due to autofmt
mighty840 Aug 13, 2023
25bd57e
reverted changes due to autofmt
mighty840 Aug 13, 2023
a3d34ab
removed old impl
mighty840 Aug 13, 2023
793b83a
reverted cfg for rocksdb
mighty840 Aug 13, 2023
2213d85
ran cargo fmt
mighty840 Aug 14, 2023
888c8e8
Update sdk/Cargo.toml
mighty840 Aug 14, 2023
7d5ba45
improved with cfg_if
mighty840 Aug 22, 2023
24859cd
updated deps
mighty840 Aug 22, 2023
81ffe17
fixed spacing
mighty840 Aug 22, 2023
84dc59f
removed return
mighty840 Aug 23, 2023
ec42d30
Update sdk/src/wallet/storage/kind.rs
mighty840 Aug 24, 2023
2a698ca
Update sdk/src/wallet/storage/adapter/mod.rs
mighty840 Aug 24, 2023
245d85c
added cfg if
mighty840 Aug 24, 2023
25d844d
removed unused dep
mighty840 Aug 24, 2023
b16d955
added cfg if
mighty840 Aug 24, 2023
bd240ab
Update sdk/src/wallet/storage/kind.rs
mighty840 Aug 25, 2023
1434b9a
Update sdk/src/wallet/storage/constants.rs
mighty840 Aug 25, 2023
301c499
Update sdk/src/wallet/core/builder.rs
mighty840 Aug 25, 2023
789f6d7
Update sdk/src/wallet/storage/kind.rs
mighty840 Aug 25, 2023
c02dfb0
Update sdk/src/wallet/core/builder.rs
Aug 25, 2023
d63b9a2
Update sdk/src/wallet/storage/kind.rs
Aug 28, 2023
ca0067c
Merge branch '1.1' into develop
mighty840 Aug 28, 2023
aa3cf39
added missing semi-colon
mighty840 Aug 28, 2023
1e595cc
fixed cfg_if parentheses
mighty840 Aug 29, 2023
44e754e
fixed cfg_if parentheses
mighty840 Aug 29, 2023
80e56b0
updated lock file
mighty840 Aug 29, 2023
0cdb68c
cargo fmt
mighty840 Aug 29, 2023
42bfa5e
Merge branch '1.1' into develop
mighty840 Aug 30, 2023
4bec772
Merge branch '1.1' into develop
mighty840 Aug 30, 2023
8a35a25
Merge branch '1.1' into develop
mighty840 Aug 30, 2023
7e35a5f
removed semi-colons added by auto-fmt
mighty840 Aug 31, 2023
de01644
Merge branch '1.1' into develop
mighty840 Sep 12, 2023
95f46c7
Merge branch '1.1' into develop
mighty840 Sep 12, 2023
36253ab
Merge branch '1.1' into develop
mighty840 Oct 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bindings/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ ledger_nano = [ "iota-sdk/ledger_nano" ]
mqtt = [ "iota-sdk/mqtt" ]
participation = [ "iota-sdk/participation" ]
rocksdb = [ "iota-sdk/rocksdb" ]
jammdb = ["iota-sdk/jammdb"]
storage = [ "iota-sdk/storage" ]
stronghold = [ "iota-sdk/stronghold" ]
2 changes: 2 additions & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ reqwest = { version = "0.11.18", default-features = false, features = [
rocksdb = { version = "0.21.0", default-features = false, features = [
"lz4",
], optional = true }
jammdb = { version = "0.10.0", optional = true}
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
rumqttc = { version = "0.22.0", default-features = false, features = [
"websocket",
], optional = true }
Expand Down Expand Up @@ -146,6 +147,7 @@ mqtt = ["std", "regex", "rumqttc", "dep:once_cell"]
participation = ["storage"]
pow = ["std", "num_cpus", "iota-crypto/curl-p"]
rand = ["dep:rand"]
jammdb = ["dep:jammdb", "storage"]
rocksdb = ["dep:rocksdb", "storage"]
serde = [
"serde_repr",
Expand Down
8 changes: 5 additions & 3 deletions sdk/src/wallet/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use super::operations::storage::SaveLoadWallet;
#[cfg(feature = "events")]
use crate::wallet::events::EventEmitter;
#[cfg(all(feature = "storage", not(feature = "rocksdb")))]
#[cfg(all(feature = "storage", not(feature = "rocksdb"), not(feature = "jammdb")))]
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
use crate::wallet::storage::adapter::memory::Memory;
#[cfg(feature = "storage")]
use crate::wallet::{
Expand Down Expand Up @@ -134,14 +134,16 @@
}
}

#[cfg(all(feature = "rocksdb", feature = "storage"))]
#[cfg(all(not(feature = "jammdb"), feature = "rocksdb", feature = "storage"))]
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
let storage =
crate::wallet::storage::adapter::rocksdb::RocksdbStorageAdapter::new(storage_options.path.clone())?;
#[cfg(all(not(feature = "rocksdb"), feature = "storage"))]
#[cfg(all(not(feature = "rocksdb"), feature = "jammdb", feature = "storage"))]
let storage = crate::wallet::storage::adapter::jammdb::JammdbStorageAdapter::new(storage_options.path.clone())?;
#[cfg(all(not(feature = "rocksdb"), not(feature = "jammdb"), feature = "storage"))]
let storage = Memory::default();

#[cfg(feature = "storage")]
let mut storage_manager = StorageManager::new(storage, storage_options.encryption_key.clone()).await?;

Check failure on line 146 in sdk/src/wallet/core/builder.rs

View workflow job for this annotation

GitHub Actions / crate (ubuntu-latest)

cannot find value `storage` in this scope

Check failure on line 146 in sdk/src/wallet/core/builder.rs

View workflow job for this annotation

GitHub Actions / crate (windows-latest)

cannot find value `storage` in this scope

Check failure on line 146 in sdk/src/wallet/core/builder.rs

View workflow job for this annotation

GitHub Actions / crate (ubuntu-latest)

cannot find value `storage` in this scope

Check failure on line 146 in sdk/src/wallet/core/builder.rs

View workflow job for this annotation

GitHub Actions / Check Unused Dependencies

cannot find value `storage` in this scope

#[cfg(feature = "storage")]
let read_manager_builder = Self::load(&storage_manager).await?;
Expand Down
7 changes: 7 additions & 0 deletions sdk/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,10 @@ impl From<rocksdb::Error> for Error {
Self::Storage(error.to_string())
}
}

#[cfg(feature = "jammdb")]
impl From<jammdb::Error> for Error {
fn from(error: jammdb::Error) -> Self {
Self::Storage(error.to_string())
}
}
83 changes: 83 additions & 0 deletions sdk/src/wallet/storage/adapter/jammdb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use super::StorageAdapter;
use jammdb::{OpenOptions, DB};
use std::{
fmt::Debug,
path::{Path, PathBuf},
sync::Arc,
};
use tokio::sync::Mutex;

/// The storage id.
pub const STORAGE_ID: &str = "JammDB";

const BUCKET_NAME: &str = "storage";

impl Debug for JammdbStorageAdapter {
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "JammDbStorageAdapter")
}
}

/// Key value storage adapter.
pub struct JammdbStorageAdapter {
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
db: Arc<Mutex<DB>>,
}

impl JammdbStorageAdapter {
/// Initialises the storage adapter.
pub fn new(path: impl AsRef<Path>) -> crate::wallet::Result<Self> {
let mut db_path = PathBuf::from("./sdk-wallet.db");
let dir_path = path.as_ref().to_string_lossy().to_string();
let mut temp_path = PathBuf::from(dir_path);
if path.as_ref().is_dir() {
temp_path.push(db_path);
}
db_path = temp_path;
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
let db = OpenOptions::new().pagesize(4096).num_pages(32).open(db_path)?;
// create a default bucket
let tx = db.tx(true)?;
let bucket = tx.get_or_create_bucket(BUCKET_NAME)?;
bucket.put("INITIAL_KEY", "INIT_VALUE")?; // needs some initial value
tx.commit()?;
Ok(Self {
db: Arc::new(Mutex::new(db)),
})
}
}

#[async_trait::async_trait]
impl StorageAdapter for JammdbStorageAdapter {
type Error = crate::wallet::Error;

async fn get_bytes(&self, key: &str) -> Result<Option<Vec<u8>>, Self::Error> {
let db = self.db.lock().await;
let tx = db.tx(false)?;
let bucket = tx.get_bucket(BUCKET_NAME)?;
match bucket.get(key) {
Some(r) => Ok(Some(r.kv().value().into())),
None => Ok(None),
}
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
}

async fn set_bytes(&self, key: &str, record: &[u8]) -> Result<(), Self::Error> {
let db = self.db.lock().await;
let tx = db.tx(true)?;
let bucket = tx.get_bucket(BUCKET_NAME)?;
bucket.put(key, record)?;
tx.commit()?;
Ok(())
}

async fn delete(&self, key: &str) -> crate::wallet::Result<()> {
let db = self.db.lock().await;
let tx = db.tx(true)?;
let bucket = tx.get_bucket(BUCKET_NAME)?;

bucket.delete(key)?;
tx.commit()?;
Ok(())
}
}
7 changes: 7 additions & 0 deletions sdk/src/wallet/storage/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
pub mod memory;
/// RocksDB storage adapter.
#[cfg(feature = "rocksdb")]
#[cfg(not(feature = "jammdb"))]
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(docsrs, doc(cfg(feature = "rocksdb")))]
pub mod rocksdb;

/// JammDB storage adapter.
#[cfg(feature = "jammdb")]
#[cfg(not(feature = "rocksdb"))]
mighty840 marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(docsrs, doc(cfg(feature = "jammdb")))]
pub mod jammdb;

use async_trait::async_trait;

use crate::client::storage::StorageAdapter;
Expand Down
10 changes: 8 additions & 2 deletions sdk/src/wallet/storage/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
/// The default storage path.
pub const DEFAULT_STORAGE_PATH: &str = "./storage";

/// The default RocksDB storage path.
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "jammdb")]
pub(crate) const JAMMDB_FOLDERNAME: &str = "walletdb";

/// The default RocksDB storage path.
#[cfg(feature = "rocksdb")]
pub(crate) const ROCKSDB_FOLDERNAME: &str = "walletdb";

pub const fn default_storage_path() -> &'static str {

Check failure on line 15 in sdk/src/wallet/storage/constants.rs

View workflow job for this annotation

GitHub Actions / crate (ubuntu-latest)

mismatched types

Check failure on line 15 in sdk/src/wallet/storage/constants.rs

View workflow job for this annotation

GitHub Actions / crate (windows-latest)

mismatched types

Check failure on line 15 in sdk/src/wallet/storage/constants.rs

View workflow job for this annotation

GitHub Actions / crate (ubuntu-latest)

mismatched types

Check failure on line 15 in sdk/src/wallet/storage/constants.rs

View workflow job for this annotation

GitHub Actions / Check Unused Dependencies

mismatched types
#[cfg(feature = "rocksdb")]
#[cfg(all(not(feature = "jammdb"), feature = "rocksdb"))]
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
return ROCKSDB_FOLDERNAME;
#[cfg(not(feature = "rocksdb"))]
#[cfg(all(not(feature = "rocksdb"), feature = "jammdb"))]
return JAMMDB_FOLDERNAME;
#[cfg(all(not(feature = "rocksdb"), not(feature = "jammdb")))]
DEFAULT_STORAGE_PATH
}

Expand Down
11 changes: 10 additions & 1 deletion sdk/src/wallet/storage/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
pub enum StorageKind {
/// RocksDB storage.
#[cfg(feature = "rocksdb")]
#[cfg(not(feature = "jammdb"))]
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
Rocksdb,
/// JammDB storage
#[cfg(feature = "jammdb")]
#[cfg(not(feature = "rocksdb"))]
mighty840 marked this conversation as resolved.
Show resolved Hide resolved
Jammdb,
/// Storage backed by a Map in memory.
Memory,
/// Wasm storage.
Expand All @@ -18,12 +23,16 @@
}

impl Default for StorageKind {
fn default() -> Self {

Check failure on line 26 in sdk/src/wallet/storage/kind.rs

View workflow job for this annotation

GitHub Actions / crate (ubuntu-latest)

mismatched types

Check failure on line 26 in sdk/src/wallet/storage/kind.rs

View workflow job for this annotation

GitHub Actions / crate (windows-latest)

mismatched types

Check failure on line 26 in sdk/src/wallet/storage/kind.rs

View workflow job for this annotation

GitHub Actions / crate (ubuntu-latest)

mismatched types

Check failure on line 26 in sdk/src/wallet/storage/kind.rs

View workflow job for this annotation

GitHub Actions / Check Unused Dependencies

mismatched types
#[cfg(feature = "rocksdb")]
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(feature = "jammdb"))]
return Self::Rocksdb;
#[cfg(feature = "jammdb")]
#[cfg(not(feature = "rocksdb"))]
return Self::Jammdb;
#[cfg(target_family = "wasm")]
return Self::Wasm;
#[cfg(not(any(feature = "rocksdb", target_family = "wasm")))]
#[cfg(not(any(feature = "rocksdb", target_family = "wasm", feature = "jammdb")))]
Self::Memory
}
}
Loading