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 Get/Put/Delete/Version() for BoltDBVersioned #4256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dustinxie
Copy link
Member

@dustinxie dustinxie commented Apr 26, 2024

Description

as title, implement 4 methos for versioned DB.

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

@dustinxie dustinxie marked this pull request as draft April 26, 2024 23:56
@dustinxie dustinxie marked this pull request as ready for review May 2, 2024 23:04
@dustinxie dustinxie changed the title [db] implement Get/Put/Version() for BoltDBVersioned [db] implement Get/Put/Delete/Version() for BoltDBVersioned May 2, 2024
Copy link

codecov bot commented May 3, 2024

Codecov Report

Attention: Patch coverage is 90.00000% with 19 lines in your changes missing coverage. Please review.

Project coverage is 76.76%. Comparing base (cc526cd) to head (e8c1e97).
Report is 43 commits behind head on master.

Current head e8c1e97 differs from pull request most recent head d651871

Please upload reports for the commit d651871 to get more accurate results.

Files Patch % Lines
db/db_versioned.go 86.82% 16 Missing and 1 partial ⚠️
db/db_versioned_util.go 96.72% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4256      +/-   ##
==========================================
- Coverage   77.02%   76.76%   -0.27%     
==========================================
  Files         342      347       +5     
  Lines       29406    29415       +9     
==========================================
- Hits        22651    22581      -70     
- Misses       5650     5737      +87     
+ Partials     1105     1097       -8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

db/db_versioned_util.go Outdated Show resolved Hide resolved
db/db_versioned_util.go Outdated Show resolved Hide resolved
db/db_versioned.go Outdated Show resolved Hide resolved
return 0
}

func (km *keyMeta) deleteInBetween(write, read uint64) (bool, error) {
Copy link
Member

Choose a reason for hiding this comment

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

naming func to notDeleteInBetween()?

Copy link
Member Author

Choose a reason for hiding this comment

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

updated name

return false, ErrDeleted
}
// delete and write fall on the same version, need to check further
// if it's write-after-delete or delete-after-write
Copy link
Member

Choose a reason for hiding this comment

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

For each singel version, only the last record (delete/put) can be retained, which can help avoid this.

return err
}
if version == km.lastVersion {
// write <key, nil> to indicate this is a delete-after-write
Copy link
Member

Choose a reason for hiding this comment

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

what if the two updates on same version:

del k
put k nil

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, but this will be a very rare corner case

  1. usually a key's value is some valid data
  2. if the user intends to use nil as a special case, then he can use a special []byte value to avoid the corner case

Copy link

sonarcloud bot commented Jul 29, 2024

Quality Gate Failed Quality Gate failed

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

See analysis details on SonarCloud

// Put writes a <key, value> record
func (b *BoltDBVersioned) Put(ns string, version uint64, key, value []byte) error {
func (b *BoltDBVersioned) Put(version uint64, ns string, key, value []byte) error {
Copy link
Member

@envestcc envestcc Jul 31, 2024

Choose a reason for hiding this comment

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

Do we really need to implement these single functionPut/Delete? I prefer only keep WriteBatch, b/c version update will be atomic, and also can simplify the impl

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, it does not hurt to keep it

// not allowed to perform write on an earlier version
return ErrInvalid
}
buf.Delete(ns, keyForDelete(key, version), fmt.Sprintf("failed to delete key %x", key))
Copy link
Member

Choose a reason for hiding this comment

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

why delete?

}
buf := batch.NewBatch()
buf.Put(ns, keyForDelete(key, version), nil, fmt.Sprintf("failed to delete key %x", key))
buf.Delete(ns, keyForWrite(key, version), fmt.Sprintf("failed to delete key %x", key))
Copy link
Member

Choose a reason for hiding this comment

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

why delete write key?

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

  1. it is to keep only the last operation on the key
  2. i recall in the original POC, we have cases where the same key is deleted, then written in the batch at around height 400k. So it is a valid request (to delete then write on the same height). We'll allow this behavior (and also write then delete on the same height)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants