Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FGET command #725

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ To set a field when an object already exists:
> fset fleet truck1 speed 90
```

To get a field when an object already exists:
```
> fget fleet truck1 speed
```

## Searching

Tile38 has support to search for objects and points that are within or intersects other objects. All object types can be searched including Polygons, MultiPolygons, GeometryCollections, etc.
Expand Down
20 changes: 20 additions & 0 deletions core/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@
"since": "1.0.0",
"group": "keys"
},
"FGET": {
"summary": "Gets the value for the field of an id",
"complexity": "O(1)",
"arguments": [
{
"name": "key",
"type": "string"
},
{
"name": "id",
"type": "string"
},
{
"name": "field",
"type": "string"
}
],
"since": "1.33.0",
"group": "keys"
},
"BOUNDS": {
"summary": "Get the combined bounds of all the objects in a key",
"complexity": "O(1)",
Expand Down
20 changes: 20 additions & 0 deletions core/commands_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,26 @@ var commandsJSON = `{
"since": "1.0.0",
"group": "keys"
},
"FGET": {
"summary": "Gets the value for the field of an id",
"complexity": "O(1)",
"arguments": [
{
"name": "key",
"type": "string"
},
{
"name": "id",
"type": "string"
},
{
"name": "field",
"type": "string"
}
],
"since": "1.33.0",
"group": "keys"
},
"BOUNDS": {
"summary": "Get the combined bounds of all the objects in a key",
"complexity": "O(1)",
Expand Down
40 changes: 40 additions & 0 deletions internal/server/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,46 @@ func (s *Server) cmdFSET(msg *Message) (resp.Value, commandDetails, error) {
return res, d, nil
}

// FGET key id field
func (s *Server) cmdFGET(msg *Message) (resp.Value, error) {
start := time.Now()

// >> Args

args := msg.Args

if len(args) < 4 {
return retrerr(errInvalidNumberOfArguments)
}
key, id, field := args[1], args[2], args[3]

// >> Operation

col, _ := s.cols.Get(key)
if col == nil {
return retrerr(errKeyNotFound)
}
o := col.Get(id)
if o == nil {
return retrerr(errIDNotFound)
}
f := o.Fields().Get(field)

// >> Response

var buf bytes.Buffer
switch msg.OutputType {
case JSON:
buf.WriteString(`{"ok":true`)
buf.WriteString(`,"value":` + f.Value().JSON())
buf.WriteString(`,"elapsed":"` + time.Since(start).String() + "\"}")
return resp.StringValue(buf.String()), nil
case RESP:
return resp.StringValue(f.Value().Data()), nil
}
return NOMessage, nil
}

// EXPIRE key id seconds
func (s *Server) cmdEXPIRE(msg *Message) (resp.Value, commandDetails, error) {
start := time.Now()
Expand Down
8 changes: 5 additions & 3 deletions internal/server/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ func (s *Server) commandInScript(msg *Message) (
res, err = s.cmdBOUNDS(msg)
case "get":
res, err = s.cmdGET(msg)
case "fget":
res, err = s.cmdFGET(msg)
case "jget":
res, err = s.cmdJget(msg)
case "jset":
Expand Down Expand Up @@ -735,7 +737,7 @@ func (s *Server) luaTile38AtomicRW(msg *Message) (resp.Value, error) {
return resp.NullValue(), errReadOnly
}
case "get", "keys", "scan", "nearby", "within", "intersects", "hooks", "search",
"ttl", "bounds", "server", "info", "type", "jget", "test":
"ttl", "bounds", "server", "info", "type", "jget", "fget", "test":
// read operations
if s.config.followHost() != "" && !s.fcuponce {
return resp.NullValue(), errCatchingUp
Expand Down Expand Up @@ -788,7 +790,7 @@ func (s *Server) luaTile38AtomicRO(msg *Message) (resp.Value, error) {
return resp.NullValue(), errReadOnly

case "get", "keys", "scan", "nearby", "within", "intersects", "hooks", "search",
"ttl", "bounds", "server", "info", "type", "jget", "test":
"ttl", "bounds", "server", "info", "type", "jget", "fget", "test":
// read operations
if s.config.followHost() != "" && !s.fcuponce {
return resp.NullValue(), errCatchingUp
Expand Down Expand Up @@ -839,7 +841,7 @@ func (s *Server) luaTile38NonAtomic(msg *Message) (resp.Value, error) {
return resp.NullValue(), errReadOnly
}
case "get", "keys", "scan", "nearby", "within", "intersects", "hooks", "search",
"ttl", "bounds", "server", "info", "type", "jget", "test":
"ttl", "bounds", "server", "info", "type", "jget", "fget", "test":
// read operations
s.mu.RLock()
defer s.mu.RUnlock()
Expand Down
4 changes: 3 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ func (s *Server) handleInputCommand(client *Client, msg *Message) error {
}
case "get", "keys", "scan", "nearby", "within", "intersects", "hooks",
"chans", "search", "ttl", "bounds", "server", "info", "type", "jget",
"evalro", "evalrosha", "healthz", "role":
"evalro", "evalrosha", "healthz", "role", "fget":
// read operations

s.mu.RLock()
Expand Down Expand Up @@ -1227,6 +1227,8 @@ func (s *Server) command(msg *Message, client *Client) (
res, err = s.cmdBOUNDS(msg)
case "get":
res, err = s.cmdGET(msg)
case "fget":
res, err = s.cmdFGET(msg)
case "jget":
res, err = s.cmdJget(msg)
case "jset":
Expand Down
20 changes: 20 additions & 0 deletions tests/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func subTestKeys(g *testGroup) {
g.regSubTest("RENAMENX", keys_RENAMENX_test)
g.regSubTest("EXPIRE", keys_EXPIRE_test)
g.regSubTest("FSET", keys_FSET_test)
g.regSubTest("FGET", keys_FGET_test)
g.regSubTest("GET", keys_GET_test)
g.regSubTest("KEYS", keys_KEYS_test)
g.regSubTest("PERSIST", keys_PERSIST_test)
Expand Down Expand Up @@ -201,6 +202,25 @@ func keys_FSET_test(mc *mockServer) error {
Do("FSET", "mykey", "myid", "f2", 0).JSON().OK(),
)
}
func keys_FGET_test(mc *mockServer) error {
return mc.DoBatch(
Do("SET", "mykey", "myid", "HASH", "9my5xp7").OK(),
Do("GET", "mykey", "myid", "WITHFIELDS", "HASH", 7).Str("[9my5xp7]"),
Do("FSET", "mykey", "myid", "f1", 105.6).Str("1"),
Do("FGET", "mykey", "myid", "f1").Str("105.6"),
Do("FSET", "mykey", "myid", "f1", 1.1, "f2", 2.2).Str("2"),
Do("FGET", "mykey", "myid", "f2").Str("2.2"),
Do("FGET", "mykey", "myid", "f1").Str("1.1"),
Do("FGET", "mykey", "myid", "f1").JSON().Str(`{"ok":true,"value":1.1}`),
Do("FSET", "mykey", "myid", "f3", "a").Str("1"),
Do("FGET", "mykey", "myid", "f3").Str("a"),
Do("FGET", "mykey", "myid", "f4").Str("0"),
Do("FGET", "mykey", "myid", "f4").JSON().Str(`{"ok":true,"value":0}`),
Do("FGET", "mykey", "myid").Err("wrong number of arguments for 'fget' command"),
Do("FGET", "mykey2", "myid", "a", "b").Err("key not found"),
Do("FGET", "mykey", "myid2", "a", "b").Err("id not found"),
)
}
func keys_GET_test(mc *mockServer) error {
return mc.DoBatch(
Do("SET", "mykey", "myid", "STRING", "value").OK(),
Expand Down
Loading