github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/store/micro_ops.go (about) 1 package store 2 3 import ( 4 "github.com/ronaksoft/rony" 5 "github.com/ronaksoft/rony/tools" 6 "google.golang.org/protobuf/proto" 7 ) 8 9 /* 10 Creation Time: 2021 - Mar - 01 11 Created by: (ehsan) 12 Maintainers: 13 1. Ehsan N. Moosa (E2) 14 Auditor: Ehsan N. Moosa (E2) 15 Copyright Ronak Software Group 2020 16 */ 17 18 func Delete(txn *rony.StoreTxn, alloc *tools.Allocator, keyParts ...interface{}) error { 19 key := alloc.Gen(keyParts...) 20 21 return txn.Delete(key) 22 } 23 24 func Move(txn *rony.StoreTxn, oldKey, newKey []byte) error { 25 item, err := txn.Get(oldKey) 26 if err != nil { 27 return err 28 } 29 err = item.Value(func(val []byte) error { 30 return txn.Set(newKey, val) 31 }) 32 if err != nil { 33 return err 34 } 35 36 return txn.Delete(oldKey) 37 } 38 39 func Set(txn *rony.StoreTxn, alloc *tools.Allocator, val []byte, keyParts ...interface{}) error { 40 key := alloc.Gen(keyParts...) 41 42 return txn.Set(key, val) 43 } 44 45 func SetByKey(txn *rony.StoreTxn, val, key []byte) error { 46 return txn.Set(key, val) 47 } 48 49 func Get(txn *rony.StoreTxn, alloc *tools.Allocator, keyParts ...interface{}) ([]byte, error) { 50 item, err := txn.Get(alloc.Gen(keyParts...)) 51 if err != nil { 52 return nil, err 53 } 54 55 var b []byte 56 _ = item.Value(func(val []byte) error { 57 b = alloc.FillWith(val) 58 59 return nil 60 }) 61 62 return b, nil 63 } 64 65 func GetByKey(txn *rony.StoreTxn, alloc *tools.Allocator, key []byte) ([]byte, error) { 66 item, err := txn.Get(key) 67 if err != nil { 68 return nil, err 69 } 70 71 var b []byte 72 _ = item.Value(func(val []byte) error { 73 b = alloc.FillWith(val) 74 75 return nil 76 }) 77 78 return b, nil 79 } 80 81 func Exists(txn *rony.StoreTxn, alloc *tools.Allocator, keyParts ...interface{}) bool { 82 _, err := Get(txn, alloc, keyParts...) 83 if err != nil && err == ErrKeyNotFound { 84 return false 85 } 86 87 return true 88 } 89 90 func ExistsByKey(txn *rony.StoreTxn, alloc *tools.Allocator, key []byte) bool { 91 _, err := GetByKey(txn, alloc, key) 92 if err != nil && err == ErrKeyNotFound { 93 return false 94 } 95 96 return true 97 } 98 99 func Marshal(txn *rony.StoreTxn, alloc *tools.Allocator, m proto.Message, keyParts ...interface{}) error { 100 val := alloc.Marshal(m) 101 102 return Set(txn, alloc, val, keyParts...) 103 } 104 105 func Unmarshal(txn *rony.StoreTxn, alloc *tools.Allocator, m proto.Message, keyParts ...interface{}) error { 106 val, err := Get(txn, alloc, keyParts...) 107 if err != nil { 108 return err 109 } 110 111 return proto.Unmarshal(val, m) 112 } 113 114 func UnmarshalMerge(txn *rony.StoreTxn, alloc *tools.Allocator, m proto.Message, keyParts ...interface{}) error { 115 val, err := Get(txn, alloc, keyParts...) 116 if err != nil { 117 return err 118 } 119 umo := proto.UnmarshalOptions{Merge: true} 120 121 return umo.Unmarshal(val, m) 122 }