Skip to content

Commit ddee3e8

Browse files
committed
feat(collections): impl ItemTransient
1 parent 3a55a64 commit ddee3e8

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

item.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
// itemKey is a constant byte key which maps an Item object.
9-
var itemKey uint64 = 0
9+
const itemKey uint64 = 0
1010

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

3333
// Set sets the item value to v.
3434
func (i Item[V]) Set(ctx sdk.Context, v V) { (Map[uint64, V])(i).Insert(ctx, itemKey, v) }
35+
36+
// NewItem instantiates a new Item instance.
37+
func NewItemTransient[V any](
38+
sk types.StoreKey, namespace Namespace, valueEncoder ValueEncoder[V],
39+
) ItemTransient[V] {
40+
return (ItemTransient[V])(NewMapTransient[uint64, V](sk, namespace, uint64Key{}, valueEncoder))
41+
}
42+
43+
// ItemTransient: An [Item] that maps to a transient key-value store (KV store)
44+
// instead of a persistent one. A Transient KV Store
45+
// is used for data that does not need to persist beyond the execution of the
46+
// current block or transaction.
47+
//
48+
// This can include temporary calculations, intermediate state data in
49+
// transactions or ephemeral data used in block processing. Data is a transient
50+
// store is cleared after the block is processed.
51+
//
52+
// Transient KV stores have markedly lower costs for all operations (10% of the
53+
// persistent cost) and a read cost per byte of zero.
54+
type ItemTransient[V any] MapTransient[uint64, V]
55+
56+
func (i ItemTransient[V]) Get(ctx sdk.Context) (V, error) {
57+
return (MapTransient[uint64, V])(i).Get(ctx, itemKey)
58+
}
59+
60+
// GetOr either returns the provided default
61+
// if it's not present in state, or the value found in state.
62+
func (i ItemTransient[V]) GetOr(ctx sdk.Context, def V) V {
63+
return (MapTransient[uint64, V])(i).GetOr(ctx, itemKey, def)
64+
}
65+
66+
// Set sets the item value to v.
67+
func (i ItemTransient[V]) Set(ctx sdk.Context, v V) {
68+
(MapTransient[uint64, V])(i).Insert(ctx, itemKey, v)
69+
}

item_test.go

+40-14
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,52 @@ import (
99

1010
func TestItemEmpty(t *testing.T) {
1111
sk, ctx, _ := deps()
12-
item := NewItem[string](sk, 0, stringValue{})
13-
14-
val, err := item.Get(ctx)
15-
assert.EqualValues(t, "", val)
16-
assert.Error(t, err)
12+
{
13+
item := NewItem[string](sk, 0, stringValue{})
14+
val, err := item.Get(ctx)
15+
assert.EqualValues(t, "", val)
16+
assert.Error(t, err)
17+
}
18+
sk, ctx, _ = deps()
19+
{
20+
item := NewItemTransient[string](sk, 0, stringValue{})
21+
val, err := item.Get(ctx)
22+
assert.EqualValues(t, "", val)
23+
assert.Error(t, err)
24+
}
1725
}
1826

1927
func TestItemGetOr(t *testing.T) {
2028
sk, ctx, _ := deps()
21-
item := NewItem[string](sk, 0, stringValue{})
22-
23-
val := item.GetOr(ctx, "default")
24-
assert.EqualValues(t, "default", val)
29+
{
30+
item := NewItem[string](sk, 0, stringValue{})
31+
val := item.GetOr(ctx, "default")
32+
assert.EqualValues(t, "default", val)
33+
}
34+
sk, ctx, _ = deps()
35+
{
36+
item := NewItemTransient[string](sk, 0, stringValue{})
37+
val := item.GetOr(ctx, "default")
38+
assert.EqualValues(t, "default", val)
39+
}
2540
}
2641

2742
func TestItemSetAndGet(t *testing.T) {
2843
sk, ctx, _ := deps()
29-
item := NewItem[string](sk, 0, stringValue{})
30-
item.Set(ctx, "bar")
31-
val, err := item.Get(ctx)
32-
require.Nil(t, err)
33-
require.EqualValues(t, "bar", val)
44+
{
45+
item := NewItem[string](sk, 0, stringValue{})
46+
item.Set(ctx, "bar")
47+
val, err := item.Get(ctx)
48+
require.Nil(t, err)
49+
require.EqualValues(t, "bar", val)
50+
}
51+
52+
sk, ctx, _ = deps()
53+
{
54+
item := NewItemTransient[string](sk, 0, stringValue{})
55+
item.Set(ctx, "bar")
56+
val, err := item.Get(ctx)
57+
require.Nil(t, err)
58+
require.EqualValues(t, "bar", val)
59+
}
3460
}

0 commit comments

Comments
 (0)