Skip to content

Commit

Permalink
fix rust/composite_query
Browse files Browse the repository at this point in the history
  • Loading branch information
mraszyk committed Sep 10, 2024
1 parent 17879bb commit f1ba18c
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust-composite_query-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ jobs:
run: |
pushd rust/composite_query
dfx start --background --clean --pocketic
dfx canister create data_partition
dfx canister create data_partition --no-wallet
dfx build data_partition
dfx canister create kv_frontend
dfx canister create kv_frontend --no-wallet
dfx build kv_frontend
dfx canister install kv_frontend
dfx canister call kv_frontend put '(1, 1337)'
Expand Down
152 changes: 139 additions & 13 deletions rust/composite_query/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions rust/composite_query/src/kv_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ crate-type = ["cdylib"]
path = "lib.rs"

[dependencies]
candid = "0.8.4"
ic-cdk = "0.8.1"
ic-cdk-macros = "0.6.10"
candid = "0.10.10"
ic-cdk = "0.16.0"
ic-cdk-macros = "0.16.0"
ic-cdk-optimizer = "0.3.5"
65 changes: 43 additions & 22 deletions rust/composite_query/src/kv_frontend/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use ic_cdk::api::call::{call};
use ic_cdk::api::management_canister::main::{CreateCanisterArgument, create_canister, InstallCodeArgument, install_code, CanisterInstallMode};
use candid::Principal;
use ic_cdk::api::call::call;
use ic_cdk::api::management_canister::main::{
create_canister, install_code, CanisterInstallMode, CreateCanisterArgument, InstallCodeArgument,
};
use ic_cdk::api::management_canister::provisional::CanisterSettings;
use ic_cdk_macros::{query, update};
use candid::Principal;

use std::sync::Arc;
use std::sync::RwLock;
Expand All @@ -20,7 +22,6 @@ thread_local! {

#[update]
async fn put(key: u128, value: u128) -> Option<u128> {

// Create partitions if they don't exist yet
if CANISTER_IDS.with(|canister_ids| {
let canister_ids = canister_ids.read().unwrap();
Expand All @@ -32,38 +33,50 @@ async fn put(key: u128, value: u128) -> Option<u128> {
}

let canister_id = get_partition_for_key(key);
ic_cdk::println!("Put in frontend for key={} .. using backend={}", key, canister_id.to_text());
match call(canister_id, "put", (key, value), ).await {
ic_cdk::println!(
"Put in frontend for key={} .. using backend={}",
key,
canister_id.to_text()
);
match call(canister_id, "put", (key, value)).await {
Ok(r) => {
let (res,): (Option<u128>,) = r;
res
},
}
Err(_) => None,
}
}

#[query(composite = true)]
async fn get(key: u128) -> Option<u128> {
let canister_id = get_partition_for_key(key);
ic_cdk::println!("Get in frontend for key={} .. using backend={}", key, canister_id.to_text());
match call(canister_id, "get", (key, ), ).await {
ic_cdk::println!(
"Get in frontend for key={} .. using backend={}",
key,
canister_id.to_text()
);
match call(canister_id, "get", (key,)).await {
Ok(r) => {
let (res,): (Option<u128>,) = r;
res
},
}
Err(_) => None,
}
}

#[update]
async fn get_update(key: u128) -> Option<u128> {
let canister_id = get_partition_for_key(key);
ic_cdk::println!("Get as update in frontend for key={} .. using backend={}", key, canister_id.to_text());
match call(canister_id, "get", (key, ), ).await {
ic_cdk::println!(
"Get as update in frontend for key={} .. using backend={}",
key,
canister_id.to_text()
);
match call(canister_id, "get", (key,)).await {
Ok(r) => {
let (res,): (Option<u128>,) = r;
res
},
}
Err(_) => None,
}
}
Expand All @@ -79,23 +92,31 @@ fn get_partition_for_key(key: u128) -> Principal {
#[query(composite = true)]
fn lookup(key: u128) -> (u128, String) {
let r = key % NUM_PARTITIONS as u128;
(r, CANISTER_IDS.with(|canister_ids| {
let canister_ids = canister_ids.read().unwrap();
canister_ids[r as usize].to_text()
}))
(
r,
CANISTER_IDS.with(|canister_ids| {
let canister_ids = canister_ids.read().unwrap();
canister_ids[r as usize].to_text()
}),
)
}

async fn create_data_partition_canister_from_wasm() {
let create_args = CreateCanisterArgument {
settings: Some(CanisterSettings {
controllers: Some(vec![ic_cdk::id()]),
compute_allocation: Some(0.into()),
memory_allocation: Some(0.into()),
freezing_threshold: Some(0.into()),
})
compute_allocation: Some(0_u64.into()),
memory_allocation: Some(0_u64.into()),
freezing_threshold: Some(0_u64.into()),
log_visibility: None,
reserved_cycles_limit: None,
wasm_memory_limit: None,
}),
};

let canister_record = create_canister(create_args).await.unwrap();
const T: u128 = 1_000_000_000_000;

let canister_record = create_canister(create_args, 10 * T).await.unwrap();
let canister_id = canister_record.0.canister_id;

ic_cdk::println!("Created canister {}", canister_id);
Expand Down

0 comments on commit f1ba18c

Please sign in to comment.