influxdb/command.go

96 lines
2.4 KiB
Go
Raw Normal View History

2013-04-14 21:37:33 +00:00
package raft
2013-05-10 03:50:57 +00:00
import (
"fmt"
"reflect"
)
//------------------------------------------------------------------------------
//
// Globals
//
//------------------------------------------------------------------------------
var commandTypes map[string]Command
func init() {
commandTypes = map[string]Command{}
}
2013-04-14 21:37:33 +00:00
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
// A command represents an action to be taken on the replicated state machine.
type Command interface {
2013-04-28 04:51:17 +00:00
CommandName() string
Apply(server *Server) (interface{}, error)
2013-04-14 21:37:33 +00:00
}
2013-05-05 19:36:23 +00:00
2013-05-10 03:50:57 +00:00
//------------------------------------------------------------------------------
//
// Functions
//
//------------------------------------------------------------------------------
//--------------------------------------
// Instantiation
//--------------------------------------
// Creates a new instance of a command by name.
2013-07-06 04:49:47 +00:00
func newCommand(name string) (Command, error) {
2013-05-10 03:50:57 +00:00
// Find the registered command.
command := commandTypes[name]
if command == nil {
return nil, fmt.Errorf("raft.Command: Unregistered command type: %s", name)
}
// Make a copy of the command.
v := reflect.New(reflect.Indirect(reflect.ValueOf(command)).Type()).Interface()
copy, ok := v.(Command)
if !ok {
2013-06-08 02:41:36 +00:00
panic(fmt.Sprintf("raft: Unable to copy command: %s (%v)", command.CommandName(), reflect.ValueOf(v).Kind().String()))
2013-05-10 03:50:57 +00:00
}
return copy, nil
}
//--------------------------------------
// Registration
//--------------------------------------
// Registers a command by storing a reference to an instance of it.
func RegisterCommand(command Command) {
if command == nil {
2013-06-08 02:41:36 +00:00
panic(fmt.Sprintf("raft: Cannot register nil"))
2013-05-10 03:50:57 +00:00
} else if commandTypes[command.CommandName()] != nil {
// we need to register NOP command at the beginning
// for testing, it may register mutliple times
// i am not quite familiar with reg prorcess
// maybe you can fix it. sorry!
//panic(fmt.Sprintf("raft: Duplicate registration: %s", command.CommandName()))
return
2013-05-10 03:50:57 +00:00
}
commandTypes[command.CommandName()] = command
2013-05-10 14:47:24 +00:00
}
//--------------------------------------
// NOP command
//--------------------------------------
// NOP command
type NOPCommand struct {
}
// The name of the NOP command in the log
func (c NOPCommand) CommandName() string {
return "nop"
}
// NOP
func (c NOPCommand) Apply(server *Server) (interface{}, error) {
return nil, nil
}