From 5f3d842f298c7bf8d55326db768dc9f372b92e91 Mon Sep 17 00:00:00 2001
From: librelois <elois@ifee.fr>
Date: Sun, 2 Feb 2020 19:21:18 +0100
Subject: [PATCH] [ref] db-tools: prepare sled mig: rename kv_db & make
 replayable read tx

rename mod kv_db -> kv_db_old
---
 lib/core/core/src/commands/mod.rs             |  4 +--
 lib/core/core/src/lib.rs                      |  4 +--
 .../bc-db-reader/src/indexes/certs.rs         |  4 +--
 .../bc-db-reader/src/indexes/identities.rs    |  2 +-
 lib/modules-lib/bc-db-reader/src/lib.rs       |  4 +--
 lib/modules-lib/bc-db-reader/src/traits.rs    |  6 ++--
 .../blockchain/bc-db-writer/src/lib.rs        |  2 +-
 lib/modules/blockchain/blockchain/src/dubp.rs |  2 +-
 .../blockchain/src/fork/fork_algo.rs          |  4 +--
 .../blockchain/src/requests/received.rs       |  6 ++--
 .../blockchain/src/sync/apply/mod.rs          |  2 +-
 lib/modules/gva/src/schema.rs                 |  4 +--
 .../gva/src/schema/inputs/block_interval.rs   |  6 ++--
 lib/modules/gva/src/schema/inputs/paging.rs   | 15 ++++++----
 lib/modules/gva/src/schema/queries/blocks.rs  |  4 +--
 lib/tools/dbs-tools/src/kv_db.rs              | 29 -------------------
 lib/tools/dbs-tools/src/kv_db_old.rs          | 15 ++++++++++
 .../src/{kv_db => kv_db_old}/file.rs          |  6 ++--
 lib/tools/dbs-tools/src/lib.rs                |  3 +-
 19 files changed, 56 insertions(+), 66 deletions(-)
 delete mode 100644 lib/tools/dbs-tools/src/kv_db.rs
 create mode 100644 lib/tools/dbs-tools/src/kv_db_old.rs
 rename lib/tools/dbs-tools/src/{kv_db => kv_db_old}/file.rs (98%)

diff --git a/lib/core/core/src/commands/mod.rs b/lib/core/core/src/commands/mod.rs
index 44676b16..d6949a82 100644
--- a/lib/core/core/src/commands/mod.rs
+++ b/lib/core/core/src/commands/mod.rs
@@ -26,7 +26,7 @@ use crate::errors::DursCoreError;
 use crate::DursCore;
 pub use dbex::*;
 use durs_conf::DuRsConf;
-use durs_dbs_tools::kv_db::KvFileDbHandler;
+use durs_dbs_tools::kv_db_old::KvFileDbHandler;
 pub use durs_network::cli::sync::SyncOpt;
 pub use keys::KeysOpt;
 use log::Level;
@@ -94,7 +94,7 @@ pub enum DursCommandEnum<T: ExecutableModuleCommand> {
 impl<T: ExecutableModuleCommand> DursCommand<T> {
     fn open_bc_db(&self, profile_path: &PathBuf) -> Result<KvFileDbHandler, DursCoreError> {
         let bc_db_path = durs_conf::get_blockchain_db_path(profile_path.clone());
-        durs_dbs_tools::kv_db::KvFileDbHandler::open_db(
+        durs_dbs_tools::kv_db_old::KvFileDbHandler::open_db(
             bc_db_path.as_path(),
             &durs_bc_db_reader::bc_db_schema(),
         )
diff --git a/lib/core/core/src/lib.rs b/lib/core/core/src/lib.rs
index 0ce8e792..d582a88c 100644
--- a/lib/core/core/src/lib.rs
+++ b/lib/core/core/src/lib.rs
@@ -155,7 +155,7 @@ impl DursCore<DuRsConf> {
 
     /// Execute core command
     pub fn execute_core_command<PlugFunc>(
-        bc_db: durs_dbs_tools::kv_db::KvFileDbHandler,
+        bc_db: durs_dbs_tools::kv_db_old::KvFileDbHandler,
         core_command: DursCoreCommand,
         durs_core_opts: DursCoreOptions,
         mut plug_modules: PlugFunc,
@@ -278,7 +278,7 @@ impl DursCore<DuRsConf> {
     /// Start durs server
     pub fn start(
         mut self,
-        bc_db: durs_dbs_tools::kv_db::KvFileDbHandler,
+        bc_db: durs_dbs_tools::kv_db_old::KvFileDbHandler,
     ) -> Result<(), DursCoreError> {
         if self.network_modules_count == 0 {
             fatal_error!(
diff --git a/lib/modules-lib/bc-db-reader/src/indexes/certs.rs b/lib/modules-lib/bc-db-reader/src/indexes/certs.rs
index 69ebc41d..237604c8 100644
--- a/lib/modules-lib/bc-db-reader/src/indexes/certs.rs
+++ b/lib/modules-lib/bc-db-reader/src/indexes/certs.rs
@@ -24,7 +24,7 @@ use std::collections::HashMap;
 /// Find certifications that emitted in indicated blocks expiring
 pub fn find_expire_certs<DB: BcDbInReadTx>(
     db: &DB,
-    blocks_expiring: Vec<BlockNumber>,
+    blocks_expiring: &[BlockNumber],
 ) -> Result<HashMap<(WotId, WotId), BlockNumber>, DbError> {
     let mut all_expire_certs = HashMap::new();
     for expire_block_id in blocks_expiring {
@@ -36,7 +36,7 @@ pub fn find_expire_certs<DB: BcDbInReadTx>(
             if let Some(value) = entry_result?.1 {
                 if let DbValue::U64(cert) = value {
                     let (source, target) = cert_from_u64(cert);
-                    all_expire_certs.insert((source, target), expire_block_id);
+                    all_expire_certs.insert((source, target), *expire_block_id);
                 } else {
                     return Err(DbError::DBCorrupted);
                 }
diff --git a/lib/modules-lib/bc-db-reader/src/indexes/identities.rs b/lib/modules-lib/bc-db-reader/src/indexes/identities.rs
index 45f93100..3a8ef7e0 100644
--- a/lib/modules-lib/bc-db-reader/src/indexes/identities.rs
+++ b/lib/modules-lib/bc-db-reader/src/indexes/identities.rs
@@ -237,7 +237,7 @@ mod test {
     use dubp_common_doc::Blockstamp;
     use dup_crypto_tests_tools::mocks::pubkey;
     use durs_common_tests_tools::collections::slice_same_elems;
-    use durs_dbs_tools::kv_db::KvFileDbHandler;
+    use durs_dbs_tools::kv_db_old::KvFileDbHandler;
 
     fn gen_mock_dal_idty(pubkey: PubKey, created_block_id: BlockNumber) -> IdentityDb {
         IdentityDb {
diff --git a/lib/modules-lib/bc-db-reader/src/lib.rs b/lib/modules-lib/bc-db-reader/src/lib.rs
index 7a0f0d27..7b63de38 100644
--- a/lib/modules-lib/bc-db-reader/src/lib.rs
+++ b/lib/modules-lib/bc-db-reader/src/lib.rs
@@ -37,7 +37,7 @@ pub mod paging;
 pub mod tools;
 pub mod traits;
 
-pub use durs_dbs_tools::kv_db::{
+pub use durs_dbs_tools::kv_db_old::{
     from_db_value, KvFileDbRead as DbReadable, KvFileDbReader as Reader,
     KvFileDbRoHandler as BcDbRo, KvFileDbSchema, KvFileDbStoreType, KvFileDbValue as DbValue,
     Readable as DbReader,
@@ -111,7 +111,7 @@ impl<'r, 'db: 'r, DB> durs_common_tools::traits::NotMock for BcDbWithReaderStruc
 pub mod tests {
 
     use super::*;
-    use durs_dbs_tools::kv_db::KvFileDbHandler;
+    use durs_dbs_tools::kv_db_old::KvFileDbHandler;
     use tempfile::tempdir;
 
     #[inline]
diff --git a/lib/modules-lib/bc-db-reader/src/traits.rs b/lib/modules-lib/bc-db-reader/src/traits.rs
index 06e4ecc7..6c70fa1c 100644
--- a/lib/modules-lib/bc-db-reader/src/traits.rs
+++ b/lib/modules-lib/bc-db-reader/src/traits.rs
@@ -34,7 +34,7 @@ where
     fn r<D, F>(&self, f: F) -> Result<D, DbError>
     where
         DB: DbReadable,
-        F: FnOnce(&BcDbWithReaderStruct<DB>) -> Result<D, DbError>;
+        F: Fn(&BcDbWithReaderStruct<DB>) -> Result<D, DbError>;
 }
 
 impl<DB> BcDbRead<DB> for DB
@@ -44,7 +44,7 @@ where
     fn r<D, F>(&self, f: F) -> Result<D, DbError>
     where
         DB: DbReadable,
-        F: FnOnce(&BcDbWithReaderStruct<DB>) -> Result<D, DbError>,
+        F: Fn(&BcDbWithReaderStruct<DB>) -> Result<D, DbError>,
     {
         self.read(|r| f(&BcDbWithReaderStruct { db: self, r }))
     }
@@ -61,7 +61,7 @@ pub trait BcDbWithReader {
 #[cfg(feature = "mock")]
 impl<'a> BcDbWithReader for MockBcDbInReadTx {
     type DB = crate::BcDbRo;
-    type R = durs_dbs_tools::kv_db::MockKvFileDbReader;
+    type R = durs_dbs_tools::kv_db_old::MockKvFileDbReader;
 
     fn db(&self) -> &Self::DB {
         unreachable!()
diff --git a/lib/modules/blockchain/bc-db-writer/src/lib.rs b/lib/modules/blockchain/bc-db-writer/src/lib.rs
index 853291e6..b48b1570 100644
--- a/lib/modules/blockchain/bc-db-writer/src/lib.rs
+++ b/lib/modules/blockchain/bc-db-writer/src/lib.rs
@@ -37,7 +37,7 @@ pub mod current_metadata;
 pub mod indexes;
 pub mod writers;
 
-pub use durs_dbs_tools::kv_db::{
+pub use durs_dbs_tools::kv_db_old::{
     KvFileDbHandler, KvFileDbRead as DbReadable, KvFileDbRoHandler, KvFileDbSchema,
     KvFileDbStoreType, KvFileDbValue, KvFileDbWriter as DbWriter,
 };
diff --git a/lib/modules/blockchain/blockchain/src/dubp.rs b/lib/modules/blockchain/blockchain/src/dubp.rs
index 3f209689..e08cd6dd 100644
--- a/lib/modules/blockchain/blockchain/src/dubp.rs
+++ b/lib/modules/blockchain/blockchain/src/dubp.rs
@@ -90,7 +90,7 @@ fn treat_chainable_block(
     let blocks_expiring = Vec::with_capacity(0); // TODO
     let expire_certs = durs_bc_db_reader::indexes::certs::find_expire_certs(
         &BcDbRwWithWriter { db, w },
-        blocks_expiring,
+        &blocks_expiring,
     )?;
 
     // If we're in block genesis, get the currency parameters
diff --git a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs
index a8f58d27..e7dd9f76 100644
--- a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs
+++ b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs
@@ -224,7 +224,7 @@ mod tests {
             ]),
             db.read(|r| fork_resolution_algo(
                 &BcDbRwWithReader { db: &db, r },
-                &mut fork_tree,
+                &fork_tree,
                 fork_window_size,
                 current_blockstamp,
                 &invalid_blocks
@@ -256,7 +256,7 @@ mod tests {
             Some(new_main_blocks.iter().map(|b| b.blockstamp()).collect()),
             db.read(|r| fork_resolution_algo(
                 &BcDbRwWithReader { db: &db, r },
-                &mut fork_tree,
+                &fork_tree,
                 fork_window_size,
                 current_blockstamp,
                 &invalid_blocks
diff --git a/lib/modules/blockchain/blockchain/src/requests/received.rs b/lib/modules/blockchain/blockchain/src/requests/received.rs
index be35e86f..9f6c8418 100644
--- a/lib/modules/blockchain/blockchain/src/requests/received.rs
+++ b/lib/modules/blockchain/blockchain/src/requests/received.rs
@@ -148,12 +148,12 @@ pub fn receive_req(
                         bc.db()
                             .r(|db_r| {
                                 Ok(pubkeys
-                                    .into_iter()
+                                    .iter()
                                     .map(|p| {
                                         (
-                                            p,
+                                            *p,
                                             durs_bc_db_reader::indexes::identities::get_uid(
-                                                db_r, &p,
+                                                db_r, p,
                                             )
                                             .unwrap_or(None),
                                         )
diff --git a/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs b/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs
index d4e59ecf..0d8b0f2b 100644
--- a/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs
+++ b/lib/modules/blockchain/blockchain/src/sync/apply/mod.rs
@@ -97,7 +97,7 @@ impl BlockApplicator {
         let expire_certs = if let Some(db) = self.db.take() {
             let expire_certs = db
                 .r(|db_r| {
-                    durs_bc_db_reader::indexes::certs::find_expire_certs(db_r, blocks_expiring)
+                    durs_bc_db_reader::indexes::certs::find_expire_certs(db_r, &blocks_expiring)
                 })
                 .expect("find_expire_certs() : DbError");
             self.db = Some(db);
diff --git a/lib/modules/gva/src/schema.rs b/lib/modules/gva/src/schema.rs
index efb63e3a..3d968b84 100644
--- a/lib/modules/gva/src/schema.rs
+++ b/lib/modules/gva/src/schema.rs
@@ -101,8 +101,8 @@ impl QueryFields for Query {
         exec_in_db_transaction!(blocks(
             executor,
             trail,
-            paging_opt,
-            block_interval_opt,
+            paging_opt.as_ref(),
+            block_interval_opt.as_ref(),
             step as usize,
             sort_order
         ))
diff --git a/lib/modules/gva/src/schema/inputs/block_interval.rs b/lib/modules/gva/src/schema/inputs/block_interval.rs
index ba70c065..57cb7c7d 100644
--- a/lib/modules/gva/src/schema/inputs/block_interval.rs
+++ b/lib/modules/gva/src/schema/inputs/block_interval.rs
@@ -29,7 +29,7 @@ pub struct FilledBlockInterval {
 
 impl FilledBlockInterval {
     pub(crate) fn new(
-        block_interval_opt: Option<BlockInterval>,
+        block_interval_opt: Option<&BlockInterval>,
         current_block_number_opt: Option<BlockNumber>,
     ) -> Self {
         let current_block_number = current_block_number_opt.unwrap_or(BlockNumber(0)).0 as usize;
@@ -120,7 +120,7 @@ mod tests {
 
         assert_eq! {
             500..=750,
-            FilledBlockInterval::new(Some(BlockInterval {
+            FilledBlockInterval::new(Some(&BlockInterval {
                 from: Some(500),
                 to: None,
             }), Some(BlockNumber(750))).get_range()
@@ -128,7 +128,7 @@ mod tests {
 
         assert_eq! {
             500..=700,
-            FilledBlockInterval::new(Some(BlockInterval {
+            FilledBlockInterval::new(Some(&BlockInterval {
                 from: Some(500),
                 to: Some(700),
             }), Some(BlockNumber(750))).get_range()
diff --git a/lib/modules/gva/src/schema/inputs/paging.rs b/lib/modules/gva/src/schema/inputs/paging.rs
index a756b7e7..04934a3e 100644
--- a/lib/modules/gva/src/schema/inputs/paging.rs
+++ b/lib/modules/gva/src/schema/inputs/paging.rs
@@ -35,11 +35,14 @@ impl Default for FilledPaging {
     }
 }
 
-impl From<Option<Paging>> for FilledPaging {
-    fn from(paging_opt: Option<Paging>) -> Self {
+impl From<Option<&Paging>> for FilledPaging {
+    fn from(paging_opt: Option<&Paging>) -> Self {
         if let Some(paging) = paging_opt {
             FilledPaging {
-                page_number: paging.page_number.unwrap_or(DEFAULT_PAGE_NUMBER_I32) as isize,
+                page_number: paging
+                    .page_number
+                    .clone()
+                    .unwrap_or(DEFAULT_PAGE_NUMBER_I32) as isize,
                 page_size: if let Some(page_size) = paging.page_size {
                     if page_size < MIN_PAGE_SIZE {
                         MIN_PAGE_SIZE as usize
@@ -115,7 +118,7 @@ mod tests {
                 page_number: 0,
                 page_size: 10,
             },
-            FilledPaging::from(Some(Paging {
+            FilledPaging::from(Some(&Paging {
                 page_number: None,
                 page_size: Some(10)
             })),
@@ -125,7 +128,7 @@ mod tests {
                 page_number: 1,
                 page_size: 50,
             },
-            FilledPaging::from(Some(Paging {
+            FilledPaging::from(Some(&Paging {
                 page_number: Some(1),
                 page_size: None
             })),
@@ -135,7 +138,7 @@ mod tests {
                 page_number: 1,
                 page_size: 10,
             },
-            FilledPaging::from(Some(Paging {
+            FilledPaging::from(Some(&Paging {
                 page_number: Some(1),
                 page_size: Some(10)
             })),
diff --git a/lib/modules/gva/src/schema/queries/blocks.rs b/lib/modules/gva/src/schema/queries/blocks.rs
index d2bdb997..467a027f 100644
--- a/lib/modules/gva/src/schema/queries/blocks.rs
+++ b/lib/modules/gva/src/schema/queries/blocks.rs
@@ -28,8 +28,8 @@ use juniper_from_schema::{QueryTrail, Walked};
 pub(crate) fn execute<DB: BcDbInReadTx>(
     db: &DB,
     trail: &QueryTrail<'_, BlocksPage, Walked>,
-    paging_opt: Option<Paging>,
-    block_interval_opt: Option<BlockInterval>,
+    paging_opt: Option<&Paging>,
+    block_interval_opt: Option<&BlockInterval>,
     step: usize,
     sort_order: SortOrder,
 ) -> Result<BlocksPage, DbError> {
diff --git a/lib/tools/dbs-tools/src/kv_db.rs b/lib/tools/dbs-tools/src/kv_db.rs
deleted file mode 100644
index ac1afbfd..00000000
--- a/lib/tools/dbs-tools/src/kv_db.rs
+++ /dev/null
@@ -1,29 +0,0 @@
-//  Copyright (C) 2017-2019  The AXIOM TEAM Association.
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as
-// published by the Free Software Foundation, either version 3 of the
-// License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program.  If not, see <https://www.gnu.org/licenses/>.
-
-//! Define Key-Value database
-
-mod file;
-
-#[cfg(feature = "mock")]
-pub use file::MockKvFileDbReader;
-pub use file::{
-    from_db_value, KvFileDbHandler, KvFileDbRead, KvFileDbReader, KvFileDbRoHandler,
-    KvFileDbSchema, KvFileDbStoreType, KvFileDbWriter,
-};
-pub use rkv::{
-    store::multi::Iter, IntegerStore, MultiIntegerStore, MultiStore,
-    OwnedValue as KvFileDbOwnedValue, Readable, SingleStore, Value as KvFileDbValue,
-};
diff --git a/lib/tools/dbs-tools/src/kv_db_old.rs b/lib/tools/dbs-tools/src/kv_db_old.rs
new file mode 100644
index 00000000..5d4b0682
--- /dev/null
+++ b/lib/tools/dbs-tools/src/kv_db_old.rs
@@ -0,0 +1,15 @@
+///////////////////////
+/// OLD A SUPPRIMER ///
+///////////////////////
+mod file;
+
+#[cfg(feature = "mock")]
+pub use file::MockKvFileDbReader;
+pub use file::{
+    from_db_value, KvFileDbHandler, KvFileDbRead, KvFileDbReader, KvFileDbRoHandler,
+    KvFileDbSchema, KvFileDbStoreType, KvFileDbWriter,
+};
+pub use rkv::{
+    store::multi::Iter, IntegerStore, MultiIntegerStore, MultiStore,
+    OwnedValue as KvFileDbOwnedValue, Readable, SingleStore, Value as KvFileDbValue,
+};
diff --git a/lib/tools/dbs-tools/src/kv_db/file.rs b/lib/tools/dbs-tools/src/kv_db_old/file.rs
similarity index 98%
rename from lib/tools/dbs-tools/src/kv_db/file.rs
rename to lib/tools/dbs-tools/src/kv_db_old/file.rs
index fa0a23e5..24ba05bb 100644
--- a/lib/tools/dbs-tools/src/kv_db/file.rs
+++ b/lib/tools/dbs-tools/src/kv_db_old/file.rs
@@ -198,7 +198,7 @@ pub trait KvFileDbRead: Sized {
     /// Read datas in transaction database
     fn read<F, R>(&self, f: F) -> Result<R, DbError>
     where
-        F: FnOnce(KvFileDbReader) -> Result<R, DbError>;
+        F: Fn(KvFileDbReader) -> Result<R, DbError>;
 }
 
 impl KvFileDbRead for KvFileDbRoHandler {
@@ -221,7 +221,7 @@ impl KvFileDbRead for KvFileDbRoHandler {
     #[inline]
     fn read<F, R>(&self, f: F) -> Result<R, DbError>
     where
-        F: FnOnce(KvFileDbReader) -> Result<R, DbError>,
+        F: Fn(KvFileDbReader) -> Result<R, DbError>,
     {
         self.0.read(f)
     }
@@ -320,7 +320,7 @@ impl KvFileDbRead for KvFileDbHandler {
     }
     fn read<F, R>(&self, f: F) -> Result<R, DbError>
     where
-        F: FnOnce(KvFileDbReader) -> Result<R, DbError>,
+        F: Fn(KvFileDbReader) -> Result<R, DbError>,
     {
         Ok(f(KvFileDbReader(&self.arc_clone().read()?.read()?))?)
     }
diff --git a/lib/tools/dbs-tools/src/lib.rs b/lib/tools/dbs-tools/src/lib.rs
index df67c79c..28e0af24 100644
--- a/lib/tools/dbs-tools/src/lib.rs
+++ b/lib/tools/dbs-tools/src/lib.rs
@@ -31,7 +31,8 @@
 
 mod errors;
 mod free_struct_db;
-pub mod kv_db;
+/// module a supprimer
+pub mod kv_db_old;
 
 pub use errors::DbError;
 pub use free_struct_db::{open_free_struct_file_db, open_free_struct_memory_db, BinFreeStructDb};
-- 
GitLab