Skip to content

Commit

Permalink
genric metadata version
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro committed Sep 24, 2024
1 parent 29aba2f commit a73f08c
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 74 deletions.
14 changes: 9 additions & 5 deletions platform/common/core/generic/vault/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TTestInterceptorConcurrency(t *testing.T, ddb VersionedPersistence, vp arti

err = ddb.BeginUpdate()
assert.NoError(t, err)
err = ddb.SetStateMetadata(ns, mk, map[string][]byte{"k": []byte("v")}, 36, 2)
err = ddb.SetStateMetadata(ns, mk, map[string][]byte{"k": []byte("v")}, nil)
assert.NoError(t, err)
err = ddb.Commit()
assert.NoError(t, err)
Expand Down Expand Up @@ -718,7 +718,7 @@ func TTestRun(t *testing.T, db1, db2 VersionedPersistence, vp artifactsProvider)
assert.NoError(t, err)
err = db1.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: fver.ToBytes(35, 1)})
assert.NoError(t, err)
err = db1.SetStateMetadata(ns, k1Meta, map[string][]byte{"metakey": []byte("metavalue")}, 35, 1)
err = db1.SetStateMetadata(ns, k1Meta, map[string][]byte{"metakey": []byte("metavalue")}, nil)
assert.NoError(t, err)
err = db1.Commit()
assert.NoError(t, err)
Expand All @@ -727,7 +727,7 @@ func TTestRun(t *testing.T, db1, db2 VersionedPersistence, vp artifactsProvider)
assert.NoError(t, err)
err = db2.SetState(ns, k1, VersionedValue{Raw: []byte("v1"), Version: fver.ToBytes(35, 1)})
assert.NoError(t, err)
err = db2.SetStateMetadata(ns, k1Meta, map[string][]byte{"metakey": []byte("metavalue")}, 35, 1)
err = db2.SetStateMetadata(ns, k1Meta, map[string][]byte{"metakey": []byte("metavalue")}, nil)
assert.NoError(t, err)
err = db2.Commit()
assert.NoError(t, err)
Expand Down Expand Up @@ -1079,9 +1079,13 @@ func TTestRun(t *testing.T, db1, db2 VersionedPersistence, vp artifactsProvider)
assert.Equal(t, VersionedValue{Raw: []byte("v2_updated"), Version: fver.ToBytes(35, 2)}, vv1)
assert.Equal(t, vv1, vv2)

meta1, b1, t1, err := db1.GetStateMetadata(ns, k1Meta)
meta1, ver1, err := db1.GetStateMetadata(ns, k1Meta)
assert.NoError(t, err)
meta2, b2, t2, err := db2.GetStateMetadata(ns, k1Meta)
b1, t1, err := fver.FromBytes(ver1)
assert.NoError(t, err)
meta2, ver2, err := db2.GetStateMetadata(ns, k1Meta)
assert.NoError(t, err)
b2, t2, err := fver.FromBytes(ver2)
assert.NoError(t, err)
assert.Equal(t, map[string][]byte{"newmetakey": []byte("newmetavalue")}, meta1)
assert.Equal(t, uint64(35), b1)
Expand Down
13 changes: 6 additions & 7 deletions platform/common/core/generic/vault/queryexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
package vault

import (
"github.com/hyperledger-labs/fabric-smart-client/platform/common/core/generic/vault/fver"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
)

Expand All @@ -32,11 +31,11 @@ func (q *directQueryExecutor[V]) GetStateRangeScanIterator(namespace driver.Name
}

func (q *directQueryExecutor[V]) GetStateMetadata(namespace driver.Namespace, key driver.PKey) (driver.Metadata, driver.RawVersion, error) {
m, b, index, err := q.vault.store.GetStateMetadata(namespace, key)
m, version, err := q.vault.store.GetStateMetadata(namespace, key)
if err != nil {
return nil, driver.RawVersion{}, err
return nil, nil, err
}
return m, fver.ToBytes(b, index), nil
return m, version, nil
}

func (q *directQueryExecutor[V]) Done() {
Expand All @@ -54,11 +53,11 @@ func (i *interceptorQueryExecutor[V]) Done() {
}

func (i *interceptorQueryExecutor[V]) GetStateMetadata(namespace driver.Namespace, key driver.PKey) (driver.Metadata, driver.RawVersion, error) {
m, b, txIndex, err := i.store.GetStateMetadata(namespace, key)
m, version, err := i.store.GetStateMetadata(namespace, key)
if err != nil {
return nil, driver.RawValue{}, err
return nil, nil, err
}
return m, fver.ToBytes(b, txIndex), nil
return m, version, nil
}

func (i *interceptorQueryExecutor[V]) GetState(namespace driver.Namespace, key driver.PKey) (VersionedValue, error) {
Expand Down
2 changes: 1 addition & 1 deletion platform/common/core/generic/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (db *Vault[V]) storeMetaWrites(ctx context.Context, writes NamespaceKeyedMe
span := trace.SpanFromContext(ctx)
for ns, keyMap := range writes {
span.AddEvent("set_tx_metadata_state")
if errs := db.store.SetStateMetadatas(ns, keyMap, block, indexInBloc); len(errs) > 0 {
if errs := db.store.SetStateMetadatas(ns, keyMap, nil); len(errs) > 0 {
return db.discard(ns, block, indexInBloc, errs)
}
}
Expand Down
30 changes: 21 additions & 9 deletions platform/view/services/db/dbtest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,18 @@ func TTestMeta(t *testing.T, db driver.TransactionalVersionedPersistence) {
assert.NoError(t, err)
assert.Equal(t, driver.VersionedValue{Raw: []byte("val"), Version: fver.ToBytes(35, 1)}, vv)

m, bn, tn, err := db.GetStateMetadata(ns, key)
m, ver, err := db.GetStateMetadata(ns, key)
assert.NoError(t, err)
assert.Len(t, m, 0)
bn, tn, err := fver.FromBytes(ver)
assert.NoError(t, err)
assert.Equal(t, uint64(35), bn)
assert.Equal(t, uint64(1), tn)

err = db.BeginUpdate()
assert.NoError(t, err)

err = db.SetStateMetadata(ns, key, map[string][]byte{"foo": []byte("bar")}, 36, 2)
err = db.SetStateMetadata(ns, key, map[string][]byte{"foo": []byte("bar")}, nil)
assert.NoError(t, err)

err = db.Commit()
Expand All @@ -221,7 +223,9 @@ func TTestMeta(t *testing.T, db driver.TransactionalVersionedPersistence) {
assert.NoError(t, err)
assert.Equal(t, driver.VersionedValue{Raw: []byte("val"), Version: fver.ToBytes(36, 2)}, vv)

m, bn, tn, err = db.GetStateMetadata(ns, key)
m, ver, err = db.GetStateMetadata(ns, key)
assert.NoError(t, err)
bn, tn, err = fver.FromBytes(ver)
assert.NoError(t, err)
assert.Equal(t, map[string][]byte{"foo": []byte("bar")}, m)
assert.Equal(t, uint64(36), bn)
Expand All @@ -238,7 +242,9 @@ func TTestSimpleReadWrite(t *testing.T, db driver.TransactionalVersionedPersiste
assert.Equal(t, driver.VersionedValue{}, vv)

// empty metadata
m, bn, tn, err := db.GetStateMetadata(ns, key)
m, ver, err := db.GetStateMetadata(ns, key)
assert.NoError(t, err)
bn, tn, err := fver.FromBytes(ver)
assert.NoError(t, err)
assert.Len(t, m, 0)
assert.Equal(t, uint64(0), bn)
Expand Down Expand Up @@ -362,33 +368,39 @@ func TTestMetadata(t *testing.T, db driver.TransactionalVersionedPersistence) {
ns := "namespace"
key := "foo"

md, bn, txn, err := db.GetStateMetadata(ns, key)
md, ver, err := db.GetStateMetadata(ns, key)
assert.NoError(t, err)
assert.Nil(t, md)
bn, txn, err := fver.FromBytes(ver)
assert.NoError(t, err)
assert.Equal(t, uint64(0x0), bn)
assert.Equal(t, uint64(0x0), txn)

err = db.BeginUpdate()
assert.NoError(t, err)
err = db.SetStateMetadata(ns, key, map[string][]byte{"foo": []byte("bar")}, 35, 1)
err = db.SetStateMetadata(ns, key, map[string][]byte{"foo": []byte("bar")}, nil)
assert.NoError(t, err)
err = db.Commit()
assert.NoError(t, err)

md, bn, txn, err = db.GetStateMetadata(ns, key)
md, ver, err = db.GetStateMetadata(ns, key)
assert.NoError(t, err)
bn, txn, err = fver.FromBytes(ver)
assert.NoError(t, err)
assert.Equal(t, map[string][]byte{"foo": []byte("bar")}, md)
assert.Equal(t, uint64(35), bn)
assert.Equal(t, uint64(1), txn)

err = db.BeginUpdate()
assert.NoError(t, err)
err = db.SetStateMetadata(ns, key, map[string][]byte{"foo1": []byte("bar1")}, 36, 2)
err = db.SetStateMetadata(ns, key, map[string][]byte{"foo1": []byte("bar1")}, nil)
assert.NoError(t, err)
err = db.Commit()
assert.NoError(t, err)

md, bn, txn, err = db.GetStateMetadata(ns, key)
md, ver, err = db.GetStateMetadata(ns, key)
assert.NoError(t, err)
bn, txn, err = fver.FromBytes(ver)
assert.NoError(t, err)
assert.Equal(t, map[string][]byte{"foo1": []byte("bar1")}, md)
assert.Equal(t, uint64(36), bn)
Expand Down
20 changes: 7 additions & 13 deletions platform/view/services/db/driver/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/proto"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/core/generic/vault/fver"
driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/common"
Expand Down Expand Up @@ -144,7 +143,7 @@ func (db *DB) SetStates(namespace driver2.Namespace, kvs map[driver2.PKey]driver
return errs
}

func (db *DB) SetStateMetadata(namespace driver2.Namespace, key driver2.PKey, metadata driver2.Metadata, block driver2.BlockNum, txnum driver2.TxNum) error {
func (db *DB) SetStateMetadata(namespace driver2.Namespace, key driver2.PKey, metadata driver2.Metadata, version driver2.RawVersion) error {
if db.Txn == nil {
panic("programming error, writing without ongoing update")
}
Expand All @@ -157,7 +156,7 @@ func (db *DB) SetStateMetadata(namespace driver2.Namespace, key driver2.PKey, me
}

v.Meta = metadata
v.KeyVersion = fver.ToBytes(block, txnum)
v.KeyVersion = version

bytes, err := proto.Marshal(v)
if err != nil {
Expand All @@ -172,10 +171,10 @@ func (db *DB) SetStateMetadata(namespace driver2.Namespace, key driver2.PKey, me
return nil
}

func (db *DB) SetStateMetadatas(ns driver2.Namespace, kvs map[driver2.PKey]driver2.Metadata, block driver2.BlockNum, txnum driver2.TxNum) map[driver2.PKey]error {
func (db *DB) SetStateMetadatas(ns driver2.Namespace, kvs map[driver2.PKey]driver2.Metadata, version driver2.RawVersion) map[driver2.PKey]error {
errs := make(map[driver2.PKey]error)
for pkey, value := range kvs {
if err := db.SetStateMetadata(ns, pkey, value, block, txnum); err != nil {
if err := db.SetStateMetadata(ns, pkey, value, version); err != nil {
errs[pkey] = err
}
}
Expand Down Expand Up @@ -237,23 +236,18 @@ func (db *DB) GetStateSetIterator(ns driver2.Namespace, keys ...driver2.PKey) (d
return &keys2.DummyVersionedIterator{Items: reads}, nil
}

func (db *DB) GetStateMetadata(namespace driver2.Namespace, key driver2.PKey) (driver2.Metadata, driver2.BlockNum, driver2.TxNum, error) {
func (db *DB) GetStateMetadata(namespace driver2.Namespace, key driver2.PKey) (driver2.Metadata, driver2.RawVersion, error) {
dbKey := dbKey(namespace, key)

txn := &Txn{db.db.NewTransaction(false)}
defer txn.Discard()

v, err := txVersionedValue(txn, dbKey)
if err != nil {
return nil, 0, 0, err
return nil, nil, err
}

block, txnum, err := fver.FromBytes(v.KeyVersion)
if err != nil {
return nil, 0, 0, err
}

return v.Meta, block, txnum, nil
return v.Meta, v.KeyVersion, nil
}

func (db *DB) NewWriteTransaction() (driver.WriteTransaction, error) {
Expand Down
10 changes: 8 additions & 2 deletions platform/view/services/db/driver/badger/badger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"time"
"unicode/utf8"

"github.com/hyperledger-labs/fabric-smart-client/platform/common/core/generic/vault/fver"

"github.com/dgraph-io/badger/v3"
"github.com/golang/protobuf/proto"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/dbtest"
Expand Down Expand Up @@ -102,9 +104,11 @@ func TestMarshallingErrors(t *testing.T) {
assert.Contains(t, err.Error(), "could not unmarshal VersionedValue for key ")
assert.Equal(t, driver.VersionedValue{}, vv)

m, bn, tn, err := db.GetStateMetadata(ns, key)
m, ver, err := db.GetStateMetadata(ns, key)
assert.Contains(t, err.Error(), "could not unmarshal VersionedValue for key")
assert.Len(t, m, 0)
bn, tn, err := fver.FromBytes(ver)
assert.NoError(t, err)
assert.Equal(t, uint64(0), bn)
assert.Equal(t, uint64(0), tn)

Expand All @@ -122,8 +126,10 @@ func TestMarshallingErrors(t *testing.T) {
assert.EqualError(t, err, "could not get value for key ns\x00key: invalid fver, expected 1, got 34")
assert.Equal(t, driver.VersionedValue{}, vv)

m, bn, tn, err = db.GetStateMetadata(ns, key)
m, ver, err = db.GetStateMetadata(ns, key)
assert.EqualError(t, err, "could not get value for key ns\x00key: invalid fver, expected 1, got 34")
bn, tn, err = fver.FromBytes(ver)
assert.NoError(t, err)
assert.Len(t, m, 0)
assert.Equal(t, uint64(0), bn)
assert.Equal(t, uint64(0), tn)
Expand Down
6 changes: 3 additions & 3 deletions platform/view/services/db/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ type UnversionedPersistence interface {
type VersionedPersistence interface {
BasePersistence[VersionedValue, VersionedRead]
// GetStateMetadata gets the metadata and version for given namespace and key
GetStateMetadata(namespace driver.Namespace, key driver.PKey) (driver.Metadata, driver.BlockNum, driver.TxNum, error)
GetStateMetadata(namespace driver.Namespace, key driver.PKey) (driver.Metadata, driver.RawVersion, error)
// SetStateMetadata sets the given metadata for the given namespace, key, and version
SetStateMetadata(namespace driver.Namespace, key driver.PKey, metadata driver.Metadata, block driver.BlockNum, txnum driver.TxNum) error
SetStateMetadata(namespace driver.Namespace, key driver.PKey, metadata driver.Metadata, version driver.RawVersion) error
// SetStateMetadatas sets the given metadata for the given namespace, keys, and version
SetStateMetadatas(ns driver.Namespace, kvs map[driver.PKey]driver.Metadata, block driver.BlockNum, txnum driver.TxNum) map[driver.PKey]error
SetStateMetadatas(ns driver.Namespace, kvs map[driver.PKey]driver.Metadata, version driver.RawVersion) map[driver.PKey]error
}

type WriteTransaction interface {
Expand Down
10 changes: 5 additions & 5 deletions platform/view/services/db/driver/notifier/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,16 @@ func (db *VersionedPersistenceNotifier[P]) GetState(namespace driver2.Namespace,
return db.Persistence.GetState(namespace, key)
}

func (db *VersionedPersistenceNotifier[P]) GetStateMetadata(namespace driver2.Namespace, key driver2.PKey) (driver2.Metadata, driver2.BlockNum, driver2.TxNum, error) {
func (db *VersionedPersistenceNotifier[P]) GetStateMetadata(namespace driver2.Namespace, key driver2.PKey) (driver2.Metadata, driver2.RawVersion, error) {
return db.Persistence.GetStateMetadata(namespace, key)
}

func (db *VersionedPersistenceNotifier[P]) SetStateMetadata(namespace driver2.Namespace, key driver2.PKey, metadata driver2.Metadata, block driver2.BlockNum, txnum driver2.TxNum) error {
return db.Persistence.SetStateMetadata(namespace, key, metadata, block, txnum)
func (db *VersionedPersistenceNotifier[P]) SetStateMetadata(namespace driver2.Namespace, key driver2.PKey, metadata driver2.Metadata, version driver2.RawVersion) error {
return db.Persistence.SetStateMetadata(namespace, key, metadata, nil)
}

func (db *VersionedPersistenceNotifier[P]) SetStateMetadatas(ns driver2.Namespace, kvs map[driver2.PKey]driver2.Metadata, block driver2.BlockNum, txnum driver2.TxNum) map[driver2.PKey]error {
return db.Persistence.SetStateMetadatas(ns, kvs, block, txnum)
func (db *VersionedPersistenceNotifier[P]) SetStateMetadatas(ns driver2.Namespace, kvs map[driver2.PKey]driver2.Metadata, version driver2.RawVersion) map[driver2.PKey]error {
return db.Persistence.SetStateMetadatas(ns, kvs, nil)
}

func (db *VersionedPersistenceNotifier[P]) GetStateRangeScanIterator(namespace driver2.Namespace, startKey, endKey driver2.PKey) (driver.VersionedResultsIterator, error) {
Expand Down
Loading

0 comments on commit a73f08c

Please sign in to comment.