-
Notifications
You must be signed in to change notification settings - Fork 328
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
Conversation
db/db_versioned.go
Outdated
// 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
split in caller side
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ok == false, panic, because of https://github.com/iotexproject/iotex-core/pull/4517/files#diff-dd423fbb3d496fc9cbbda508288b69c003928d4937de168e9cf69be774f8f19fR224
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, good check, fixed
db/db_versioned.go
Outdated
switch write.WriteType() { | ||
case batch.Put: | ||
if bytes.Equal(key, _minKey) { | ||
// create namespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create namespace?
There was a problem hiding this comment.
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
db/db_versioned.go
Outdated
continue | ||
} | ||
// wrong-size key should be caught in dedup(), but check anyway | ||
if vnsize[ns] != len(key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
db/db_versioned.go
Outdated
if val := bucket.Get(_minKey); val == nil { | ||
// namespace not created yet | ||
vn = &versionedNamespace{ | ||
keyLen: uint32(size), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
db/db_versioned.go
Outdated
nonDBErr = true | ||
return ErrInvalid | ||
} | ||
if err = bucket.Put(keyForWrite(key, version), val); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use actualKey
db/db_versioned.go
Outdated
keyLen: uint32(size), | ||
} | ||
ve = append(ve, batch.NewWriteInfo( | ||
batch.Put, ns, _minKey, vn.serialize(), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
7e35527
to
cac2874
Compare
db/db_versioned.go
Outdated
} | ||
|
||
// 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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, fixed
db/db_versioned.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
db/db_versioned.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
db/db_versioned.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
db/db_versioned.go
Outdated
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) |
There was a problem hiding this comment.
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?
Quality Gate failedFailed conditions |
Description
as title.
Fixes #(issue)
Type of change
Please delete options that are not relevant.
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
Test Configuration:
Checklist: