Skip to content

Commit

Permalink
Flatten ads package to avoid exporting concrete types
Browse files Browse the repository at this point in the history
  • Loading branch information
karimodm committed Aug 1, 2023
1 parent 529eb51 commit b693e94
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 86 deletions.
6 changes: 2 additions & 4 deletions ads/ads.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ads

import (
"github.com/iotaledger/hive.go/ads/amap"
"github.com/iotaledger/hive.go/ads/aset"
"github.com/iotaledger/hive.go/ds/types"
"github.com/iotaledger/hive.go/kvstore"
)
Expand Down Expand Up @@ -40,7 +38,7 @@ type Map[K, V any] interface {

// NewMap creates a new AuthenticatedMap.
func NewMap[K, V any](store kvstore.KVStore, kToBytes kvstore.ObjectToBytes[K], bytesToK kvstore.BytesToObject[K], vToBytes kvstore.ObjectToBytes[V], bytesToV kvstore.BytesToObject[V]) Map[K, V] {
return amap.NewAuthenticatedMap(store, kToBytes, bytesToK, vToBytes, bytesToV)
return newAuthenticatedMap(store, kToBytes, bytesToK, vToBytes, bytesToV)
}

// Set is a sparse merkle tree based set.
Expand All @@ -57,5 +55,5 @@ type Set[K any] interface {

// NewSet creates a new sparse merkle tree based map.
func NewSet[K any](store kvstore.KVStore, kToBytes kvstore.ObjectToBytes[K], bytesToK kvstore.BytesToObject[K]) Set[K] {
return aset.NewAuthenticatedSet(store, kToBytes, bytesToK)
return newAuthenticatedSet(store, kToBytes, bytesToK)
}
31 changes: 0 additions & 31 deletions ads/aset/authenticated_set.go

This file was deleted.

30 changes: 15 additions & 15 deletions ads/amap/authenticated_map.go → ads/authenticated_map.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package amap
package ads

import (
"crypto/sha256"
Expand All @@ -22,7 +22,7 @@ const (
)

// AuthenticatedMap is a sparse merkle tree based map.
type AuthenticatedMap[K, V any] struct {
type authenticatedMap[K, V any] struct {
rawKeysStore *kvstore.TypedStore[K, types.Empty]
tree *smt.SMT
size *typedkey.Number[uint64]
Expand All @@ -36,14 +36,14 @@ type AuthenticatedMap[K, V any] struct {
}

// NewAuthenticatedMap creates a new authenticated map.
func NewAuthenticatedMap[K, V any](
func newAuthenticatedMap[K, V any](
store kvstore.KVStore,
kToBytes kvstore.ObjectToBytes[K],
bytesToK kvstore.BytesToObject[K],
vToBytes kvstore.ObjectToBytes[V],
bytesToV kvstore.BytesToObject[V],
) *AuthenticatedMap[K, V] {
newMap := &AuthenticatedMap[K, V]{
) *authenticatedMap[K, V] {
newMap := &authenticatedMap[K, V]{
rawKeysStore: kvstore.NewTypedStore(lo.PanicOnErr(store.WithExtendedRealm([]byte{prefixRawKeysStorage})), kToBytes, bytesToK, types.Empty.Bytes, types.EmptyFromBytes),
size: typedkey.NewNumber[uint64](store, prefixSizeKey),
root: typedkey.NewBytes(store, prefixRootKey),
Expand All @@ -64,12 +64,12 @@ func NewAuthenticatedMap[K, V any](
}

// WasRestoredFromStorage returns true if the map has been restored from storage.
func (m *AuthenticatedMap[K, V]) WasRestoredFromStorage() bool {
func (m *authenticatedMap[K, V]) WasRestoredFromStorage() bool {
return len(m.root.Get()) != 0
}

// Root returns the root of the state sparse merkle tree at the latest committed slot.
func (m *AuthenticatedMap[K, V]) Root() (root types.Identifier) {
func (m *authenticatedMap[K, V]) Root() (root types.Identifier) {
m.mutex.RLock()
defer m.mutex.RUnlock()

Expand All @@ -79,7 +79,7 @@ func (m *AuthenticatedMap[K, V]) Root() (root types.Identifier) {
}

// Set sets the output to unspent outputs set.
func (m *AuthenticatedMap[K, V]) Set(key K, value V) error {
func (m *authenticatedMap[K, V]) Set(key K, value V) error {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand Down Expand Up @@ -114,15 +114,15 @@ func (m *AuthenticatedMap[K, V]) Set(key K, value V) error {
}

// Size returns the number of elements in the map.
func (m *AuthenticatedMap[K, V]) Size() int {
func (m *authenticatedMap[K, V]) Size() int {
m.mutex.RLock()
defer m.mutex.RUnlock()

return int(m.size.Get())
}

// Commit persists the current state of the map to the storage.
func (m *AuthenticatedMap[K, V]) Commit() error {
func (m *authenticatedMap[K, V]) Commit() error {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand All @@ -132,7 +132,7 @@ func (m *AuthenticatedMap[K, V]) Commit() error {
}

// Delete removes the key from the map.
func (m *AuthenticatedMap[K, V]) Delete(key K) (deleted bool, err error) {
func (m *authenticatedMap[K, V]) Delete(key K) (deleted bool, err error) {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand Down Expand Up @@ -166,7 +166,7 @@ func (m *AuthenticatedMap[K, V]) Delete(key K) (deleted bool, err error) {
}

// Has returns true if the key is in the set.
func (m *AuthenticatedMap[K, V]) Has(key K) (has bool, err error) {
func (m *authenticatedMap[K, V]) Has(key K) (has bool, err error) {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand All @@ -179,7 +179,7 @@ func (m *AuthenticatedMap[K, V]) Has(key K) (has bool, err error) {
}

// Get returns the value for the given key.
func (m *AuthenticatedMap[K, V]) Get(key K) (value V, exists bool, err error) {
func (m *authenticatedMap[K, V]) Get(key K) (value V, exists bool, err error) {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand Down Expand Up @@ -210,7 +210,7 @@ func (m *AuthenticatedMap[K, V]) Get(key K) (value V, exists bool, err error) {
}

// Stream streams all the keys and values.
func (m *AuthenticatedMap[K, V]) Stream(callback func(key K, value V) error) error {
func (m *authenticatedMap[K, V]) Stream(callback func(key K, value V) error) error {
m.mutex.Lock()
defer m.mutex.Unlock()

Expand Down Expand Up @@ -252,7 +252,7 @@ func (m *AuthenticatedMap[K, V]) Stream(callback func(key K, value V) error) err
}

// has returns true if the key is in the map.
func (m *AuthenticatedMap[K, V]) has(keyBytes []byte) (has bool, err error) {
func (m *authenticatedMap[K, V]) has(keyBytes []byte) (has bool, err error) {
value, err := m.tree.Get(keyBytes)
if err != nil {
return false, ierrors.Wrap(err, "failed to get from tree")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package amap_test
package ads

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/iotaledger/hive.go/ads/amap"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore/mapdb"
"github.com/iotaledger/hive.go/lo"
Expand All @@ -15,7 +14,7 @@ var ErrStopIteration = ierrors.New("stop")

func TestMap(t *testing.T) {
store := mapdb.NewMapDB()
newMap := amap.NewAuthenticatedMap(store,
newMap := newAuthenticatedMap(store,
testKey.Bytes,
testKeyFromBytes,
testValue.Bytes,
Expand Down Expand Up @@ -81,20 +80,20 @@ func TestMap(t *testing.T) {
require.NoError(t, newMap.Commit())

// The root should be same if loading the same store to map
newMap1 := amap.NewAuthenticatedMap(store,
newMap1 := newAuthenticatedMap(store,
testKey.Bytes,
testKeyFromBytes,
testValue.Bytes,
testValueFromBytes,
)

require.True(t, newMap.WasRestoredFromStorage())
require.EqualValues(t, newMap.Root(), newMap1.Root())
}

func TestStreamMap(t *testing.T) {
store := mapdb.NewMapDB()
newMap := amap.NewAuthenticatedMap[testKey, testValue](store,
newMap := newAuthenticatedMap[testKey, testValue](store,
testKey.Bytes,
testKeyFromBytes,
testValue.Bytes,
Expand Down
30 changes: 30 additions & 0 deletions ads/authenticated_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ads

import (
"github.com/iotaledger/hive.go/ds/types"
"github.com/iotaledger/hive.go/kvstore"
)

// Set is a sparse merkle tree based set.
type authenticatedSet[K any] struct {
*authenticatedMap[K, types.Empty]
}

// NewAuthenticatedSet creates a new sparse merkle tree based set.
func newAuthenticatedSet[K any](store kvstore.KVStore, kToBytes kvstore.ObjectToBytes[K], bytesToK kvstore.BytesToObject[K]) *authenticatedSet[K] {
return &authenticatedSet[K]{
authenticatedMap: newAuthenticatedMap(store, kToBytes, bytesToK, types.Empty.Bytes, types.EmptyFromBytes),
}
}

// Add adds the key to the set.
func (s *authenticatedSet[K]) Add(key K) error {
return s.Set(key, types.Void)
}

// Stream iterates over the set and calls the callback for each element.
func (s *authenticatedSet[K]) Stream(callback func(key K) error) error {
return s.authenticatedMap.Stream(func(key K, _ types.Empty) error {
return callback(key)
})
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package aset_test
package ads

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/iotaledger/hive.go/ads/aset"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore/mapdb"
"github.com/iotaledger/hive.go/lo"
)

var ErrStopIteration = ierrors.New("stop")
var errStopIteration = ierrors.New("stop")

func TestSet(t *testing.T) {
store := mapdb.NewMapDB()
newSet := aset.NewAuthenticatedSet(
newSet := newAuthenticatedSet(
store,
testKey.Bytes,
testKeyFromBytes,
Expand Down Expand Up @@ -50,7 +49,7 @@ func TestSet(t *testing.T) {
require.NotEqualValues(t, root, root1)

// new set from old store, make sure the root is correct
newSet1 := aset.NewAuthenticatedSet(store,
newSet1 := newAuthenticatedSet(store,
testKey.Bytes,
testKeyFromBytes,
)
Expand All @@ -59,7 +58,7 @@ func TestSet(t *testing.T) {

func TestStreamSet(t *testing.T) {
store := mapdb.NewMapDB()
newSet := aset.NewAuthenticatedSet(store,
newSet := newAuthenticatedSet(store,
testKey.Bytes,
testKeyFromBytes,
)
Expand Down Expand Up @@ -90,27 +89,3 @@ func TestStreamSet(t *testing.T) {
require.ErrorIs(t, err, ErrStopIteration)
require.Equal(t, 1, len(firstSeen))
}

type testKey [1]byte

func (t testKey) Bytes() ([]byte, error) {
return t[:], nil
}

func testKeyFromBytes(b []byte) (testKey, int, error) {
return testKey(b), 1, nil
}

type testValue []byte

func testValueFromString(s string) testValue {
return testValue(s)
}

func (t testValue) Bytes() ([]byte, error) {
return t[:], nil
}

func testValueFromBytes(b []byte) (testValue, int, error) {
return b, len(b), nil
}

0 comments on commit b693e94

Please sign in to comment.