@@ -80,7 +80,7 @@ func (txn *Txn[T]) Insert(key []byte, value T) (old T, hadOld bool) {
8080// Returns the old value if it exists and a watch channel that closes when the
8181// key changes again.
8282func (txn * Txn [T ]) InsertWatch (key []byte , value T ) (old T , hadOld bool , watch <- chan struct {}) {
83- old , hadOld , watch , txn .root = txn .insert (txn .root , key , value )
83+ old , _ , hadOld , watch , txn .root = txn .insert (txn .root , key , value )
8484 validateTree (txn .root , nil , txn .watches , txn .txnID )
8585 if ! hadOld {
8686 txn .size ++
@@ -93,19 +93,19 @@ func (txn *Txn[T]) InsertWatch(key []byte, value T) (old T, hadOld bool, watch <
9393
9494// Modify a value in the tree. It is up to the
9595// caller to not mutate the value in-place and to return a clone.
96- // Returns the old value if it exists.
97- func (txn * Txn [T ]) Modify (key []byte , value T , mod func (T , T ) T ) (old T , hadOld bool ) {
98- old , hadOld , _ = txn .ModifyWatch (key , value , mod )
96+ // Returns the old value ( if it exists) and the new possibly merged value .
97+ func (txn * Txn [T ]) Modify (key []byte , value T , mod func (T , T ) T ) (old T , newValue T , hadOld bool ) {
98+ old , newValue , hadOld , _ = txn .ModifyWatch (key , value , mod )
9999 return
100100}
101101
102102// Modify a value in the tree. If the key does not exist the modify
103103// function is called with the zero value for T. It is up to the
104104// caller to not mutate the value in-place and to return a clone.
105- // Returns the old value if it exists and a watch channel that closes
106- // when the key changes again.
107- func (txn * Txn [T ]) ModifyWatch (key []byte , value T , mod func (T , T ) T ) (old T , hadOld bool , watch <- chan struct {}) {
108- old , hadOld , watch , txn .root = txn .modify (txn .root , key , value , mod )
105+ // Returns the old value ( if it exists) and the new possibly merged value,
106+ // and a watch channel that closes when the key changes again.
107+ func (txn * Txn [T ]) ModifyWatch (key []byte , value T , mod func (T , T ) T ) (old T , newValue T , hadOld bool , watch <- chan struct {}) {
108+ old , newValue , hadOld , watch , txn .root = txn .modify (txn .root , key , value , mod )
109109 validateTree (txn .root , nil , txn .watches , txn .txnID )
110110 if ! hadOld {
111111 txn .size ++
@@ -243,17 +243,18 @@ func (txn *Txn[T]) cloneNode(n *header[T]) *header[T] {
243243 return n
244244}
245245
246- func (txn * Txn [T ]) insert (root * header [T ], key []byte , value T ) (oldValue T , hadOld bool , watch <- chan struct {}, newRoot * header [T ]) {
246+ func (txn * Txn [T ]) insert (root * header [T ], key []byte , value T ) (oldValue T , newValue T , hadOld bool , watch <- chan struct {}, newRoot * header [T ]) {
247247 return txn .modify (root , key , value , nil )
248248}
249249
250- func (txn * Txn [T ]) modify (root * header [T ], key []byte , newValue T , mod func (T , T ) T ) (oldValue T , hadOld bool , watch <- chan struct {}, newRoot * header [T ]) {
250+ func (txn * Txn [T ]) modify (root * header [T ], key []byte , newValue T , mod func (T , T ) T ) (oldValue T , newValueOut T , hadOld bool , watch <- chan struct {}, newRoot * header [T ]) {
251251 txn .dirty = true
252252 fullKey := key
253+ newValueOut = newValue
253254
254255 if root == nil {
255256 leaf := newLeaf (txn .opts , key , fullKey , newValue )
256- return oldValue , false , leaf .watch , leaf .self ()
257+ return oldValue , newValueOut , false , leaf .watch , leaf .self ()
257258 }
258259
259260 // Start recursing from the root to find the insertion point.
@@ -323,7 +324,8 @@ func (txn *Txn[T]) modify(root *header[T], key []byte, newValue T, mod func(T, T
323324 }
324325 watch = leaf .watch
325326 if mod != nil {
326- leaf .value = mod (oldValue , newValue )
327+ newValueOut = mod (oldValue , newValue )
328+ leaf .value = newValueOut
327329 } else {
328330 leaf .value = newValue
329331 }
0 commit comments