command.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. //------------------------------------------------------------------------------
  3. //
  4. // Commands
  5. //
  6. //------------------------------------------------------------------------------
  7. import (
  8. "github.com/benbjohnson/go-raft"
  9. "encoding/json"
  10. )
  11. // A command represents an action to be taken on the replicated state machine.
  12. type Command interface {
  13. CommandName() string
  14. Apply(server *raft.Server) ([]byte, error)
  15. GeneratePath() string
  16. Type() string
  17. GetValue() string
  18. GetKey() string
  19. }
  20. // Set command
  21. type SetCommand struct {
  22. Key string `json:"key"`
  23. Value string `json:"value"`
  24. }
  25. // The name of the command in the log
  26. func (c *SetCommand) CommandName() string {
  27. return "set"
  28. }
  29. // Set the value of key to value
  30. func (c *SetCommand) Apply(server *raft.Server) ([]byte, error) {
  31. res := s.Set(c.Key, c.Value)
  32. return json.Marshal(res)
  33. }
  34. func (c *SetCommand) GeneratePath() string{
  35. return "/set/" + c.Key
  36. }
  37. func (c *SetCommand) Type() string{
  38. return "POST"
  39. }
  40. func (c *SetCommand) GetValue() string{
  41. return c.Value
  42. }
  43. func (c *SetCommand) GetKey() string{
  44. return c.Key
  45. }
  46. // Get command
  47. type GetCommand struct {
  48. Key string `json:"key"`
  49. }
  50. // The name of the command in the log
  51. func (c *GetCommand) CommandName() string {
  52. return "get"
  53. }
  54. // Set the value of key to value
  55. func (c *GetCommand) Apply(server *raft.Server) ([]byte, error){
  56. res := s.Get(c.Key)
  57. return json.Marshal(res)
  58. }
  59. func (c *GetCommand) GeneratePath() string{
  60. return "/get/" + c.Key
  61. }
  62. func (c *GetCommand) Type() string{
  63. return "GET"
  64. }
  65. func (c *GetCommand) GetValue() string{
  66. return ""
  67. }
  68. func (c *GetCommand) GetKey() string{
  69. return c.Key
  70. }
  71. // Delete command
  72. type DeleteCommand struct {
  73. Key string `json:"key"`
  74. }
  75. // The name of the command in the log
  76. func (c *DeleteCommand) CommandName() string {
  77. return "delete"
  78. }
  79. // Set the value of key to value
  80. func (c *DeleteCommand) Apply(server *raft.Server) ([]byte, error){
  81. res := s.Delete(c.Key)
  82. return json.Marshal(res)
  83. }
  84. func (c *DeleteCommand) GeneratePath() string{
  85. return "/delete/" + c.Key
  86. }
  87. func (c *DeleteCommand) Type() string{
  88. return "GET"
  89. }
  90. func (c *DeleteCommand) GetValue() string{
  91. return ""
  92. }
  93. func (c *DeleteCommand) GetKey() string{
  94. return c.Key
  95. }
  96. // joinCommand
  97. type JoinCommand struct {
  98. Name string `json:"name"`
  99. }
  100. func (c *JoinCommand) CommandName() string {
  101. return "join"
  102. }
  103. func (c *JoinCommand) Apply(server *raft.Server) ([]byte, error) {
  104. err := server.AddPeer(c.Name)
  105. return nil, err
  106. }