github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/store/event.go (about) 1 package store 2 3 const ( 4 Get = "get" 5 Create = "create" 6 Set = "set" 7 Update = "update" 8 Delete = "delete" 9 CompareAndSwap = "compareAndSwap" 10 CompareAndDelete = "compareAndDelete" 11 Expire = "expire" 12 ) 13 14 type Event struct { 15 Action string `json:"action"` 16 Node *NodeExtern `json:"node,omitempty"` 17 PrevNode *NodeExtern `json:"prevNode,omitempty"` 18 } 19 20 func newEvent(action string, key string, modifiedIndex, createdIndex uint64) *Event { 21 n := &NodeExtern{ 22 Key: key, 23 ModifiedIndex: modifiedIndex, 24 CreatedIndex: createdIndex, 25 } 26 27 return &Event{ 28 Action: action, 29 Node: n, 30 } 31 } 32 33 func (e *Event) IsCreated() bool { 34 if e.Action == Create { 35 return true 36 } 37 38 if e.Action == Set && e.PrevNode == nil { 39 return true 40 } 41 42 return false 43 } 44 45 func (e *Event) Index() uint64 { 46 return e.Node.ModifiedIndex 47 } 48 49 // Converts an event object into a response object. 50 func (event *Event) Response(currentIndex uint64) interface{} { 51 if !event.Node.Dir { 52 response := &Response{ 53 Action: event.Action, 54 Key: event.Node.Key, 55 Value: event.Node.Value, 56 Index: event.Node.ModifiedIndex, 57 TTL: event.Node.TTL, 58 Expiration: event.Node.Expiration, 59 } 60 61 if event.PrevNode != nil { 62 response.PrevValue = event.PrevNode.Value 63 } 64 65 if currentIndex != 0 { 66 response.Index = currentIndex 67 } 68 69 if response.Action == Set { 70 if response.PrevValue == nil { 71 response.NewKey = true 72 } 73 } 74 75 if response.Action == CompareAndSwap || response.Action == Create { 76 response.Action = "testAndSet" 77 } 78 79 return response 80 } else { 81 responses := make([]*Response, len(event.Node.Nodes)) 82 83 for i, node := range event.Node.Nodes { 84 responses[i] = &Response{ 85 Action: event.Action, 86 Key: node.Key, 87 Value: node.Value, 88 Dir: node.Dir, 89 Index: node.ModifiedIndex, 90 } 91 92 if currentIndex != 0 { 93 responses[i].Index = currentIndex 94 } 95 } 96 return responses 97 } 98 }