event.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2015 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 store
  15. const (
  16. Get = "get"
  17. Create = "create"
  18. Set = "set"
  19. Update = "update"
  20. Delete = "delete"
  21. CompareAndSwap = "compareAndSwap"
  22. CompareAndDelete = "compareAndDelete"
  23. Expire = "expire"
  24. )
  25. type Event struct {
  26. Action string `json:"action"`
  27. Node *NodeExtern `json:"node,omitempty"`
  28. PrevNode *NodeExtern `json:"prevNode,omitempty"`
  29. EtcdIndex uint64 `json:"-"`
  30. Refresh bool `json:"refresh,omitempty"`
  31. }
  32. func newEvent(action string, key string, modifiedIndex, createdIndex uint64) *Event {
  33. n := &NodeExtern{
  34. Key: key,
  35. ModifiedIndex: modifiedIndex,
  36. CreatedIndex: createdIndex,
  37. }
  38. return &Event{
  39. Action: action,
  40. Node: n,
  41. }
  42. }
  43. func (e *Event) IsCreated() bool {
  44. if e.Action == Create {
  45. return true
  46. }
  47. return e.Action == Set && e.PrevNode == nil
  48. }
  49. func (e *Event) Index() uint64 {
  50. return e.Node.ModifiedIndex
  51. }
  52. func (e *Event) Clone() *Event {
  53. return &Event{
  54. Action: e.Action,
  55. EtcdIndex: e.EtcdIndex,
  56. Node: e.Node.Clone(),
  57. PrevNode: e.PrevNode.Clone(),
  58. }
  59. }
  60. func (e *Event) SetRefresh() {
  61. e.Refresh = true
  62. }