From 4dc84b0086d56324345de44b1b255c8559a5bf70 Mon Sep 17 00:00:00 2001 From: Alexandros Filios Date: Sat, 5 Oct 2024 15:02:48 +0200 Subject: [PATCH] Review feedback Signed-off-by: Alexandros Filios --- .../fabricdev/core/fabricdev/vault/version.go | 54 +++++++++---------- platform/common/core/generic/vault/rwset.go | 4 +- .../view/services/db/driver/badger/badger.go | 54 +++++++------------ 3 files changed, 45 insertions(+), 67 deletions(-) diff --git a/docs/fabric/fabricdev/core/fabricdev/vault/version.go b/docs/fabric/fabricdev/core/fabricdev/vault/version.go index 86d60fcda..8db8121bf 100644 --- a/docs/fabric/fabricdev/core/fabricdev/vault/version.go +++ b/docs/fabric/fabricdev/core/fabricdev/vault/version.go @@ -22,47 +22,43 @@ func (c *CounterBasedVersionBuilder) VersionedValues(rws *vault.ReadWriteSet, ns reads := rws.Reads[ns] for pkey, val := range writes { - // Search the corresponding read. - version, ok := reads[pkey] - if ok { - // parse the version as an integer, then increment it - counter, err := Unmarshal(version) - if err != nil { - return nil, errors.Wrapf(err, "failed unmarshalling version for %s:%v", pkey, version) - } - version = Marshal(counter + 1) - } else { - // this is a blind write, we should check the vault. - // Let's assume here that a blind write always starts from version 0 - version = Marshal(0) + v, err := version(reads, pkey) + if err != nil { + return nil, err } - - vals[pkey] = vault.VersionedValue{Raw: val, Version: version} + vals[pkey] = vault.VersionedValue{Raw: val, Version: v} } return vals, nil } +func version(reads vault.NamespaceReads, pkey driver.PKey) (vault.Version, error) { + // Search the corresponding read. + v, ok := reads[pkey] + if !ok { + // this is a blind write, we should check the vault. + // Let's assume here that a blind write always starts from version 0 + return Marshal(0), nil + } + + // parse the version as an integer, then increment it + counter, err := Unmarshal(v) + if err != nil { + return nil, errors.Wrapf(err, "failed unmarshalling version for %s:%v", pkey, v) + } + return Marshal(counter + 1), nil +} + func (c *CounterBasedVersionBuilder) VersionedMetaValues(rws *vault.ReadWriteSet, ns driver.Namespace, writes vault.KeyedMetaWrites, block driver.BlockNum, indexInBloc driver.TxNum) (map[driver.PKey]driver.VersionedMetadataValue, error) { vals := make(map[driver.PKey]driver.VersionedMetadataValue, len(writes)) reads := rws.Reads[ns] for pkey, val := range writes { - // Search the corresponding read. - version, ok := reads[pkey] - if ok { - // parse the version as an integer, then increment it - counter, err := Unmarshal(version) - if err != nil { - return nil, errors.Wrapf(err, "failed unmarshalling version for %s:%v", pkey, version) - } - version = Marshal(counter + 1) - } else { - // this is a blind write, we should check the vault. - // Let's assume here that a blind write always starts from version 0 - version = Marshal(0) + v, err := version(reads, pkey) + if err != nil { + return nil, err } - vals[pkey] = driver.VersionedMetadataValue{Metadata: val, Version: version} + vals[pkey] = driver.VersionedMetadataValue{Metadata: val, Version: v} } return vals, nil } diff --git a/platform/common/core/generic/vault/rwset.go b/platform/common/core/generic/vault/rwset.go index 498adee56..e0a9ba80a 100644 --- a/platform/common/core/generic/vault/rwset.go +++ b/platform/common/core/generic/vault/rwset.go @@ -145,9 +145,7 @@ type Version = driver.RawVersion type NamespaceReads map[string]Version func (r NamespaceReads) Equals(o NamespaceReads) error { - return entriesEqual(r, o, func(v, v2 Version) bool { - return bytes.Equal(v, v2) - }) + return entriesEqual(r, o, bytes.Equal) } type Reads map[driver.Namespace]NamespaceReads diff --git a/platform/view/services/db/driver/badger/badger.go b/platform/view/services/db/driver/badger/badger.go index 53d26bc49..8de574841 100644 --- a/platform/view/services/db/driver/badger/badger.go +++ b/platform/view/services/db/driver/badger/badger.go @@ -105,29 +105,41 @@ func (db *DB) SetState(namespace driver2.Namespace, key string, value driver.Ver logger.Warnf("set key [%s:%v] to nil value, will be deleted instead", key, value.Version) return db.DeleteState(namespace, key) } + return db.setValues(namespace, key, value.Raw, nil, value.Version) +} +func (db *DB) SetStateMetadata(namespace driver2.Namespace, key driver2.PKey, metadata driver2.Metadata, version driver2.RawVersion) error { + return db.setValues(namespace, key, nil, metadata, version) +} + +func (db *DB) setValues(namespace driver2.Namespace, key driver2.PKey, value driver2.RawValue, metadata driver2.Metadata, version driver2.RawVersion) error { if db.Txn == nil { panic("programming error, writing without ongoing update") } - dbKey := dbKey(namespace, key) + k := dbKey(namespace, key) - v, err := txVersionedValue(db.Txn, dbKey) + v, err := txVersionedValue(db.Txn, k) if err != nil { return err } - v.Value = value.Raw - v.KeyVersion = value.Version + if value != nil { + v.Value = value + } + if metadata != nil { + v.Meta = metadata + } + v.KeyVersion = version bytes, err := proto.Marshal(v) if err != nil { - return errors.Wrapf(err, "could not marshal VersionedValue for key %s", dbKey) + return errors.Wrapf(err, "could not marshal VersionedValue for key %s", k) } - err = db.Txn.Set([]byte(dbKey), bytes) + err = db.Txn.Set([]byte(k), bytes) if err != nil { - return errors.Wrapf(err, "could not set value for key %s", dbKey) + return errors.Wrapf(err, "could not set value for key %s", k) } return nil @@ -143,34 +155,6 @@ 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, version driver2.RawVersion) error { - if db.Txn == nil { - panic("programming error, writing without ongoing update") - } - - dbKey := dbKey(namespace, key) - - v, err := txVersionedValue(db.Txn, dbKey) - if err != nil { - return err - } - - v.Meta = metadata - v.KeyVersion = version - - bytes, err := proto.Marshal(v) - if err != nil { - return errors.Wrapf(err, "could not marshal VersionedValue for key %s", dbKey) - } - - err = db.Txn.Set([]byte(dbKey), bytes) - if err != nil { - return errors.Wrapf(err, "could not set value for key %s", dbKey) - } - - return nil -} - func (db *DB) SetStateMetadatas(ns driver2.Namespace, kvs map[driver2.PKey]driver.VersionedMetadataValue) map[driver2.PKey]error { errs := make(map[driver2.PKey]error) for pkey, value := range kvs {