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.
Files changed (59) hide show
  1. package/package.json +1 -1
  2. package/template/{nextjs/sui-template/contracts/counter/sources/codegen/schemas/default → 101/sui-template/contracts/counter/sources/codegen}/dapp/metadata.move +6 -6
  3. package/template/101/sui-template/contracts/counter/sources/codegen/dapp/schema.move +112 -0
  4. package/template/{nextjs/sui-template/contracts/counter/sources/codegen/schemas/default → 101/sui-template/contracts/counter/sources/codegen}/dapp/system.move +17 -17
  5. package/template/101/sui-template/contracts/counter/sources/codegen/{events → data}/increment_event.move +0 -6
  6. package/template/101/sui-template/contracts/counter/sources/codegen/errors.move +16 -0
  7. package/template/101/sui-template/contracts/counter/sources/codegen/events.move +23 -0
  8. package/template/101/sui-template/contracts/counter/sources/codegen/genesis.move +20 -0
  9. package/template/101/sui-template/contracts/counter/sources/codegen/schema.move +54 -0
  10. package/template/101/sui-template/contracts/counter/sources/scripts/deploy_hook.move +12 -22
  11. package/template/101/sui-template/contracts/counter/sources/systems/counter.move +10 -13
  12. package/template/101/sui-template/contracts/counter/sources/tests/counter.move +8 -8
  13. package/template/101/sui-template/contracts/counter/sources/tests/init.move +1 -1
  14. package/template/101/sui-template/contracts/dubhe-framework/sources/events.move +33 -0
  15. package/template/101/sui-template/contracts/dubhe-framework/sources/storages/double_map.move +111 -153
  16. package/template/101/sui-template/contracts/dubhe-framework/sources/storages/map.move +87 -141
  17. package/template/{nextjs/sui-template/contracts/dubhe-framework/sources/storages/migration.move → 101/sui-template/contracts/dubhe-framework/sources/storages/storage.move} +3 -3
  18. package/template/101/sui-template/contracts/dubhe-framework/sources/storages/tests.move +82 -334
  19. package/template/101/sui-template/contracts/dubhe-framework/sources/storages/value.move +77 -83
  20. package/template/101/sui-template/dubhe.config.ts +1 -3
  21. package/template/101/sui-template/package.json +6 -4
  22. package/template/101/sui-template/src/chain/config.ts +2 -4
  23. package/template/101/sui-template/src/pages/home/index.tsx +4 -4
  24. package/template/contract/sui-template/contracts/dubhe-framework/sources/events.move +33 -0
  25. package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/double_map.move +111 -153
  26. package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/map.move +87 -141
  27. package/template/{101/sui-template/contracts/dubhe-framework/sources/storages/migration.move → contract/sui-template/contracts/dubhe-framework/sources/storages/storage.move} +3 -3
  28. package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/tests.move +82 -334
  29. package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/value.move +77 -83
  30. package/template/contract/sui-template/package.json +6 -4
  31. package/template/nextjs/sui-template/contracts/counter/Move.toml +1 -1
  32. package/template/{101/sui-template/contracts/counter/sources/codegen/schemas/default → nextjs/sui-template/contracts/counter/sources/codegen}/dapp/metadata.move +6 -6
  33. package/template/nextjs/sui-template/contracts/counter/sources/codegen/dapp/schema.move +112 -0
  34. package/template/{101/sui-template/contracts/counter/sources/codegen/schemas/default → nextjs/sui-template/contracts/counter/sources/codegen}/dapp/system.move +17 -17
  35. package/template/nextjs/sui-template/contracts/counter/sources/codegen/{events → data}/increment_event.move +0 -6
  36. package/template/nextjs/sui-template/contracts/counter/sources/codegen/errors.move +16 -0
  37. package/template/nextjs/sui-template/contracts/counter/sources/codegen/events.move +23 -0
  38. package/template/nextjs/sui-template/contracts/counter/sources/codegen/genesis.move +20 -0
  39. package/template/nextjs/sui-template/contracts/counter/sources/codegen/schema.move +54 -0
  40. package/template/nextjs/sui-template/contracts/counter/sources/scripts/deploy_hook.move +12 -22
  41. package/template/nextjs/sui-template/contracts/counter/sources/systems/counter.move +10 -13
  42. package/template/nextjs/sui-template/contracts/counter/sources/tests/counter.move +8 -8
  43. package/template/nextjs/sui-template/contracts/counter/sources/tests/init.move +1 -1
  44. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/events.move +33 -0
  45. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/double_map.move +111 -153
  46. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/map.move +87 -141
  47. package/template/{contract/sui-template/contracts/dubhe-framework/sources/storages/migration.move → nextjs/sui-template/contracts/dubhe-framework/sources/storages/storage.move} +3 -3
  48. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/tests.move +82 -334
  49. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/value.move +77 -83
  50. package/template/nextjs/sui-template/dubhe.config.ts +1 -3
  51. package/template/nextjs/sui-template/package.json +7 -5
  52. package/template/nextjs/sui-template/src/chain/config.ts +3 -5
  53. package/template/nextjs/sui-template/src/pages/home/index.tsx +4 -4
  54. package/template/101/sui-template/contracts/counter/sources/codegen/errors/invalid_increment_error.move +0 -30
  55. package/template/101/sui-template/contracts/counter/sources/codegen/schemas/counter.move +0 -60
  56. package/template/101/sui-template/contracts/counter/sources/codegen/schemas/default/dapp/schema.move +0 -114
  57. package/template/nextjs/sui-template/contracts/counter/sources/codegen/errors/invalid_increment_error.move +0 -30
  58. package/template/nextjs/sui-template/contracts/counter/sources/codegen/schemas/counter.move +0 -60
  59. package/template/nextjs/sui-template/contracts/counter/sources/codegen/schemas/default/dapp/schema.move +0 -114
@@ -1,153 +1,111 @@
1
- #[allow(unused_variable)]
2
- module dubhe::storage_double_map {
3
-
4
- // This key does not exist in the map
5
- const EKeyDoesNotExist: u64 = 0;
6
-
7
- /// This key already exists in the map
8
- const EKeyAlreadyExists: u64 = 1;
9
-
10
- // An entry in the map
11
- public struct Entry<K1: copy + drop + store, K2: copy + drop + store, V: store> has copy, drop, store {
12
- key1: K1,
13
- key2: K2,
14
- value: V,
15
- }
16
-
17
- // A map data structure backed by a vector. The map is guaranteed not to contain duplicate keys, but entries
18
- public struct StorageDoubleMap<K1: copy + drop + store, K2: copy + drop + store, V: store> has store {
19
- contents: vector<Entry<K1, K2, V>>,
20
- }
21
-
22
- // Create an empty `StorageDoubleMap`
23
- public fun new<K1: copy + drop + store, K2: copy + drop + store, V: store>(): StorageDoubleMap<K1, K2,V> {
24
- StorageDoubleMap { contents: vector[] }
25
- }
26
-
27
- // Return true if `self` contains_key an entry for `key`, false otherwise
28
- public fun contains_key<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): bool {
29
- get_idx_opt(self, key1, key2).is_some()
30
- }
31
-
32
- // Return the number of entries in `self`
33
- public fun length<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>): u64 {
34
- self.contents.length()
35
- }
36
-
37
- // Get a reference to the value bound to `key` in `self`.
38
- // Aborts if `key` is not bound in `self`.
39
- public fun borrow<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): &V {
40
- let idx = self.get_idx(key1, key2);
41
- let entry = &self.contents[idx];
42
- &entry.value
43
- }
44
-
45
- // Get a mutable reference to the value bound to `key` in `self`.
46
- // Aborts if `key` is not bound in `self`.
47
- public fun borrow_mut<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &mut StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): &mut V {
48
- let idx = self.get_idx(key1, key2);
49
- let entry = &mut self.contents[idx];
50
- &mut entry.value
51
- }
52
-
53
- // Remove the entry `key` |-> `value` from self. Aborts if `key` is not bound in `self`.
54
- public fun take<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &mut StorageDoubleMap<K1, K2,V>, key1: K1, key2: K2): V {
55
- let idx = self.get_idx(key1, key2);
56
- let Entry { key1, key2, value } = self.contents.remove(idx);
57
- value
58
- }
59
-
60
- public macro fun mutate<$K1: copy + drop, $K2: copy + drop, $V: copy + drop>(
61
- $self: &mut StorageDoubleMap<$K1, $K2, $V>,
62
- $key1: $K1,
63
- $key2: $K2,
64
- $f: |&mut $V|
65
- ) {
66
- let self = $self;
67
- let key1 = $key1;
68
- let key2 = $key2;
69
- $f(borrow_mut(self, key1, key2));
70
- }
71
-
72
- // Returns a list of keys in the map.
73
- // Do not assume any particular ordering.
74
- public fun keys<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>): (vector<K1>, vector<K2>) {
75
- let mut i = 0;
76
- let n = self.contents.length();
77
- let mut keys1 = vector[];
78
- let mut keys2 = vector[];
79
- while (i < n) {
80
- let entry = self.contents.borrow(i);
81
- keys1.push_back(entry.key1);
82
- keys2.push_back(entry.key2);
83
- i = i + 1;
84
- };
85
- (keys1, keys2)
86
- }
87
-
88
- // Find the index of `key` in `self`. Return `None` if `key` is not in `self`.
89
- // Note that map entries are stored in insertion order, *not* sorted by key.
90
- public fun get_idx_opt<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): Option<u64> {
91
- self.contents.find_index!(|entry| { entry.key1 == key1 && entry.key2 == key2 })
92
- }
93
-
94
- // Find the index of `key` in `self`. Aborts if `key` is not in `self`.
95
- // Note that map entries are stored in insertion order, *not* sorted by key.
96
- public fun get_idx<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): u64 {
97
- let idx_opt = self.get_idx_opt(key1, key2);
98
- assert!(idx_opt.is_some(), EKeyDoesNotExist);
99
- idx_opt.destroy_some()
100
- }
101
-
102
- /// Insert the entry `key` |-> `value` into `self`.
103
- /// Aborts if `key` is already bound in `self`.
104
- public fun insert<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &mut StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2, value: V) {
105
- assert!(!self.contains_key(key1, key2), EKeyAlreadyExists);
106
- self.contents.push_back(Entry { key1, key2, value })
107
- }
108
-
109
- // =======================================Value: drop + copy + store=======================================
110
-
111
- // Insert the entry `key` |-> `value` into `self`.
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
- #[allow(unused_variable)]
2
- module dubhe::storage_map {
3
-
4
- /// This key already exists in the map
5
- const EKeyAlreadyExists: u64 = 0;
6
-
7
- /// This key does not exist in the map
8
- const EKeyDoesNotExist: u64 = 1;
9
-
10
- /// An entry in the map
11
- public struct Entry<K: copy + drop + store, V: store> has copy, drop, store {
12
- key: K,
13
- value: V,
14
- }
15
-
16
- /// A map data structure backed by a vector. The map is guaranteed not to contain duplicate keys, but entries
17
- /// are *not* sorted by key--entries are included in insertion order.
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
- public macro fun mutate<$K: copy + drop + store, $V: store>(
66
- $self: &mut StorageMap<$K, $V>,
67
- $key: $K,
68
- $f: |&mut $V|
69
- ) {
70
- let self = $self;
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
- /// Returns a list of keys in the map.
76
- /// Do not assume any particular ordering.
77
- public fun keys<K: copy + drop + store, V: store>(self: &StorageMap<K, V>): vector<K> {
78
- let mut i = 0;
79
- let n = self.contents.length();
80
- let mut keys = vector[];
81
- while (i < n) {
82
- let entry = self.contents.borrow(i);
83
- keys.push_back(entry.key);
84
- i = i + 1;
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
- /// Find the index of `key` in `self`. Return `None` if `key` is not in `self`.
90
- /// Note that map entries are stored in insertion order, *not* sorted by key.
91
- public fun get_idx_opt<K: copy + drop + store, V: store>(self: &StorageMap<K,V>, key: K): Option<u64> {
92
- self.contents.find_index!(|entry| { entry.key == key})
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
- /// Find the index of `key` in `self`. Aborts if `key` is not in `self`.
96
- /// Note that map entries are stored in insertion order, *not* sorted by key.
97
- public fun get_idx<K: copy + drop + store, V: store>(self: &StorageMap<K,V>, key: K): u64 {
98
- let idx_opt = self.get_idx_opt(key);
99
- assert!(idx_opt.is_some(), EKeyDoesNotExist);
100
- idx_opt.destroy_some()
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
- /// Insert the entry `key` |-> `value` into `self`.
104
- /// Aborts if `key` is already bound in `self`.
105
- public fun insert<K: copy + drop + store, V: store>(self: &mut StorageMap<K,V>, key: K, value: V) {
106
- assert!(!self.contains_key(key), EKeyAlreadyExists);
107
- self.contents.push_back(Entry { key, value })
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
- // =======================================Value: drop + copy + store=======================================
111
-
112
- /// Adds a key-value pair to the self `self: &mut StorageMap<K, V>`
113
- /// Aborts with `sui::dynamic_field::EFieldAlreadyExists` if the self already has an entry with
114
- /// that key `k: K`.
115
- public fun set<K: copy + drop + store, V: copy + drop + store>(self: &mut StorageMap<K, V>, key: K, value: V) {
116
- let idx = self.get_idx_opt(key);
117
- if (idx.is_some()) {
118
- self.contents[idx.destroy_some()].value = value;
119
- } else {
120
- self.contents.push_back(Entry { key, value })
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
- /// Get a reference to the value bound to `key` in `self`.
125
- /// Aborts if `key` is not bound in `self`.
126
- public fun get<K: copy + drop + store, V: copy + drop + store>(self: &StorageMap<K,V>, key: K): V {
127
- let idx = self.get_idx(key);
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
- /// Safely try borrow a value bound to `key` in `self`.
133
- /// Return Some(V) if the value exists, None otherwise.
134
- /// Only works for a "copyable" value as references cannot be stored in `vector`.
135
- public fun try_get<K: copy + drop + store, V: copy + drop + store>(self: &StorageMap<K,V>, key: K): Option<V> {
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
- /// Remove the entry `key` |-> `value` from self. Aborts if `key` is not bound in `self`.
144
- public fun remove<K: copy + drop + store, V: copy + drop + store>(self: &mut StorageMap<K,V>, key: K) {
145
- let idx = self.get_idx_opt(key);
146
- if (idx.is_some()) {
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
- // Returns a list of values in the map.
152
- // Do not assume any particular ordering.
153
- public fun values<K: copy + drop + store, V: copy + drop + store>(self: &StorageMap<K, V>): vector<V> {
154
- self.contents.map!(|entry| entry.value)
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::storage_migration {
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>, storagre_type: StorageType) {
5
- df::add(uid, field_name, storagre_type);
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 {