etcd/store/event.go

94 lines
1.8 KiB
Go
Raw Normal View History

2013-09-29 03:26:19 +04:00
package store
2013-09-03 22:30:42 +04:00
const (
Get = "get"
Create = "create"
2013-10-15 10:04:21 +04:00
Set = "set"
Update = "update"
Delete = "delete"
CompareAndSwap = "compareAndSwap"
Expire = "expire"
2013-10-03 09:15:12 +04:00
)
2013-09-03 22:30:42 +04:00
type Event struct {
Action string `json:"action"`
Node *NodeExtern `json:"node,omitempty"`
2013-09-03 22:30:42 +04:00
}
2013-11-28 08:04:52 +04:00
func newEvent(action string, key string, modifiedIndex, createdIndex uint64) *Event {
n := &NodeExtern{
2013-11-10 08:49:19 +04:00
Key: key,
2013-11-28 08:04:52 +04:00
ModifiedIndex: modifiedIndex,
CreatedIndex: createdIndex,
}
return &Event{
Action: action,
Node: n,
2013-09-03 22:30:42 +04:00
}
}
func (e *Event) IsCreated() bool {
if e.Action == Create {
return true
}
2013-11-28 08:04:52 +04:00
if e.Action == Set && e.Node.PrevValue == "" {
return true
}
return false
}
2013-11-10 08:49:19 +04:00
func (e *Event) Index() uint64 {
2013-11-28 08:04:52 +04:00
return e.Node.ModifiedIndex
2013-11-10 08:49:19 +04:00
}
2013-10-13 01:56:43 +04:00
// Converts an event object into a response object.
2013-12-11 23:12:39 +04:00
func (event *Event) Response(currentIndex uint64) interface{} {
2013-11-28 08:04:52 +04:00
if !event.Node.Dir {
2013-10-13 01:56:43 +04:00
response := &Response{
Action: event.Action,
2013-11-28 08:04:52 +04:00
Key: event.Node.Key,
Value: event.Node.Value,
PrevValue: event.Node.PrevValue,
Index: event.Node.ModifiedIndex,
TTL: event.Node.TTL,
Expiration: event.Node.Expiration,
2013-09-03 22:30:42 +04:00
}
2013-12-11 23:12:39 +04:00
if currentIndex != 0 {
response.Index = currentIndex
}
if response.Action == Set {
2013-10-13 01:56:43 +04:00
if response.PrevValue == "" {
response.NewKey = true
}
}
2013-09-03 22:30:42 +04:00
if response.Action == CompareAndSwap || response.Action == Create {
response.Action = "testAndSet"
}
2013-10-13 01:56:43 +04:00
return response
} else {
2013-11-28 08:04:52 +04:00
responses := make([]*Response, len(event.Node.Nodes))
2013-10-13 01:56:43 +04:00
2013-11-28 08:04:52 +04:00
for i, node := range event.Node.Nodes {
2013-10-13 01:56:43 +04:00
responses[i] = &Response{
Action: event.Action,
2013-11-28 08:04:52 +04:00
Key: node.Key,
Value: node.Value,
Dir: node.Dir,
Index: node.ModifiedIndex,
2013-10-13 01:56:43 +04:00
}
2013-12-11 23:12:39 +04:00
if currentIndex != 0 {
responses[i].Index = currentIndex
}
2013-09-03 22:30:42 +04:00
}
2013-10-13 01:56:43 +04:00
return responses
2013-09-03 22:30:42 +04:00
}
}