Skip to main content

execute.go

execute.go - Overview

This file defines the Eval struct and its methods for executing commands in the DiceDB system. It handles command preprocessing, execution, and interaction with the store and other internal components.

Detailed Documentation

type Eval struct

type Eval struct {
cmd *cmd.DiceDBCmd
client *comm.Client
store *dstore.Store
isHTTPOperation bool
isWebSocketOperation bool
isPreprocessOperation bool
}
  • Purpose: Represents the evaluation context for a DiceDB command.
    • cmd: A pointer to the DiceDB command to be executed.
    • client: A pointer to the communication client.
    • store: A pointer to the data store.
    • isHTTPOperation: A boolean indicating if the operation is an HTTP operation.
    • isWebSocketOperation: A boolean indicating if the operation is a WebSocket operation.
    • isPreprocessOperation: A boolean indicating if the operation is a preprocessing operation.

func NewEval(c *cmd.DiceDBCmd, client *comm.Client, store *dstore.Store, httpOp bool, websocketOp bool, preProcessing bool) *Eval

func NewEval(c *cmd.DiceDBCmd, client *comm.Client, store *dstore.Store, httpOp, websocketOp, preProcessing bool) *Eval {
return &Eval{
cmd: c,
client: client,
store: store,
isHTTPOperation: httpOp,
isWebSocketOperation: websocketOp,
isPreprocessOperation: preProcessing,
}
}
  • Purpose: Creates a new Eval instance.
    • Parameters:
      • c: A pointer to the DiceDB command.
      • client: A pointer to the communication client.
      • store: A pointer to the data store.
      • httpOp: A boolean indicating if the operation is an HTTP operation.
      • websocketOp: A boolean indicating if the operation is a WebSocket operation.
      • preProcessing: A boolean indicating if the operation is a preprocessing operation.
    • Returns: A pointer to the newly created Eval instance.

func (e *Eval) PreProcessCommand() *EvalResponse

func (e *Eval) PreProcessCommand() *EvalResponse {
if f, ok := PreProcessing[e.cmd.Cmd]; ok {
return f(e.cmd.Args, e.store)
}
return &EvalResponse{Result: nil, Error: diceerrors.ErrInternalServer}
}
  • Purpose: Preprocesses the command based on the PreProcessing map.
    • Returns: An EvalResponse containing the result of the preprocessing or an internal server error if the command is not found in the PreProcessing map.

func (e *Eval) ExecuteCommand() *EvalResponse

func (e *Eval) ExecuteCommand() *EvalResponse {
diceCmd, ok := DiceCmds[e.cmd.Cmd]
if !ok {
return &EvalResponse{Result: diceerrors.NewErrWithFormattedMessage("unknown command '%s', with args beginning with: %s", e.cmd.Cmd, strings.Join(e.cmd.Args, " ")), Error: nil}
}

// Temporary logic till we move all commands to new eval logic.
// MigratedDiceCmds map contains refactored eval commands
// For any command we will first check in the existing map
// if command is NA then we will check in the new map
// Check if the dice command has been migrated
if diceCmd.IsMigrated {
// ===============================================================================
// dealing with store object is not recommended for all commands
// These operations are specialised for the commands which requires
// transferring data across multiple shards. e.g. COPY, RENAME, PFMERGE
// ===============================================================================
if e.cmd.InternalObjs != nil {
// This involves handling object at store level, evaluating it, modifying it, and then storing it back.
return diceCmd.StoreObjectEval(e.cmd, e.store)
}

// If the 'Obj' field is nil, handle the command using the arguments.
// This path likely involves evaluating the command based on its provided arguments.
return diceCmd.NewEval(e.cmd.Args, e.store)
}

// The following commands could be handled at the shard level, however, we can randomly let any shard handle them
// to reduce load on main server.
switch diceCmd.Name {
// Old implementation kept as it is, but we will be moving
// to the new implementation soon for all commands
case auth.Cmd:
return &EvalResponse{Result: EvalAUTH(e.cmd.Args, e.client), Error: nil}
case "ABORT":
return &EvalResponse{Result: RespOK, Error: nil}
default:
return &EvalResponse{Result: diceCmd.Eval(e.cmd.Args, e.store), Error: nil}
}
}
  • Purpose: Executes the command based on the DiceCmds map. It handles both migrated and non-migrated commands.
    • Returns: An EvalResponse containing the result of the execution or an error if the command is unknown.

Getting Started Relevance: NO