Skip to content

Commit

Permalink
feat(collections): Implement LookupMap (#18933)
Browse files Browse the repository at this point in the history
Co-authored-by: cool-developer <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 30, 2024
1 parent 14fd0ce commit 639735d
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
1 change: 1 addition & 0 deletions collections/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* [#18933](https://github.com/cosmos/cosmos-sdk/pull/18933) Add LookupMap implementation. It is basic wrapping of the standard Map methods but is not iterable.
* [#17656](https://github.com/cosmos/cosmos-sdk/pull/17656) – Introduces `Vec`, a collection type that allows to represent a growable array on top of a KVStore.

## [v0.4.0](https://github.com/cosmos/cosmos-sdk/releases/tag/collections%2Fv0.4.0)
Expand Down
104 changes: 104 additions & 0 deletions collections/lookup_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package collections

import (
"context"
"fmt"

"cosmossdk.io/collections/codec"
)

// LookupMap represents a map that is not iterable.
type LookupMap[K, V any] Map[K, V]

// NewLookupMap creates a new LookupMap.
func NewLookupMap[K, V any](
schemaBuilder *SchemaBuilder,
prefix Prefix,
name string,
keyCodec codec.KeyCodec[K],
valueCodec codec.ValueCodec[V],
) LookupMap[K, V] {
m := LookupMap[K, V](NewMap[K, V](schemaBuilder, prefix, name, keyCodec, valueCodec))
return m
}

// GetName returns the name of the collection.
func (m LookupMap[K, V]) GetName() string {
return m.name
}

// GetPrefix returns the prefix of the collection.
func (m LookupMap[K, V]) GetPrefix() []byte {
return m.prefix
}

// Set maps the provided value to the provided key in the store.
// Errors with ErrEncoding if key or value encoding fails.
func (m LookupMap[K, V]) Set(ctx context.Context, key K, value V) error {
bytesKey, err := EncodeKeyWithPrefix(m.prefix, m.kc, key)
if err != nil {
return err
}

valueBytes, err := m.vc.Encode(value)
if err != nil {
return fmt.Errorf("%w: value encode: %w", ErrEncoding, err)
}

kvStore := m.sa(ctx)
return kvStore.Set(bytesKey, valueBytes)
}

// Get returns the value associated with the provided key,
// errors with ErrNotFound if the key does not exist, or
// with ErrEncoding if the key or value decoding fails.
func (m LookupMap[K, V]) Get(ctx context.Context, key K) (v V, err error) {
bytesKey, err := EncodeKeyWithPrefix(m.prefix, m.kc, key)
if err != nil {
return v, err
}

kvStore := m.sa(ctx)
valueBytes, err := kvStore.Get(bytesKey)
if err != nil {
return v, err
}
if valueBytes == nil {
return v, fmt.Errorf("%w: key '%s' of type %s", ErrNotFound, m.kc.Stringify(key), m.vc.ValueType())
}

v, err = m.vc.Decode(valueBytes)
if err != nil {
return v, fmt.Errorf("%w: value decode: %w", ErrEncoding, err)
}
return v, nil
}

// Has reports whether the key is present in storage or not.
// Errors with ErrEncoding if key encoding fails.
func (m LookupMap[K, V]) Has(ctx context.Context, key K) (bool, error) {
bytesKey, err := EncodeKeyWithPrefix(m.prefix, m.kc, key)
if err != nil {
return false, err
}
kvStore := m.sa(ctx)
return kvStore.Has(bytesKey)
}

// Remove removes the key from the storage.
// Errors with ErrEncoding if key encoding fails.
// If the key does not exist then this is a no-op.
func (m LookupMap[K, V]) Remove(ctx context.Context, key K) error {
bytesKey, err := EncodeKeyWithPrefix(m.prefix, m.kc, key)
if err != nil {
return err
}
kvStore := m.sa(ctx)
return kvStore.Delete(bytesKey)
}

// KeyCodec returns the Map's KeyCodec.
func (m LookupMap[K, V]) KeyCodec() codec.KeyCodec[K] { return m.kc }

// ValueCodec returns the Map's ValueCodec.
func (m LookupMap[K, V]) ValueCodec() codec.ValueCodec[V] { return m.vc }
40 changes: 40 additions & 0 deletions collections/lookup_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package collections_test

import (
"testing"

"cosmossdk.io/collections"
"cosmossdk.io/collections/colltest"
"github.com/stretchr/testify/require"
)

func TestLookupMap(t *testing.T) {
sk, ctx := colltest.MockStore()
schema := collections.NewSchemaBuilder(sk)

lm := collections.NewLookupMap(schema, collections.NewPrefix("hi"), "lm", collections.Uint64Key, collections.Uint64Value)
_, err := schema.Build()
require.NoError(t, err)

// test not has
has, err := lm.Has(ctx, 1)
require.NoError(t, err)
require.False(t, has)
// test get error
_, err = lm.Get(ctx, 1)
require.ErrorIs(t, err, collections.ErrNotFound)

// test set/get
err = lm.Set(ctx, 1, 100)
require.NoError(t, err)
v, err := lm.Get(ctx, 1)
require.NoError(t, err)
require.Equal(t, uint64(100), v)

// test remove
err = lm.Remove(ctx, 1)
require.NoError(t, err)
has, err = lm.Has(ctx, 1)
require.NoError(t, err)
require.False(t, has)
}
6 changes: 3 additions & 3 deletions collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (m Map[K, V]) Set(ctx context.Context, key K, value V) error {

valueBytes, err := m.vc.Encode(value)
if err != nil {
return fmt.Errorf("%w: value encode: %s", ErrEncoding, err) // TODO: use multi err wrapping in go1.20: https://github.com/golang/go/issues/53435
return fmt.Errorf("%w: value encode: %w", ErrEncoding, err)
}

kvStore := m.sa(ctx)
Expand All @@ -88,7 +88,7 @@ func (m Map[K, V]) Get(ctx context.Context, key K) (v V, err error) {

v, err = m.vc.Decode(valueBytes)
if err != nil {
return v, fmt.Errorf("%w: value decode: %s", ErrEncoding, err) // TODO: use multi err wrapping in go1.20: https://github.com/golang/go/issues/53435
return v, fmt.Errorf("%w: value decode: %w", ErrEncoding, err)
}
return v, nil
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func EncodeKeyWithPrefix[K any](prefix []byte, kc codec.KeyCodec[K], key K) ([]b
// put key
_, err := kc.Encode(keyBytes[prefixLen:], key)
if err != nil {
return nil, fmt.Errorf("%w: key encode: %s", ErrEncoding, err) // TODO: use multi err wrapping in go1.20: https://github.com/golang/go/issues/53435
return nil, fmt.Errorf("%w: key encode: %w", ErrEncoding, err)
}
return keyBytes, nil
}

0 comments on commit 639735d

Please sign in to comment.