command.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. Sensitive() bool
  20. }
  21. // Set command
  22. type SetCommand struct {
  23. Key string `json:"key"`
  24. Value string `json:"value"`
  25. }
  26. // The name of the command in the log
  27. func (c *SetCommand) CommandName() string {
  28. return "set"
  29. }
  30. // Set the value of key to value
  31. func (c *SetCommand) Apply(server *raft.Server) ([]byte, error) {
  32. res := s.Set(c.Key, c.Value)
  33. return json.Marshal(res)
  34. }
  35. func (c *SetCommand) GeneratePath() string {
  36. return "/set/" + c.Key
  37. }
  38. func (c *SetCommand) Type() string {
  39. return "POST"
  40. }
  41. func (c *SetCommand) GetValue() string {
  42. return c.Value
  43. }
  44. func (c *SetCommand) GetKey() string {
  45. return c.Key
  46. }
  47. func (c *SetCommand) Sensitive() bool {
  48. return true
  49. }
  50. // Get command
  51. type GetCommand struct {
  52. Key string `json:"key"`
  53. }
  54. // The name of the command in the log
  55. func (c *GetCommand) CommandName() string {
  56. return "get"
  57. }
  58. // Set the value of key to value
  59. func (c *GetCommand) Apply(server *raft.Server) ([]byte, error){
  60. res := s.Get(c.Key)
  61. return json.Marshal(res)
  62. }
  63. func (c *GetCommand) GeneratePath() string{
  64. return "/get/" + c.Key
  65. }
  66. func (c *GetCommand) Type() string{
  67. return "GET"
  68. }
  69. func (c *GetCommand) GetValue() string{
  70. return ""
  71. }
  72. func (c *GetCommand) GetKey() string{
  73. return c.Key
  74. }
  75. func (c *GetCommand) Sensitive() bool {
  76. return false
  77. }
  78. // Delete command
  79. type DeleteCommand struct {
  80. Key string `json:"key"`
  81. }
  82. // The name of the command in the log
  83. func (c *DeleteCommand) CommandName() string {
  84. return "delete"
  85. }
  86. // Delete the key
  87. func (c *DeleteCommand) Apply(server *raft.Server) ([]byte, error){
  88. res := s.Delete(c.Key)
  89. return json.Marshal(res)
  90. }
  91. func (c *DeleteCommand) GeneratePath() string{
  92. return "/delete/" + c.Key
  93. }
  94. func (c *DeleteCommand) Type() string{
  95. return "GET"
  96. }
  97. func (c *DeleteCommand) GetValue() string{
  98. return ""
  99. }
  100. func (c *DeleteCommand) GetKey() string{
  101. return c.Key
  102. }
  103. func (c *DeleteCommand) Sensitive() bool {
  104. return true
  105. }
  106. // Watch command
  107. type WatchCommand struct {
  108. Key string `json:"key"`
  109. }
  110. //The name of the command in the log
  111. func (c *WatchCommand) CommandName() string {
  112. return "watch"
  113. }
  114. func (c *WatchCommand) Apply(server *raft.Server) ([]byte, error){
  115. ch := make(chan Response)
  116. w.add(c.Key, ch)
  117. res := <- ch
  118. return json.Marshal(res)
  119. }
  120. func (c *WatchCommand) GeneratePath() string{
  121. return "/watch/" + c.Key
  122. }
  123. func (c *WatchCommand) Type() string{
  124. return "GET"
  125. }
  126. func (c *WatchCommand) GetValue() string{
  127. return ""
  128. }
  129. func (c *WatchCommand) GetKey() string{
  130. return c.Key
  131. }
  132. func (c *WatchCommand) Sensitive() bool {
  133. return false
  134. }
  135. // JoinCommand
  136. type JoinCommand struct {
  137. Name string `json:"name"`
  138. }
  139. func (c *JoinCommand) CommandName() string {
  140. return "join"
  141. }
  142. func (c *JoinCommand) Apply(server *raft.Server) ([]byte, error) {
  143. err := server.AddPeer(c.Name)
  144. return nil, err
  145. }