Skip to content

Commit

Permalink
Make MapID generic - Into<u16>
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed Apr 24, 2023
1 parent 82dc292 commit f29e06e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion node/store/src/rocksdb/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> fmt::Debu
#[cfg(test)]
mod tests {
use super::*;
use crate::{rocksdb::tests::temp_dir, TestMap};
use crate::{rocksdb::tests::temp_dir, MapID, TestMap};
use snarkvm::prelude::{Address, FromStr, Testnet3};

use serial_test::serial;
Expand Down
18 changes: 8 additions & 10 deletions node/store/src/rocksdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use iterator::*;
#[cfg(test)]
mod tests;

use crate::MapID;

use anyhow::{bail, Result};
use core::{fmt::Debug, hash::Hash};
use once_cell::sync::OnceCell;
Expand All @@ -46,10 +44,10 @@ pub trait Database {
Self: Sized;

/// Opens the map with the given `network_id`, `(optional) development ID`, and `map_id` from storage.
fn open_map<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned>(
fn open_map<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned, T: Into<u16>>(
network_id: u16,
dev: Option<u16>,
map_id: MapID,
map_id: T,
) -> Result<DataMap<K, V>>;
}

Expand Down Expand Up @@ -111,17 +109,17 @@ impl Database for RocksDB {
}

/// Opens the map with the given `network_id`, `(optional) development ID`, and `map_id` from storage.
fn open_map<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned>(
fn open_map<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned, T: Into<u16>>(
network_id: u16,
dev: Option<u16>,
map_id: MapID,
map_id: T,
) -> Result<DataMap<K, V>> {
// Open the RocksDB database.
let database = Self::open(network_id, dev)?;

// Combine contexts to create a new scope.
let mut context = database.network_id.to_le_bytes().to_vec();
context.extend_from_slice(&(u16::from(map_id)).to_le_bytes());
context.extend_from_slice(&(map_id.into()).to_le_bytes());

// Return the DataMap.
Ok(DataMap { database, context, batch_in_progress: Default::default(), atomic_batch: Default::default() })
Expand Down Expand Up @@ -165,17 +163,17 @@ impl RocksDB {

/// Opens the test map.
#[cfg(test)]
fn open_map_testing<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned>(
fn open_map_testing<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned, T: Into<u16>>(
temp_dir: std::path::PathBuf,
dev: Option<u16>,
map_id: MapID,
map_id: T,
) -> Result<DataMap<K, V>> {
// Open the RocksDB test database.
let database = Self::open_testing(temp_dir, dev)?;

// Combine contexts to create a new scope.
let mut context = database.network_id.to_le_bytes().to_vec();
context.extend_from_slice(&(u16::from(map_id)).to_le_bytes());
context.extend_from_slice(&(map_id.into()).to_le_bytes());

// Return the DataMap.
Ok(DataMap { database, context, batch_in_progress: Default::default(), atomic_batch: Default::default() })
Expand Down
2 changes: 1 addition & 1 deletion node/store/src/rocksdb/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn test_open() {
#[test]
#[serial]
fn test_open_map() {
let _map = RocksDB::open_map_testing::<u32, String>(temp_dir(), None, MapID::Test(TestMapID::Test))
let _map = RocksDB::open_map_testing::<u32, String, _>(temp_dir(), None, MapID::Test(TestMapID::Test))
.expect("Failed to open data map");
}

Expand Down

0 comments on commit f29e06e

Please sign in to comment.