return command index

This commit is contained in:
Xiang Li 2013-06-29 12:49:05 -07:00
parent c2f436a58b
commit 5681e1a9d0
2 changed files with 14 additions and 12 deletions

View File

@ -33,7 +33,7 @@ func (c *SetCommand) CommandName() string {
// Set the value of key to value
func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
return store.Set(c.Key, c.Value, c.ExpireTime)
return store.Set(c.Key, c.Value, c.ExpireTime, server.CommittedIndex())
}
// Get the path for http request
@ -73,7 +73,7 @@ func (c *DeleteCommand) CommandName() string {
// Delete the key
func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
return store.Delete(c.Key)
return store.Delete(c.Key, server.CommittedIndex())
}
// Watch command

View File

@ -51,6 +51,8 @@ type Response struct {
Exist bool `json:"exist"`
Expiration time.Time `json:"expiration"`
Index uint64 `json:"index"`
}
func init() {
@ -76,7 +78,7 @@ func (s *Store) SetMessager(messager *chan string) {
}
// set the key to value, return the old value if the key exists
func Set(key string, value string, expireTime time.Time) ([]byte, error) {
func Set(key string, value string, expireTime time.Time, index uint64) ([]byte, error) {
key = path.Clean(key)
@ -88,7 +90,7 @@ func Set(key string, value string, expireTime time.Time) ([]byte, error) {
// the key may be expired, we should not add the node
// also if the node exist, we need to delete the node
if isExpire && expireTime.Sub(time.Now()) < 0 {
return Delete(key)
return Delete(key, index)
}
// get the node
@ -113,7 +115,7 @@ func Set(key string, value string, expireTime time.Time) ([]byte, error) {
// update the information of the node
s.Nodes[key] = Node{value, expireTime, node.update}
resp := Response{SET, key, node.Value, value, true, expireTime}
resp := Response{SET, key, node.Value, value, true, expireTime, index}
msg, err := json.Marshal(resp)
@ -138,7 +140,7 @@ func Set(key string, value string, expireTime time.Time) ([]byte, error) {
go expire(key, update, expireTime)
}
resp := Response{SET, key, "", value, false, expireTime}
resp := Response{SET, key, "", value, false, expireTime, index}
msg, err := json.Marshal(resp)
@ -170,7 +172,7 @@ func expire(key string, update chan time.Time, expireTime time.Time) {
delete(s.Nodes, key)
resp := Response{DELETE, key, node.Value, "", true, node.ExpireTime}
resp := Response{DELETE, key, node.Value, "", true, node.ExpireTime, 0}
msg, err := json.Marshal(resp)
@ -207,14 +209,14 @@ func Get(key string) Response {
node, ok := s.Nodes[key]
if ok {
return Response{GET, key, node.Value, node.Value, true, node.ExpireTime}
return Response{GET, key, node.Value, node.Value, true, node.ExpireTime, 0}
} else {
return Response{GET, key, "", "", false, time.Unix(0, 0)}
return Response{GET, key, "", "", false, time.Unix(0, 0), 0}
}
}
// delete the key
func Delete(key string) ([]byte, error) {
func Delete(key string, index uint64) ([]byte, error) {
key = path.Clean(key)
node, ok := s.Nodes[key]
@ -233,7 +235,7 @@ func Delete(key string) ([]byte, error) {
}
resp := Response{DELETE, key, node.Value, "", true, node.ExpireTime}
resp := Response{DELETE, key, node.Value, "", true, node.ExpireTime, index}
msg, err := json.Marshal(resp)
@ -249,7 +251,7 @@ func Delete(key string) ([]byte, error) {
} else {
return json.Marshal(Response{DELETE, key, "", "", false, time.Unix(0, 0)})
return json.Marshal(Response{DELETE, key, "", "", false, time.Unix(0, 0), index})
}
}