-
Notifications
You must be signed in to change notification settings - Fork 649
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
CreateBucketIfNotExists try opening bucket before creating #504
Conversation
return b.Bucket(key), nil | ||
} else if err != nil { | ||
return nil, err | ||
child := b.Bucket(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.
Side effect: When the db is opened in readonly mode, the caller can get a valid bucket if present.
The existing implementation will return a ErrTxNotWritable
error.
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.
Suggest not to change the behavior.
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.
Calling CreateBucketIfNotExists
in read-only mode doesn't make sense. I don't think anybody relied on this behavior.
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.
This PR creates a hole (readonly transaction can even create bucket (actually already exist) ) which might cause unnecessary confusion to users. It can even be regarded as a minor harmless CVE.
Please add the following code at the beginning of the method,
if b.tx.db == nil {
return nil, ErrTxClosed
} else if !b.tx.writable {
return nil, ErrTxNotWritable
} else if len(key) == 0 {
return nil, ErrBucketNameRequired
}
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.
Added.
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.
Also added benchmark results. See PR description. It doesn't help with the operation speed but reduces allocations when there are many buckets which is a valid use cases where buckets are dynamically generated.
Signed-off-by: Cenk Alti <cenkalti@gmail.com>
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.
LGTM
Thanks
Just as #118 (comment) mentioned, it's a tradeoff. When it's highly likely the bucket doesn't exist, then If you really want the PR makes sense for both cases,
Attached the patch, feel free to apply the patch if it makes sense to you. |
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.
please update this PR per #504 (comment)
@ahrtr I think the patch adds too much code (+complexity) to gain little benefit. My first intention was to optimize the code for cases where a lot of buckets are automatically generated and kept around.
So that's why I sent this PR. We should either accept the PR as is or reject and close the issue as wontfix. |
Just as I mentioned, you PR only applies to one case. You resolved one "issue", but introduced another "issue". It isn't accepted.
The point is we should do the right thing. Although it's a little complicated, it's still under control and should be accepted.
Let's close this PR, and raise a new one with my patch. |
FYI. #532 |
Fixes #118
Benchmark with 1 million keys (137 MB database):
Benchmark with 10 million keys (1.2 GB database):
Benchmark code: 97e5461