verifier_store.gno
2.09 Kb · 83 lines
1package chunk
2
3import "gno.land/p/akkadia/v0/ds/btree"
4
5var verifierStore *VerifierStore = newVerifierStore()
6
7// VerifierStore owns chunk verifier records.
8//
9// Data shape:
10// - verifiers: worldID(uint32) -> *StringBTree(canonicalChunkKey(string) -> verifier(string))
11type VerifierStore struct {
12 verifiers *btree.Uint32BTree
13}
14
15func newVerifierStore() *VerifierStore {
16 return &VerifierStore{verifiers: btree.NewUint32BTree(32)}
17}
18
19// ==================== DANGER: MUTABLE MIGRATION CAPABILITIES ====================
20//
21// The getters in this section expose mutable store internals.
22// Only authorized migration exporter paths may depend on these capabilities.
23// Runtime reads and test setup must use copy-returning methods or total helpers.
24func (s *VerifierStore) Verifiers() *btree.Uint32BTree {
25 return s.verifiers
26}
27
28// ================= END DANGER: MUTABLE MIGRATION CAPABILITIES ==================
29
30func (s *VerifierStore) Set(worldID uint32, chunkKey string, verifier string) {
31 t := s.getOrCreateWorldTree(worldID)
32 t.Set(chunkKey, verifier)
33}
34
35func (s *VerifierStore) Get(worldID uint32, chunkKey string) (string, bool) {
36 t := s.getWorldTree(worldID)
37 if t == nil {
38 return "", false
39 }
40 result, found := t.Get(chunkKey)
41 if !found {
42 return "", false
43 }
44 return result.(string), true
45}
46
47func (s *VerifierStore) Delete(worldID uint32) {
48 s.verifiers.Remove(worldID)
49}
50
51func (s *VerifierStore) Size(worldID uint32) int {
52 t := s.getWorldTree(worldID)
53 if t == nil {
54 return 0
55 }
56 return t.Size()
57}
58
59func (s *VerifierStore) Total() int {
60 total := 0
61 s.verifiers.Iterate(nil, nil, func(_ uint32, value any) bool {
62 total += value.(*btree.StringBTree).Size()
63 return false
64 })
65 return total
66}
67
68func (s *VerifierStore) getWorldTree(worldID uint32) *btree.StringBTree {
69 value, exists := s.verifiers.Get(worldID)
70 if !exists {
71 return nil
72 }
73 return value.(*btree.StringBTree)
74}
75
76func (s *VerifierStore) getOrCreateWorldTree(worldID uint32) *btree.StringBTree {
77 if value, exists := s.verifiers.Get(worldID); exists {
78 return value.(*btree.StringBTree)
79 }
80 t := btree.NewStringBTree(32)
81 s.verifiers.Set(worldID, t)
82 return t
83}