etcd/command.go

131 lines
2.8 KiB
Go
Raw Normal View History

package main
2013-06-21 02:59:23 +04:00
//------------------------------------------------------------------------------
//
// Commands
//
//------------------------------------------------------------------------------
import (
2013-06-21 02:59:23 +04:00
"encoding/json"
"github.com/xiangli-cmu/go-raft"
"github.com/xiangli-cmu/raft-etcd/store"
2013-06-17 01:02:07 +04:00
"time"
2013-06-21 02:59:23 +04:00
)
// A command represents an action to be taken on the replicated state machine.
type Command interface {
CommandName() string
Apply(server *raft.Server) (interface{}, error)
}
// Set command
type SetCommand struct {
2013-06-21 02:59:23 +04:00
Key string `json:"key"`
Value string `json:"value"`
ExpireTime time.Time `json:"expireTime"`
}
// The name of the command in the log
func (c *SetCommand) CommandName() string {
return "set"
}
// Set the value of key to value
func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
2013-06-29 23:49:05 +04:00
return store.Set(c.Key, c.Value, c.ExpireTime, server.CommittedIndex())
}
2013-06-13 22:01:06 +04:00
// Get the path for http request
func (c *SetCommand) GeneratePath() string {
2013-06-12 03:01:12 +04:00
return "set/" + c.Key
}
// Get command
type GetCommand struct {
Key string `json:"key"`
}
// The name of the command in the log
func (c *GetCommand) CommandName() string {
return "get"
}
// Set the value of key to value
func (c *GetCommand) Apply(server *raft.Server) (interface{}, error) {
2013-06-18 22:13:24 +04:00
res := store.Get(c.Key)
return json.Marshal(res)
}
2013-06-21 02:59:23 +04:00
func (c *GetCommand) GeneratePath() string {
2013-06-12 03:01:12 +04:00
return "get/" + c.Key
}
2013-07-06 03:22:45 +04:00
// List command
type ListCommand struct {
Prefix string `json:"prefix"`
}
// The name of the command in the log
func (c *ListCommand) CommandName() string {
return "list"
}
// Set the value of key to value
func (c *ListCommand) Apply(server *raft.Server) (interface{}, error) {
return store.List(c.Prefix)
}
// Delete command
type DeleteCommand struct {
Key string `json:"key"`
}
// The name of the command in the log
func (c *DeleteCommand) CommandName() string {
return "delete"
}
2013-06-21 02:59:23 +04:00
// Delete the key
func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
2013-06-29 23:49:05 +04:00
return store.Delete(c.Key, server.CommittedIndex())
}
// Watch command
type WatchCommand struct {
2013-07-01 03:30:41 +04:00
Key string `json:"key"`
SinceIndex uint64 `json:"sinceIndex"`
}
//The name of the command in the log
func (c *WatchCommand) CommandName() string {
return "watch"
}
func (c *WatchCommand) Apply(server *raft.Server) (interface{}, error) {
2013-06-30 02:29:10 +04:00
ch := make(chan store.Response, 1)
2013-06-13 22:01:06 +04:00
// add to the watchers list
2013-07-01 03:30:41 +04:00
store.AddWatcher(c.Key, ch, c.SinceIndex)
2013-06-13 22:01:06 +04:00
// wait for the notification for any changing
2013-06-21 02:59:23 +04:00
res := <-ch
return json.Marshal(res)
}
// JoinCommand
type JoinCommand struct {
Name string `json:"name"`
}
func (c *JoinCommand) CommandName() string {
return "join"
}
func (c *JoinCommand) Apply(server *raft.Server) (interface{}, error) {
err := server.AddPeer(c.Name)
2013-06-13 22:01:06 +04:00
// no result will be returned
return nil, err
}