Skip to content

Commit

Permalink
Make data structure stable
Browse files Browse the repository at this point in the history
  • Loading branch information
patnorris authored and patnorris committed Feb 25, 2024
1 parent b4fae8c commit 17b207e
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions backend/donation_tracker_canister/src/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ actor class DonationTracker(_donation_canister_id : Text) {
stable var donationsStable : [Donation] = [];

// Map each principal to a list of donation indices (DTIs)
private var donationsByPrincipal = HashMap.HashMap<Principal, Buffer.Buffer<DTI>>(0, Principal.equal, Principal.hash);
//TODO: stable var donationsByPrincipalStable : [(Principal, Buffer.Buffer<DTI>)] = [];
// Alternative: use [DTI] instead of Buffer.Buffer<DTI>; less efficient but straightforward to make stable
// Or: stable Buffer implementation?
private var donationsByPrincipal = HashMap.HashMap<Principal, [DTI]>(0, Principal.equal, Principal.hash);
stable var donationsByPrincipalStable : [(Principal, [DTI])] = []; // Alternative: Buffer.Buffer<DTI> instead of [DTI]; more efficient but less straightforward to make stable

// Store recipients and map each recipientId to the corresponding Recipient record
private var recipientsById = HashMap.HashMap<Types.RecipientId, Types.Recipient>(0, Text.equal, Text.hash);
Expand Down Expand Up @@ -222,12 +220,12 @@ actor class DonationTracker(_donation_canister_id : Text) {

// Update the map for the caller's principal
if (Principal.isAnonymous(msg.caller)) {} else {
let existingDonations = switch (donationsByPrincipal.get(msg.caller)) {
case (null) { Buffer.Buffer<DTI>(0) };
let existingDonations : [DTI] = switch (donationsByPrincipal.get(msg.caller)) {
case (null) { [] };
case (?ds) { ds };
};
let addDonationResult = existingDonations.add(newDti);
donationsByPrincipal.put(msg.caller, existingDonations);
let addDonationResult : [DTI] = Array.append<DTI>(existingDonations, [newDti]);
donationsByPrincipal.put(msg.caller, addDonationResult);
};

let associatedDonations = switch (donationsByTxId.get(donationInput.paymentTransactionId)) {
Expand Down Expand Up @@ -273,12 +271,12 @@ actor class DonationTracker(_donation_canister_id : Text) {
// No donations found
return #Ok({ donations = [] });
};
case (?dtiBuffer) {
case (?dtiArray) {
// Donations found for user
let dtis : [DTI] = Buffer.toArray(dtiBuffer);
let dtis : [DTI] = dtiArray;
// Iterate over dtis, get donation for each dti
// push to return array
let userDonations : Buffer.Buffer<Donation> = Buffer.Buffer<Donation>(dtiBuffer.capacity());
let userDonations : Buffer.Buffer<Donation> = Buffer.Buffer<Donation>(dtiArray.size());
for (i : Nat in dtis.keys()) {
userDonations.add(donations.get(dtis[i]));
};
Expand Down Expand Up @@ -587,7 +585,7 @@ actor class DonationTracker(_donation_canister_id : Text) {
system func preupgrade() {
// Copy the runtime state back into the stable variable before upgrade.
donationsStable := Buffer.toArray<Donation>(donations);
//TODO: donationsByPrincipalStable := Iter.toArray(donationsByPrincipal.entries());
donationsByPrincipalStable := Iter.toArray(donationsByPrincipal.entries());
recipientsByIdStable := Iter.toArray(recipientsById.entries());
studentsBySchoolStable := Iter.toArray(studentsBySchool.entries());
donationsByTxIdStable := Iter.toArray(donationsByTxId.entries());
Expand All @@ -599,8 +597,8 @@ actor class DonationTracker(_donation_canister_id : Text) {
// After upgrade, reload the runtime state from the stable variable.
donations := Buffer.fromArray<Donation>(donationsStable);
donationsStable := [];
//TODO: donationsByPrincipal := HashMap.fromIter(Iter.fromArray(donationsByPrincipalStable), donationsByPrincipalStable.size(), Text.equal, Text.hash);
//TODO: donationsByPrincipalStable := [];
donationsByPrincipal := HashMap.fromIter(Iter.fromArray(donationsByPrincipalStable), donationsByPrincipalStable.size(), Principal.equal, Principal.hash);
donationsByPrincipalStable := [];
recipientsById := HashMap.fromIter(Iter.fromArray(recipientsByIdStable), recipientsByIdStable.size(), Text.equal, Text.hash);
recipientsByIdStable := [];
studentsBySchool := HashMap.fromIter(Iter.fromArray(studentsBySchoolStable), studentsBySchoolStable.size(), Text.equal, Text.hash);
Expand Down

0 comments on commit 17b207e

Please sign in to comment.