watchable_store_txn.go 1.4 KB

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