Skip to content

Commit

Permalink
add kv storage to individual canisters (#320)
Browse files Browse the repository at this point in the history
* 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
ravi-sawlani-yral authored Jun 18, 2024
1 parent 5e0cabf commit 92e20c7
Show file tree
Hide file tree
Showing 19 changed files with 1,127 additions and 479 deletions.
941 changes: 508 additions & 433 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ic-cdk = "0.12.0"
ic-cdk-timers = "0.6.0"
ic-cdk-macros = "0.8.4"
ic-stable-structures = "0.6.2"
ic-ledger-types = "0.9.0"
ic-test-state-machine-client = "3.0.0"
rmp-serde = "1.1.2"
serde = "1.0.186"
Expand Down
95 changes: 60 additions & 35 deletions src/canister/individual_user_template/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ type HotOrNotOutcomePayoutEvent = variant {
type HttpRequest = record {
url : text;
method : text;
body : vec nat8;
body : blob;
headers : vec record { text; text };
};
type HttpResponse = record {
body : vec nat8;
body : blob;
headers : vec record { text; text };
status_code : nat16;
};
Expand Down Expand Up @@ -145,6 +145,17 @@ type MintEvent = variant {
referee_user_principal_id : principal;
};
};
type NamespaceErrors = variant {
UserNotSignedUp;
ValueTooBig;
NamespaceNotFound;
Unauthorized;
};
type NamespaceForFrontend = record {
id : nat64;
title : text;
owner_id : principal;
};
type PlaceBetArg = record {
bet_amount : nat64;
post_id : nat64;
Expand Down Expand Up @@ -229,25 +240,29 @@ type Result_1 = variant {
Err : BetOnCurrentlyViewingPostError;
};
type Result_10 = variant {
Ok : vec record { nat64; TokenEvent };
Err : GetPostsOfUserProfileError;
};
type Result_11 = variant { Ok : vec text; Err : NamespaceErrors };
type Result_12 = variant { Ok; Err : MigrationErrors };
type Result_13 = variant { Ok : text; Err : text };
type Result_14 = variant {
Ok : UserProfileDetailsForFrontend;
Err : UpdateProfileDetailsError;
};
type Result_11 = variant { Ok; Err : text };
type Result_12 = variant { Ok; Err : UpdateProfileSetUniqueUsernameError };
type Result_2 = variant { Ok : bool; Err : FollowAnotherUserProfileError };
type Result_3 = variant { Ok : Post; Err };
type Result_4 = variant { Ok : SystemTime; Err : text };
type Result_5 = variant {
type Result_15 = variant { Ok; Err : text };
type Result_16 = variant { Ok; Err : UpdateProfileSetUniqueUsernameError };
type Result_2 = variant { Ok : NamespaceForFrontend; Err : NamespaceErrors };
type Result_3 = variant { Ok : opt text; Err : NamespaceErrors };
type Result_4 = variant { Ok; Err : NamespaceErrors };
type Result_5 = variant { Ok : bool; Err : FollowAnotherUserProfileError };
type Result_6 = variant { Ok : Post; Err };
type Result_7 = variant { Ok : SystemTime; Err : text };
type Result_8 = variant {
Ok : vec PostDetailsForFrontend;
Err : GetPostsOfUserProfileError;
};
type Result_6 = variant { Ok : SessionType; Err : text };
type Result_7 = variant {
Ok : vec record { nat64; TokenEvent };
Err : GetPostsOfUserProfileError;
};
type Result_8 = variant { Ok; Err : MigrationErrors };
type Result_9 = variant { Ok : text; Err : text };
type Result_9 = variant { Ok : SessionType; Err : text };
type RoomBetPossibleOutcomes = variant { HotWon; BetOngoing; Draw; NotWon };
type RoomDetails = record {
total_hot_bets : nat64;
Expand Down Expand Up @@ -346,9 +361,12 @@ service : (IndividualUserTemplateInitArgs) -> {
vec nat64,
) -> ();
clear_snapshot : () -> ();
do_i_follow_this_user : (FolloweeArg) -> (Result_2) query;
download_snapshot : (nat64, nat64) -> (vec nat8) query;
get_entire_individual_post_detail_by_id : (nat64) -> (Result_3) query;
create_a_namespace : (text) -> (Result_2);
delete_key_value_pair : (nat64, text) -> (Result_3);
delete_multiple_key_value_pairs : (nat64, vec text) -> (Result_4);
do_i_follow_this_user : (FolloweeArg) -> (Result_5) query;
download_snapshot : (nat64, nat64) -> (blob) query;
get_entire_individual_post_detail_by_id : (nat64) -> (Result_6) query;
get_hot_or_not_bet_details_for_this_post : (nat64) -> (BettingStatus) query;
get_hot_or_not_bets_placed_by_this_profile_with_pagination : (nat64) -> (
vec PlacedBetDetail,
Expand All @@ -357,13 +375,13 @@ service : (IndividualUserTemplateInitArgs) -> {
opt PlacedBetDetail,
) query;
get_individual_post_details_by_id : (nat64) -> (PostDetailsForFrontend) query;
get_last_access_time : () -> (Result_4) query;
get_last_canister_functionality_access_time : () -> (Result_4) query;
get_last_access_time : () -> (Result_7) query;
get_last_canister_functionality_access_time : () -> (Result_7) query;
get_posts_of_this_user_profile_with_pagination : (nat64, nat64) -> (
Result_5,
Result_8,
) query;
get_posts_of_this_user_profile_with_pagination_cursor : (nat64, nat64) -> (
Result_5,
Result_8,
) query;
get_principals_that_follow_this_profile_paginated : (opt nat64) -> (
vec record { nat64; FollowEntryDetail },
Expand All @@ -375,25 +393,28 @@ service : (IndividualUserTemplateInitArgs) -> {
get_profile_details_v2 : () -> (UserProfileDetailsForFrontendV2) query;
get_rewarded_for_referral : (principal, principal) -> ();
get_rewarded_for_signing_up : () -> ();
get_session_type : () -> (Result_6) query;
get_session_type : () -> (Result_9) query;
get_stable_memory_size : () -> (nat32) query;
get_user_caniser_cycle_balance : () -> (nat) query;
get_user_utility_token_transaction_history_with_pagination : (
nat64,
nat64,
) -> (Result_7) query;
) -> (Result_10) query;
get_utility_token_balance : () -> (nat64) query;
get_version : () -> (text) query;
get_version_number : () -> (nat64) query;
get_well_known_principal_value : (KnownPrincipalType) -> (
opt principal,
) query;
http_request : (HttpRequest) -> (HttpResponse) query;
list_namespace_keys : (nat64) -> (Result_11) query;
list_namespaces : (nat64, nat64) -> (vec NamespaceForFrontend) query;
load_snapshot : (nat64) -> ();
receive_and_save_snaphot : (nat64, vec nat8) -> ();
read_key_value_pair : (nat64, text) -> (Result_3) query;
receive_and_save_snaphot : (nat64, blob) -> ();
receive_bet_from_bet_makers_canister : (PlaceBetArg, principal) -> (Result_1);
receive_bet_winnings_when_distributed : (nat64, BetOutcomeForBetMaker) -> ();
receive_data_from_hotornot : (principal, nat64, vec Post) -> (Result_8);
receive_data_from_hotornot : (principal, nat64, vec Post) -> (Result_12);
receive_my_created_posts_from_data_backup_canister : (vec Post) -> ();
receive_my_profile_from_data_backup_canister : (UserProfile) -> ();
receive_my_utility_token_balance_from_data_backup_canister : (nat64) -> ();
Expand All @@ -406,26 +427,30 @@ service : (IndividualUserTemplateInitArgs) -> {
) -> ();
return_cycles_to_user_index_canister : (opt nat) -> ();
save_snapshot_json : () -> (nat32);
transfer_tokens_and_posts : (principal, principal) -> (Result_8);
update_last_access_time : () -> (Result_9);
transfer_tokens_and_posts : (principal, principal) -> (Result_12);
update_last_access_time : () -> (Result_13);
update_last_canister_functionality_access_time : () -> ();
update_post_add_view_details : (nat64, PostViewDetailsFromFrontend) -> ();
update_post_as_ready_to_view : (nat64) -> ();
update_post_increment_share_count : (nat64) -> (nat64);
update_post_status : (nat64, PostStatus) -> ();
update_post_toggle_like_status_by_caller : (nat64) -> (bool);
update_profile_display_details : (UserProfileUpdateDetailsFromFrontend) -> (
Result_10,
Result_14,
);
update_profile_owner : (opt principal) -> (Result_11);
update_profile_set_unique_username_once : (text) -> (Result_12);
update_profile_owner : (opt principal) -> (Result_15);
update_profile_set_unique_username_once : (text) -> (Result_16);
update_profiles_i_follow_toggle_list_with_specified_profile : (
FolloweeArg,
) -> (Result_2);
) -> (Result_5);
update_profiles_that_follow_me_toggle_list_with_specified_profile : (
FollowerArg,
) -> (Result_2);
update_referrer_details : (UserCanisterDetails) -> (Result_9);
update_session_type : (SessionType) -> (Result_9);
) -> (Result_5);
update_referrer_details : (UserCanisterDetails) -> (Result_13);
update_session_type : (SessionType) -> (Result_13);
update_well_known_principal : (KnownPrincipalType, principal) -> ();
write_key_value_pair : (nat64, text, text) -> (Result_3);
write_multiple_key_value_pairs : (nat64, vec record { text; text }) -> (
Result_4,
);
}
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))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod key_value;
pub mod namespace;
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)
}
1 change: 1 addition & 0 deletions src/canister/individual_user_template/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod cycle_management;
pub mod follow;
pub mod hot_or_not_bet;
pub mod http;
pub mod kv_storage;
pub mod monitoring;
pub mod post;
pub mod profile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use shared_utils::{
HotOrNotDetails, PlacedBetDetail, RoomDetailsV1, RoomId, SlotDetailsV1, SlotId,
StablePrincipal,
},
migration::{MigrationInfo},
migration::MigrationInfo,
post::{FeedScore, Post, PostViewStatistics},
profile::UserProfile,
session::SessionType,
Expand Down Expand Up @@ -364,6 +364,7 @@ impl From<CanisterDataForSnapshot> for CanisterData {
last_canister_functionality_access_time: canister_data
.last_canister_functionality_access_time,
migration_info: canister_data.migration_info,
..Default::default()
}
}
}
Loading

0 comments on commit 92e20c7

Please sign in to comment.