Skip to content

Commit

Permalink
feat(collections): impl ItemTransient
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed May 14, 2024
1 parent 3a55a64 commit ddee3e8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 15 deletions.
37 changes: 36 additions & 1 deletion item.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// itemKey is a constant byte key which maps an Item object.
var itemKey uint64 = 0
const itemKey uint64 = 0

// NewItem instantiates a new Item instance.
func NewItem[V any](sk types.StoreKey, namespace Namespace, valueEncoder ValueEncoder[V]) Item[V] {
Expand All @@ -32,3 +32,38 @@ func (i Item[V]) GetOr(ctx sdk.Context, def V) V { return (Map[uint64, V])(i).Ge

// Set sets the item value to v.
func (i Item[V]) Set(ctx sdk.Context, v V) { (Map[uint64, V])(i).Insert(ctx, itemKey, v) }

// NewItem instantiates a new Item instance.
func NewItemTransient[V any](
sk types.StoreKey, namespace Namespace, valueEncoder ValueEncoder[V],
) ItemTransient[V] {
return (ItemTransient[V])(NewMapTransient[uint64, V](sk, namespace, uint64Key{}, valueEncoder))
}

// ItemTransient: An [Item] that maps to a transient key-value store (KV store)
// instead of a persistent one. A Transient KV Store
// is used for data that does not need to persist beyond the execution of the
// current block or transaction.
//
// This can include temporary calculations, intermediate state data in
// transactions or ephemeral data used in block processing. Data is a transient
// store is cleared after the block is processed.
//
// Transient KV stores have markedly lower costs for all operations (10% of the
// persistent cost) and a read cost per byte of zero.
type ItemTransient[V any] MapTransient[uint64, V]

func (i ItemTransient[V]) Get(ctx sdk.Context) (V, error) {
return (MapTransient[uint64, V])(i).Get(ctx, itemKey)
}

// GetOr either returns the provided default
// if it's not present in state, or the value found in state.
func (i ItemTransient[V]) GetOr(ctx sdk.Context, def V) V {
return (MapTransient[uint64, V])(i).GetOr(ctx, itemKey, def)
}

// Set sets the item value to v.
func (i ItemTransient[V]) Set(ctx sdk.Context, v V) {
(MapTransient[uint64, V])(i).Insert(ctx, itemKey, v)
}
54 changes: 40 additions & 14 deletions item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,52 @@ import (

func TestItemEmpty(t *testing.T) {
sk, ctx, _ := deps()
item := NewItem[string](sk, 0, stringValue{})

val, err := item.Get(ctx)
assert.EqualValues(t, "", val)
assert.Error(t, err)
{
item := NewItem[string](sk, 0, stringValue{})
val, err := item.Get(ctx)
assert.EqualValues(t, "", val)
assert.Error(t, err)
}
sk, ctx, _ = deps()
{
item := NewItemTransient[string](sk, 0, stringValue{})
val, err := item.Get(ctx)
assert.EqualValues(t, "", val)
assert.Error(t, err)
}
}

func TestItemGetOr(t *testing.T) {
sk, ctx, _ := deps()
item := NewItem[string](sk, 0, stringValue{})

val := item.GetOr(ctx, "default")
assert.EqualValues(t, "default", val)
{
item := NewItem[string](sk, 0, stringValue{})
val := item.GetOr(ctx, "default")
assert.EqualValues(t, "default", val)
}
sk, ctx, _ = deps()
{
item := NewItemTransient[string](sk, 0, stringValue{})
val := item.GetOr(ctx, "default")
assert.EqualValues(t, "default", val)
}
}

func TestItemSetAndGet(t *testing.T) {
sk, ctx, _ := deps()
item := NewItem[string](sk, 0, stringValue{})
item.Set(ctx, "bar")
val, err := item.Get(ctx)
require.Nil(t, err)
require.EqualValues(t, "bar", val)
{
item := NewItem[string](sk, 0, stringValue{})
item.Set(ctx, "bar")
val, err := item.Get(ctx)
require.Nil(t, err)
require.EqualValues(t, "bar", val)
}

sk, ctx, _ = deps()
{
item := NewItemTransient[string](sk, 0, stringValue{})
item.Set(ctx, "bar")
val, err := item.Get(ctx)
require.Nil(t, err)
require.EqualValues(t, "bar", val)
}
}

0 comments on commit ddee3e8

Please sign in to comment.