watchable_store_txn.go 1.4 KB

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