github.com/0chain/gosdk@v1.17.11/zmagmacore/storage/interface.go (about) 1 // DEPRECATED: This package is deprecated and will be removed in a future release. 2 package storage 3 4 import ( 5 "sync" 6 7 "github.com/dgraph-io/badger/v3" 8 9 "github.com/0chain/gosdk/zmagmacore/errors" 10 ) 11 12 type ( 13 // Storage represents the main storage based on badger.DB. 14 Storage struct { 15 db *badger.DB 16 17 singleton sync.Once // used for opening connection only once 18 } 19 20 // Value represent value that can be stored as encoded bytes. 21 Value interface { 22 Encode() []byte 23 } 24 ) 25 26 var ( 27 // storageInst represents singleton storage. 28 storageInst = &Storage{} 29 ) 30 31 // Open opens singleton connection to storage. 32 // 33 // If an error occurs during execution, the program terminates with code 2 and the error will be written in os.Stderr. 34 // 35 // Should be used only once while application is starting. 36 func Open(path string) { 37 storageInst.singleton.Do(func() { 38 opts := badger.DefaultOptions(path) 39 opts.Logger = nil 40 41 db, err := badger.Open(opts) 42 if err != nil { 43 errors.ExitErr("error while opening storage: %v", err, 2) 44 } 45 46 storageInst.db = db 47 }) 48 } 49 50 // GetStorage returns current storage implementation. 51 func GetStorage() *Storage { 52 return storageInst 53 } 54 55 // Del deletes entry by the key. 56 func (s *Storage) Del(key []byte) error { 57 return s.db.Update(func(txn *badger.Txn) error { 58 return txn.Delete(key) 59 }) 60 } 61 62 // Get retrieves entry by the key. 63 func (s *Storage) Get(key []byte) (value []byte, err error) { 64 err = s.db.View(func(txn *badger.Txn) error { 65 var item *badger.Item 66 item, err = txn.Get(key) 67 if err != nil { 68 return err 69 } 70 71 return item.Value(func(val []byte) error { 72 value = val 73 return nil 74 }) 75 }) 76 77 return 78 } 79 80 // Set sets encoded Value with provided key. 81 func (s *Storage) Set(key []byte, value Value) error { 82 return s.db.Update(func(txn *badger.Txn) error { 83 blob := value.Encode() 84 return txn.Set(key, blob) 85 }) 86 } 87 88 // SetWithRetries sets encoded Value with provided key with retries. 89 func (s *Storage) SetWithRetries(key []byte, value Value, numRetries int) error { 90 var err error 91 for i := 0; i < numRetries; i++ { 92 if err = s.Set(key, value); err != nil { 93 continue 94 } 95 break 96 } 97 98 return err 99 } 100 101 // Iterate iterates all elements with provided prefix and processes it with the handler. 102 func (s *Storage) Iterate(handler func(item *badger.Item) error, prefix []byte) error { 103 return s.db.View(func(txn *badger.Txn) error { 104 it := txn.NewIterator(badger.DefaultIteratorOptions) 105 defer it.Close() 106 107 for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { 108 if err := handler(it.Item()); err != nil { 109 return err 110 } 111 } 112 return nil 113 }) 114 } 115 116 // NewTransaction creates new badger.Txn. 117 // 118 // For read-only transactions set update flag to false. 119 func (s *Storage) NewTransaction(update bool) *badger.Txn { 120 return s.db.NewTransaction(update) 121 } 122 123 // Close closes a DB. 124 func (s *Storage) Close() error { 125 return s.db.Close() 126 }