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