-
Notifications
You must be signed in to change notification settings - Fork 25
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
SetIfAbsent #77
SetIfAbsent #77
Conversation
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.
Looking good, needs tests.
The boolean constants now flying around can be a little hard to follow. Consider adding two new boolean type wrappers for N/OVERWRITE
for the parameter and UN/MODIFIED
for the return value, so their meaning is transparent wherever they are used.
I had a bit of a go of extending this but pushing the |
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 after nits
hamt.go
Outdated
@@ -19,6 +19,26 @@ import ( | |||
const bucketSize = 3 | |||
const defaultBitWidth = 8 | |||
|
|||
//----------------------------------------------------------------------------- | |||
// Boolean constants | |||
type overwrite = bool |
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.
For the compiler to help us use these correctly, could you remove the =
here and define the consts like OVERWRITE = overwrite(false)
?
hamt.go
Outdated
|
||
// SetIfAbsent sets key k to value v only if k is not already set to some value. | ||
// Returns true if the value mapped to k is changed by this operation | ||
// is set, false otherwise. |
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.
// is set, false otherwise. | |
// false otherwise. |
hamt.go
Outdated
@@ -575,17 +625,17 @@ func (n *Node) cleanChild(chnd *Node, cindex byte) error { | |||
// cleanNode()). Recursive calls use the same arguments on child nodes but | |||
// note that `hv.Next()` is not idempotent. Each call will increment the number | |||
// of bits chomped off the hash digest for this key. | |||
func (n *Node) modifyValue(ctx context.Context, hv *hashBits, k []byte, v *cbg.Deferred) error { | |||
func (n *Node) modifyValue(ctx context.Context, hv *hashBits, k []byte, v *cbg.Deferred, overwrite bool) (modified, 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.
func (n *Node) modifyValue(ctx context.Context, hv *hashBits, k []byte, v *cbg.Deferred, overwrite bool) (modified, error) { | |
func (n *Node) modifyValue(ctx context.Context, hv *hashBits, k []byte, v *cbg.Deferred, replace overwrite) (modified, error) { |
hamt.go
Outdated
} | ||
} | ||
return ErrNotFound | ||
return false, ErrNotFound |
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.
return false, ErrNotFound | |
return UNMODIFIED, ErrNotFound |
hamt_test.go
Outdated
val1 := []byte("owl bear") | ||
key := "favorite-animal" | ||
success, err := begn.SetIfAbsent(ctx, key, val1) | ||
if 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.
We depend on testify
already, so require.NoError(err)
.
Closes #75 with the
SetIfAbsent
approachmodifyValue
now returns a boolean indicating whether or not it has changed the subtreemodifyValue
now takes in an overwrite boolean which only allows overwriting existing keys when trueSetIfAbsent
exported functionTests still needed