Skip to main content

cmd_get_watch.go

Overview

This file defines the GET.WATCH command, which allows a client to subscribe to changes to a specific key. When the value associated with the key is updated, the client receives the updated value.

Detailed Documentation

cGETWATCH

var cGETWATCH = &CommandMeta{
Name: "GET.WATCH",
Syntax: "GET.WATCH key",
HelpShort: "GET.WATCH creates a query subscription over the GET command",
HelpLong: `
GET.WATCH creates a query subscription over the GET command. The client invoking the command
will receive the output of the GET command (not just the notification) whenever the value against
the key is updated.

You can update the key in any other client. The GET.WATCH client will receive the updated value.
`,
Examples: `
client1:7379> SET k1 v1
OK OK
client1:7379> GET.WATCH k1
entered the watch mode for GET.WATCH k1


client2:7379> SET k1 v2
OK OK


client1:7379> ...
entered the watch mode for GET.WATCH k1
OK [fingerprint=2356444921] v2
`,
Eval: evalGETWATCH,
Execute: executeGETWATCH,
}
  • Purpose: Defines the metadata for the GET.WATCH command, including its name, syntax, help text, examples, and the functions to evaluate and execute the command.
  • Fields:
    • Name: The name of the command (GET.WATCH).
    • Syntax: The command's syntax (GET.WATCH key).
    • HelpShort: A short description of the command.
    • HelpLong: A more detailed description of the command.
    • Examples: Examples of how to use the command.
    • Eval: The evalGETWATCH function, which is responsible for evaluating the command.
    • Execute: The executeGETWATCH function, which is responsible for executing the command.

init

func init() {
CommandRegistry.AddCommand(cGETWATCH)
}
  • Purpose: Registers the GET.WATCH command with the CommandRegistry.

evalGETWATCH

func evalGETWATCH(c *Cmd, s *dstore.Store) (*CmdRes, error) {
r, err := evalGET(c, s)
if err != nil {
return nil, err
}

if r.R.Attrs == nil {
r.R.Attrs = &structpb.Struct{
Fields: make(map[string]*structpb.Value),
}
}

r.R.Attrs.Fields["fingerprint"] = structpb.NewStringValue(strconv.FormatUint(uint64(c.Fingerprint()), 10))
return r, nil
}
  • Purpose: Evaluates the GET.WATCH command. It calls evalGET to perform the get operation and adds a fingerprint attribute to the result.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • s: A pointer to the dstore.Store struct representing the data store.
  • Returns:
    • A pointer to a CmdRes struct representing the result of the command.
    • An error, if any occurred.

executeGETWATCH

func executeGETWATCH(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errors.ErrWrongArgumentCount("GET.WATCH")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalGETWATCH(c, shard.Thread.Store())
}
  • Purpose: Executes the GET.WATCH command. It retrieves the appropriate shard based on the key, and then calls evalGETWATCH on that shard's store.
  • Parameters:
    • c: A pointer to the Cmd struct representing the command.
    • sm: A pointer to the shardmanager.ShardManager struct.
  • Returns:
    • A pointer to a CmdRes struct representing the result of the command.
    • An error, if any occurred (e.g., wrong number of arguments).

Getting Started Relevance