command_factory.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package v2
  2. import (
  3. "time"
  4. "github.com/coreos/etcd/store"
  5. "github.com/coreos/raft"
  6. )
  7. func init() {
  8. store.RegisterCommandFactory(&CommandFactory{})
  9. }
  10. // CommandFactory provides a pluggable way to create version 2 commands.
  11. type CommandFactory struct {
  12. }
  13. // Version returns the version of this factory.
  14. func (f *CommandFactory) Version() int {
  15. return 2
  16. }
  17. // CreateUpgradeCommand is a no-op since version 2 is the first version to support store versioning.
  18. func (f *CommandFactory) CreateUpgradeCommand() raft.Command {
  19. return &raft.NOPCommand{}
  20. }
  21. // CreateSetCommand creates a version 2 command to set a key to a given value in the store.
  22. func (f *CommandFactory) CreateSetCommand(key string, value string, expireTime time.Time) raft.Command {
  23. return &SetCommand{
  24. Key: key,
  25. Value: value,
  26. ExpireTime: expireTime,
  27. }
  28. }
  29. // CreateCreateCommand creates a version 2 command to create a new key in the store.
  30. func (f *CommandFactory) CreateCreateCommand(key string, value string, expireTime time.Time, unique bool) raft.Command {
  31. return &CreateCommand{
  32. Key: key,
  33. Value: value,
  34. ExpireTime: expireTime,
  35. Unique: unique,
  36. }
  37. }
  38. // CreateUpdateCommand creates a version 2 command to update a key to a given value in the store.
  39. func (f *CommandFactory) CreateUpdateCommand(key string, value string, expireTime time.Time) raft.Command {
  40. return &UpdateCommand{
  41. Key: key,
  42. Value: value,
  43. ExpireTime: expireTime,
  44. }
  45. }
  46. // CreateDeleteCommand creates a version 2 command to delete a key from the store.
  47. func (f *CommandFactory) CreateDeleteCommand(key string, recursive bool) raft.Command {
  48. return &DeleteCommand{
  49. Key: key,
  50. Recursive: recursive,
  51. }
  52. }
  53. // CreateCompareAndSwapCommand creates a version 2 command to conditionally set a key in the store.
  54. func (f *CommandFactory) CreateCompareAndSwapCommand(key string, value string, prevValue string, prevIndex uint64, expireTime time.Time) raft.Command {
  55. return &CompareAndSwapCommand{
  56. Key: key,
  57. Value: value,
  58. PrevValue: prevValue,
  59. PrevIndex: prevIndex,
  60. ExpireTime: expireTime,
  61. }
  62. }
  63. // CreateCompareAndDeleteCommand creates a version 2 command to conditionally delete a key from the store.
  64. func (f *CommandFactory) CreateCompareAndDeleteCommand(key string, recursive bool, prevValue string, prevIndex uint64) raft.Command {
  65. return &CompareAndDeleteCommand{
  66. Key: key,
  67. Recursive: recursive,
  68. PrevValue: prevValue,
  69. PrevIndex: prevIndex,
  70. }
  71. }
  72. func (f *CommandFactory) CreateSyncCommand(now time.Time) raft.Command {
  73. return &SyncCommand{
  74. Time: time.Now(),
  75. }
  76. }