command_factory.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package v2
  2. import (
  3. "time"
  4. "github.com/coreos/etcd/store"
  5. "github.com/coreos/go-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. // CreateSetCommand creates a version 2 command to set a key to a given value in the store.
  18. func (f *CommandFactory) CreateSetCommand(key string, value string, expireTime time.Time) raft.Command {
  19. return &SetCommand{
  20. Key: key,
  21. Value: value,
  22. ExpireTime: expireTime,
  23. }
  24. }
  25. // CreateCreateCommand creates a version 2 command to create a new key in the store.
  26. func (f *CommandFactory) CreateCreateCommand(key string, value string, expireTime time.Time, unique bool) raft.Command {
  27. return &CreateCommand{
  28. Key: key,
  29. Value: value,
  30. ExpireTime: expireTime,
  31. Unique: unique,
  32. }
  33. }
  34. // CreateUpdateCommand creates a version 2 command to update a key to a given value in the store.
  35. func (f *CommandFactory) CreateUpdateCommand(key string, value string, expireTime time.Time) raft.Command {
  36. return &UpdateCommand{
  37. Key: key,
  38. Value: value,
  39. ExpireTime: expireTime,
  40. }
  41. }
  42. // CreateDeleteCommand creates a version 2 command to delete a key from the store.
  43. func (f *CommandFactory) CreateDeleteCommand(key string, recursive bool) raft.Command {
  44. return &DeleteCommand{
  45. Key: key,
  46. Recursive: recursive,
  47. }
  48. }
  49. // CreateCompareAndSwapCommand creates a version 2 command to conditionally set a key in the store.
  50. func (f *CommandFactory) CreateCompareAndSwapCommand(key string, value string, prevValue string, prevIndex uint64, expireTime time.Time) raft.Command {
  51. return &CompareAndSwapCommand{
  52. Key: key,
  53. Value: value,
  54. PrevValue: prevValue,
  55. PrevIndex: prevIndex,
  56. ExpireTime: expireTime,
  57. }
  58. }