-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add kv storage to individual canisters (#320)
* add kv storage to individual canisters * added test * add errors for value too big in kv storage * add ic-ledger-types dependency * fix dependencies * fix depedency issue * fix ic-ledger-types issue * change method type of read_key_value_pair to query
- Loading branch information
1 parent
5e0cabf
commit 92e20c7
Showing
19 changed files
with
1,127 additions
and
479 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/canister/individual_user_template/src/api/kv_storage/key_value.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use ic_cdk::caller; | ||
use ic_cdk_macros::{query, update}; | ||
use shared_utils::canister_specific::individual_user_template::types::kv_storage::NamespaceErrors; | ||
use std::collections::BTreeMap; | ||
|
||
use crate::data_model::kv_storage::AppStorage; | ||
|
||
#[update] | ||
fn delete_multiple_key_value_pairs( | ||
namespace_id: u64, | ||
keys: Vec<String>, | ||
) -> Result<(), NamespaceErrors> { | ||
let namespace = AppStorage::get_a_namespace(caller(), namespace_id)?; | ||
namespace.delete_multiple_keys(keys); | ||
Ok(()) | ||
} | ||
|
||
#[update] | ||
fn write_multiple_key_value_pairs( | ||
namespace_id: u64, | ||
pairs: BTreeMap<String, String>, | ||
) -> Result<(), NamespaceErrors> { | ||
let namespace = AppStorage::get_a_namespace(caller(), namespace_id)?; | ||
namespace.write_multiple_key_value_pairs(pairs) | ||
} | ||
|
||
#[update] | ||
fn write_key_value_pair( | ||
namespace_id: u64, | ||
key: String, | ||
value: String, | ||
) -> Result<Option<String>, NamespaceErrors> { | ||
let namespace = AppStorage::get_a_namespace(caller(), namespace_id)?; | ||
let prev_value = namespace.write_key_value_pair(key, value)?; | ||
Ok(prev_value) | ||
} | ||
|
||
#[query] | ||
fn list_namespace_keys(namespace_id: u64) -> Result<Vec<String>, NamespaceErrors> { | ||
let namespace = AppStorage::get_a_namespace(caller(), namespace_id)?; | ||
Ok(namespace.list_keys()) | ||
} | ||
|
||
#[update] | ||
fn delete_key_value_pair( | ||
namespace_id: u64, | ||
key: String, | ||
) -> Result<Option<String>, NamespaceErrors> { | ||
let namespace = AppStorage::get_a_namespace(caller(), namespace_id)?; | ||
Ok(namespace.delete_key_value_pair(key)) | ||
} | ||
|
||
#[query] | ||
fn read_key_value_pair(namespace_id: u64, key: String) -> Result<Option<String>, NamespaceErrors> { | ||
let namespace = AppStorage::get_a_namespace(caller(), namespace_id)?; | ||
Ok(namespace.read_key_value_pair(key)) | ||
} |
2 changes: 2 additions & 0 deletions
2
src/canister/individual_user_template/src/api/kv_storage/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod key_value; | ||
pub mod namespace; |
22 changes: 22 additions & 0 deletions
22
src/canister/individual_user_template/src/api/kv_storage/namespace.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::borrow::Borrow; | ||
|
||
use ic_cdk::caller; | ||
use ic_cdk_macros::{query, update}; | ||
use shared_utils::canister_specific::individual_user_template::types::kv_storage::{ | ||
NamespaceErrors, NamespaceForFrontend, | ||
}; | ||
|
||
use crate::{ | ||
data_model::kv_storage::{AppStorage, Namespace}, | ||
CANISTER_DATA, | ||
}; | ||
|
||
#[update] | ||
fn create_a_namespace(title: String) -> Result<NamespaceForFrontend, NamespaceErrors> { | ||
AppStorage::create_a_namespace(caller(), title) | ||
} | ||
|
||
#[query] | ||
fn list_namespaces(start_index: usize, limit: usize) -> Vec<NamespaceForFrontend> { | ||
AppStorage::list_namespaces(start_index, limit) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.