Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandros Filios <[email protected]>
  • Loading branch information
alexandrosfilios committed Oct 5, 2024
1 parent e483af8 commit 4dc84b0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 67 deletions.
54 changes: 25 additions & 29 deletions docs/fabric/fabricdev/core/fabricdev/vault/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions platform/common/core/generic/vault/rwset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 19 additions & 35 deletions platform/view/services/db/driver/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 4dc84b0

Please sign in to comment.