| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package main
- //------------------------------------------------------------------------------
- //
- // Commands
- //
- //------------------------------------------------------------------------------
- import (
- "github.com/benbjohnson/go-raft"
- "encoding/json"
- )
- // A command represents an action to be taken on the replicated state machine.
- type Command interface {
- CommandName() string
- Apply(server *raft.Server) ([]byte, error)
- GeneratePath() string // Gererate a path for http request
- Type() string // http request type
- GetValue() string
- GetKey() string
- Sensitive() bool // Sensitive to the stateMachine
- }
- // Set command
- type SetCommand struct {
- Key string `json:"key"`
- Value string `json:"value"`
- }
- // 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) ([]byte, error) {
- res := s.Set(c.Key, c.Value)
- return json.Marshal(res)
- }
- // Get the path for http request
- func (c *SetCommand) GeneratePath() string {
- return "set/" + c.Key
- }
- // Get the type for http request
- func (c *SetCommand) Type() string {
- return "POST"
- }
- func (c *SetCommand) GetValue() string {
- return c.Value
- }
- func (c *SetCommand) GetKey() string {
- return c.Key
- }
- func (c *SetCommand) Sensitive() bool {
- return true
- }
- // 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) ([]byte, error){
- res := s.Get(c.Key)
- return json.Marshal(res)
- }
- func (c *GetCommand) GeneratePath() string{
- return "get/" + c.Key
- }
- func (c *GetCommand) Type() string{
- return "GET"
- }
- func (c *GetCommand) GetValue() string{
- return ""
- }
- func (c *GetCommand) GetKey() string{
- return c.Key
- }
- func (c *GetCommand) Sensitive() bool {
- return false
- }
- // Delete command
- type DeleteCommand struct {
- Key string `json:"key"`
- }
- // The name of the command in the log
- func (c *DeleteCommand) CommandName() string {
- return "delete"
- }
- // Delete the key
- func (c *DeleteCommand) Apply(server *raft.Server) ([]byte, error){
- res := s.Delete(c.Key)
- return json.Marshal(res)
- }
- func (c *DeleteCommand) GeneratePath() string{
- return "delete/" + c.Key
- }
- func (c *DeleteCommand) Type() string{
- return "GET"
- }
- func (c *DeleteCommand) GetValue() string{
- return ""
- }
- func (c *DeleteCommand) GetKey() string{
- return c.Key
- }
- func (c *DeleteCommand) Sensitive() bool {
- return true
- }
- // Watch command
- type WatchCommand struct {
- Key string `json:"key"`
- }
- //The name of the command in the log
- func (c *WatchCommand) CommandName() string {
- return "watch"
- }
- func (c *WatchCommand) Apply(server *raft.Server) ([]byte, error){
- ch := make(chan Response)
- // add to the watchers list
- w.add(c.Key, ch)
- // wait for the notification for any changing
- res := <- ch
- return json.Marshal(res)
- }
- func (c *WatchCommand) GeneratePath() string{
- return "watch/" + c.Key
- }
- func (c *WatchCommand) Type() string{
- return "GET"
- }
- func (c *WatchCommand) GetValue() string{
- return ""
- }
- func (c *WatchCommand) GetKey() string{
- return c.Key
- }
- func (c *WatchCommand) Sensitive() bool {
- return false
- }
- // JoinCommand
- type JoinCommand struct {
- Name string `json:"name"`
- }
- func (c *JoinCommand) CommandName() string {
- return "join"
- }
- func (c *JoinCommand) Apply(server *raft.Server) ([]byte, error) {
- err := server.AddPeer(c.Name)
- // no result will be returned
- return nil, err
- }
|