Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db] implement CommitToDB() for BoltDBVersioned #4517

Merged
merged 9 commits into from
Jan 17, 2025
Merged

[db] implement CommitToDB() for BoltDBVersioned #4517

merged 9 commits into from
Jan 17, 2025

Conversation

dustinxie
Copy link
Member

Description

as title.

Fixes #(issue)

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

db/db_versioned.go Outdated Show resolved Hide resolved
db/db_versioned.go Outdated Show resolved Hide resolved
// CommitToDB write a batch to DB, where the batch can contain keys for
// both versioned and non-versioned namespace
func (b *BoltDBVersioned) CommitToDB(version uint64, vns map[string]bool, kvsb batch.KVStoreBatch) error {
vnsize, ve, nve, err := dedup(vns, kvsb)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split in caller side

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's doable, then the interface would become

(version uint64, vnsize map[string]int, ve, nve []*batch.WriteInfo)

feels a bit clumsy and too low-level, the current interface is cleaner IMO

)
// get bucket
bucket, ok := buckets[ns]
if !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, good check, fixed

switch write.WriteType() {
case batch.Put:
if bytes.Equal(key, _minKey) {
// create namespace
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create namespace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upon the first write to the namespace, it will create the metadata, updated the comment

continue
}
// wrong-size key should be caught in dedup(), but check anyway
if vnsize[ns] != len(key) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are if conditions (like L289, L309) that this check won't apply, so do it here

if val := bucket.Get(_minKey); val == nil {
// namespace not created yet
vn = &versionedNamespace{
keyLen: uint32(size),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you give an example of using different keyLen for current use case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, for Account namespace, keyLen = 20, for Contract, keyLen = 32

nonDBErr = true
return ErrInvalid
}
if err = bucket.Put(keyForWrite(key, version), val); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use actualKey

keyLen: uint32(size),
}
ve = append(ve, batch.NewWriteInfo(
batch.Put, ns, _minKey, vn.serialize(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the advantage of using _minKey, instead of a system namespace to store ns config as meta data?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when check every ns, the config is stored within itself, so each namespace is self-contained, and avoid need for an extra system namespace

db/db_versioned.go Outdated Show resolved Hide resolved
}

// Put writes a <key, value> record
func (b *BoltDBVersioned) Put(version uint64, ns string, key, value []byte) error {
if !b.db.IsReady() {
return ErrDBNotStarted
}
if _, ok := b.vns[ns]; !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use vn returned from vn, ok := b.vns[ns] for line 112, so we don't need to read from disk every time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, fixed

@@ -110,11 +135,17 @@ func (b *BoltDBVersioned) Get(version uint64, ns string, key []byte) ([]byte, er
if !b.db.IsReady() {
return nil, ErrDBNotStarted
}
if _, ok := b.vns[ns]; !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@@ -161,6 +192,9 @@ func (b *BoltDBVersioned) Delete(version uint64, ns string, key []byte) error {
if !b.db.IsReady() {
return ErrDBNotStarted
}
if _, ok := b.vns[ns]; !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

@@ -184,6 +221,9 @@ func (b *BoltDBVersioned) Version(ns string, key []byte) (uint64, error) {
if !b.db.IsReady() {
return 0, ErrDBNotStarted
}
if _, ok := b.vns[ns]; !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

Comment on lines 275 to 281
val := bucket.Get(_minKey)
if val == nil {
// namespace not created yet
nonDBErr = true
return errors.Wrapf(ErrInvalid, "namespace %s has not been added", ns)
}
vn, err := deserializeVersionedNamespace(val)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use b.vns as well?

db/db_versioned.go Outdated Show resolved Hide resolved
db/db_versioned.go Outdated Show resolved Hide resolved
db/db_versioned.go Outdated Show resolved Hide resolved
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
6.3% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@dustinxie dustinxie merged commit 8abedd1 into master Jan 17, 2025
3 of 4 checks passed
@dustinxie dustinxie deleted the commitodb branch January 17, 2025 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants