Documentation ¶
Overview ¶
Package orm is a convenient object to data store mapper.
Index ¶
- Constants
- func AddLengthPrefix(bytes []byte) []byte
- func DecodeSequence(bz []byte) uint64
- func EncodeSequence(val uint64) []byte
- func NullTerminatedBytes(s string) []byte
- func Paginate(it Iterator, pageRequest *query.PageRequest, dest ModelSlicePtr) (*query.PageResponse, error)
- func PrefixRange(prefix []byte) ([]byte, []byte)
- func PrimaryKey(obj PrimaryKeyed) []byte
- type AfterDeleteInterceptor
- type AfterSetInterceptor
- type AutoUInt64Table
- func (a AutoUInt64Table) AddAfterDeleteInterceptor(interceptor AfterDeleteInterceptor)
- func (a AutoUInt64Table) AddAfterSetInterceptor(interceptor AfterSetInterceptor)
- func (a AutoUInt64Table) Create(store sdk.KVStore, obj codec.ProtoMarshaler) (uint64, error)
- func (a AutoUInt64Table) Delete(store sdk.KVStore, rowID uint64) error
- func (a AutoUInt64Table) Export(store sdk.KVStore, dest ModelSlicePtr) (uint64, error)
- func (a AutoUInt64Table) GetOne(store sdk.KVStore, rowID uint64, dest codec.ProtoMarshaler) (RowID, error)
- func (a AutoUInt64Table) Has(store sdk.KVStore, rowID uint64) bool
- func (a AutoUInt64Table) Import(store sdk.KVStore, data interface{}, seqValue uint64) error
- func (a AutoUInt64Table) PrefixScan(store sdk.KVStore, start, end uint64) (Iterator, error)
- func (a AutoUInt64Table) ReversePrefixScan(store sdk.KVStore, start uint64, end uint64) (Iterator, error)
- func (a AutoUInt64Table) RowGetter() RowGetter
- func (a AutoUInt64Table) Sequence() Sequence
- func (a AutoUInt64Table) Set(store sdk.KVStore, rowID RowID, newValue codec.ProtoMarshaler) error
- func (a AutoUInt64Table) Update(store sdk.KVStore, rowID uint64, newValue codec.ProtoMarshaler) error
- type GasCountingMockContext
- type Index
- type Indexable
- type Indexer
- func (i Indexer) IndexerFunc() IndexerFunc
- func (i Indexer) OnCreate(store sdk.KVStore, rowID RowID, value interface{}) error
- func (i Indexer) OnDelete(store sdk.KVStore, rowID RowID, value interface{}) error
- func (i Indexer) OnUpdate(store sdk.KVStore, rowID RowID, newValue, oldValue interface{}) error
- type IndexerFunc
- type Iterator
- type IteratorFunc
- type LimitedIterator
- type MockContext
- type ModelSlicePtr
- type MultiKeyIndex
- func (i MultiKeyIndex) Get(store sdk.KVStore, searchKey interface{}) (Iterator, error)
- func (i MultiKeyIndex) GetPaginated(store sdk.KVStore, searchKey interface{}, pageRequest *query.PageRequest) (Iterator, error)
- func (i MultiKeyIndex) Has(store sdk.KVStore, key interface{}) (bool, error)
- func (i MultiKeyIndex) PrefixScan(store sdk.KVStore, startI interface{}, endI interface{}) (Iterator, error)
- func (i MultiKeyIndex) ReversePrefixScan(store sdk.KVStore, startI interface{}, endI interface{}) (Iterator, error)
- type PrimaryKeyTable
- func (a PrimaryKeyTable) AddAfterDeleteInterceptor(interceptor AfterDeleteInterceptor)
- func (a PrimaryKeyTable) AddAfterSetInterceptor(interceptor AfterSetInterceptor)
- func (a PrimaryKeyTable) Contains(store sdk.KVStore, obj PrimaryKeyed) bool
- func (a PrimaryKeyTable) Create(store sdk.KVStore, obj PrimaryKeyed) error
- func (a PrimaryKeyTable) Delete(store sdk.KVStore, obj PrimaryKeyed) error
- func (a PrimaryKeyTable) Export(store sdk.KVStore, dest ModelSlicePtr) (uint64, error)
- func (a PrimaryKeyTable) GetOne(store sdk.KVStore, primKey RowID, dest codec.ProtoMarshaler) error
- func (a PrimaryKeyTable) Has(store sdk.KVStore, primaryKey RowID) bool
- func (a PrimaryKeyTable) Import(store sdk.KVStore, data interface{}, seqValue uint64) error
- func (a PrimaryKeyTable) PrefixScan(store sdk.KVStore, start, end []byte) (Iterator, error)
- func (a PrimaryKeyTable) ReversePrefixScan(store sdk.KVStore, start, end []byte) (Iterator, error)
- func (a PrimaryKeyTable) RowGetter() RowGetter
- func (a PrimaryKeyTable) Set(store sdk.KVStore, newValue PrimaryKeyed) error
- func (a PrimaryKeyTable) Update(store sdk.KVStore, newValue PrimaryKeyed) error
- type PrimaryKeyed
- type RowGetter
- type RowID
- type Sequence
- type TableExportable
- type UniqueIndex
- type UniqueIndexerFunc
- type Validateable
Constants ¶
const EncodedSeqLength = 8
EncodedSeqLength number of bytes used for the binary representation of a sequence value.
const MaxBytesLen = 255
MaxBytesLen is the maximum allowed length for a key part of type []byte
Variables ¶
This section is empty.
Functions ¶
func AddLengthPrefix ¶
AddLengthPrefix prefixes the byte array with its length as 8 bytes. The function will panic if the bytes length is bigger than 255.
func DecodeSequence ¶
DecodeSequence converts the binary representation into an Uint64 value.
func EncodeSequence ¶
EncodeSequence converts the sequence value into the binary representation.
func NullTerminatedBytes ¶
NullTerminatedBytes converts string to byte array and null terminate it
func Paginate ¶
func Paginate( it Iterator, pageRequest *query.PageRequest, dest ModelSlicePtr, ) (*query.PageResponse, error)
Paginate does pagination with a given Iterator based on the provided PageRequest and unmarshals the results into the dest interface that must be an non-nil pointer to a slice.
If pageRequest is nil, then we will use these default values:
- Offset: 0
- Key: nil
- Limit: 100
- CountTotal: true
If pageRequest.Key was provided, it got used beforehand to instantiate the Iterator, using for instance UInt64Index.GetPaginated method. Only one of pageRequest.Offset or pageRequest.Key should be set. Using pageRequest.Key is more efficient for querying the next page.
If pageRequest.CountTotal is set, we'll visit all iterators elements. pageRequest.CountTotal is only respected when offset is used.
This function will call it.Close().
func PrefixRange ¶
PrefixRange turns a prefix into a (start, end) range. The start is the given prefix value and the end is calculated by adding 1 bit to the start value. Nil is not allowed as prefix.
Example: []byte{1, 3, 4} becomes []byte{1, 3, 5} []byte{15, 42, 255, 255} becomes []byte{15, 43, 0, 0}
In case of an overflow the end is set to nil.
Example: []byte{255, 255, 255, 255} becomes nil
func PrimaryKey ¶
func PrimaryKey(obj PrimaryKeyed) []byte
PrimaryKey returns the immutable and serialized primary key of this object. The primary key has to be unique within it's domain so that not two with same value can exist in the same table. This means PrimaryKeyFields() has to return a unique value for each object.
Types ¶
type AfterDeleteInterceptor ¶
AfterDeleteInterceptor defines a callback function to be called on Delete operations.
type AfterSetInterceptor ¶
type AfterSetInterceptor func(store sdk.KVStore, rowID RowID, newValue, oldValue codec.ProtoMarshaler) error
AfterSetInterceptor defines a callback function to be called on Create + Update.
type AutoUInt64Table ¶
type AutoUInt64Table struct {
// contains filtered or unexported fields
}
AutoUInt64Table is the table type with an auto incrementing ID.
func NewAutoUInt64Table ¶
func NewAutoUInt64Table(prefixData [2]byte, prefixSeq byte, model codec.ProtoMarshaler, cdc codec.Codec) (*AutoUInt64Table, error)
NewAutoUInt64Table creates a new AutoUInt64Table.
func (AutoUInt64Table) AddAfterDeleteInterceptor ¶
func (a AutoUInt64Table) AddAfterDeleteInterceptor(interceptor AfterDeleteInterceptor)
AddAfterDeleteInterceptor can be used to register a callback function that is executed after an object is deleted.
func (AutoUInt64Table) AddAfterSetInterceptor ¶
func (a AutoUInt64Table) AddAfterSetInterceptor(interceptor AfterSetInterceptor)
AddAfterSetInterceptor can be used to register a callback function that is executed after an object is created and/or updated.
func (AutoUInt64Table) Create ¶
func (a AutoUInt64Table) Create(store sdk.KVStore, obj codec.ProtoMarshaler) (uint64, error)
Create a new persistent object with an auto generated uint64 primary key. The key is returned.
Create iterates through the registered callbacks that may add secondary index keys.
func (AutoUInt64Table) Delete ¶
func (a AutoUInt64Table) Delete(store sdk.KVStore, rowID uint64) error
Delete removes the object under the rowID key. It expects the key to exists already and fails with a `ErrNotFound` otherwise. Any caller must therefore make sure that this contract is fulfilled.
Delete iterates though the registered callbacks and removes secondary index keys by them.
func (AutoUInt64Table) Export ¶
func (a AutoUInt64Table) Export(store sdk.KVStore, dest ModelSlicePtr) (uint64, error)
Export stores all the values in the table in the passed ModelSlicePtr and returns the current value of the associated sequence.
func (AutoUInt64Table) GetOne ¶
func (a AutoUInt64Table) GetOne(store sdk.KVStore, rowID uint64, dest codec.ProtoMarshaler) (RowID, error)
GetOne load the object persisted for the given RowID into the dest parameter. If none exists `ErrNotFound` is returned instead. Parameters must not be nil.
func (AutoUInt64Table) Has ¶
func (a AutoUInt64Table) Has(store sdk.KVStore, rowID uint64) bool
Has checks if a rowID exists.
func (AutoUInt64Table) Import ¶
func (a AutoUInt64Table) Import(store sdk.KVStore, data interface{}, seqValue uint64) error
Import clears the table and initializes it from the given data interface{}. data should be a slice of structs that implement PrimaryKeyed.
func (AutoUInt64Table) PrefixScan ¶
PrefixScan returns an Iterator over a domain of keys in ascending order. End is exclusive. Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. Iterator must be closed by caller. To iterate over entire domain, use PrefixScan(1, math.MaxUint64)
WARNING: The use of a PrefixScan can be very expensive in terms of Gas. Please make sure you do not expose this as an endpoint to the public without further limits. Example:
it, err := idx.PrefixScan(ctx, start, end) if err !=nil { return err } const defaultLimit = 20 it = LimitIterator(it, defaultLimit)
CONTRACT: No writes may happen within a domain while an iterator exists over it.
func (AutoUInt64Table) ReversePrefixScan ¶
func (a AutoUInt64Table) ReversePrefixScan(store sdk.KVStore, start uint64, end uint64) (Iterator, error)
ReversePrefixScan returns an Iterator over a domain of keys in descending order. End is exclusive. Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. Iterator must be closed by caller. To iterate over entire domain, use PrefixScan(1, math.MaxUint64)
WARNING: The use of a ReversePrefixScan can be very expensive in terms of Gas. Please make sure you do not expose this as an endpoint to the public without further limits. See `LimitIterator`
CONTRACT: No writes may happen within a domain while an iterator exists over it.
func (AutoUInt64Table) RowGetter ¶
func (a AutoUInt64Table) RowGetter() RowGetter
RowGetter returns a type safe RowGetter.
func (AutoUInt64Table) Sequence ¶
func (a AutoUInt64Table) Sequence() Sequence
Sequence returns the sequence used by this table
func (AutoUInt64Table) Set ¶
Set persists the given object under the rowID key. It does not check if the key already exists and overwrites the value if it does.
Set iterates through the registered callbacks that may add secondary index keys.
func (AutoUInt64Table) Update ¶
func (a AutoUInt64Table) Update(store sdk.KVStore, rowID uint64, newValue codec.ProtoMarshaler) error
Update updates the given object under the rowID key. It expects the key to exists already and fails with an `ErrNotFound` otherwise. Any caller must therefore make sure that this contract is fulfilled. Parameters must not be nil.
Update iterates through the registered callbacks that may add or remove secondary index keys.
type GasCountingMockContext ¶
func NewGasCountingMockContext ¶
func NewGasCountingMockContext() *GasCountingMockContext
func (GasCountingMockContext) GasConsumed ¶
func (g GasCountingMockContext) GasConsumed() types.Gas
func (GasCountingMockContext) GasRemaining ¶
func (g GasCountingMockContext) GasRemaining() types.Gas
func (GasCountingMockContext) KVStore ¶
func (g GasCountingMockContext) KVStore(store sdk.KVStore) sdk.KVStore
func (*GasCountingMockContext) ResetGasMeter ¶
func (g *GasCountingMockContext) ResetGasMeter()
type Index ¶
type Index interface { // Has checks if a key exists. Panics on nil key. Has(store sdk.KVStore, key interface{}) (bool, error) // Get returns a result iterator for the searchKey. // searchKey must not be nil. Get(store sdk.KVStore, searchKey interface{}) (Iterator, error) // GetPaginated returns a result iterator for the searchKey and optional pageRequest. // searchKey must not be nil. GetPaginated(store sdk.KVStore, searchKey interface{}, pageRequest *query.PageRequest) (Iterator, error) // PrefixScan returns an Iterator over a domain of keys in ascending order. End is exclusive. // Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. // Iterator must be closed by caller. // To iterate over entire domain, use PrefixScan(nil, nil) // // WARNING: The use of a PrefixScan can be very expensive in terms of Gas. Please make sure you do not expose // this as an endpoint to the public without further limits. // Example: // it, err := idx.PrefixScan(ctx, start, end) // if err !=nil { // return err // } // const defaultLimit = 20 // it = LimitIterator(it, defaultLimit) // // CONTRACT: No writes may happen within a domain while an iterator exists over it. PrefixScan(store sdk.KVStore, startI interface{}, endI interface{}) (Iterator, error) // ReversePrefixScan returns an Iterator over a domain of keys in descending order. End is exclusive. // Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. // Iterator must be closed by caller. // To iterate over entire domain, use PrefixScan(nil, nil) // // WARNING: The use of a ReversePrefixScan can be very expensive in terms of Gas. Please make sure you do not expose // this as an endpoint to the public without further limits. See `LimitIterator` // // CONTRACT: No writes may happen within a domain while an iterator exists over it. ReversePrefixScan(store sdk.KVStore, startI interface{}, endI interface{}) (Iterator, error) }
Index allows efficient prefix scans is stored as key = concat(indexKeyBytes, rowIDUint64) with value empty so that the row PrimaryKey is allows a fixed with 8 byte integer. This allows the MultiKeyIndex key bytes to be variable length and scanned iteratively.
type Indexable ¶
type Indexable interface { RowGetter() RowGetter AddAfterSetInterceptor(interceptor AfterSetInterceptor) AddAfterDeleteInterceptor(interceptor AfterDeleteInterceptor) }
Indexable types are used to setup new tables. This interface provides a set of functions that can be called by indexes to register and interact with the tables.
type Indexer ¶
type Indexer struct {
// contains filtered or unexported fields
}
Indexer manages the persistence of an Index based on searchable keys and operations.
func NewIndexer ¶
func NewIndexer(indexerFunc IndexerFunc) (*Indexer, error)
NewIndexer returns an indexer that supports multiple reference keys for an entity.
func NewUniqueIndexer ¶
func NewUniqueIndexer(f UniqueIndexerFunc) (*Indexer, error)
NewUniqueIndexer returns an indexer that requires exactly one reference keys for an entity.
func (Indexer) IndexerFunc ¶
func (i Indexer) IndexerFunc() IndexerFunc
IndexerFunc returns the indexer IndexerFunc, ensuring it has been prune from empty keys.
type IndexerFunc ¶
type IndexerFunc func(value interface{}) ([]interface{}, error)
IndexerFunc creates one or multiple index keys for the source object.
type Iterator ¶
type Iterator interface { // LoadNext loads the next value in the sequence into the pointer passed as dest and returns the key. If there // are no more items the ErrORMIteratorDone error is returned // The key is the rowID. LoadNext(dest codec.ProtoMarshaler) (RowID, error) // Close releases the iterator and should be called at the end of iteration io.Closer }
Iterator allows iteration through a sequence of key value pairs
func NewInvalidIterator ¶
func NewInvalidIterator() Iterator
Iterator that return ErrORMInvalidIterator only.
func NewSingleValueIterator ¶
type IteratorFunc ¶
type IteratorFunc func(dest codec.ProtoMarshaler) (RowID, error)
IteratorFunc is a function type that satisfies the Iterator interface The passed function is called on LoadNext operations.
func (IteratorFunc) LoadNext ¶
func (i IteratorFunc) LoadNext(dest codec.ProtoMarshaler) (RowID, error)
LoadNext loads the next value in the sequence into the pointer passed as dest and returns the key. If there are no more items the errors.ErrORMIteratorDone error is returned The key is the rowID and not any MultiKeyIndex key.
type LimitedIterator ¶
type LimitedIterator struct {
// contains filtered or unexported fields
}
LimitedIterator returns up to defined maximum number of elements.
func LimitIterator ¶
func LimitIterator(parent Iterator, max int) (*LimitedIterator, error)
LimitIterator returns a new iterator that returns max number of elements. The parent iterator must not be nil max can be 0 or any positive number
func (LimitedIterator) Close ¶
func (i LimitedIterator) Close() error
Close releases the iterator and should be called at the end of iteration
func (*LimitedIterator) LoadNext ¶
func (i *LimitedIterator) LoadNext(dest codec.ProtoMarshaler) (RowID, error)
LoadNext loads the next value in the sequence into the pointer passed as dest and returns the key. If there are no more items or the defined max number of elements was returned the `errors.ErrORMIteratorDone` error is returned The key is the rowID and not any MultiKeyIndex key.
type MockContext ¶
type MockContext struct {
// contains filtered or unexported fields
}
func NewMockContext ¶
func NewMockContext() *MockContext
func (MockContext) KVStore ¶
func (m MockContext) KVStore(key storetypes.StoreKey) sdk.KVStore
type ModelSlicePtr ¶
type ModelSlicePtr interface{}
ModelSlicePtr represents a pointer to a slice of models. Think of it as *[]Model Because of Go's type system, using []Model type would not work for us. Instead we use a placeholder type and the validation is done during the runtime.
type MultiKeyIndex ¶
type MultiKeyIndex struct {
// contains filtered or unexported fields
}
MultiKeyIndex is an index where multiple entries can point to the same underlying object as opposite to a unique index where only one entry is allowed.
func NewIndex ¶
func NewIndex(tb Indexable, prefix byte, indexerF IndexerFunc, indexKey interface{}) (MultiKeyIndex, error)
NewIndex builds a MultiKeyIndex. Only single-field indexes are supported and `indexKey` represents such a field value, which can be []byte, string or uint64.
func (MultiKeyIndex) Get ¶
func (i MultiKeyIndex) Get(store sdk.KVStore, searchKey interface{}) (Iterator, error)
Get returns a result iterator for the searchKey. Parameters must not be nil.
func (MultiKeyIndex) GetPaginated ¶
func (i MultiKeyIndex) GetPaginated(store sdk.KVStore, searchKey interface{}, pageRequest *query.PageRequest) (Iterator, error)
GetPaginated creates an iterator for the searchKey starting from pageRequest.Key if provided. The pageRequest.Key is the rowID while searchKey is a MultiKeyIndex key.
func (MultiKeyIndex) Has ¶
func (i MultiKeyIndex) Has(store sdk.KVStore, key interface{}) (bool, error)
Has checks if a key exists. Returns an error on nil key.
func (MultiKeyIndex) PrefixScan ¶
func (i MultiKeyIndex) PrefixScan(store sdk.KVStore, startI interface{}, endI interface{}) (Iterator, error)
PrefixScan returns an Iterator over a domain of keys in ascending order. End is exclusive. Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. Iterator must be closed by caller. To iterate over entire domain, use PrefixScan(nil, nil)
WARNING: The use of a PrefixScan can be very expensive in terms of Gas. Please make sure you do not expose this as an endpoint to the public without further limits. Example:
it, err := idx.PrefixScan(ctx, start, end) if err !=nil { return err } const defaultLimit = 20 it = LimitIterator(it, defaultLimit)
CONTRACT: No writes may happen within a domain while an iterator exists over it.
func (MultiKeyIndex) ReversePrefixScan ¶
func (i MultiKeyIndex) ReversePrefixScan(store sdk.KVStore, startI interface{}, endI interface{}) (Iterator, error)
ReversePrefixScan returns an Iterator over a domain of keys in descending order. End is exclusive. Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. Iterator must be closed by caller. To iterate over entire domain, use PrefixScan(nil, nil)
WARNING: The use of a ReversePrefixScan can be very expensive in terms of Gas. Please make sure you do not expose this as an endpoint to the public without further limits. See `LimitIterator`
CONTRACT: No writes may happen within a domain while an iterator exists over it.
type PrimaryKeyTable ¶
type PrimaryKeyTable struct {
// contains filtered or unexported fields
}
PrimaryKeyTable provides simpler object style orm methods without passing database RowIDs. Entries are persisted and loaded with a reference to their unique primary key.
func NewPrimaryKeyTable ¶
func NewPrimaryKeyTable(prefixData [2]byte, model PrimaryKeyed, cdc codec.Codec) (*PrimaryKeyTable, error)
NewPrimaryKeyTable creates a new PrimaryKeyTable.
func (PrimaryKeyTable) AddAfterDeleteInterceptor ¶
func (a PrimaryKeyTable) AddAfterDeleteInterceptor(interceptor AfterDeleteInterceptor)
AddAfterDeleteInterceptor can be used to register a callback function that is executed after an object is deleted.
func (PrimaryKeyTable) AddAfterSetInterceptor ¶
func (a PrimaryKeyTable) AddAfterSetInterceptor(interceptor AfterSetInterceptor)
AddAfterSetInterceptor can be used to register a callback function that is executed after an object is created and/or updated.
func (PrimaryKeyTable) Contains ¶
func (a PrimaryKeyTable) Contains(store sdk.KVStore, obj PrimaryKeyed) bool
Contains returns true when an object with same type and primary key is persisted in this table.
func (PrimaryKeyTable) Create ¶
func (a PrimaryKeyTable) Create(store sdk.KVStore, obj PrimaryKeyed) error
Create persists the given object under their primary key. It checks if the key already exists and may return an `ErrUniqueConstraint`.
Create iterates through the registered callbacks that may add secondary index keys.
func (PrimaryKeyTable) Delete ¶
func (a PrimaryKeyTable) Delete(store sdk.KVStore, obj PrimaryKeyed) error
Delete removes the object. It expects the primary key to exists already and fails with a `ErrNotFound` otherwise. Any caller must therefore make sure that this contract is fulfilled.
Delete iterates through the registered callbacks that remove secondary index keys.
func (PrimaryKeyTable) Export ¶
func (a PrimaryKeyTable) Export(store sdk.KVStore, dest ModelSlicePtr) (uint64, error)
Export stores all the values in the table in the passed ModelSlicePtr.
func (PrimaryKeyTable) GetOne ¶
func (a PrimaryKeyTable) GetOne(store sdk.KVStore, primKey RowID, dest codec.ProtoMarshaler) error
GetOne loads the object persisted for the given primary Key into the dest parameter. If none exists `ErrNotFound` is returned instead. Parameters must not be nil.
func (PrimaryKeyTable) Has ¶
func (a PrimaryKeyTable) Has(store sdk.KVStore, primaryKey RowID) bool
Has checks if a key exists. Always returns false on nil or empty key.
func (PrimaryKeyTable) Import ¶
func (a PrimaryKeyTable) Import(store sdk.KVStore, data interface{}, seqValue uint64) error
Import clears the table and initializes it from the given data interface{}. data should be a slice of structs that implement PrimaryKeyed.
func (PrimaryKeyTable) PrefixScan ¶
PrefixScan returns an Iterator over a domain of keys in ascending order. End is exclusive. Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. Iterator must be closed by caller. To iterate over entire domain, use PrefixScan(nil, nil)
WARNING: The use of a PrefixScan can be very expensive in terms of Gas. Please make sure you do not expose this as an endpoint to the public without further limits. Example:
it, err := idx.PrefixScan(ctx, start, end) if err !=nil { return err } const defaultLimit = 20 it = LimitIterator(it, defaultLimit)
CONTRACT: No writes may happen within a domain while an iterator exists over it.
func (PrimaryKeyTable) ReversePrefixScan ¶
ReversePrefixScan returns an Iterator over a domain of keys in descending order. End is exclusive. Start is an MultiKeyIndex key or prefix. It must be less than end, or the Iterator is invalid and error is returned. Iterator must be closed by caller. To iterate over entire domain, use PrefixScan(nil, nil)
WARNING: The use of a ReversePrefixScan can be very expensive in terms of Gas. Please make sure you do not expose this as an endpoint to the public without further limits. See `LimitIterator`
CONTRACT: No writes may happen within a domain while an iterator exists over it.
func (PrimaryKeyTable) RowGetter ¶
func (a PrimaryKeyTable) RowGetter() RowGetter
RowGetter returns a type safe RowGetter.
func (PrimaryKeyTable) Set ¶
func (a PrimaryKeyTable) Set(store sdk.KVStore, newValue PrimaryKeyed) error
Set persists the given object under the rowID key. It does not check if the key already exists and overwrites the value if it does.
Set iterates through the registered callbacks that may add secondary index keys.
func (PrimaryKeyTable) Update ¶
func (a PrimaryKeyTable) Update(store sdk.KVStore, newValue PrimaryKeyed) error
Update updates the given object under the primary key. It expects the key to exists already and fails with an `ErrNotFound` otherwise. Any caller must therefore make sure that this contract is fulfilled. Parameters must not be nil.
Update iterates through the registered callbacks that may add or remove secondary index keys.
type PrimaryKeyed ¶
type PrimaryKeyed interface { // PrimaryKeyFields returns the fields of the object that will make up // the primary key. The PrimaryKey function will encode and concatenate // the fields to build the primary key. // // PrimaryKey parts can be []byte, string, and integer types. []byte is // encoded with a length prefix, strings are null-terminated, and // integers are encoded using 8 byte big endian. // // IMPORTANT: []byte parts are encoded with a single byte length prefix, // so cannot be longer than 255 bytes. PrimaryKeyFields() []interface{} codec.ProtoMarshaler }
PrimaryKeyed defines an object type that is aware of its immutable primary key.
type RowGetter ¶
RowGetter loads a persistent object by row ID into the destination object. The dest parameter must therefore be a pointer. Any implementation must return `sdkerrors.ErrNotFound` when no object for the rowID exists
type RowID ¶
type RowID []byte
Unique identifier of a persistent table.
func First ¶
func First(it Iterator, dest codec.ProtoMarshaler) (RowID, error)
First loads the first element into the given destination type and closes the iterator. When the iterator is closed or has no elements the according error is passed as return value.
func ReadAll ¶
func ReadAll(it Iterator, dest ModelSlicePtr) ([]RowID, error)
ReadAll consumes all values for the iterator and stores them in a new slice at the passed ModelSlicePtr. The slice can be empty when the iterator does not return any values but not nil. The iterator is closed afterwards. Example:
var loaded []testdata.GroupInfo rowIDs, err := ReadAll(it, &loaded) require.NoError(t, err)
type Sequence ¶
type Sequence struct {
// contains filtered or unexported fields
}
sequence is a persistent unique key generator based on a counter.
func NewSequence ¶
func (Sequence) InitVal ¶
InitVal sets the start value for the sequence. It must be called only once on an empty DB. Otherwise an error is returned when the key exists. The given start value is stored as current value.
It is recommended to call this method only for a sequence start value other than `1` as the method consumes unnecessary gas otherwise. A scenario would be an import from genesis.
type TableExportable ¶
type TableExportable interface { // Export stores all the values in the table in the passed // ModelSlicePtr. If the table has an associated sequence, then its // current value is returned, otherwise 0 is returned by default. Export(store sdk.KVStore, dest ModelSlicePtr) (uint64, error) // Import clears the table and initializes it from the given data // interface{}. data should be a slice of structs that implement // PrimaryKeyed. The seqValue is optional and only // used with tables that have an associated sequence. Import(store sdk.KVStore, data interface{}, seqValue uint64) error }
TableExportable
type UniqueIndex ¶
type UniqueIndex struct {
MultiKeyIndex
}
func NewUniqueIndex ¶
func NewUniqueIndex(tb Indexable, prefix byte, uniqueIndexerFunc UniqueIndexerFunc, indexKey interface{}) (UniqueIndex, error)
NewUniqueIndex create a new Index object where duplicate keys are prohibited.
type UniqueIndexerFunc ¶
type UniqueIndexerFunc func(value interface{}) (interface{}, error)
IndexerFunc creates exactly one index key for the source object.
type Validateable ¶
type Validateable interface { // ValidateBasic is a sanity check on the data. Any error returned prevents create or updates. ValidateBasic() error }
Validateable is an interface that ProtoMarshaler types can implement and is called on any orm save or update operation.