1
0
Fork 0

WriteCommand -> ExecuteCommand

master
Philip O'Toole 10 years ago
parent b126cb37a8
commit 79e14c2be1

@ -8,51 +8,51 @@ import (
)
// This command encapsulates a sqlite statement.
type WriteCommand struct {
type ExecuteCommand struct {
Stmt string `json:"stmt"`
}
// Creates a new write command.
func NewWriteCommand(stmt string) *WriteCommand {
return &WriteCommand{
func NewExecuteCommand(stmt string) *ExecuteCommand {
return &ExecuteCommand{
Stmt: stmt,
}
}
// The name of the command in the log.
func (c *WriteCommand) CommandName() string {
func (c *ExecuteCommand) CommandName() string {
return "write"
}
// Executes an sqlite statement.
func (c *WriteCommand) Apply(server raft.Server) (interface{}, error) {
log.Trace("Applying WriteCommand: '%s'", c.Stmt)
func (c *ExecuteCommand) Apply(server raft.Server) (interface{}, error) {
log.Trace("Applying ExecuteCommand: '%s'", c.Stmt)
db := server.Context().(*db.DB)
return nil, db.Execute(c.Stmt)
}
// This command encapsulates a set of sqlite statement, which are executed
// within a transaction.
type TransactionWriteCommandSet struct {
type TransactionExecuteCommandSet struct {
Stmts []string `json:"stmts"`
}
// Creates a new set of sqlite commands, which execute within a transaction.
func NewTransactionWriteCommandSet(stmts []string) *TransactionWriteCommandSet {
return &TransactionWriteCommandSet{
func NewTransactionExecuteCommandSet(stmts []string) *TransactionExecuteCommandSet {
return &TransactionExecuteCommandSet{
Stmts: stmts,
}
}
// The name of the command in the log.
func (c *TransactionWriteCommandSet) CommandName() string {
return "transaction_write"
func (c *TransactionExecuteCommandSet) CommandName() string {
return "transaction_execute"
}
// Executes a set of sqlite statements, within a transaction. All statements
// will take effect, or none.
func (c *TransactionWriteCommandSet) Apply(server raft.Server) (interface{}, error) {
log.Trace("Applying TransactionWriteCommandSet of size %d", len(c.Stmts))
func (c *TransactionExecuteCommandSet) Apply(server raft.Server) (interface{}, error) {
log.Trace("Applying TransactionExecuteCommandSet of size %d", len(c.Stmts))
db := server.Context().(*db.DB)
err := db.StartTransaction()

@ -99,8 +99,8 @@ func main() {
rand.Seed(time.Now().UnixNano())
// Setup commands.
raft.RegisterCommand(&command.WriteCommand{})
raft.RegisterCommand(&command.TransactionWriteCommandSet{})
raft.RegisterCommand(&command.ExecuteCommand{})
raft.RegisterCommand(&command.TransactionExecuteCommandSet{})
// Set the data directory.
if flag.NArg() == 0 {

@ -268,7 +268,7 @@ func (s *Server) writeHandler(w http.ResponseWriter, req *http.Request) {
transaction, _ := isTransaction(req)
if transaction {
log.Trace("Transaction requested")
_, err = s.raftServer.Do(command.NewTransactionWriteCommandSet(stmts))
_, err = s.raftServer.Do(command.NewTransactionExecuteCommandSet(stmts))
if err != nil {
failures = append(failures, FailedSqlStmt{stmts[0], err.Error()})
log.Trace("Transaction failed: %s", err.Error())
@ -276,7 +276,7 @@ func (s *Server) writeHandler(w http.ResponseWriter, req *http.Request) {
} else {
log.Trace("No transaction requested")
for i := range stmts {
_, err = s.raftServer.Do(command.NewWriteCommand(stmts[i]))
_, err = s.raftServer.Do(command.NewExecuteCommand(stmts[i]))
if err != nil {
log.Trace("Execute statement %s failed: %s", stmts[i], err.Error())
failures = append(failures, FailedSqlStmt{stmts[i], err.Error()})

Loading…
Cancel
Save