etcd/store/event.go

84 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
import (
"time"
)
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
)
const (
UndefIndex = 0
UndefTerm = 0
2013-09-03 22:30:42 +04:00
)
type Event struct {
Action string `json:"action"`
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
2013-09-03 22:30:42 +04:00
// The command index of the raft machine when the command is executed
Index uint64 `json:"index"`
Term uint64 `json:"term"`
}
func newEvent(action string, key string, index uint64, term uint64) *Event {
return &Event{
Action: action,
Key: key,
Index: index,
Term: term,
}
}
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,
Index: event.Index,
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,
Index: event.Index,
}
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
}
}