github.com/cosmos/cosmos-sdk@v0.50.10/x/group/internal/orm/auto_uint64.go (about) 1 package orm 2 3 import ( 4 "github.com/cosmos/gogoproto/proto" 5 6 "cosmossdk.io/errors" 7 storetypes "cosmossdk.io/store/types" 8 9 "github.com/cosmos/cosmos-sdk/codec" 10 ) 11 12 var ( 13 _ Indexable = &AutoUInt64Table{} 14 _ TableExportable = &AutoUInt64Table{} 15 ) 16 17 // AutoUInt64Table is the table type with an auto incrementing ID. 18 type AutoUInt64Table struct { 19 *table 20 seq Sequence 21 } 22 23 // NewAutoUInt64Table creates a new AutoUInt64Table. 24 func NewAutoUInt64Table(prefixData [2]byte, prefixSeq byte, model proto.Message, cdc codec.Codec) (*AutoUInt64Table, error) { 25 table, err := newTable(prefixData, model, cdc) 26 if err != nil { 27 return nil, err 28 } 29 return &AutoUInt64Table{ 30 table: table, 31 seq: NewSequence(prefixSeq), 32 }, nil 33 } 34 35 // Create a new persistent object with an auto generated uint64 primary key. The 36 // key is returned. 37 // 38 // Create iterates through the registered callbacks that may add secondary index 39 // keys. 40 func (a AutoUInt64Table) Create(store storetypes.KVStore, obj proto.Message) (uint64, error) { 41 autoIncID := a.seq.NextVal(store) 42 err := a.table.Create(store, EncodeSequence(autoIncID), obj) 43 if err != nil { 44 return 0, err 45 } 46 return autoIncID, nil 47 } 48 49 // Update updates the given object under the rowID key. It expects the key to 50 // exists already and fails with an `ErrNotFound` otherwise. Any caller must 51 // therefore make sure that this contract is fulfilled. Parameters must not be 52 // nil. 53 // 54 // Update iterates through the registered callbacks that may add or remove 55 // secondary index keys. 56 func (a AutoUInt64Table) Update(store storetypes.KVStore, rowID uint64, newValue proto.Message) error { 57 return a.table.Update(store, EncodeSequence(rowID), newValue) 58 } 59 60 // Delete removes the object under the rowID key. It expects the key to exists already 61 // and fails with a `ErrNotFound` otherwise. Any caller must therefore make sure that this contract 62 // is fulfilled. 63 // 64 // Delete iterates though the registered callbacks and removes secondary index keys by them. 65 func (a AutoUInt64Table) Delete(store storetypes.KVStore, rowID uint64) error { 66 return a.table.Delete(store, EncodeSequence(rowID)) 67 } 68 69 // Has checks if a rowID exists. 70 func (a AutoUInt64Table) Has(store storetypes.KVStore, rowID uint64) bool { 71 return a.table.Has(store, EncodeSequence(rowID)) 72 } 73 74 // GetOne load the object persisted for the given RowID into the dest parameter. 75 // If none exists `ErrNotFound` is returned instead. Parameters must not be nil. 76 func (a AutoUInt64Table) GetOne(store storetypes.KVStore, rowID uint64, dest proto.Message) (RowID, error) { 77 rawRowID := EncodeSequence(rowID) 78 if err := a.table.GetOne(store, rawRowID, dest); err != nil { 79 return nil, err 80 } 81 return rawRowID, nil 82 } 83 84 // PrefixScan returns an Iterator over a domain of keys in ascending order. End is exclusive. 85 // Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. 86 // Iterator must be closed by caller. 87 // To iterate over entire domain, use PrefixScan(1, math.MaxUint64) 88 // 89 // WARNING: The use of a PrefixScan can be very expensive in terms of Gas. Please make sure you do not expose 90 // this as an endpoint to the public without further limits. 91 // Example: 92 // 93 // it, err := idx.PrefixScan(ctx, start, end) 94 // if err !=nil { 95 // return err 96 // } 97 // const defaultLimit = 20 98 // it = LimitIterator(it, defaultLimit) 99 // 100 // CONTRACT: No writes may happen within a domain while an iterator exists over it. 101 func (a AutoUInt64Table) PrefixScan(store storetypes.KVStore, start, end uint64) (Iterator, error) { 102 return a.table.PrefixScan(store, EncodeSequence(start), EncodeSequence(end)) 103 } 104 105 // ReversePrefixScan returns an Iterator over a domain of keys in descending order. End is exclusive. 106 // Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. 107 // Iterator must be closed by caller. 108 // To iterate over entire domain, use PrefixScan(1, math.MaxUint64) 109 // 110 // WARNING: The use of a ReversePrefixScan can be very expensive in terms of Gas. Please make sure you do not expose 111 // this as an endpoint to the public without further limits. See `LimitIterator` 112 // 113 // CONTRACT: No writes may happen within a domain while an iterator exists over it. 114 func (a AutoUInt64Table) ReversePrefixScan(store storetypes.KVStore, start, end uint64) (Iterator, error) { 115 return a.table.ReversePrefixScan(store, EncodeSequence(start), EncodeSequence(end)) 116 } 117 118 // Sequence returns the sequence used by this table 119 func (a AutoUInt64Table) Sequence() Sequence { 120 return a.seq 121 } 122 123 // Export stores all the values in the table in the passed ModelSlicePtr and 124 // returns the current value of the associated sequence. 125 func (a AutoUInt64Table) Export(store storetypes.KVStore, dest ModelSlicePtr) (uint64, error) { 126 _, err := a.table.Export(store, dest) 127 if err != nil { 128 return 0, err 129 } 130 return a.seq.CurVal(store), nil 131 } 132 133 // Import clears the table and initializes it from the given data interface{}. 134 // data should be a slice of structs that implement PrimaryKeyed. 135 func (a AutoUInt64Table) Import(store storetypes.KVStore, data interface{}, seqValue uint64) error { 136 if err := a.seq.InitVal(store, seqValue); err != nil { 137 return errors.Wrap(err, "sequence") 138 } 139 return a.table.Import(store, data, seqValue) 140 }