diff --git a/examples/stable_structures/src/canister1/index.did b/examples/stable_structures/src/canister1/index.did index fe659ffda6..4b871ac106 100644 --- a/examples/stable_structures/src/canister1/index.did +++ b/examples/stable_structures/src/canister1/index.did @@ -1,50 +1,65 @@ -type BlogPost = record { title : text }; -type Reaction = variant { Sad; Happy }; -type User = record { username : text; posts : vec BlogPost }; -service : () -> { - stableMap0ContainsKey : (nat8) -> (bool) query; - stableMap0Get : (nat8) -> (opt text) query; - stableMap0Insert : (nat8, text) -> (opt text); - stableMap0IsEmpty : () -> (bool) query; - stableMap0Items : () -> (vec record { nat8; text }) query; - stableMap0Keys : () -> (vec nat8) query; - stableMap0Len : () -> (nat64) query; - stableMap0Remove : (nat8) -> (opt text); - stableMap0Values : () -> (vec text) query; - stableMap1ContainsKey : (nat16) -> (bool) query; - stableMap1Get : (nat16) -> (opt vec nat8) query; - stableMap1Insert : (nat16, vec nat8) -> (opt vec nat8); - stableMap1IsEmpty : () -> (bool) query; - stableMap1Items : () -> (vec record { nat16; vec nat8 }) query; - stableMap1Keys : () -> (vec nat16) query; - stableMap1Len : () -> (nat64) query; - stableMap1Remove : (nat16) -> (opt vec nat8); - stableMap1Values : () -> (vec vec nat8) query; - stableMap2ContainsKey : (nat32) -> (bool) query; - stableMap2Get : (nat32) -> (opt nat) query; - stableMap2Insert : (nat32, nat) -> (opt nat); - stableMap2IsEmpty : () -> (bool) query; - stableMap2Items : () -> (vec record { nat32; nat }) query; - stableMap2Keys : () -> (vec nat32) query; - stableMap2Len : () -> (nat64) query; - stableMap2Remove : (nat32) -> (opt nat); - stableMap2Values : () -> (vec nat) query; - stableMap3ContainsKey : (Reaction) -> (bool) query; - stableMap3Get : (Reaction) -> (opt int) query; - stableMap3Insert : (Reaction, int) -> (opt int); - stableMap3IsEmpty : () -> (bool) query; - stableMap3Items : () -> (vec record { Reaction; int }) query; - stableMap3Keys : () -> (vec Reaction) query; - stableMap3Len : () -> (nat64) query; - stableMap3Remove : (Reaction) -> (opt int); - stableMap3Values : () -> (vec int) query; - stableMap4ContainsKey : (User) -> (bool) query; - stableMap4Get : (User) -> (opt float32) query; - stableMap4Insert : (User, float32) -> (opt float32); - stableMap4IsEmpty : () -> (bool) query; - stableMap4Items : () -> (vec record { User; float32 }) query; - stableMap4Keys : () -> (vec User) query; - stableMap4Len : () -> (nat64) query; - stableMap4Remove : (User) -> (opt float32); - stableMap4Values : () -> (vec float32) query; -} \ No newline at end of file +type rec_0 = variant {Sad; Happy}; +type rec_1 = variant {Sad; Happy}; +type rec_2 = variant {Sad; Happy}; +type rec_3 = variant {Sad; Happy}; +type rec_4 = variant {Sad; Happy}; +type rec_5 = variant {Sad; Happy}; +type rec_7 = record {title:text}; +type rec_6 = record {username:text; posts:vec rec_7}; +type rec_9 = record {title:text}; +type rec_8 = record {username:text; posts:vec rec_9}; +type rec_11 = record {title:text}; +type rec_10 = record {username:text; posts:vec rec_11}; +type rec_13 = record {title:text}; +type rec_12 = record {username:text; posts:vec rec_13}; +type rec_15 = record {title:text}; +type rec_14 = record {username:text; posts:vec rec_15}; +type rec_17 = record {title:text}; +type rec_16 = record {username:text; posts:vec rec_17}; +service: () -> { + stableMap0ContainsKey: (nat8) -> (bool) query; + stableMap0Get: (nat8) -> (opt text) query; + stableMap0Insert: (nat8, text) -> (opt text); + stableMap0IsEmpty: () -> (bool) query; + stableMap0Items: () -> (vec record {nat8; text}) query; + stableMap0Keys: () -> (vec nat8) query; + stableMap0Len: () -> (nat64) query; + stableMap0Remove: (nat8) -> (opt text); + stableMap0Values: () -> (vec text) query; + stableMap1ContainsKey: (nat16) -> (bool) query; + stableMap1Get: (nat16) -> (opt vec nat8) query; + stableMap1Insert: (nat16, vec nat8) -> (opt vec nat8); + stableMap1IsEmpty: () -> (bool) query; + stableMap1Items: () -> (vec record {nat16; vec nat8}) query; + stableMap1Keys: () -> (vec nat16) query; + stableMap1Len: () -> (nat64) query; + stableMap1Remove: (nat16) -> (opt vec nat8); + stableMap1Values: () -> (vec vec nat8) query; + stableMap2ContainsKey: (nat32) -> (bool) query; + stableMap2Get: (nat32) -> (opt nat) query; + stableMap2Insert: (nat32, nat) -> (opt nat); + stableMap2IsEmpty: () -> (bool) query; + stableMap2Items: () -> (vec record {nat32; nat}) query; + stableMap2Keys: () -> (vec nat32) query; + stableMap2Len: () -> (nat64) query; + stableMap2Remove: (nat32) -> (opt nat); + stableMap2Values: () -> (vec nat) query; + stableMap3ContainsKey: (rec_0) -> (bool) query; + stableMap3Get: (rec_1) -> (opt int) query; + stableMap3Insert: (rec_2, int) -> (opt int); + stableMap3IsEmpty: () -> (bool) query; + stableMap3Items: () -> (vec record {rec_3; int}) query; + stableMap3Keys: () -> (vec rec_4) query; + stableMap3Len: () -> (nat64) query; + stableMap3Remove: (rec_5) -> (opt int); + stableMap3Values: () -> (vec int) query; + stableMap4ContainsKey: (rec_6) -> (bool) query; + stableMap4Get: (rec_8) -> (opt float32) query; + stableMap4Insert: (rec_10, float32) -> (opt float32); + stableMap4IsEmpty: () -> (bool) query; + stableMap4Items: () -> (vec record {rec_12; float32}) query; + stableMap4Keys: () -> (vec rec_14) query; + stableMap4Len: () -> (nat64) query; + stableMap4Remove: (rec_16) -> (opt float32); + stableMap4Values: () -> (vec float32) query; +} diff --git a/examples/stable_structures/src/canister1/index.ts b/examples/stable_structures/src/canister1/index.ts index 164a9a253d..44516c0fcc 100644 --- a/examples/stable_structures/src/canister1/index.ts +++ b/examples/stable_structures/src/canister1/index.ts @@ -1,55 +1,257 @@ -export { - stableMap0ContainsKey, - stableMap0Get, - stableMap0Insert, - stableMap0IsEmpty, - stableMap0Items, - stableMap0Keys, - stableMap0Len, - stableMap0Remove, - stableMap0Values -} from './stable_map_00'; -export { - stableMap1ContainsKey, - stableMap1Get, - stableMap1Insert, - stableMap1IsEmpty, - stableMap1Items, - stableMap1Keys, - stableMap1Len, - stableMap1Remove, - stableMap1Values -} from './stable_map_01'; -export { - stableMap2ContainsKey, - stableMap2Get, - stableMap2Insert, - stableMap2IsEmpty, - stableMap2Items, - stableMap2Keys, - stableMap2Len, - stableMap2Remove, - stableMap2Values -} from './stable_map_02'; -export { - stableMap3ContainsKey, - stableMap3Get, - stableMap3Insert, - stableMap3IsEmpty, - stableMap3Items, - stableMap3Keys, - stableMap3Len, - stableMap3Remove, - stableMap3Values -} from './stable_map_03'; -export { - stableMap4ContainsKey, - stableMap4Get, - stableMap4Insert, - stableMap4IsEmpty, - stableMap4Items, - stableMap4Keys, - stableMap4Len, - stableMap4Remove, - stableMap4Values -} from './stable_map_04'; +import { + blob, + bool, + float32, + int, + nat, + nat8, + nat16, + nat32, + nat64, + Opt, + query, + Service, + StableBTreeMap, + text, + Tuple, + update, + Vec +} from 'azle'; +import { Reaction, User } from '../types'; + +export default class extends Service { + stableMap0 = new StableBTreeMap(nat8, text, 0); + + @query([nat8], bool) + stableMap0ContainsKey(key: nat8): bool { + return this.stableMap0.containsKey(key); + } + + @query([nat8], Opt(text)) + stableMap0Get(key: nat8): Opt { + return this.stableMap0.get(key); + } + + @update([nat8, text], Opt(text)) + stableMap0Insert(key: nat8, value: text): Opt { + return this.stableMap0.insert(key, value); + } + + @query([], bool) + stableMap0IsEmpty(): bool { + return this.stableMap0.isEmpty(); + } + + @query([], Vec(Tuple(nat8, text))) + stableMap0Items(): Vec> { + return this.stableMap0.items(); + } + + @query([], Vec(nat8)) + stableMap0Keys(): Vec { + return this.stableMap0.keys(); + } + + @query([], nat64) + stableMap0Len(): nat64 { + return this.stableMap0.len(); + } + + @update([nat8], Opt(text)) + stableMap0Remove(key: nat8): Opt { + return this.stableMap0.remove(key); + } + + @query([], Vec(text)) + stableMap0Values(): Vec { + return this.stableMap0.values(); + } + + stableMap1 = new StableBTreeMap(nat16, blob, 1); + + @query([nat16], bool) + stableMap1ContainsKey(key: nat16): bool { + return this.stableMap1.containsKey(key); + } + + @query([nat16], Opt(blob)) + stableMap1Get(key: nat16): Opt { + return this.stableMap1.get(key); + } + + @update([nat16, blob], Opt(blob)) + stableMap1Insert(key: nat16, value: blob): Opt { + return this.stableMap1.insert(key, value); + } + + @query([], bool) + stableMap1IsEmpty(): bool { + return this.stableMap1.isEmpty(); + } + + @query([], Vec(Tuple(nat16, blob))) + stableMap1Items(): Vec> { + return this.stableMap1.items(); + } + + @query([], Vec(nat16)) + stableMap1Keys(): Vec { + return this.stableMap1.keys(); + } + + @query([], nat64) + stableMap1Len(): nat64 { + return this.stableMap1.len(); + } + + @update([nat16], Opt(blob)) + stableMap1Remove(key: nat16): Opt { + return this.stableMap1.remove(key); + } + + @query([], Vec(blob)) + stableMap1Values(): Vec { + return this.stableMap1.values(); + } + + stableMap2 = new StableBTreeMap(nat32, nat, 2); + + @query([nat32], bool) + stableMap2ContainsKey(key: nat32): bool { + return this.stableMap2.containsKey(key); + } + + @query([nat32], Opt(nat)) + stableMap2Get(key: nat32): Opt { + return this.stableMap2.get(key); + } + + @update([nat32, nat], Opt(nat)) + stableMap2Insert(key: nat32, value: nat): Opt { + return this.stableMap2.insert(key, value); + } + + @query([], bool) + stableMap2IsEmpty(): bool { + return this.stableMap2.isEmpty(); + } + + @query([], Vec(Tuple(nat32, nat))) + stableMap2Items(): Vec> { + return this.stableMap2.items(); + } + + @query([], Vec(nat32)) + stableMap2Keys(): Vec { + return this.stableMap2.keys(); + } + + @query([], nat64) + stableMap2Len(): nat64 { + return this.stableMap2.len(); + } + + @update([nat32], Opt(nat)) + stableMap2Remove(key: nat32): Opt { + return this.stableMap2.remove(key); + } + + @query([], Vec(nat)) + stableMap2Values(): Vec { + return this.stableMap2.values(); + } + + stableMap3 = new StableBTreeMap(Reaction as any, int, 3); + + @query([Reaction], bool) + stableMap3ContainsKey(key: Reaction): bool { + return this.stableMap3.containsKey(key); + } + + @query([Reaction], Opt(int)) + stableMap3Get(key: Reaction): Opt { + return this.stableMap3.get(key); + } + + @update([Reaction, int], Opt(int)) + stableMap3Insert(key: Reaction, value: int): Opt { + return this.stableMap3.insert(key, value); + } + + @query([], bool) + stableMap3IsEmpty(): bool { + return this.stableMap3.isEmpty(); + } + + @query([], Vec(Tuple(Reaction, int))) + stableMap3Items(): Vec> { + return this.stableMap3.items(); + } + + @query([], Vec(Reaction)) + stableMap3Keys(): Vec { + return this.stableMap3.keys(); + } + + @query([], nat64) + stableMap3Len(): nat64 { + return this.stableMap3.len(); + } + + @update([Reaction], Opt(int)) + stableMap3Remove(key: Reaction): Opt { + return this.stableMap3.remove(key); + } + + @query([], Vec(int)) + stableMap3Values(): Vec { + return this.stableMap3.values(); + } + + stableMap4 = new StableBTreeMap(User as any, float32, 4); + + @query([User], bool) + stableMap4ContainsKey(key: User): bool { + return this.stableMap4.containsKey(key); + } + + @query([User], Opt(float32)) + stableMap4Get(key: User): Opt { + return this.stableMap4.get(key); + } + + @update([User, float32], Opt(float32)) + stableMap4Insert(key: User, value: float32): Opt { + return this.stableMap4.insert(key, value); + } + + @query([], bool) + stableMap4IsEmpty(): bool { + return this.stableMap4.isEmpty(); + } + + @query([], Vec(Tuple(User, float32))) + stableMap4Items(): Vec> { + return this.stableMap4.items(); + } + + @query([], Vec(User)) + stableMap4Keys(): Vec { + return this.stableMap4.keys(); + } + + @query([], nat64) + stableMap4Len(): nat64 { + return this.stableMap4.len(); + } + + @update([User], Opt(float32)) + stableMap4Remove(key: User): Opt { + return this.stableMap4.remove(key); + } + + @query([], Vec(float32)) + stableMap4Values(): Vec { + return this.stableMap4.values(); + } +} diff --git a/examples/stable_structures/src/canister1/stable_map_00.ts b/examples/stable_structures/src/canister1/stable_map_00.ts deleted file mode 100644 index 9eabffa6a0..0000000000 --- a/examples/stable_structures/src/canister1/stable_map_00.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { - nat64, - nat8, - Opt, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; - -let stableMap0 = new StableBTreeMap(0, 100, 100); - -$query; -export function stableMap0ContainsKey(key: nat8): boolean { - return stableMap0.containsKey(key); -} - -$query; -export function stableMap0Get(key: nat8): Opt { - return stableMap0.get(key); -} - -$update; -export function stableMap0Insert(key: nat8, value: string): Opt { - return stableMap0.insert(key, value); -} - -$query; -export function stableMap0IsEmpty(): boolean { - return stableMap0.isEmpty(); -} - -$query; -export function stableMap0Items(): Vec> { - return stableMap0.items(); -} - -$query; -export function stableMap0Keys(): Vec { - return stableMap0.keys(); -} - -$query; -export function stableMap0Len(): nat64 { - return stableMap0.len(); -} - -$update; -export function stableMap0Remove(key: nat8): Opt { - return stableMap0.remove(key); -} - -$query; -export function stableMap0Values(): Vec { - return stableMap0.values(); -} diff --git a/examples/stable_structures/src/canister1/stable_map_01.ts b/examples/stable_structures/src/canister1/stable_map_01.ts deleted file mode 100644 index 8c836a19cd..0000000000 --- a/examples/stable_structures/src/canister1/stable_map_01.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { - blob, - nat16, - nat64, - Opt, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; - -let stableMap1 = new StableBTreeMap(1, 100, 1_000); - -$query; -export function stableMap1ContainsKey(key: nat16): boolean { - return stableMap1.containsKey(key); -} - -$query; -export function stableMap1Get(key: nat16): Opt { - return stableMap1.get(key); -} - -$update; -export function stableMap1Insert(key: nat16, value: blob): Opt { - return stableMap1.insert(key, value); -} - -$query; -export function stableMap1IsEmpty(): boolean { - return stableMap1.isEmpty(); -} - -$query; -export function stableMap1Items(): Vec> { - return stableMap1.items(); -} - -$query; -export function stableMap1Keys(): Vec { - return stableMap1.keys(); -} - -$query; -export function stableMap1Len(): nat64 { - return stableMap1.len(); -} - -$update; -export function stableMap1Remove(key: nat16): Opt { - return stableMap1.remove(key); -} - -$query; -export function stableMap1Values(): Vec { - return stableMap1.values(); -} diff --git a/examples/stable_structures/src/canister1/stable_map_02.ts b/examples/stable_structures/src/canister1/stable_map_02.ts deleted file mode 100644 index 004da88e52..0000000000 --- a/examples/stable_structures/src/canister1/stable_map_02.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { - nat, - nat32, - nat64, - Opt, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; - -let stableMap2 = new StableBTreeMap(2, 100, 1_000); - -$query; -export function stableMap2ContainsKey(key: nat32): boolean { - return stableMap2.containsKey(key); -} - -$query; -export function stableMap2Get(key: nat32): Opt { - return stableMap2.get(key); -} - -$update; -export function stableMap2Insert(key: nat32, value: nat): Opt { - return stableMap2.insert(key, value); -} - -$query; -export function stableMap2IsEmpty(): boolean { - return stableMap2.isEmpty(); -} - -$query; -export function stableMap2Items(): Vec> { - return stableMap2.items(); -} - -$query; -export function stableMap2Keys(): Vec { - return stableMap2.keys(); -} - -$query; -export function stableMap2Len(): nat64 { - return stableMap2.len(); -} - -$update; -export function stableMap2Remove(key: nat32): Opt { - return stableMap2.remove(key); -} - -$query; -export function stableMap2Values(): Vec { - return stableMap2.values(); -} diff --git a/examples/stable_structures/src/canister1/stable_map_03.ts b/examples/stable_structures/src/canister1/stable_map_03.ts deleted file mode 100644 index 01f89b36ae..0000000000 --- a/examples/stable_structures/src/canister1/stable_map_03.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { - int, - nat64, - Opt, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; -import { Reaction } from '../types'; - -let stableMap3 = new StableBTreeMap(3, 100, 1_000); - -$query; -export function stableMap3ContainsKey(key: Reaction): boolean { - return stableMap3.containsKey(key); -} - -$query; -export function stableMap3Get(key: Reaction): Opt { - return stableMap3.get(key); -} - -$update; -export function stableMap3Insert(key: Reaction, value: int): Opt { - return stableMap3.insert(key, value); -} - -$query; -export function stableMap3IsEmpty(): boolean { - return stableMap3.isEmpty(); -} - -$query; -export function stableMap3Items(): Vec> { - return stableMap3.items(); -} - -$query; -export function stableMap3Keys(): Vec { - return stableMap3.keys(); -} - -$query; -export function stableMap3Len(): nat64 { - return stableMap3.len(); -} - -$update; -export function stableMap3Remove(key: Reaction): Opt { - return stableMap3.remove(key); -} - -$query; -export function stableMap3Values(): Vec { - return stableMap3.values(); -} diff --git a/examples/stable_structures/src/canister1/stable_map_04.ts b/examples/stable_structures/src/canister1/stable_map_04.ts deleted file mode 100644 index 5b6587332d..0000000000 --- a/examples/stable_structures/src/canister1/stable_map_04.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { - float32, - nat64, - Opt, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; -import { User } from '../types'; - -let stableMap4 = new StableBTreeMap(4, 100, 1_000); - -$query; -export function stableMap4ContainsKey(key: User): boolean { - return stableMap4.containsKey(key); -} - -$query; -export function stableMap4Get(key: User): Opt { - return stableMap4.get(key); -} - -$update; -export function stableMap4Insert(key: User, value: float32): Opt { - return stableMap4.insert(key, value); -} - -$query; -export function stableMap4IsEmpty(): boolean { - return stableMap4.isEmpty(); -} - -$query; -export function stableMap4Items(): Vec> { - return stableMap4.items(); -} - -$query; -export function stableMap4Keys(): Vec { - return stableMap4.keys(); -} - -$query; -export function stableMap4Len(): nat64 { - return stableMap4.len(); -} - -$update; -export function stableMap4Remove(key: User): Opt { - return stableMap4.remove(key); -} - -$query; -export function stableMap4Values(): Vec { - return stableMap4.values(); -} diff --git a/examples/stable_structures/src/canister2/index.did b/examples/stable_structures/src/canister2/index.did index e3598cb113..f5e72a536a 100644 --- a/examples/stable_structures/src/canister2/index.did +++ b/examples/stable_structures/src/canister2/index.did @@ -1,47 +1,47 @@ -service : () -> { - stableMap5ContainsKey : (opt text) -> (bool) query; - stableMap5Get : (opt text) -> (opt float64) query; - stableMap5Insert : (opt text, float64) -> (opt float64); - stableMap5IsEmpty : () -> (bool) query; - stableMap5Items : () -> (vec record { opt text; float64 }) query; - stableMap5Keys : () -> (vec opt text) query; - stableMap5Len : () -> (nat64) query; - stableMap5Remove : (opt text) -> (opt float64); - stableMap5Values : () -> (vec float64) query; - stableMap6ContainsKey : (vec nat64) -> (bool) query; - stableMap6Get : (vec nat64) -> (opt bool) query; - stableMap6Insert : (vec nat64, bool) -> (opt bool); - stableMap6IsEmpty : () -> (bool) query; - stableMap6Items : () -> (vec record { vec nat64; bool }) query; - stableMap6Keys : () -> (vec vec nat64) query; - stableMap6Len : () -> (nat64) query; - stableMap6Remove : (vec nat64) -> (opt bool); - stableMap6Values : () -> (vec bool) query; - stableMap7ContainsKey : (null) -> (bool) query; - stableMap7Get : (null) -> (opt null) query; - stableMap7Insert : (null, null) -> (opt null); - stableMap7IsEmpty : () -> (bool) query; - stableMap7Items : () -> (vec record { null; null }) query; - stableMap7Keys : () -> (vec null) query; - stableMap7Len : () -> (nat64) query; - stableMap7Remove : (null) -> (opt null); - stableMap7Values : () -> (vec null) query; - stableMap8ContainsKey : (bool) -> (bool) query; - stableMap8Get : (bool) -> (opt null) query; - stableMap8Insert : (bool, null) -> (opt null); - stableMap8IsEmpty : () -> (bool) query; - stableMap8Items : () -> (vec record { bool; null }) query; - stableMap8Keys : () -> (vec bool) query; - stableMap8Len : () -> (nat64) query; - stableMap8Remove : (bool) -> (opt null); - stableMap8Values : () -> (vec null) query; - stableMap9ContainsKey : (float64) -> (bool) query; - stableMap9Get : (float64) -> (opt vec text) query; - stableMap9Insert : (float64, vec text) -> (opt vec text); - stableMap9IsEmpty : () -> (bool) query; - stableMap9Items : () -> (vec record { float64; vec text }) query; - stableMap9Keys : () -> (vec float64) query; - stableMap9Len : () -> (nat64) query; - stableMap9Remove : (float64) -> (opt vec text); - stableMap9Values : () -> (vec vec text) query; -} \ No newline at end of file +service: () -> { + stableMap5ContainsKey: (opt text) -> (bool) query; + stableMap5Get: (opt text) -> (opt float64) query; + stableMap5Insert: (opt text, float64) -> (opt float64); + stableMap5IsEmpty: () -> (bool) query; + stableMap5Items: () -> (vec record {opt text; float64}) query; + stableMap5Keys: () -> (vec opt text) query; + stableMap5Len: () -> (nat64) query; + stableMap5Remove: (opt text) -> (opt float64); + stableMap5Values: () -> (vec float64) query; + stableMap6ContainsKey: (vec nat64) -> (bool) query; + stableMap6Get: (vec nat64) -> (opt bool) query; + stableMap6Insert: (vec nat64, bool) -> (opt bool); + stableMap6IsEmpty: () -> (bool) query; + stableMap6Items: () -> (vec record {vec nat64; bool}) query; + stableMap6Keys: () -> (vec vec nat64) query; + stableMap6Len: () -> (nat64) query; + stableMap6Remove: (vec nat64) -> (opt bool); + stableMap6Values: () -> (vec bool) query; + stableMap7ContainsKey: (null) -> (bool) query; + stableMap7Get: (null) -> (opt null) query; + stableMap7Insert: (null, null) -> (opt null); + stableMap7IsEmpty: () -> (bool) query; + stableMap7Items: () -> (vec record {null; null}) query; + stableMap7Keys: () -> (vec null) query; + stableMap7Len: () -> (nat64) query; + stableMap7Remove: (null) -> (opt null); + stableMap7Values: () -> (vec null) query; + stableMap8ContainsKey: (bool) -> (bool) query; + stableMap8Get: (bool) -> (opt null) query; + stableMap8Insert: (bool, null) -> (opt null); + stableMap8IsEmpty: () -> (bool) query; + stableMap8Items: () -> (vec record {bool; null}) query; + stableMap8Keys: () -> (vec bool) query; + stableMap8Len: () -> (nat64) query; + stableMap8Remove: (bool) -> (opt null); + stableMap8Values: () -> (vec null) query; + stableMap9ContainsKey: (float64) -> (bool) query; + stableMap9Get: (float64) -> (opt vec text) query; + stableMap9Insert: (float64, vec text) -> (opt vec text); + stableMap9IsEmpty: () -> (bool) query; + stableMap9Items: () -> (vec record {float64; vec text}) query; + stableMap9Keys: () -> (vec float64) query; + stableMap9Len: () -> (nat64) query; + stableMap9Remove: (float64) -> (opt vec text); + stableMap9Values: () -> (vec vec text) query; +} diff --git a/examples/stable_structures/src/canister2/index.ts b/examples/stable_structures/src/canister2/index.ts index bd31921ab1..e593d63c54 100644 --- a/examples/stable_structures/src/canister2/index.ts +++ b/examples/stable_structures/src/canister2/index.ts @@ -1,55 +1,263 @@ -export { - stableMap5ContainsKey, - stableMap5Get, - stableMap5Insert, - stableMap5IsEmpty, - stableMap5Items, - stableMap5Keys, - stableMap5Len, - stableMap5Remove, - stableMap5Values -} from './stable_map_05'; -export { - stableMap6ContainsKey, - stableMap6Get, - stableMap6Insert, - stableMap6IsEmpty, - stableMap6Items, - stableMap6Keys, - stableMap6Len, - stableMap6Remove, - stableMap6Values -} from './stable_map_06'; -export { - stableMap7ContainsKey, - stableMap7Get, - stableMap7Insert, - stableMap7IsEmpty, - stableMap7Items, - stableMap7Keys, - stableMap7Len, - stableMap7Remove, - stableMap7Values -} from './stable_map_07'; -export { - stableMap8ContainsKey, - stableMap8Get, - stableMap8Insert, - stableMap8IsEmpty, - stableMap8Items, - stableMap8Keys, - stableMap8Len, - stableMap8Remove, - stableMap8Values -} from './stable_map_08'; -export { - stableMap9ContainsKey, - stableMap9Get, - stableMap9Insert, - stableMap9IsEmpty, - stableMap9Items, - stableMap9Keys, - stableMap9Len, - stableMap9Remove, - stableMap9Values -} from './stable_map_09'; +import { + bool, + float64, + nat64, + Null, + Opt, + query, + update, + Service, + StableBTreeMap, + Vec, + text, + Tuple +} from 'azle'; + +export default class extends Service { + stableMap5 = new StableBTreeMap, float64>( + Opt(text) as any, + float64, + 5 + ); + + @query([Opt(text)], bool) + stableMap5ContainsKey(key: Opt): boolean { + return this.stableMap5.containsKey(key); + } + + @query([Opt(text)], Opt(float64)) + stableMap5Get(key: Opt): Opt { + return this.stableMap5.get(key); + } + + @update([Opt(text), float64], Opt(float64)) + stableMap5Insert(key: Opt, value: float64): Opt { + return this.stableMap5.insert(key, value); + } + + @query([], bool) + stableMap5IsEmpty(): bool { + return this.stableMap5.isEmpty(); + } + + @query([], Vec(Tuple(Opt(text), float64))) + stableMap5Items(): Vec, float64]>> { + return this.stableMap5.items(); + } + + @query([], Vec(Opt(text))) + stableMap5Keys(): Vec> { + return this.stableMap5.keys(); + } + + @query([], nat64) + stableMap5Len(): nat64 { + return this.stableMap5.len(); + } + + @update([Opt(text)], Opt(float64)) + stableMap5Remove(key: Opt): Opt { + return this.stableMap5.remove(key); + } + + @query([], Vec(float64)) + stableMap5Values(): Vec { + return this.stableMap5.values(); + } + + stableMap6 = new StableBTreeMap, bool>( + Vec(nat64) as any, + bool, + 6 + ); + + @query([Vec(nat64)], bool) + stableMap6ContainsKey(key: Vec): bool { + return this.stableMap6.containsKey(key); + } + + @query([Vec(nat64)], Opt(bool)) + stableMap6Get(key: Vec): Opt { + return this.stableMap6.get(key); + } + + @update([Vec(nat64), bool], Opt(bool)) + stableMap6Insert(key: Vec, value: bool): Opt { + return this.stableMap6.insert(key, value); + } + + @query([], bool) + stableMap6IsEmpty(): bool { + return this.stableMap6.isEmpty(); + } + + @query([], Vec(Tuple(Vec(nat64), bool))) + stableMap6Items(): Vec, bool]>> { + return this.stableMap6.items(); + } + + @query([], Vec(Vec(nat64))) + stableMap6Keys(): Vec> { + return this.stableMap6.keys(); + } + + @query([], nat64) + stableMap6Len(): nat64 { + return this.stableMap6.len(); + } + + @update([Vec(nat64)], Opt(bool)) + stableMap6Remove(key: Vec): Opt { + return this.stableMap6.remove(key); + } + + @query([], Vec(bool)) + stableMap6Values(): Vec { + return this.stableMap6.values(); + } + + stableMap7 = new StableBTreeMap(Null, Null, 7); + + @query([Null], bool) + stableMap7ContainsKey(key: Null): bool { + return this.stableMap7.containsKey(key); + } + + @query([Null], Opt(Null)) + stableMap7Get(key: Null): Opt { + return this.stableMap7.get(key); + } + + @update([Null, Null], Opt(Null)) + stableMap7Insert(key: Null, value: Null): Opt { + return this.stableMap7.insert(key, value); + } + + @query([], bool) + stableMap7IsEmpty(): bool { + return this.stableMap7.isEmpty(); + } + + @query([], Vec(Tuple(Null, Null))) + stableMap7Items(): Vec> { + return this.stableMap7.items(); + } + + @query([], Vec(Null)) + stableMap7Keys(): Vec { + return this.stableMap7.keys(); + } + + @query([], nat64) + stableMap7Len(): nat64 { + return this.stableMap7.len(); + } + + @update([Null], Opt(Null)) + stableMap7Remove(key: null): Opt { + return this.stableMap7.remove(key); + } + + @query([], Vec(Null)) + stableMap7Values(): Vec { + return this.stableMap7.values(); + } + + stableMap8 = new StableBTreeMap(bool, Null, 8); + + @query([bool], bool) + stableMap8ContainsKey(key: bool): bool { + return this.stableMap8.containsKey(key); + } + + @query([bool], Opt(Null)) + stableMap8Get(key: bool): Opt { + return this.stableMap8.get(key); + } + + @update([bool, Null], Opt(Null)) + stableMap8Insert(key: bool, value: Null): Opt { + return this.stableMap8.insert(key, value); + } + + @query([], bool) + stableMap8IsEmpty(): bool { + return this.stableMap8.isEmpty(); + } + + @query([], Vec(Tuple(bool, Null))) + stableMap8Items(): Vec> { + return this.stableMap8.items(); + } + + @query([], Vec(bool)) + stableMap8Keys(): Vec { + return this.stableMap8.keys(); + } + + @query([], nat64) + stableMap8Len(): nat64 { + return this.stableMap8.len(); + } + + @update([bool], Opt(Null)) + stableMap8Remove(key: bool): Opt { + return this.stableMap8.remove(key); + } + + @query([], Vec(Null)) + stableMap8Values(): Vec { + return this.stableMap8.values(); + } + + stableMap9 = new StableBTreeMap>( + float64, + Vec(text) as any, + 9 + ); + + @query([float64], bool) + stableMap9ContainsKey(key: float64): bool { + return this.stableMap9.containsKey(key); + } + + @query([float64], Opt(Vec(text))) + stableMap9Get(key: float64): Opt> { + return this.stableMap9.get(key); + } + + @update([float64, Vec(text)], Opt(Vec(text))) + stableMap9Insert(key: float64, value: Vec): Opt> { + return this.stableMap9.insert(key, value); + } + + @query([], bool) + stableMap9IsEmpty(): bool { + return this.stableMap9.isEmpty(); + } + + @query([], Vec(Tuple(float64, Vec(text)))) + stableMap9Items(): Vec]>> { + return this.stableMap9.items(); + } + + @query([], Vec(float64)) + stableMap9Keys(): Vec { + return this.stableMap9.keys(); + } + + @query([], nat64) + stableMap9Len(): nat64 { + return this.stableMap9.len(); + } + + @update([float64], Opt(Vec(text))) + stableMap9Remove(key: float64): Opt> { + return this.stableMap9.remove(key); + } + + @query([], Vec(Vec(text))) + stableMap9Values(): Vec> { + return this.stableMap9.values(); + } +} diff --git a/examples/stable_structures/src/canister2/stable_map_05.ts b/examples/stable_structures/src/canister2/stable_map_05.ts deleted file mode 100644 index c47cb3c860..0000000000 --- a/examples/stable_structures/src/canister2/stable_map_05.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { - float64, - nat64, - Opt, - $query, - StableBTreeMap, - $update, - Tuple, - Vec -} from 'azle'; - -let stableMap5 = new StableBTreeMap, float64>(5, 100, 1_000); - -$query; -export function stableMap5ContainsKey(key: Opt): boolean { - return stableMap5.containsKey(key); -} - -$query; -export function stableMap5Get(key: Opt): Opt { - return stableMap5.get(key); -} - -$update; -export function stableMap5Insert( - key: Opt, - value: float64 -): Opt { - return stableMap5.insert(key, value); -} - -$query; -export function stableMap5IsEmpty(): boolean { - return stableMap5.isEmpty(); -} - -$query; -export function stableMap5Items(): Vec, float64]>> { - return stableMap5.items(); -} - -$query; -export function stableMap5Keys(): Vec> { - return stableMap5.keys(); -} - -$query; -export function stableMap5Len(): nat64 { - return stableMap5.len(); -} - -$update; -export function stableMap5Remove(key: Opt): Opt { - return stableMap5.remove(key); -} - -$query; -export function stableMap5Values(): Vec { - return stableMap5.values(); -} diff --git a/examples/stable_structures/src/canister2/stable_map_06.ts b/examples/stable_structures/src/canister2/stable_map_06.ts deleted file mode 100644 index 50516f9ce0..0000000000 --- a/examples/stable_structures/src/canister2/stable_map_06.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { nat64, Opt, $query, StableBTreeMap, Tuple, $update, Vec } from 'azle'; - -let stableMap6 = new StableBTreeMap, boolean>(6, 100, 1_000); - -$query; -export function stableMap6ContainsKey(key: Vec): boolean { - return stableMap6.containsKey(key); -} - -$query; -export function stableMap6Get(key: Vec): Opt { - return stableMap6.get(key); -} - -$update; -export function stableMap6Insert( - key: Vec, - value: boolean -): Opt { - return stableMap6.insert(key, value); -} - -$query; -export function stableMap6IsEmpty(): boolean { - return stableMap6.isEmpty(); -} - -$query; -export function stableMap6Items(): Vec, boolean]>> { - return stableMap6.items(); -} - -$query; -export function stableMap6Keys(): Vec> { - return stableMap6.keys(); -} - -$query; -export function stableMap6Len(): nat64 { - return stableMap6.len(); -} - -$update; -export function stableMap6Remove(key: Vec): Opt { - return stableMap6.remove(key); -} - -$query; -export function stableMap6Values(): Vec { - return stableMap6.values(); -} diff --git a/examples/stable_structures/src/canister2/stable_map_07.ts b/examples/stable_structures/src/canister2/stable_map_07.ts deleted file mode 100644 index aa2947f96a..0000000000 --- a/examples/stable_structures/src/canister2/stable_map_07.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { nat64, Opt, $query, StableBTreeMap, Tuple, $update, Vec } from 'azle'; - -let stableMap7 = new StableBTreeMap(7, 100, 1_000); - -$query; -export function stableMap7ContainsKey(key: null): boolean { - return stableMap7.containsKey(key); -} - -$query; -export function stableMap7Get(key: null): Opt { - return stableMap7.get(key); -} - -$update; -export function stableMap7Insert(key: null, value: null): Opt { - return stableMap7.insert(key, value); -} - -$query; -export function stableMap7IsEmpty(): boolean { - return stableMap7.isEmpty(); -} - -$query; -export function stableMap7Items(): Vec> { - return stableMap7.items(); -} - -$query; -export function stableMap7Keys(): Vec { - return stableMap7.keys(); -} - -$query; -export function stableMap7Len(): nat64 { - return stableMap7.len(); -} - -$update; -export function stableMap7Remove(key: null): Opt { - return stableMap7.remove(key); -} - -$query; -export function stableMap7Values(): Vec { - return stableMap7.values(); -} diff --git a/examples/stable_structures/src/canister2/stable_map_08.ts b/examples/stable_structures/src/canister2/stable_map_08.ts deleted file mode 100644 index e255a21f59..0000000000 --- a/examples/stable_structures/src/canister2/stable_map_08.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { nat64, Opt, $query, StableBTreeMap, Tuple, $update, Vec } from 'azle'; - -let stableMap8 = new StableBTreeMap(8, 100, 1_000); - -$query; -export function stableMap8ContainsKey(key: boolean): boolean { - return stableMap8.containsKey(key); -} - -$query; -export function stableMap8Get(key: boolean): Opt { - return stableMap8.get(key); -} - -$update; -export function stableMap8Insert(key: boolean, value: null): Opt { - return stableMap8.insert(key, value); -} - -$query; -export function stableMap8IsEmpty(): boolean { - return stableMap8.isEmpty(); -} - -$query; -export function stableMap8Items(): Vec> { - return stableMap8.items(); -} - -$query; -export function stableMap8Keys(): Vec { - return stableMap8.keys(); -} - -$query; -export function stableMap8Len(): nat64 { - return stableMap8.len(); -} - -$update; -export function stableMap8Remove(key: boolean): Opt { - return stableMap8.remove(key); -} - -$query; -export function stableMap8Values(): Vec { - return stableMap8.values(); -} diff --git a/examples/stable_structures/src/canister2/stable_map_09.ts b/examples/stable_structures/src/canister2/stable_map_09.ts deleted file mode 100644 index ce7a5ab56c..0000000000 --- a/examples/stable_structures/src/canister2/stable_map_09.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { - float64, - nat64, - Opt, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; - -let stableMap9 = new StableBTreeMap>(9, 100, 1_000); - -$query; -export function stableMap9ContainsKey(key: float64): boolean { - return stableMap9.containsKey(key); -} - -$query; -export function stableMap9Get(key: float64): Opt> { - return stableMap9.get(key); -} - -$update; -export function stableMap9Insert( - key: float64, - value: Vec -): Opt> { - return stableMap9.insert(key, value); -} - -$query; -export function stableMap9IsEmpty(): boolean { - return stableMap9.isEmpty(); -} - -$query; -export function stableMap9Items(): Vec]>> { - return stableMap9.items(); -} - -$query; -export function stableMap9Keys(): Vec { - return stableMap9.keys(); -} - -$query; -export function stableMap9Len(): nat64 { - return stableMap9.len(); -} - -$update; -export function stableMap9Remove(key: float64): Opt> { - return stableMap9.remove(key); -} - -$query; -export function stableMap9Values(): Vec> { - return stableMap9.values(); -} diff --git a/examples/stable_structures/src/canister3/index.did b/examples/stable_structures/src/canister3/index.did index 7ca03ebce2..9a8e8cd443 100644 --- a/examples/stable_structures/src/canister3/index.did +++ b/examples/stable_structures/src/canister3/index.did @@ -1,41 +1,56 @@ -type BlogPost = record { title : text }; -type Reaction = variant { Sad; Happy }; -type User = record { username : text; posts : vec BlogPost }; -service : () -> { - stableMap10ContainsKey : (float32) -> (bool) query; - stableMap10Get : (float32) -> (opt opt bool) query; - stableMap10Insert : (float32, opt bool) -> (opt opt bool); - stableMap10IsEmpty : () -> (bool) query; - stableMap10Items : () -> (vec record { float32; opt bool }) query; - stableMap10Keys : () -> (vec float32) query; - stableMap10Len : () -> (nat64) query; - stableMap10Remove : (float32) -> (opt opt bool); - stableMap10Values : () -> (vec opt bool) query; - stableMap11ContainsKey : (nat) -> (bool) query; - stableMap11Get : (nat) -> (opt User) query; - stableMap11Insert : (nat, User) -> (opt User); - stableMap11IsEmpty : () -> (bool) query; - stableMap11Items : () -> (vec record { nat; User }) query; - stableMap11Keys : () -> (vec nat) query; - stableMap11Len : () -> (nat64) query; - stableMap11Remove : (nat) -> (opt User); - stableMap11Values : () -> (vec User) query; - stableMap12ContainsKey : (vec nat8) -> (bool) query; - stableMap12Get : (vec nat8) -> (opt Reaction) query; - stableMap12Insert : (vec nat8, Reaction) -> (opt Reaction); - stableMap12IsEmpty : () -> (bool) query; - stableMap12Items : () -> (vec record { vec nat8; Reaction }) query; - stableMap12Keys : () -> (vec vec nat8) query; - stableMap12Len : () -> (nat64) query; - stableMap12Remove : (vec nat8) -> (opt Reaction); - stableMap12Values : () -> (vec Reaction) query; - stableMap13ContainsKey : (text) -> (bool) query; - stableMap13Get : (text) -> (opt principal) query; - stableMap13Insert : (text, principal) -> (opt principal); - stableMap13IsEmpty : () -> (bool) query; - stableMap13Items : () -> (vec record { text; principal }) query; - stableMap13Keys : () -> (vec text) query; - stableMap13Len : () -> (nat64) query; - stableMap13Remove : (text) -> (opt principal); - stableMap13Values : () -> (vec principal) query; -} \ No newline at end of file +type rec_1 = record {title:text}; +type rec_0 = record {username:text; posts:vec rec_1}; +type rec_3 = record {title:text}; +type rec_2 = record {username:text; posts:vec rec_3}; +type rec_5 = record {title:text}; +type rec_4 = record {username:text; posts:vec rec_5}; +type rec_7 = record {title:text}; +type rec_6 = record {username:text; posts:vec rec_7}; +type rec_9 = record {title:text}; +type rec_8 = record {username:text; posts:vec rec_9}; +type rec_11 = record {title:text}; +type rec_10 = record {username:text; posts:vec rec_11}; +type rec_12 = variant {Sad; Happy}; +type rec_13 = variant {Sad; Happy}; +type rec_14 = variant {Sad; Happy}; +type rec_15 = variant {Sad; Happy}; +type rec_16 = variant {Sad; Happy}; +type rec_17 = variant {Sad; Happy}; +service: () -> { + stableMap10ContainsKey: (float32) -> (bool) query; + stableMap10Get: (float32) -> (opt opt bool) query; + stableMap10Insert: (float32, opt bool) -> (opt opt bool); + stableMap10IsEmpty: () -> (bool) query; + stableMap10Items: () -> (vec record {float32; opt bool}) query; + stableMap10Keys: () -> (vec float32) query; + stableMap10Len: () -> (nat64) query; + stableMap10Remove: (float32) -> (opt opt bool); + stableMap10Values: () -> (vec opt bool) query; + stableMap11ContainsKey: (nat) -> (bool) query; + stableMap11Get: (nat) -> (opt rec_0) query; + stableMap11Insert: (nat, rec_2) -> (opt rec_4); + stableMap11IsEmpty: () -> (bool) query; + stableMap11Items: () -> (vec record {nat; rec_6}) query; + stableMap11Keys: () -> (vec nat) query; + stableMap11Len: () -> (nat64) query; + stableMap11Remove: (nat) -> (opt rec_8); + stableMap11Values: () -> (vec rec_10) query; + stableMap12ContainsKey: (vec nat8) -> (bool) query; + stableMap12Get: (vec nat8) -> (opt rec_12) query; + stableMap12Insert: (vec nat8, rec_13) -> (opt rec_14); + stableMap12IsEmpty: () -> (bool) query; + stableMap12Items: () -> (vec record {vec nat8; rec_15}) query; + stableMap12Keys: () -> (vec vec nat8) query; + stableMap12Len: () -> (nat64) query; + stableMap12Remove: (vec nat8) -> (opt rec_16); + stableMap12Values: () -> (vec rec_17) query; + stableMap13ContainsKey: (text) -> (bool) query; + stableMap13Get: (text) -> (opt principal) query; + stableMap13Insert: (text, principal) -> (opt principal); + stableMap13IsEmpty: () -> (bool) query; + stableMap13Items: () -> (vec record {text; principal}) query; + stableMap13Keys: () -> (vec text) query; + stableMap13Len: () -> (nat64) query; + stableMap13Remove: (text) -> (opt principal); + stableMap13Values: () -> (vec principal) query; +} diff --git a/examples/stable_structures/src/canister3/index.ts b/examples/stable_structures/src/canister3/index.ts index f33bc856b2..b267aba9a4 100644 --- a/examples/stable_structures/src/canister3/index.ts +++ b/examples/stable_structures/src/canister3/index.ts @@ -1,44 +1,212 @@ -export { - stableMap10ContainsKey, - stableMap10Get, - stableMap10Insert, - stableMap10IsEmpty, - stableMap10Items, - stableMap10Keys, - stableMap10Len, - stableMap10Remove, - stableMap10Values -} from './stable_map_10'; -export { - stableMap11ContainsKey, - stableMap11Get, - stableMap11Insert, - stableMap11IsEmpty, - stableMap11Items, - stableMap11Keys, - stableMap11Len, - stableMap11Remove, - stableMap11Values -} from './stable_map_11'; -export { - stableMap12ContainsKey, - stableMap12Get, - stableMap12Insert, - stableMap12IsEmpty, - stableMap12Items, - stableMap12Keys, - stableMap12Len, - stableMap12Remove, - stableMap12Values -} from './stable_map_12'; -export { - stableMap13ContainsKey, - stableMap13Get, - stableMap13Insert, - stableMap13IsEmpty, - stableMap13Items, - stableMap13Keys, - stableMap13Len, - stableMap13Remove, - stableMap13Values -} from './stable_map_13'; +import { + blob, + bool, + float32, + nat, + nat64, + Opt, + principal, + Principal, + query, + Service, + StableBTreeMap, + text, + Tuple, + update, + Vec +} from 'azle'; +import { Reaction, User } from '../types'; + +export default class extends Service { + stableMap10 = new StableBTreeMap>( + float32, + Opt(bool) as any, + 10 + ); + + @query([float32], bool) + stableMap10ContainsKey(key: float32): bool { + return this.stableMap10.containsKey(key); + } + + @query([float32], Opt(Opt(bool))) + stableMap10Get(key: float32): Opt> { + return this.stableMap10.get(key); + } + + @update([float32, Opt(bool)], Opt(Opt(bool))) + stableMap10Insert(key: float32, value: Opt): Opt> { + return this.stableMap10.insert(key, value); + } + + @query([], bool) + stableMap10IsEmpty(): bool { + return this.stableMap10.isEmpty(); + } + + @query([], Vec(Tuple(float32, Opt(bool)))) + stableMap10Items(): Vec]>> { + return this.stableMap10.items(); + } + + @query([], Vec(float32)) + stableMap10Keys(): Vec { + return this.stableMap10.keys(); + } + + @query([], nat64) + stableMap10Len(): nat64 { + return this.stableMap10.len(); + } + + @update([float32], Opt(Opt(bool))) + stableMap10Remove(key: float32): Opt> { + return this.stableMap10.remove(key); + } + + @query([], Vec(Opt(bool))) + stableMap10Values(): Vec> { + return this.stableMap10.values(); + } + + stableMap11 = new StableBTreeMap(nat, User as any, 11); + + @query([nat], bool) + stableMap11ContainsKey(key: nat): bool { + return this.stableMap11.containsKey(key); + } + + @query([nat], Opt(User)) + stableMap11Get(key: nat): Opt { + return this.stableMap11.get(key); + } + + @update([nat, User], Opt(User)) + stableMap11Insert(key: nat, value: User): Opt { + return this.stableMap11.insert(key, value); + } + + @query([], bool) + stableMap11IsEmpty(): bool { + return this.stableMap11.isEmpty(); + } + + @query([], Vec(Tuple(nat, User))) + stableMap11Items(): Vec> { + return this.stableMap11.items(); + } + + @query([], Vec(nat)) + stableMap11Keys(): Vec { + return this.stableMap11.keys(); + } + + @query([], nat64) + stableMap11Len(): nat64 { + return this.stableMap11.len(); + } + + @update([nat], Opt(User)) + stableMap11Remove(key: nat): Opt { + return this.stableMap11.remove(key); + } + + @query([], Vec(User)) + stableMap11Values(): Vec { + return this.stableMap11.values(); + } + + stableMap12 = new StableBTreeMap(blob, Reaction as any, 12); + + @query([blob], bool) + stableMap12ContainsKey(key: blob): bool { + return this.stableMap12.containsKey(key); + } + + @query([blob], Opt(Reaction)) + stableMap12Get(key: blob): Opt { + return this.stableMap12.get(key); + } + + @update([blob, Reaction], Opt(Reaction)) + stableMap12Insert(key: blob, value: Reaction): Opt { + return this.stableMap12.insert(key, value); + } + + @query([], bool) + stableMap12IsEmpty(): bool { + return this.stableMap12.isEmpty(); + } + + @query([], Vec(Tuple(blob, Reaction))) + stableMap12Items(): Vec> { + return this.stableMap12.items(); + } + + @query([], Vec(blob)) + stableMap12Keys(): Vec { + return this.stableMap12.keys(); + } + + @query([], nat64) + stableMap12Len(): nat64 { + return this.stableMap12.len(); + } + + @update([blob], Opt(Reaction)) + stableMap12Remove(key: blob): Opt { + return this.stableMap12.remove(key); + } + + @query([], Vec(Reaction)) + stableMap12Values(): Vec { + return this.stableMap12.values(); + } + + stableMap13 = new StableBTreeMap(text, principal, 13); + + @query([text], bool) + stableMap13ContainsKey(key: text): bool { + return this.stableMap13.containsKey(key); + } + + @query([text], Opt(principal)) + stableMap13Get(key: text): Opt { + return this.stableMap13.get(key); + } + + @update([text, principal], Opt(principal)) + stableMap13Insert(key: text, value: Principal): Opt { + return this.stableMap13.insert(key, value); + } + + @query([], bool) + stableMap13IsEmpty(): bool { + return this.stableMap13.isEmpty(); + } + + @query([], Vec(Tuple(text, principal))) + stableMap13Items(): Vec> { + return this.stableMap13.items(); + } + + @query([], Vec(text)) + stableMap13Keys(): Vec { + return this.stableMap13.keys(); + } + + @query([], nat64) + stableMap13Len(): nat64 { + return this.stableMap13.len(); + } + + @update([text], Opt(principal)) + stableMap13Remove(key: text): Opt { + return this.stableMap13.remove(key); + } + + @query([], Vec(principal)) + stableMap13Values(): Vec { + return this.stableMap13.values(); + } +} diff --git a/examples/stable_structures/src/canister3/stable_map_10.ts b/examples/stable_structures/src/canister3/stable_map_10.ts deleted file mode 100644 index fe7e943b56..0000000000 --- a/examples/stable_structures/src/canister3/stable_map_10.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { - float32, - nat64, - Opt, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; - -let stableMap10 = new StableBTreeMap>(10, 100, 1_000); - -$query; -export function stableMap10ContainsKey(key: float32): boolean { - return stableMap10.containsKey(key); -} - -$query; -export function stableMap10Get(key: float32): Opt> { - return stableMap10.get(key); -} - -$update; -export function stableMap10Insert( - key: float32, - value: Opt -): Opt> { - return stableMap10.insert(key, value); -} - -$query; -export function stableMap10IsEmpty(): boolean { - return stableMap10.isEmpty(); -} - -$query; -export function stableMap10Items(): Vec]>> { - return stableMap10.items(); -} - -$query; -export function stableMap10Keys(): Vec { - return stableMap10.keys(); -} - -$query; -export function stableMap10Len(): nat64 { - return stableMap10.len(); -} - -$update; -export function stableMap10Remove(key: float32): Opt> { - return stableMap10.remove(key); -} - -$query; -export function stableMap10Values(): Vec> { - return stableMap10.values(); -} diff --git a/examples/stable_structures/src/canister3/stable_map_11.ts b/examples/stable_structures/src/canister3/stable_map_11.ts deleted file mode 100644 index 6e75dfd06b..0000000000 --- a/examples/stable_structures/src/canister3/stable_map_11.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { - nat, - nat64, - Opt, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; -import { User } from '../types'; - -let stableMap11 = new StableBTreeMap(11, 100, 1_000); - -$query; -export function stableMap11ContainsKey(key: nat): boolean { - return stableMap11.containsKey(key); -} - -$query; -export function stableMap11Get(key: nat): Opt { - return stableMap11.get(key); -} - -$update; -export function stableMap11Insert(key: nat, value: User): Opt { - return stableMap11.insert(key, value); -} - -$query; -export function stableMap11IsEmpty(): boolean { - return stableMap11.isEmpty(); -} - -$query; -export function stableMap11Items(): Vec> { - return stableMap11.items(); -} - -$query; -export function stableMap11Keys(): Vec { - return stableMap11.keys(); -} - -$query; -export function stableMap11Len(): nat64 { - return stableMap11.len(); -} - -$update; -export function stableMap11Remove(key: nat): Opt { - return stableMap11.remove(key); -} - -$query; -export function stableMap11Values(): Vec { - return stableMap11.values(); -} diff --git a/examples/stable_structures/src/canister3/stable_map_12.ts b/examples/stable_structures/src/canister3/stable_map_12.ts deleted file mode 100644 index 1ba01a1a47..0000000000 --- a/examples/stable_structures/src/canister3/stable_map_12.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { - blob, - nat64, - Opt, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; -import { Reaction } from '../types'; - -let stableMap12 = new StableBTreeMap(12, 100, 1_000); - -$query; -export function stableMap12ContainsKey(key: blob): boolean { - return stableMap12.containsKey(key); -} - -$query; -export function stableMap12Get(key: blob): Opt { - return stableMap12.get(key); -} - -$update; -export function stableMap12Insert(key: blob, value: Reaction): Opt { - return stableMap12.insert(key, value); -} - -$query; -export function stableMap12IsEmpty(): boolean { - return stableMap12.isEmpty(); -} - -$query; -export function stableMap12Items(): Vec> { - return stableMap12.items(); -} - -$query; -export function stableMap12Keys(): Vec { - return stableMap12.keys(); -} - -$query; -export function stableMap12Len(): nat64 { - return stableMap12.len(); -} - -$update; -export function stableMap12Remove(key: blob): Opt { - return stableMap12.remove(key); -} - -$query; -export function stableMap12Values(): Vec { - return stableMap12.values(); -} diff --git a/examples/stable_structures/src/canister3/stable_map_13.ts b/examples/stable_structures/src/canister3/stable_map_13.ts deleted file mode 100644 index 06fa2220bb..0000000000 --- a/examples/stable_structures/src/canister3/stable_map_13.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { - nat64, - Opt, - Principal, - $query, - StableBTreeMap, - Tuple, - $update, - Vec -} from 'azle'; - -let stableMap13 = new StableBTreeMap(13, 100, 1_000); - -$query; -export function stableMap13ContainsKey(key: string): boolean { - return stableMap13.containsKey(key); -} - -$query; -export function stableMap13Get(key: string): Opt { - return stableMap13.get(key); -} - -$update; -export function stableMap13Insert( - key: string, - value: Principal -): Opt { - return stableMap13.insert(key, value); -} - -$query; -export function stableMap13IsEmpty(): boolean { - return stableMap13.isEmpty(); -} - -$query; -export function stableMap13Items(): Vec> { - return stableMap13.items(); -} - -$query; -export function stableMap13Keys(): Vec { - return stableMap13.keys(); -} - -$query; -export function stableMap13Len(): nat64 { - return stableMap13.len(); -} - -$update; -export function stableMap13Remove(key: string): Opt { - return stableMap13.remove(key); -} - -$query; -export function stableMap13Values(): Vec { - return stableMap13.values(); -} diff --git a/examples/stable_structures/src/invalid_declarations.ts b/examples/stable_structures/src/invalid_declarations.ts deleted file mode 100644 index 68a1334e3a..0000000000 --- a/examples/stable_structures/src/invalid_declarations.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { StableBTreeMap } from 'azle'; - -let withoutTypeParams = new StableBTreeMap(0, 10, 100); -let withIncorrectTypeParams = new StableBTreeMap(0, 10, 100); -let withoutArgs = new StableBTreeMap(); -let withIncorrectArgs = new StableBTreeMap('withIncorrectArgs'); -const args = ['stableMap', 100, 1000]; -let withSpreadArgs = new StableBTreeMap(...args); -let withNonNumberLiteralMemoryId = new StableBTreeMap( - 'stableMap4', - 10, - 100 -); -let withOutOfRangeMemoryId = new StableBTreeMap(300, 10, 100); -let withFloatMemoryId = new StableBTreeMap(100.5, 10, 100); -let withLargeSecondParam = new StableBTreeMap( - 0, - 4_294_967_296, - 100 -); -let withLargeThirdParam = new StableBTreeMap( - 0, - 100, - 4_294_967_296 -); diff --git a/examples/stable_structures/src/types.ts b/examples/stable_structures/src/types.ts index 6918288f82..5658651566 100644 --- a/examples/stable_structures/src/types.ts +++ b/examples/stable_structures/src/types.ts @@ -1,15 +1,22 @@ -import { Record, Variant, Vec } from 'azle'; +import { candid, Null, Record, text, Variant, Vec } from 'azle'; -export type BlogPost = Record<{ - title: string; -}>; +export class BlogPost extends Record { + @candid(text) + title: text; +} -export type Reaction = Variant<{ - Happy: null; - Sad: null; -}>; +export class Reaction extends Variant { + @candid(Null) + Happy?: Null; -export type User = Record<{ - username: string; + @candid(Null) + Sad?: Null; +} + +export class User extends Record { + @candid(text) + username: text; + + @candid(Vec(BlogPost)) posts: Vec; -}>; +} diff --git a/examples/stable_structures/test/tests.ts b/examples/stable_structures/test/tests.ts index ae8a9260ea..59c9c5f934 100644 --- a/examples/stable_structures/test/tests.ts +++ b/examples/stable_structures/test/tests.ts @@ -1,10 +1,7 @@ -import { ok, Test } from 'azle/test'; +import { Test } from 'azle/test'; import { execSync } from 'child_process'; -import { - Reaction, - User, - _SERVICE as CANISTER1_SERVICE -} from './dfx_generated/canister1/canister1.did'; +import { _SERVICE as CANISTER1_SERVICE } from './dfx_generated/canister1/canister1.did'; +import { Reaction, User } from '../src/types'; import { _SERVICE as CANISTER2_SERVICE } from './dfx_generated/canister2/canister2.did'; import { _SERVICE as CANISTER3_SERVICE } from './dfx_generated/canister3/canister3.did'; import { ActorSubclass } from '@dfinity/agent'; @@ -194,11 +191,7 @@ export function getTests( }, ...postRedeployTests(stableStructuresCanister_1, 0, 4), ...postRedeployTests(stableStructuresCanister_2, 5, 9), - ...postRedeployTests(stableStructuresCanister_3, 10, 13), - ...insertErrorTests( - stableStructuresCanister_1, - stableStructuresCanister_3 - ) + ...postRedeployTests(stableStructuresCanister_3, 10, 13) ]; } @@ -479,60 +472,6 @@ function valuesIsLength( }); } -export function insertErrorTests( - canister1: ActorSubclass, - canister3: ActorSubclass -): Test[] { - return [ - { - name: 'insert() returns a KeyTooLarge error if the key is too large', - test: async () => { - try { - const keyOver_100Bytes = - '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'; - await canister3.stableMap13Insert( - keyOver_100Bytes, - Principal.fromText('aaaaa-aa') - ); - - return { - Ok: false - }; - } catch (error) { - return { - Ok: (error as Error).message.includes( - 'Key is too large' - ) - }; - } - } - }, - { - name: 'insert() returns a ValueTooLarge error if the value is too large', - test: async () => { - try { - const valueOver_100Bytes = - '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'; - const result = await canister1.stableMap0Insert( - 1, - valueOver_100Bytes - ); - - return { - Ok: false - }; - } catch (error) { - return { - Ok: (error as Error).message.includes( - 'Value is too large' - ) - }; - } - } - } - ]; -} - /** * Determines whether the provided value is an empty array * @param value the value to test. diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/mod.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/mod.rs index dbae185c98..bd311084fe 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/mod.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/mod.rs @@ -38,9 +38,14 @@ mod stable64_grow; mod stable64_read; mod stable64_size; mod stable64_write; +mod stable_b_tree_map_contains_key; mod stable_b_tree_map_get; mod stable_b_tree_map_init; mod stable_b_tree_map_insert; +mod stable_b_tree_map_is_empty; +mod stable_b_tree_map_items; +mod stable_b_tree_map_keys; +mod stable_b_tree_map_len; mod stable_b_tree_map_remove; mod stable_b_tree_map_values; mod stable_bytes; @@ -94,9 +99,14 @@ pub fn generate() -> TokenStream { let stable64_read = stable64_read::generate(); let stable64_size = stable64_size::generate(); let stable64_write = stable64_write::generate(); + let stable_b_tree_map_contains_key = stable_b_tree_map_contains_key::generate(); let stable_b_tree_map_get = stable_b_tree_map_get::generate(); let stable_b_tree_map_init = stable_b_tree_map_init::generate(); let stable_b_tree_map_insert = stable_b_tree_map_insert::generate(); + let stable_b_tree_map_is_empty = stable_b_tree_map_is_empty::generate(); + let stable_b_tree_map_items = stable_b_tree_map_items::generate(); + let stable_b_tree_map_keys = stable_b_tree_map_keys::generate(); + let stable_b_tree_map_len = stable_b_tree_map_len::generate(); let stable_b_tree_map_remove = stable_b_tree_map_remove::generate(); let stable_b_tree_map_values = stable_b_tree_map_values::generate(); let time = time::generate(); @@ -145,9 +155,14 @@ pub fn generate() -> TokenStream { #stable64_read #stable64_size #stable64_write + #stable_b_tree_map_contains_key #stable_b_tree_map_get #stable_b_tree_map_init #stable_b_tree_map_insert + #stable_b_tree_map_is_empty + #stable_b_tree_map_items + #stable_b_tree_map_keys + #stable_b_tree_map_len #stable_b_tree_map_remove #stable_b_tree_map_values #time @@ -197,9 +212,14 @@ pub fn generate() -> TokenStream { ic.set_property("stableRead", context.wrap_callback2(stable_read).unwrap()).unwrap(); ic.set_property("stableSize", context.wrap_callback2(stable_size).unwrap()).unwrap(); ic.set_property("stableWrite", context.wrap_callback2(stable_write).unwrap()).unwrap(); + ic.set_property("stableBTreeMapContainsKey", context.wrap_callback2(stable_b_tree_map_contains_key).unwrap()).unwrap(); ic.set_property("stableBTreeMapGet", context.wrap_callback2(stable_b_tree_map_get).unwrap()).unwrap(); ic.set_property("stableBTreeMapInit", context.wrap_callback2(stable_b_tree_map_init).unwrap()).unwrap(); ic.set_property("stableBTreeMapInsert", context.wrap_callback2(stable_b_tree_map_insert).unwrap()).unwrap(); + ic.set_property("stableBTreeMapIsEmpty", context.wrap_callback2(stable_b_tree_map_is_empty).unwrap()).unwrap(); + ic.set_property("stableBTreeMapItems", context.wrap_callback2(stable_b_tree_map_items).unwrap()).unwrap(); + ic.set_property("stableBTreeMapKeys", context.wrap_callback2(stable_b_tree_map_keys).unwrap()).unwrap(); + ic.set_property("stableBTreeMapLen", context.wrap_callback2(stable_b_tree_map_len).unwrap()).unwrap(); ic.set_property("stableBTreeMapRemove", context.wrap_callback2(stable_b_tree_map_remove).unwrap()).unwrap(); ic.set_property("stableBTreeMapValues", context.wrap_callback2(stable_b_tree_map_values).unwrap()).unwrap(); ic.set_property("time", context.wrap_callback2(time).unwrap()).unwrap(); diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_contains_key.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_contains_key.rs new file mode 100644 index 0000000000..b5ced532e2 --- /dev/null +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_contains_key.rs @@ -0,0 +1,27 @@ +use proc_macro2::TokenStream; +use quote::quote; + +pub fn generate() -> TokenStream { + quote! { + fn stable_b_tree_map_contains_key<'a>( + context: &'a JSContextRef, + _this: &CallbackArg, + args: &[CallbackArg], + ) -> Result, anyhow::Error> { + let memory_id_candid_bytes: Vec = args.get(0).expect("stable_b_tree_map_get argument 0 is undefined").to_js_value()?.try_into()?; + let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; + + let key: Vec = args.get(1).expect("stable_b_tree_map_get argument 1 is undefined").to_js_value()?.try_into()?; + + let result_js_value: JSValue = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { + let stable_b_tree_maps = stable_b_tree_maps.borrow(); + + stable_b_tree_maps[&memory_id].contains_key(&AzleStableBTreeMapKey { + candid_bytes: key + }) + }).into(); + + to_qjs_value(&context, &result_js_value) + } + } +} diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_get.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_get.rs index 131ff2142c..1d34562811 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_get.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_get.rs @@ -16,7 +16,7 @@ pub fn generate() -> TokenStream { let value_option = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let stable_b_tree_maps = stable_b_tree_maps.borrow(); - stable_b_tree_maps[memory_id as usize].get(&AzleStableBTreeMapKey { + stable_b_tree_maps[&memory_id].get(&AzleStableBTreeMapKey { candid_bytes: key }) }); diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_init.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_init.rs index dcf975d5d5..54d92ca88e 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_init.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_init.rs @@ -13,7 +13,8 @@ pub fn generate() -> TokenStream { STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let mut stable_b_tree_maps = stable_b_tree_maps.borrow_mut(); - stable_b_tree_maps.push( + stable_b_tree_maps.insert( + memory_id, StableBTreeMap::init_v2(MEMORY_MANAGER_REF_CELL.with(|m| m.borrow().get(MemoryId::new(memory_id))),) ); }); diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_insert.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_insert.rs index 6a369b46ec..b46d0e181b 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_insert.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_insert.rs @@ -17,14 +17,12 @@ pub fn generate() -> TokenStream { let value_option = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let mut stable_b_tree_maps = stable_b_tree_maps.borrow_mut(); - let result = stable_b_tree_maps[memory_id as usize].insert(AzleStableBTreeMapKey { + let result = stable_b_tree_maps.get_mut(&memory_id).unwrap().insert(AzleStableBTreeMapKey { candid_bytes: key }, AzleStableBTreeMapValue { candid_bytes: value }); - ic_cdk::println!("insert values: {:#?}", stable_b_tree_maps[memory_id as usize].len()); - result }); diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_is_empty.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_is_empty.rs new file mode 100644 index 0000000000..d94f127940 --- /dev/null +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_is_empty.rs @@ -0,0 +1,23 @@ +use proc_macro2::TokenStream; +use quote::quote; + +pub fn generate() -> TokenStream { + quote! { + fn stable_b_tree_map_is_empty<'a>( + context: &'a JSContextRef, + _this: &CallbackArg, + args: &[CallbackArg], + ) -> Result, anyhow::Error> { + let memory_id_candid_bytes: Vec = args.get(0).expect("stable_b_tree_map_get argument 0 is undefined").to_js_value()?.try_into()?; + let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; + + let result_js_value: JSValue = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { + let stable_b_tree_maps = stable_b_tree_maps.borrow(); + + stable_b_tree_maps[&memory_id].is_empty() + }).into(); + + to_qjs_value(&context, &result_js_value) + } + } +} diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_items.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_items.rs new file mode 100644 index 0000000000..3b84ab034c --- /dev/null +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_items.rs @@ -0,0 +1,33 @@ +use proc_macro2::TokenStream; +use quote::quote; + +pub fn generate() -> TokenStream { + quote! { + fn stable_b_tree_map_items<'a>( + context: &'a JSContextRef, + _this: &CallbackArg, + args: &[CallbackArg], + ) -> Result, anyhow::Error> { + let memory_id_candid_bytes: Vec = args.get(0).expect("stable_b_tree_map_get argument 0 is undefined").to_js_value()?.try_into()?; + let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; + + let items: Vec>> = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { + let stable_b_tree_maps = stable_b_tree_maps.borrow(); + + stable_b_tree_maps[&memory_id].iter().map(|(key, value)| vec![key.candid_bytes, value.candid_bytes]).collect() + }); + + let js_values: Vec> = items.into_iter().map(|items| { + let keys = items[0].clone(); + let values = items[1].clone(); + + vec![keys.into(), values.into()] + }).collect(); + let js_values: Vec = js_values.into_iter().map(|items| items.into()).collect(); + + let js_value: JSValue = js_values.into(); + + to_qjs_value(&context, &js_value) + } + } +} diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_keys.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_keys.rs new file mode 100644 index 0000000000..eca41ccffc --- /dev/null +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_keys.rs @@ -0,0 +1,27 @@ +use proc_macro2::TokenStream; +use quote::quote; + +pub fn generate() -> TokenStream { + quote! { + fn stable_b_tree_map_keys<'a>( + context: &'a JSContextRef, + _this: &CallbackArg, + args: &[CallbackArg], + ) -> Result, anyhow::Error> { + let memory_id_candid_bytes: Vec = args.get(0).expect("stable_b_tree_map_get argument 0 is undefined").to_js_value()?.try_into()?; + let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; + + let keys: Vec> = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { + let stable_b_tree_maps = stable_b_tree_maps.borrow(); + + stable_b_tree_maps[&memory_id].iter().map(|(key, _)| key.candid_bytes).collect() + }); + + let js_values: Vec = keys.into_iter().map(|key| key.into()).collect(); + + let js_value: JSValue = js_values.into(); + + to_qjs_value(&context, &js_value) + } + } +} diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_len.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_len.rs new file mode 100644 index 0000000000..fdff6f7957 --- /dev/null +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_len.rs @@ -0,0 +1,24 @@ +use proc_macro2::TokenStream; +use quote::quote; + +pub fn generate() -> TokenStream { + quote! { + fn stable_b_tree_map_len<'a>( + context: &'a JSContextRef, + _this: &CallbackArg, + args: &[CallbackArg], + ) -> Result, anyhow::Error> { + let memory_id_candid_bytes: Vec = args.get(0).expect("stable_b_tree_map_get argument 0 is undefined").to_js_value()?.try_into()?; + let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; + + let len = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { + let stable_b_tree_maps = stable_b_tree_maps.borrow(); + + stable_b_tree_maps[&memory_id].len() + }); + + let len_js_value: JSValue = candid::encode_one(len)?.into(); + to_qjs_value(&context, &len_js_value) + } + } +} diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_remove.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_remove.rs index 2a058006d9..64a469a0f0 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_remove.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_remove.rs @@ -16,7 +16,7 @@ pub fn generate() -> TokenStream { let value_option = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let mut stable_b_tree_maps = stable_b_tree_maps.borrow_mut(); - stable_b_tree_maps[memory_id as usize].remove(&AzleStableBTreeMapKey { + stable_b_tree_maps.get_mut(&memory_id).unwrap().remove(&AzleStableBTreeMapKey { candid_bytes: key }) }); diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_values.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_values.rs index 612ea02b6e..b0e31d5865 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_values.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/ic/stable_b_tree_map_values.rs @@ -14,7 +14,7 @@ pub fn generate() -> TokenStream { let values: Vec> = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let stable_b_tree_maps = stable_b_tree_maps.borrow(); - stable_b_tree_maps[memory_id as usize].iter().map(|(_, value)| value.candid_bytes).collect() + stable_b_tree_maps[&memory_id].iter().map(|(_, value)| value.candid_bytes).collect() }); let js_values: Vec = values.into_iter().map(|value| value.into()).collect(); diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs index 5c95a6fec3..fc1f339166 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs @@ -166,7 +166,7 @@ fn main() -> Result<(), String> { static MEMORY_MANAGER_REF_CELL: RefCell> = RefCell::new(MemoryManager::init(DefaultMemoryImpl::default())); - static STABLE_B_TREE_MAPS: RefCell> = RefCell::new(vec![]); + static STABLE_B_TREE_MAPS: RefCell> = RefCell::new(BTreeMap::new()); } #[ic_cdk_macros::init] diff --git a/src/lib_new/stable_b_tree_map.ts b/src/lib_new/stable_b_tree_map.ts index 9bfb527efb..ce80e54b25 100644 --- a/src/lib_new/stable_b_tree_map.ts +++ b/src/lib_new/stable_b_tree_map.ts @@ -1,4 +1,4 @@ -import { IDL, nat8, Opt, Vec } from './index'; +import { IDL, nat8, nat64, Opt } from './index'; import { CandidClass, toCandidClass } from './utils'; // TODO something like this is how we would do the inference @@ -24,6 +24,26 @@ export class StableBTreeMap { (globalThis as any)._azleIc.stableBTreeMapInit(candidEncodedMemoryId); } + /** + * Checks if the given key exists in the map. + * @param key the key to check. + * @returns `true` if the key exists in the map, `false` otherwise. + */ + containsKey(key: Key): boolean { + const candidEncodedMemoryId = new Uint8Array( + IDL.encode([IDL.Nat8], [this.memoryId]) + ).buffer; + + const candidEncodedKey = new Uint8Array( + IDL.encode([this.keyIdl as any], [key]) + ).buffer; + + return (globalThis as any)._azleIc.stableBTreeMapContainsKey( + candidEncodedMemoryId, + candidEncodedKey + ); + } + /** * Retrieves the value stored at the provided key. * @param key the location from which to retrieve. @@ -93,6 +113,77 @@ export class StableBTreeMap { } } + /** + * Checks if the map is empty. + * @returns `true` if the map contains no elements, `false` otherwise. + */ + isEmpty(): boolean { + const candidEncodedMemoryId = new Uint8Array( + IDL.encode([IDL.Nat8], [this.memoryId]) + ).buffer; + + return (globalThis as any)._azleIc.stableBTreeMapIsEmpty( + candidEncodedMemoryId + ); + } + + /** + * Retrieves the items in the map in sorted order. + * @returns tuples representing key/value pairs. + */ + items(): [Key, Value][] { + const candidEncodedMemoryId = new Uint8Array( + IDL.encode([IDL.Nat8], [this.memoryId]) + ).buffer; + + const candidEncodedItems = ( + globalThis as any + )._azleIc.stableBTreeMapItems(candidEncodedMemoryId); + + // TODO too much copying + return candidEncodedItems.map((candidEncodedItem: any) => { + return [ + IDL.decode([this.keyIdl as any], candidEncodedItem[0])[0], + IDL.decode([this.valueIdl as any], candidEncodedItem[1])[0] + ]; + }); + } + + /** + * The keys for each element in the map in sorted order. + * @returns they keys in the map. + */ + keys(): Key[] { + const candidEncodedMemoryId = new Uint8Array( + IDL.encode([IDL.Nat8], [this.memoryId]) + ).buffer; + + const candidEncodedKeys = ( + globalThis as any + )._azleIc.stableBTreeMapKeys(candidEncodedMemoryId); + + // TODO too much copying + return candidEncodedKeys.map((candidEncodedKey: any) => { + return IDL.decode([this.keyIdl as any], candidEncodedKey)[0]; + }); + } + + /** + * Checks to see how many elements are in the map. + * @returns the number of elements in the map. + */ + len(): nat64 { + const candidEncodedMemoryId = new Uint8Array( + IDL.encode([IDL.Nat8], [this.memoryId]) + ).buffer; + + const candidEncodedLen = (globalThis as any)._azleIc.stableBTreeMapLen( + candidEncodedMemoryId + ); + + return IDL.decode([IDL.Nat64], candidEncodedLen)[0] as any; + } + /** * Removes a key from the map. * @param key the location from which to remove.