...

Source file src/go.etcd.io/etcd/server/v3/mvcc/watchable_store_txn.go

Documentation: go.etcd.io/etcd/server/v3/mvcc

     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  	"go.etcd.io/etcd/api/v3/mvccpb"
    19  	"go.etcd.io/etcd/pkg/v3/traceutil"
    20  )
    21  
    22  func (tw *watchableStoreTxnWrite) End() {
    23  	changes := tw.Changes()
    24  	if len(changes) == 0 {
    25  		tw.TxnWrite.End()
    26  		return
    27  	}
    28  
    29  	rev := tw.Rev() + 1
    30  	evs := make([]mvccpb.Event, len(changes))
    31  	for i, change := range changes {
    32  		evs[i].Kv = &changes[i]
    33  		if change.CreateRevision == 0 {
    34  			evs[i].Type = mvccpb.DELETE
    35  			evs[i].Kv.ModRevision = rev
    36  		} else {
    37  			evs[i].Type = mvccpb.PUT
    38  		}
    39  	}
    40  
    41  	// end write txn under watchable store lock so the updates are visible
    42  	// when asynchronous event posting checks the current store revision
    43  	tw.s.mu.Lock()
    44  	tw.s.notify(rev, evs)
    45  	tw.TxnWrite.End()
    46  	tw.s.mu.Unlock()
    47  }
    48  
    49  type watchableStoreTxnWrite struct {
    50  	TxnWrite
    51  	s *watchableStore
    52  }
    53  
    54  func (s *watchableStore) Write(trace *traceutil.Trace) TxnWrite {
    55  	return &watchableStoreTxnWrite{s.store.Write(trace), s}
    56  }
    57  

View as plain text