event.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2015 CoreOS, Inc.
  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. }
  31. func newEvent(action string, key string, modifiedIndex, createdIndex uint64) *Event {
  32. n := &NodeExtern{
  33. Key: key,
  34. ModifiedIndex: modifiedIndex,
  35. CreatedIndex: createdIndex,
  36. }
  37. return &Event{
  38. Action: action,
  39. Node: n,
  40. }
  41. }
  42. func (e *Event) IsCreated() bool {
  43. if e.Action == Create {
  44. return true
  45. }
  46. if e.Action == Set && e.PrevNode == nil {
  47. return true
  48. }
  49. return false
  50. }
  51. func (e *Event) Index() uint64 {
  52. return e.Node.ModifiedIndex
  53. }
  54. func (e *Event) Clone() *Event {
  55. return &Event{
  56. Action: e.Action,
  57. EtcdIndex: e.EtcdIndex,
  58. Node: e.Node.Clone(),
  59. PrevNode: e.PrevNode.Clone(),
  60. }
  61. }