watchable_store_txn.go 1.5 KB

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