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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

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

return b.commitToDB(version, vnsize, ve, nve)
}

func (b *BoltDBVersioned) commitToDB(version uint64, vnsize map[string]int, ve, nve []*batch.WriteInfo) error {
Copy link
Member

@envestcc envestcc Dec 10, 2024

Choose a reason for hiding this comment

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

can simplify this function, it's a bit too long and difficult to understand.

Copy link
Member Author

Choose a reason for hiding this comment

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

all these logic need to be in db.Update(), i can go over to explain if you have any questions

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess what @envestcc means is:

  1. create at least three functions: createNamespace, processVE, and processNVE
  2. pass tx as parameter into these three functions

moreover, we may discard dedup, then the function will look like:

loop number of retries:
  for each entry in batch:
    if the entry is in vns:
      if the ns hasn't been created yet:
         call createNamespace
       call processVE
     else:
       call processNVE

// 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

return b.commitToDB(version, vnsize, ve, nve)
}

func (b *BoltDBVersioned) commitToDB(version uint64, vnsize map[string]int, ve, nve []*batch.WriteInfo) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess what @envestcc means is:

  1. create at least three functions: createNamespace, processVE, and processNVE
  2. pass tx as parameter into these three functions

moreover, we may discard dedup, then the function will look like:

loop number of retries:
  for each entry in batch:
    if the entry is in vns:
      if the ns hasn't been created yet:
         call createNamespace
       call processVE
     else:
       call processNVE

if entryKeySet[k] {
continue
}
if writeType == batch.Put {
Copy link
Member

Choose a reason for hiding this comment

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

still cann't understand why only set Put in entryKeySet?

Copy link
Member Author

Choose a reason for hiding this comment

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

discussed offline

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.

3 participants