create-dubhe 0.0.19 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/template/{nextjs/sui-template/contracts/counter/sources/codegen/schemas/default → 101/sui-template/contracts/counter/sources/codegen}/dapp/metadata.move +6 -6
- package/template/101/sui-template/contracts/counter/sources/codegen/dapp/schema.move +112 -0
- package/template/{nextjs/sui-template/contracts/counter/sources/codegen/schemas/default → 101/sui-template/contracts/counter/sources/codegen}/dapp/system.move +17 -17
- package/template/101/sui-template/contracts/counter/sources/codegen/{events → data}/increment_event.move +0 -6
- package/template/101/sui-template/contracts/counter/sources/codegen/errors.move +16 -0
- package/template/101/sui-template/contracts/counter/sources/codegen/events.move +23 -0
- package/template/101/sui-template/contracts/counter/sources/codegen/genesis.move +20 -0
- package/template/101/sui-template/contracts/counter/sources/codegen/schema.move +54 -0
- package/template/101/sui-template/contracts/counter/sources/scripts/deploy_hook.move +12 -22
- package/template/101/sui-template/contracts/counter/sources/systems/counter.move +10 -13
- package/template/101/sui-template/contracts/counter/sources/tests/counter.move +8 -8
- package/template/101/sui-template/contracts/counter/sources/tests/init.move +1 -1
- package/template/101/sui-template/contracts/dubhe-framework/sources/events.move +33 -0
- package/template/101/sui-template/contracts/dubhe-framework/sources/storages/double_map.move +111 -153
- package/template/101/sui-template/contracts/dubhe-framework/sources/storages/map.move +87 -141
- package/template/{nextjs/sui-template/contracts/dubhe-framework/sources/storages/migration.move → 101/sui-template/contracts/dubhe-framework/sources/storages/storage.move} +3 -3
- package/template/101/sui-template/contracts/dubhe-framework/sources/storages/tests.move +82 -334
- package/template/101/sui-template/contracts/dubhe-framework/sources/storages/value.move +77 -83
- package/template/101/sui-template/dubhe.config.ts +1 -3
- package/template/101/sui-template/package.json +6 -4
- package/template/101/sui-template/src/chain/config.ts +2 -4
- package/template/101/sui-template/src/pages/home/index.tsx +4 -4
- package/template/contract/sui-template/contracts/dubhe-framework/sources/events.move +33 -0
- package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/double_map.move +111 -153
- package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/map.move +87 -141
- package/template/{101/sui-template/contracts/dubhe-framework/sources/storages/migration.move → contract/sui-template/contracts/dubhe-framework/sources/storages/storage.move} +3 -3
- package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/tests.move +82 -334
- package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/value.move +77 -83
- package/template/contract/sui-template/package.json +6 -4
- package/template/nextjs/sui-template/contracts/counter/Move.toml +1 -1
- package/template/{101/sui-template/contracts/counter/sources/codegen/schemas/default → nextjs/sui-template/contracts/counter/sources/codegen}/dapp/metadata.move +6 -6
- package/template/nextjs/sui-template/contracts/counter/sources/codegen/dapp/schema.move +112 -0
- package/template/{101/sui-template/contracts/counter/sources/codegen/schemas/default → nextjs/sui-template/contracts/counter/sources/codegen}/dapp/system.move +17 -17
- package/template/nextjs/sui-template/contracts/counter/sources/codegen/{events → data}/increment_event.move +0 -6
- package/template/nextjs/sui-template/contracts/counter/sources/codegen/errors.move +16 -0
- package/template/nextjs/sui-template/contracts/counter/sources/codegen/events.move +23 -0
- package/template/nextjs/sui-template/contracts/counter/sources/codegen/genesis.move +20 -0
- package/template/nextjs/sui-template/contracts/counter/sources/codegen/schema.move +54 -0
- package/template/nextjs/sui-template/contracts/counter/sources/scripts/deploy_hook.move +12 -22
- package/template/nextjs/sui-template/contracts/counter/sources/systems/counter.move +10 -13
- package/template/nextjs/sui-template/contracts/counter/sources/tests/counter.move +8 -8
- package/template/nextjs/sui-template/contracts/counter/sources/tests/init.move +1 -1
- package/template/nextjs/sui-template/contracts/dubhe-framework/sources/events.move +33 -0
- package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/double_map.move +111 -153
- package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/map.move +87 -141
- package/template/{contract/sui-template/contracts/dubhe-framework/sources/storages/migration.move → nextjs/sui-template/contracts/dubhe-framework/sources/storages/storage.move} +3 -3
- package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/tests.move +82 -334
- package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/value.move +77 -83
- package/template/nextjs/sui-template/dubhe.config.ts +1 -3
- package/template/nextjs/sui-template/package.json +7 -5
- package/template/nextjs/sui-template/src/chain/config.ts +3 -5
- package/template/nextjs/sui-template/src/pages/home/index.tsx +4 -4
- package/template/101/sui-template/contracts/counter/sources/codegen/errors/invalid_increment_error.move +0 -30
- package/template/101/sui-template/contracts/counter/sources/codegen/schemas/counter.move +0 -60
- package/template/101/sui-template/contracts/counter/sources/codegen/schemas/default/dapp/schema.move +0 -114
- package/template/nextjs/sui-template/contracts/counter/sources/codegen/errors/invalid_increment_error.move +0 -30
- package/template/nextjs/sui-template/contracts/counter/sources/codegen/schemas/counter.move +0 -60
- package/template/nextjs/sui-template/contracts/counter/sources/codegen/schemas/default/dapp/schema.move +0 -114
package/template/101/sui-template/contracts/dubhe-framework/sources/storages/double_map.move
CHANGED
|
@@ -1,153 +1,111 @@
|
|
|
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
|
-
let
|
|
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
|
-
public fun set<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &mut StorageDoubleMap<K1, K2,V>, key1: K1, key2: K2, value: V) {
|
|
113
|
-
let idx = self.get_idx_opt(key1, key2);
|
|
114
|
-
if (idx.is_some()) {
|
|
115
|
-
self.contents[idx.destroy_some()].value = value;
|
|
116
|
-
} else {
|
|
117
|
-
self.contents.push_back(Entry { key1, key2, value })
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Get a reference to the value bound to `key` in `self`.
|
|
122
|
-
// Aborts if `key` is not bound in `self`.
|
|
123
|
-
public fun get<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): V {
|
|
124
|
-
let idx = self.get_idx(key1, key2);
|
|
125
|
-
let entry = &self.contents[idx];
|
|
126
|
-
entry.value
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Safely try borrow a value bound to `key` in `self`.
|
|
130
|
-
// Return Some(V) if the value exists, None otherwise.
|
|
131
|
-
// Only works for a "copyable" value as references cannot be stored in `vector`.
|
|
132
|
-
public fun try_get<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): Option<V> {
|
|
133
|
-
if (self.contains_key(key1, key2)) {
|
|
134
|
-
option::some(get(self, key1, key2))
|
|
135
|
-
} else {
|
|
136
|
-
option::none()
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Remove the entry `key` |-> `value` from self.
|
|
141
|
-
public fun remove<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &mut StorageDoubleMap<K1, K2,V>, key1: K1, key2: K2) {
|
|
142
|
-
let idx = self.get_idx_opt(key1, key2);
|
|
143
|
-
if (idx.is_some()) {
|
|
144
|
-
self.contents.remove(idx.destroy_some());
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// Returns a list of values in the map.
|
|
149
|
-
// Do not assume any particular ordering.
|
|
150
|
-
public fun values<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &StorageDoubleMap<K1, K2, V>): vector<V> {
|
|
151
|
-
self.contents.map!(|entry| entry.value)
|
|
152
|
-
}
|
|
153
|
-
}
|
|
1
|
+
module dubhe::storage_double_map;
|
|
2
|
+
|
|
3
|
+
use std::ascii::{String, string};
|
|
4
|
+
use sui::dynamic_field as field;
|
|
5
|
+
use dubhe::storage_event;
|
|
6
|
+
use std::option::some;
|
|
7
|
+
|
|
8
|
+
// An entry in the map
|
|
9
|
+
public struct Entry<K1: copy + drop + store, K2: copy + drop + store> has copy, drop, store {
|
|
10
|
+
key1: K1,
|
|
11
|
+
key2: K2,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public struct StorageDoubleMap<phantom K1: copy + drop + store, phantom K2: copy + drop + store, phantom V: copy + drop + store> has key, store {
|
|
15
|
+
/// the ID of this Storage
|
|
16
|
+
id: UID,
|
|
17
|
+
// the name of the Storage
|
|
18
|
+
name: String,
|
|
19
|
+
/// the number of key-value pairs in the Storage
|
|
20
|
+
size: u64,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// Creates a new, empty table
|
|
24
|
+
public fun new<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(name: vector<u8>, ctx: &mut TxContext): StorageDoubleMap<K1, K2, V> {
|
|
25
|
+
StorageDoubleMap {
|
|
26
|
+
id: object::new(ctx),
|
|
27
|
+
name: string(name),
|
|
28
|
+
size: 0,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/// Adds a key-value pair to the table `table: &mut Table<K, V>`
|
|
33
|
+
public fun set<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(table: &mut StorageDoubleMap<K1, K2, V>, k1: K1, k2: K2, v: V) {
|
|
34
|
+
let k = Entry { key1: k1, key2: k2 };
|
|
35
|
+
if (table.contains(k1, k2)) {
|
|
36
|
+
field::remove<Entry<K1, K2>, V>(&mut table.id, k);
|
|
37
|
+
field::add(&mut table.id, k, v);
|
|
38
|
+
} else {
|
|
39
|
+
field::add(&mut table.id, k, v);
|
|
40
|
+
table.size = table.size + 1;
|
|
41
|
+
};
|
|
42
|
+
storage_event::emit_set_record<K1, K2, V>(table.name, some(k1), some(k2), some(v));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// Immutable borrows the value associated with the key in the table `table: &Table<K, V>`.
|
|
46
|
+
public fun try_get<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(table: &StorageDoubleMap<K1, K2, V>, k1: K1, k2: K2): Option<V> {
|
|
47
|
+
if (table.contains(k1, k2)) {
|
|
48
|
+
option::some(table[k1, k2])
|
|
49
|
+
} else {
|
|
50
|
+
option::none()
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
#[syntax(index)]
|
|
55
|
+
/// Immutable borrows the value associated with the key in the table `table: &Table<K, V>`.
|
|
56
|
+
/// Aborts with `sui::dynamic_field::EFieldDoesNotExist` if the table does not have an entry with
|
|
57
|
+
/// that key `k: K`.
|
|
58
|
+
public fun get<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(table: &StorageDoubleMap<K1, K2, V>, k1: K1, k2: K2): &V {
|
|
59
|
+
let k = Entry { key1: k1, key2: k2 };
|
|
60
|
+
field::borrow(&table.id, k)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/// Removes the key-value pair in the table `table: &mut Table<K, V>` and returns the value.
|
|
64
|
+
/// Aborts with `sui::dynamic_field::EFieldDoesNotExist` if the table does not have an entry with
|
|
65
|
+
/// that key `k: K`.
|
|
66
|
+
public fun remove<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(table: &mut StorageDoubleMap<K1, K2, V>, k1: K1, k2: K2) {
|
|
67
|
+
if (table.contains(k1, k2)) {
|
|
68
|
+
let k = Entry { key1: k1, key2: k2 };
|
|
69
|
+
field::remove<Entry<K1, K2>, V>(&mut table.id, k);
|
|
70
|
+
table.size = table.size - 1;
|
|
71
|
+
storage_event::emit_remove_record<K1, K2>(table.name, some(k1), some(k2));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// Removes the key-value pair in the table `table: &mut Table<K, V>` and returns the value.
|
|
76
|
+
/// Aborts with `sui::dynamic_field::EFieldDoesNotExist` if the table does not have an entry with
|
|
77
|
+
/// that key `k: K`.
|
|
78
|
+
public fun try_remove<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(table: &mut StorageDoubleMap<K1, K2, V>, k1: K1, k2: K2): Option<V> {
|
|
79
|
+
if (table.contains(k1, k2)) {
|
|
80
|
+
let k = Entry { key1: k1, key2: k2 };
|
|
81
|
+
let v = field::remove<Entry<K1, K2>, V>(&mut table.id, k);
|
|
82
|
+
table.size = table.size - 1;
|
|
83
|
+
storage_event::emit_remove_record<K1, K2>(table.name, some(k1), some(k2));
|
|
84
|
+
option::some(v)
|
|
85
|
+
} else {
|
|
86
|
+
option::none()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Returns true iff there is a value associated with the key `k: K` in table `table: &Table<K, V>`
|
|
91
|
+
public fun contains<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(table: &StorageDoubleMap<K1, K2, V>, k1: K1, k2: K2): bool {
|
|
92
|
+
let k = Entry { key1: k1, key2: k2 };
|
|
93
|
+
field::exists_with_type<Entry<K1, K2>, V>(&table.id, k)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// Returns the size of the table, the number of key-value pairs
|
|
97
|
+
public fun length<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(table: &StorageDoubleMap<K1, K2, V>): u64 {
|
|
98
|
+
table.size
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/// Returns true iff the table is empty (if `length` returns `0`)
|
|
102
|
+
public fun is_empty<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(table: &StorageDoubleMap<K1, K2, V>): bool {
|
|
103
|
+
table.size == 0
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// Drop a possibly non-empty table.
|
|
107
|
+
/// Usable only if the value type `V` has the `drop` ability
|
|
108
|
+
public fun drop<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(table: StorageDoubleMap<K1, K2, V>) {
|
|
109
|
+
let StorageDoubleMap { id, name: _, size: _ } = table;
|
|
110
|
+
id.delete()
|
|
111
|
+
}
|
|
@@ -1,156 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/// All operations are O(N) in the size of the map--the intention of this data structure is only to provide
|
|
19
|
-
/// the convenience of programming against a map API.
|
|
20
|
-
/// Large maps should use handwritten parent/child relationships instead.
|
|
21
|
-
/// Maps that need sorted iteration rather than insertion order iteration should also be handwritten.
|
|
22
|
-
public struct StorageMap<K: copy + drop + store, V: store> has store {
|
|
23
|
-
contents: vector<Entry<K, V>>,
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/// Create an empty `StorageMap`
|
|
27
|
-
public fun new<K: copy + drop + store, V: store>(): StorageMap<K, V> {
|
|
28
|
-
StorageMap { contents: vector[] }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/// Return true if `self` contains_key an entry for `key`, false otherwise
|
|
32
|
-
public fun contains_key<K: copy + drop + store, V: store>(self: &StorageMap<K, V>, key: K): bool {
|
|
33
|
-
get_idx_opt(self, key).is_some()
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/// Return the number of entries in `self`
|
|
37
|
-
public fun length<K: copy + drop + store, V: store>(self: &StorageMap<K,V>): u64 {
|
|
38
|
-
self.contents.length()
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/// Get a reference to the value bound to `key` in `self`.
|
|
42
|
-
/// Aborts if `key` is not bound in `self`.
|
|
43
|
-
public fun borrow<K: copy + drop + store, V: store>(self: &StorageMap<K,V>, key: K): &V {
|
|
44
|
-
let idx = self.get_idx(key);
|
|
45
|
-
let entry = &self.contents[idx];
|
|
46
|
-
&entry.value
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
/// Get a mutable reference to the value bound to `key` in `self`.
|
|
51
|
-
/// Aborts if `key` is not bound in `self`.
|
|
52
|
-
public fun borrow_mut<K: copy + drop + store, V: store>(self: &mut StorageMap<K,V>, key: K): &mut V {
|
|
53
|
-
let idx = self.get_idx(key);
|
|
54
|
-
let entry = &mut self.contents[idx];
|
|
55
|
-
&mut entry.value
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/// Remove the entry `key` |-> `value` from self. Aborts if `key` is not bound in `self`.
|
|
59
|
-
public fun take<K: copy + drop + store, V: store>(self: &mut StorageMap<K,V>, key: K): V {
|
|
60
|
-
let idx = self.get_idx(key);
|
|
61
|
-
let Entry { key, value } = self.contents.remove(idx);
|
|
62
|
-
value
|
|
63
|
-
}
|
|
1
|
+
module dubhe::storage_map;
|
|
2
|
+
|
|
3
|
+
use std::ascii::String;
|
|
4
|
+
use std::ascii::string;
|
|
5
|
+
use sui::dynamic_field as field;
|
|
6
|
+
use dubhe::storage_event;
|
|
7
|
+
use std::option::some;
|
|
8
|
+
use std::option::none;
|
|
9
|
+
|
|
10
|
+
public struct StorageMap<phantom K: copy + drop + store, phantom V: copy + drop + store> has key, store {
|
|
11
|
+
/// the ID of this Storage
|
|
12
|
+
id: UID,
|
|
13
|
+
// the name of the Storage
|
|
14
|
+
name: String,
|
|
15
|
+
/// the number of key-value pairs in the Storage
|
|
16
|
+
size: u64,
|
|
17
|
+
}
|
|
64
18
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
let key = $key;
|
|
72
|
-
$f(borrow_mut(self, key));
|
|
19
|
+
/// Creates a new, empty table
|
|
20
|
+
public fun new<K: copy + drop + store, V: copy + drop + store>(name: vector<u8>, ctx: &mut TxContext): StorageMap<K, V> {
|
|
21
|
+
StorageMap {
|
|
22
|
+
id: object::new(ctx),
|
|
23
|
+
name: string(name),
|
|
24
|
+
size: 0,
|
|
73
25
|
}
|
|
26
|
+
}
|
|
74
27
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
keys
|
|
87
|
-
}
|
|
28
|
+
/// Adds a key-value pair to the table `table: &mut Table<K, V>`
|
|
29
|
+
public fun set<K: copy + drop + store, V: copy + drop + store>(table: &mut StorageMap<K, V>, k: K, v: V) {
|
|
30
|
+
if (table.contains(k)) {
|
|
31
|
+
field::remove<K, V>(&mut table.id, k);
|
|
32
|
+
field::add(&mut table.id, k, v);
|
|
33
|
+
} else {
|
|
34
|
+
field::add(&mut table.id, k, v);
|
|
35
|
+
table.size = table.size + 1;
|
|
36
|
+
};
|
|
37
|
+
storage_event::emit_set_record<K, K, V>(table.name, some(k), none(), some(v));
|
|
38
|
+
}
|
|
88
39
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
40
|
+
/// Immutable borrows the value associated with the key in the table `table: &Table<K, V>`.
|
|
41
|
+
public fun try_get<K: copy + drop + store, V: copy + drop + store>(table: &StorageMap<K, V>, k: K): Option<V> {
|
|
42
|
+
if (table.contains(k)) {
|
|
43
|
+
option::some(table[k])
|
|
44
|
+
} else {
|
|
45
|
+
option::none()
|
|
93
46
|
}
|
|
47
|
+
}
|
|
94
48
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
49
|
+
#[syntax(index)]
|
|
50
|
+
/// Immutable borrows the value associated with the key in the table `table: &Table<K, V>`.
|
|
51
|
+
/// Aborts with `sui::dynamic_field::EFieldDoesNotExist` if the table does not have an entry with
|
|
52
|
+
/// that key `k: K`.
|
|
53
|
+
public fun get<K: copy + drop + store, V: copy + drop + store>(table: &StorageMap<K, V>, k: K): &V {
|
|
54
|
+
field::borrow(&table.id, k)
|
|
55
|
+
}
|
|
102
56
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
57
|
+
/// Removes the key-value pair in the table `table: &mut Table<K, V>` and returns the value.
|
|
58
|
+
/// Aborts with `sui::dynamic_field::EFieldDoesNotExist` if the table does not have an entry with
|
|
59
|
+
/// that key `k: K`.
|
|
60
|
+
public fun remove<K: copy + drop + store, V: copy + drop + store>(table: &mut StorageMap<K, V>, k: K) {
|
|
61
|
+
if (table.contains(k)) {
|
|
62
|
+
field::remove<K, V>(&mut table.id, k);
|
|
63
|
+
table.size = table.size - 1;
|
|
64
|
+
storage_event::emit_remove_record<K, K>(table.name, some(k), none());
|
|
108
65
|
}
|
|
66
|
+
}
|
|
109
67
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
68
|
+
/// Removes the key-value pair in the table `table: &mut Table<K, V>` and returns the value.
|
|
69
|
+
/// Aborts with `sui::dynamic_field::EFieldDoesNotExist` if the table does not have an entry with
|
|
70
|
+
/// that key `k: K`.
|
|
71
|
+
public fun try_remove<K: copy + drop + store, V: copy + drop + store>(table: &mut StorageMap<K, V>, k: K): Option<V> {
|
|
72
|
+
if (table.contains(k)) {
|
|
73
|
+
let v = field::remove(&mut table.id, k);
|
|
74
|
+
table.size = table.size - 1;
|
|
75
|
+
storage_event::emit_remove_record<K, K>(table.name, some(k), none());
|
|
76
|
+
option::some(v)
|
|
77
|
+
} else {
|
|
78
|
+
option::none()
|
|
122
79
|
}
|
|
80
|
+
}
|
|
123
81
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
let entry = &self.contents[idx];
|
|
129
|
-
entry.value
|
|
130
|
-
}
|
|
82
|
+
/// Returns true iff there is a value associated with the key `k: K` in table `table: &Table<K, V>`
|
|
83
|
+
public fun contains<K: copy + drop + store, V: copy + drop + store>(table: &StorageMap<K, V>, k: K): bool {
|
|
84
|
+
field::exists_with_type<K, V>(&table.id, k)
|
|
85
|
+
}
|
|
131
86
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (self.contains_key(key)) {
|
|
137
|
-
option::some(get(self, key))
|
|
138
|
-
} else {
|
|
139
|
-
option::none()
|
|
140
|
-
}
|
|
141
|
-
}
|
|
87
|
+
/// Returns the size of the table, the number of key-value pairs
|
|
88
|
+
public fun length<K: copy + drop + store, V: copy + drop + store>(table: &StorageMap<K, V>): u64 {
|
|
89
|
+
table.size
|
|
90
|
+
}
|
|
142
91
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
self.contents.remove(idx.destroy_some());
|
|
148
|
-
}
|
|
149
|
-
}
|
|
92
|
+
/// Returns true iff the table is empty (if `length` returns `0`)
|
|
93
|
+
public fun is_empty<K: copy + drop + store, V: copy + drop + store>(table: &StorageMap<K, V>): bool {
|
|
94
|
+
table.size == 0
|
|
95
|
+
}
|
|
150
96
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
97
|
+
/// Drop a possibly non-empty table.
|
|
98
|
+
/// Usable only if the value type `V` has the `drop` ability
|
|
99
|
+
public fun drop<K: copy + drop + store, V: copy + drop + store>(table: StorageMap<K, V>) {
|
|
100
|
+
let StorageMap { id, name: _, size: _ } = table;
|
|
101
|
+
id.delete()
|
|
156
102
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
module dubhe::
|
|
1
|
+
module dubhe::storage {
|
|
2
2
|
use sui::dynamic_field as df;
|
|
3
3
|
|
|
4
|
-
public fun add_field<StorageType: store>(uid: &mut UID, field_name: vector<u8>,
|
|
5
|
-
df::add(uid, field_name,
|
|
4
|
+
public fun add_field<StorageType: store>(uid: &mut UID, field_name: vector<u8>, storage_type: StorageType) {
|
|
5
|
+
df::add(uid, field_name, storage_type);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
public fun borrow_field<StorageType: store>(uid: &UID, field_name: vector<u8>): &StorageType {
|