event.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. const (
  15. Get = "get"
  16. Create = "create"
  17. Set = "set"
  18. Update = "update"
  19. Delete = "delete"
  20. CompareAndSwap = "compareAndSwap"
  21. CompareAndDelete = "compareAndDelete"
  22. Expire = "expire"
  23. )
  24. type Event struct {
  25. Action string `json:"action"`
  26. Node *NodeExtern `json:"node,omitempty"`
  27. PrevNode *NodeExtern `json:"prevNode,omitempty"`
  28. EtcdIndex uint64 `json:"-"`
  29. }
  30. func newEvent(action string, key string, modifiedIndex, createdIndex uint64) *Event {
  31. n := &NodeExtern{
  32. Key: key,
  33. ModifiedIndex: modifiedIndex,
  34. CreatedIndex: createdIndex,
  35. }
  36. return &Event{
  37. Action: action,
  38. Node: n,
  39. }
  40. }
  41. func (e *Event) IsCreated() bool {
  42. if e.Action == Create {
  43. return true
  44. }
  45. if e.Action == Set && e.PrevNode == nil {
  46. return true
  47. }
  48. return false
  49. }
  50. func (e *Event) Index() uint64 {
  51. return e.Node.ModifiedIndex
  52. }
  53. func (e *Event) Clone() *Event {
  54. return &Event{
  55. Action: e.Action,
  56. EtcdIndex: e.EtcdIndex,
  57. Node: e.Node.Clone(),
  58. PrevNode: e.PrevNode.Clone(),
  59. }
  60. }