-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample.mo
119 lines (108 loc) · 3.52 KB
/
example.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import Bucket "Bucket";
import Blob "mo:base/Blob";
import Text "mo:base/Text";
import Array "mo:base/Array";
import Result "mo:base/Result";
import Nat "mo:base/Nat";
import Debug "mo:base/Debug";
actor example{
type Error = Bucket.Error;
type S = {
text : Text;
bool : Bool
};
stable var bucket_entries : [(Text, [(Nat64, Nat)])] = [];
let bucket = Bucket.Bucket(true); // true : upgradable, false : unupgradable
public query func getBlob(key : Text) : async Result.Result<[Blob], Error>{
switch(bucket.get(key)){
case(#err(e)){ #err(e) };
case(#ok(blob)){
#ok(blob)
}
}
};
public query func get(key : Text) : async Result.Result<[S], Error>{
switch(bucket.get(key)){
case(#err(info)){ #err(info) };
case(#ok(data)){ #ok(deserialize(data)) };
};
};
public func put() : async Result.Result<(), Error>{
let key = "key";
let value_1 : S = {
text = "this is the first slice of value";
bool = true
};
let value_2 : S = {
text = "this is the second slice of value";
bool = false
};
switch(bucket.put(key, serialize(value_1))){
case(#err(e)){ return #err(e) };
case(_){};
};
// you can storage the two different value using the same key
switch(bucket.append(key, serialize(value_2))){
case(#err(e)){ return #err(e) };
case(_){};
};
#ok(())
};
public func putBlob() : async Result.Result<(), Error>{
let key = "key";
let value = Text.encodeUtf8("this is the value");
switch(bucket.put(key, value)){
case(#err(e)){ return #err(e) };
case(_){};
};
#ok(())
};
system func preupgrade(){
bucket_entries := bucket.preupgrade();
};
system func postupgrade(){
bucket.postupgrade(bucket_entries);
bucket_entries := [];
};
// you should encode the segment of the struct into nat8
// then you should merge them and transform the [Nat8] to Blob
private func serialize(s : S) : Blob{
let bool_nat8 = if(s.bool){
1 : Nat8
}else{ 0 : Nat8 };
let text_blob = Text.encodeUtf8(s.text);
let text_nat8 = Blob.toArray(text_blob);
let serialize_data = Array.append<Nat8>(text_nat8, [bool_nat8]);
Blob.fromArray(serialize_data)
};
private func deserialize(data : [Blob]) : [S] {
let res = Array.init<S>(data.size(), {
text = "";
bool = true;
});
var res_index = 0;
for(d in data.vals()){
let raw = Blob.toArray(d);
let bool = if(raw[Nat.sub(raw.size(), 1)] == 1){ true }else{ false };
let text = Array.init<Nat8>(Nat.sub(data.size(), 2), 0:Nat8);// the last byte is used to store the "bool"
var index = 0;
label l for(d in raw.vals()){
text[index] := d;
index += 1;
if(index == text.size()){ break l };
};
let t =
switch(Text.decodeUtf8(Blob.fromArray(Array.freeze<Nat8>(text)))){
case null { "" };
case(?te){ te };
};
res[res_index] :=
{
text = t;
bool = bool
};
res_index += 1;
};
Array.freeze(res)
};
}