added transaction for get_or_create_single_key

Niko PLP 1 year ago
parent d1bf0a85d4
commit cb547ee0de
  1. 2
      README.md
  2. 4
      p2p-broker/src/broker_store/account.rs
  3. 4
      p2p-broker/src/broker_store/invitation.rs
  4. 2
      p2p-broker/src/broker_store/overlay.rs
  5. 4
      p2p-broker/src/broker_store/peer.rs
  6. 37
      p2p-broker/src/broker_store/wallet.rs
  7. 2
      p2p-repo/src/kcv_store.rs
  8. 10
      stores-lmdb/src/kcv_store.rs

@ -28,7 +28,7 @@ Read our [getting started guide](https://docs.nextgraph.org/en/getting-started/)
## For contributors ## For contributors
- [Install Rust](https://www.rust-lang.org/tools/install) minimum required 1.64.0 - [Install Rust](https://www.rust-lang.org/tools/install) minimum required MSRV 1.64.0
- [Install Nodejs](https://nodejs.org/en/download/) - [Install Nodejs](https://nodejs.org/en/download/)
until this [PR](https://github.com/rustwasm/wasm-pack/pull/1271) is accepted, will have to install wasm-pack this way: until this [PR](https://github.com/rustwasm/wasm-pack/pull/1271) is accepted, will have to install wasm-pack this way:

@ -116,7 +116,7 @@ impl<'a> Account<'a> {
let info_ser = to_vec(info)?; let info_ser = to_vec(info)?;
self.store.write_transaction(&|tx| { self.store.write_transaction(&mut |tx| {
if tx if tx
.has_property_value( .has_property_value(
Self::PREFIX, Self::PREFIX,
@ -218,7 +218,7 @@ impl<'a> Account<'a> {
} }
pub fn del(&self) -> Result<(), StorageError> { pub fn del(&self) -> Result<(), StorageError> {
self.store.write_transaction(&|tx| { self.store.write_transaction(&mut |tx| {
if let Ok(clients) = tx.get_all(Self::PREFIX, &to_vec(&self.id)?, Some(Self::CLIENT)) { if let Ok(clients) = tx.get_all(Self::PREFIX, &to_vec(&self.id)?, Some(Self::CLIENT)) {
for client in clients { for client in clients {
tx.del_all(Self::PREFIX_CLIENT, &client, &Self::ALL_CLIENT_PROPERTIES)?; tx.del_all(Self::PREFIX_CLIENT, &client, &Self::ALL_CLIENT_PROPERTIES)?;

@ -74,7 +74,7 @@ impl<'a> Invitation<'a> {
return Err(StorageError::BackendError); return Err(StorageError::BackendError);
} }
let mut value = to_vec(&(code_type, expiry, memo.clone()))?; let mut value = to_vec(&(code_type, expiry, memo.clone()))?;
store.write_transaction(&|tx| { store.write_transaction(&mut |tx| {
tx.put(Self::PREFIX, &to_vec(code)?, Some(Self::TYPE), &value)?; tx.put(Self::PREFIX, &to_vec(code)?, Some(Self::TYPE), &value)?;
Ok(()) Ok(())
})?; })?;
@ -168,7 +168,7 @@ impl<'a> Invitation<'a> {
} }
pub fn del(&self) -> Result<(), StorageError> { pub fn del(&self) -> Result<(), StorageError> {
self.store.write_transaction(&|tx| { self.store.write_transaction(&mut |tx| {
tx.del_all(Self::PREFIX, &to_vec(&self.id)?, &Self::ALL_PROPERTIES)?; tx.del_all(Self::PREFIX, &to_vec(&self.id)?, &Self::ALL_PROPERTIES)?;
Ok(()) Ok(())
}) })

@ -73,7 +73,7 @@ impl<'a> Overlay<'a> {
if acc.exists() { if acc.exists() {
return Err(StorageError::BackendError); return Err(StorageError::BackendError);
} }
store.write_transaction(&|tx| { store.write_transaction(&mut |tx| {
tx.put( tx.put(
Self::PREFIX, Self::PREFIX,
&to_vec(&id)?, &to_vec(&id)?,

@ -71,7 +71,7 @@ impl<'a> Peer<'a> {
if acc.exists() { if acc.exists() {
return Err(StorageError::BackendError); return Err(StorageError::BackendError);
} }
store.write_transaction(&|tx| { store.write_transaction(&mut |tx| {
tx.put( tx.put(
Self::PREFIX, Self::PREFIX,
&to_vec(&id)?, &to_vec(&id)?,
@ -128,7 +128,7 @@ impl<'a> Peer<'a> {
if current_advert.version() >= advert.version() { if current_advert.version() >= advert.version() {
return Ok(()); return Ok(());
} }
self.store.write_transaction(&|tx| { self.store.write_transaction(&mut |tx| {
tx.replace( tx.replace(
Self::PREFIX, Self::PREFIX,
&to_vec(&self.id)?, &to_vec(&self.id)?,

@ -11,6 +11,7 @@
use p2p_net::types::*; use p2p_net::types::*;
use p2p_repo::kcv_store::KCVStore; use p2p_repo::kcv_store::KCVStore;
use p2p_repo::kcv_store::WriteTransaction;
use p2p_repo::log::*; use p2p_repo::log::*;
use p2p_repo::store::*; use p2p_repo::store::*;
use p2p_repo::types::*; use p2p_repo::types::*;
@ -44,18 +45,17 @@ impl<'a> Wallet<'a> {
prefix: u8, prefix: u8,
key: &Vec<u8>, key: &Vec<u8>,
) -> Result<SymKey, StorageError> { ) -> Result<SymKey, StorageError> {
// FIXME. this get or create is not using a transaction, because calls will be made from the broker, that is behind a mutex. let mut result: Option<SymKey> = None;
// if this was to change, we should make the get and put inside one transaction. self.store.write_transaction(&mut |tx| {
let get = self let got = tx.get(prefix, key, Some(Self::SUFFIX_FOR_EXIST_CHECK));
.store match got {
.get(prefix, key, Some(Self::SUFFIX_FOR_EXIST_CHECK));
match get {
Err(e) => { Err(e) => {
if e == StorageError::NotFound { if e == StorageError::NotFound {
self.create_single_key(prefix, key) let res = Self::create_single_key(tx, prefix, key)?;
result = Some(res);
} else { } else {
log_debug!("Error while creating single key {}", e); log_debug!("Error while creating single key {}", e);
Err(StorageError::BackendError) return Err(StorageError::BackendError);
} }
} }
Ok(p) => { Ok(p) => {
@ -63,9 +63,12 @@ impl<'a> Wallet<'a> {
.as_slice() .as_slice()
.try_into() .try_into()
.map_err(|_| StorageError::BackendError)?; .map_err(|_| StorageError::BackendError)?;
Ok(k) result = Some(k);
} }
} }
Ok(())
})?;
Ok(result.unwrap())
} }
pub fn get_or_create_user_key(&self, user: &UserId) -> Result<SymKey, StorageError> { pub fn get_or_create_user_key(&self, user: &UserId) -> Result<SymKey, StorageError> {
@ -76,10 +79,14 @@ impl<'a> Wallet<'a> {
self.get_or_create_single_key(Self::PREFIX_USER, &to_vec(overlay)?) self.get_or_create_single_key(Self::PREFIX_USER, &to_vec(overlay)?)
} }
pub fn create_single_key(&self, prefix: u8, key: &Vec<u8>) -> Result<SymKey, StorageError> { pub fn create_single_key(
tx: &mut dyn WriteTransaction,
prefix: u8,
key: &Vec<u8>,
) -> Result<SymKey, StorageError> {
let symkey = SymKey::random(); let symkey = SymKey::random();
let vec = symkey.slice().to_vec(); let vec = symkey.slice().to_vec();
self.store.put(prefix, key, Some(Self::SYM_KEY), vec)?; tx.put(prefix, key, Some(Self::SYM_KEY), &vec)?;
Ok(symkey) Ok(symkey)
} }
pub fn exists_single_key(&self, prefix: u8, key: &Vec<u8>) -> bool { pub fn exists_single_key(&self, prefix: u8, key: &Vec<u8>) -> bool {
@ -92,7 +99,13 @@ impl<'a> Wallet<'a> {
self.exists_single_key(Self::PREFIX, &Self::KEY_ACCOUNTS.to_vec()) self.exists_single_key(Self::PREFIX, &Self::KEY_ACCOUNTS.to_vec())
} }
pub fn create_accounts_key(&self) -> Result<SymKey, StorageError> { pub fn create_accounts_key(&self) -> Result<SymKey, StorageError> {
self.create_single_key(Self::PREFIX, &Self::KEY_ACCOUNTS.to_vec()) let mut result: Option<SymKey> = None;
self.store.write_transaction(&mut |tx| {
let res = Self::create_single_key(tx, Self::PREFIX, &Self::KEY_ACCOUNTS.to_vec())?;
result = Some(res);
Ok(())
})?;
Ok(result.unwrap())
} }
pub fn get_or_create_peers_key(&self) -> Result<SymKey, StorageError> { pub fn get_or_create_peers_key(&self) -> Result<SymKey, StorageError> {
self.get_or_create_single_key(Self::PREFIX, &Self::KEY_PEERS.to_vec()) self.get_or_create_single_key(Self::PREFIX, &Self::KEY_PEERS.to_vec())

@ -81,7 +81,7 @@ pub trait ReadTransaction {
pub trait KCVStore: ReadTransaction { pub trait KCVStore: ReadTransaction {
fn write_transaction( fn write_transaction(
&self, &self,
method: &dyn Fn(&mut dyn WriteTransaction) -> Result<(), StorageError>, method: &mut dyn FnMut(&mut dyn WriteTransaction) -> Result<(), StorageError>,
) -> Result<(), StorageError>; ) -> Result<(), StorageError>;
/// Save a property value to the store. /// Save a property value to the store.

@ -364,7 +364,7 @@ impl ReadTransaction for LmdbKCVStore {
impl KCVStore for LmdbKCVStore { impl KCVStore for LmdbKCVStore {
fn write_transaction( fn write_transaction(
&self, &self,
method: &dyn Fn(&mut dyn WriteTransaction) -> Result<(), StorageError>, method: &mut dyn FnMut(&mut dyn WriteTransaction) -> Result<(), StorageError>,
) -> Result<(), StorageError> { ) -> Result<(), StorageError> {
let lock = self.environment.read().unwrap(); let lock = self.environment.read().unwrap();
let writer = lock.write().unwrap(); let writer = lock.write().unwrap();
@ -389,7 +389,7 @@ impl KCVStore for LmdbKCVStore {
suffix: Option<u8>, suffix: Option<u8>,
value: Vec<u8>, value: Vec<u8>,
) -> Result<(), StorageError> { ) -> Result<(), StorageError> {
self.write_transaction(&|tx| tx.put(prefix, key, suffix, &value)) self.write_transaction(&mut |tx| tx.put(prefix, key, suffix, &value))
} }
/// Replace the property of a key (single value) to the store. /// Replace the property of a key (single value) to the store.
@ -400,12 +400,12 @@ impl KCVStore for LmdbKCVStore {
suffix: Option<u8>, suffix: Option<u8>,
value: Vec<u8>, value: Vec<u8>,
) -> Result<(), StorageError> { ) -> Result<(), StorageError> {
self.write_transaction(&|tx| tx.replace(prefix, key, suffix, &value)) self.write_transaction(&mut |tx| tx.replace(prefix, key, suffix, &value))
} }
/// Delete a property from the store. /// Delete a property from the store.
fn del(&self, prefix: u8, key: &Vec<u8>, suffix: Option<u8>) -> Result<(), StorageError> { fn del(&self, prefix: u8, key: &Vec<u8>, suffix: Option<u8>) -> Result<(), StorageError> {
self.write_transaction(&|tx| tx.del(prefix, key, suffix)) self.write_transaction(&mut |tx| tx.del(prefix, key, suffix))
} }
/// Delete a specific value for a property from the store. /// Delete a specific value for a property from the store.
@ -416,7 +416,7 @@ impl KCVStore for LmdbKCVStore {
suffix: Option<u8>, suffix: Option<u8>,
value: Vec<u8>, value: Vec<u8>,
) -> Result<(), StorageError> { ) -> Result<(), StorageError> {
self.write_transaction(&|tx| tx.del_property_value(prefix, key, suffix, &value)) self.write_transaction(&mut |tx| tx.del_property_value(prefix, key, suffix, &value))
} }
/// Delete all properties of a key from the store. /// Delete all properties of a key from the store.

Loading…
Cancel
Save