go.etcd.io/etcd@v3.3.27+incompatible/mvcc/watchable_store_txn.go (about)

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mvcc
    16  
    17  import (
    18  	"github.com/coreos/etcd/mvcc/mvccpb"
    19  )
    20  
    21  func (tw *watchableStoreTxnWrite) End() {
    22  	changes := tw.Changes()
    23  	if len(changes) == 0 {
    24  		tw.TxnWrite.End()
    25  		return
    26  	}
    27  
    28  	rev := tw.Rev() + 1
    29  	evs := make([]mvccpb.Event, len(changes))
    30  	for i, change := range changes {
    31  		evs[i].Kv = &changes[i]
    32  		if change.CreateRevision == 0 {
    33  			evs[i].Type = mvccpb.DELETE
    34  			evs[i].Kv.ModRevision = rev
    35  		} else {
    36  			evs[i].Type = mvccpb.PUT
    37  		}
    38  	}
    39  
    40  	// end write txn under watchable store lock so the updates are visible
    41  	// when asynchronous event posting checks the current store revision
    42  	tw.s.mu.Lock()
    43  	tw.s.notify(rev, evs)
    44  	tw.TxnWrite.End()
    45  	tw.s.mu.Unlock()
    46  }
    47  
    48  type watchableStoreTxnWrite struct {
    49  	TxnWrite
    50  	s *watchableStore
    51  }
    52  
    53  func (s *watchableStore) Write() TxnWrite { return &watchableStoreTxnWrite{s.store.Write(), s} }