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