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] fix Size() and refactor counting index interface #4490

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

Conversation

dustinxie
Copy link
Member

Description

improve the counting index interface.

Fixes #4489

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 force-pushed the fixcounting branch 3 times, most recently from 2673dd6 to 7642dea Compare November 15, 2024 01:02
@@ -31,8 +31,14 @@ type (
CountingIndex interface {
// Size returns the total number of keys so far
Size() uint64
// Add inserts a value into the index
Add([]byte, bool) error
// AddOne inserts one entry into the index
Copy link
Member

Choose a reason for hiding this comment

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

suggest following interface, it seems more clear:

CountingIndex interface{
    Size() uint64
    Add([]byte, bool) error
    Get(uint64) ([]byte, error)
    Range(uint64, uint64) ([][]byte, error)
    Revert(uint64) error
    Close()
    
    // batch support
    BatchAdd(batch, []byte) error
    Commit(batch) error
}

Copy link
Collaborator

@CoderZhi CoderZhi left a comment

Choose a reason for hiding this comment

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

counting index maintains the logic without any in memory storage. The only dependency countKey has a copy in kvstore and a copy in the batch we are going to write. If everything is processed in batch, why the readiness, aka lock is needed?

if inBatch {
return c.addBatch(value)
func (c *countingIndex) AddOne(value []byte) error {
if err := c.TurnOn(); 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.

the definition of TurnOn is the service is ready, which doesn't match the usage here.

Comment on lines 35 to 39
AddOne([]byte) error
// AddMultiple inserts multiple entries into the index
AddMultiple([][]byte) error
// AddToExternalBatch inserts entries into external batch
AddToExternalBatch(batch.KVStoreBatch, [][]byte) error
Copy link
Collaborator

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 distinguish internal and external?

Copy link

sonarcloud bot commented Nov 18, 2024

}, nil
}

// Size returns the total number of keys so far
func (c *countingIndex) Size() uint64 {
return atomic.LoadUint64(&c.size)
c.lock.RLock()
Copy link
Member

Choose a reason for hiding this comment

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

didn't see the benefits of the changes

}

func (c *countingIndex) BeginBatch(b batch.KVStoreBatch) {
c.lock.Lock()
Copy link
Member

Choose a reason for hiding this comment

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

why lock at begin instead of end?

// AddToBatch inserts an entry into external batch
AddToBatch(batch.KVStoreBatch, []byte)
// BeginBatch should be called before writing the batch
BeginBatch(batch.KVStoreBatch)
Copy link
Member

Choose a reason for hiding this comment

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

maybe can put PreCommit and PostCommit into batch intead

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.

failed to get block at height
3 participants