command_factory.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package store
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/coreos/etcd/third_party/github.com/goraft/raft"
  6. )
  7. // A lookup of factories by version.
  8. var factories = make(map[int]CommandFactory)
  9. var minVersion, maxVersion int
  10. // The CommandFactory provides a way to create different types of commands
  11. // depending on the current version of the store.
  12. type CommandFactory interface {
  13. Version() int
  14. CreateUpgradeCommand() raft.Command
  15. CreateSetCommand(key string, dir bool, value string, expireTime time.Time) raft.Command
  16. CreateCreateCommand(key string, dir bool, value string, expireTime time.Time, unique bool) raft.Command
  17. CreateUpdateCommand(key string, value string, expireTime time.Time) raft.Command
  18. CreateDeleteCommand(key string, dir, recursive bool) raft.Command
  19. CreateCompareAndSwapCommand(key string, value string, prevValue string,
  20. prevIndex uint64, expireTime time.Time) raft.Command
  21. CreateCompareAndDeleteCommand(key string, prevValue string, prevIndex uint64) raft.Command
  22. CreateSyncCommand(now time.Time) raft.Command
  23. CreateGetCommand(key string, recursive, sorted bool) raft.Command
  24. }
  25. // RegisterCommandFactory adds a command factory to the global registry.
  26. func RegisterCommandFactory(factory CommandFactory) {
  27. version := factory.Version()
  28. if GetCommandFactory(version) != nil {
  29. panic(fmt.Sprintf("Command factory already registered for version: %d", factory.Version()))
  30. }
  31. factories[version] = factory
  32. // Update compatibility versions.
  33. if minVersion == 0 || version > minVersion {
  34. minVersion = version
  35. }
  36. if maxVersion == 0 || version > maxVersion {
  37. maxVersion = version
  38. }
  39. }
  40. // GetCommandFactory retrieves a command factory for a given command version.
  41. func GetCommandFactory(version int) CommandFactory {
  42. return factories[version]
  43. }
  44. // MinVersion returns the minimum compatible store version.
  45. func MinVersion() int {
  46. return minVersion
  47. }
  48. // MaxVersion returns the maximum compatible store version.
  49. func MaxVersion() int {
  50. return maxVersion
  51. }