etcd/store/event.go

93 lines
2.0 KiB
Go
Raw Normal View History

2013-09-29 03:26:19 +04:00
package store
2013-09-03 22:30:42 +04:00
import (
"time"
)
const (
Get = "get"
Create = "create"
2013-10-15 10:04:21 +04:00
Set = "set"
Update = "update"
Delete = "delete"
CompareAndSwap = "compareAndSwap"
2013-11-30 21:08:25 +04:00
CompareAndDelete = "compareAndDelete"
Expire = "expire"
2013-10-03 09:15:12 +04:00
)
2013-09-03 22:30:42 +04:00
type Event struct {
2013-11-12 09:19:30 +04:00
Action string `json:"action"`
2013-11-10 08:49:19 +04:00
Key string `json:"key, omitempty"`
Dir bool `json:"dir,omitempty"`
PrevValue string `json:"prevValue,omitempty"`
Value string `json:"value,omitempty"`
KVPairs kvPairs `json:"kvs,omitempty"`
Expiration *time.Time `json:"expiration,omitempty"`
TTL int64 `json:"ttl,omitempty"` // Time to live in second
ModifiedIndex uint64 `json:"modifiedIndex"`
2013-09-03 22:30:42 +04:00
}
func newEvent(action string, key string, index uint64) *Event {
2013-09-03 22:30:42 +04:00
return &Event{
2013-11-10 08:49:19 +04:00
Action: action,
Key: key,
ModifiedIndex: index,
2013-09-03 22:30:42 +04:00
}
}
func (e *Event) IsCreated() bool {
if e.Action == Create {
return true
}
if e.Action == Set && e.PrevValue == "" {
return true
}
return false
}
2013-11-10 08:49:19 +04:00
func (e *Event) Index() uint64 {
return e.ModifiedIndex
}
2013-10-13 01:56:43 +04:00
// Converts an event object into a response object.
func (event *Event) Response() interface{} {
if !event.Dir {
response := &Response{
Action: event.Action,
Key: event.Key,
Value: event.Value,
PrevValue: event.PrevValue,
2013-11-10 08:49:19 +04:00
Index: event.ModifiedIndex,
2013-10-13 01:56:43 +04:00
TTL: event.TTL,
Expiration: event.Expiration,
2013-09-03 22:30:42 +04:00
}
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 {
responses := make([]*Response, len(event.KVPairs))
for i, kv := range event.KVPairs {
responses[i] = &Response{
Action: event.Action,
Key: kv.Key,
Value: kv.Value,
Dir: kv.Dir,
2013-11-10 08:49:19 +04:00
Index: event.ModifiedIndex,
2013-10-13 01:56:43 +04:00
}
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
}
}