Skip to content

Commit

Permalink
Merge pull request #11 from absolutelightning/longest-prefix
Browse files Browse the repository at this point in the history
Longest Prefix command
  • Loading branch information
absolutelightning authored Sep 28, 2024
2 parents 8720f86 + 4ab0b0e commit eaa41ab
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ This graph shows the performance comparison between Treds - ZRangeScoreKeys and
* `MSET key1 value1 [key2 value2 key3 value3 ....]`- Set values for multiple keys
* `MGET key1 [key2 key3 ....]`- Get values for multiple keys
* `DELPREFIX prefix` - Delete all keys having a common prefix. Returns number of keys deleted
* `LNGPREFIX prefix` - Returns the key value pair in which key is the longest prefix in given prefix
* `DBSIZE` - Get number of keys in the db
* `SCANKEYS cursor prefix count` - Returns the count number of keys matching prefix starting from an index in lex order only present in Key/Value Store. Last element is the next cursor
* `SCANKVS cursor prefix count` - Returns the count number of keys/value pair in which keys match prefix starting from an index in lex order only present in Key/Value Store. Last element is the next cursor
Expand Down
1 change: 1 addition & 0 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func completer(d prompt.Document) []prompt.Suggest {
{Text: "KVS", Description: "KVS regex - Returns all keys/values in which keys match a regex in lex order"},
{Text: "LINDEX", Description: "LINDEX key index - Returns the element at index of list with key"},
{Text: "LLEN", Description: "LLEN key - Returns the length of list with key"},
{Text: "LNGPREFIX", Description: "LNGPREFIX prefix - Returns the key value pair in which key is the longest prefix in given prefix"},
{Text: "LPOP", Description: "LPOP key count - Removes count elements from left of list with key and returns the popped elements"},
{Text: "LPUSH", Description: "LPUSH key element [element ...] - Adds elements to the left of list with key"},
{Text: "LRANGE", Description: "LRANGE key start stop - Returns the elements from start index to stop index in the list with key"},
Expand Down
1 change: 1 addition & 0 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ func RegisterCommands(r CommandRegistry) {
RegisterHValsCommand(r)
RegisterExpireCommand(r)
RegisterTtlCommand(r)
RegisterLongestPrefixCommand(r)
}
22 changes: 22 additions & 0 deletions commands/lng_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package commands

import (
"treds/store"
)

const LongestPrefixCommand = "LNGPREFIX"

func RegisterLongestPrefixCommand(r CommandRegistry) {
r.Add(&CommandRegistration{
Name: LongestPrefixCommand,
Validate: validateDeletePrefix(),
Execute: executeLongestPrefixCommand(),
})
}

func executeLongestPrefixCommand() ExecutionHook {
return func(args []string, store store.Store) (string, error) {
prefix := args[0]
return store.LongestPrefix(prefix)
}
}
4 changes: 4 additions & 0 deletions commands/mock_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,7 @@ func (rs *MockStore) Expire(key string, expiration time.Time) error {
func (rs *MockStore) Ttl(key string) int {
return 0
}

func (rs *MockStore) LongestPrefix(key string) (string, error) {
return "", nil
}
1 change: 1 addition & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ type Store interface {
CleanUpExpiredKeys()
Expire(key string, at time.Time) error
Ttl(key string) int
LongestPrefix(string) (string, error)
}
13 changes: 13 additions & 0 deletions store/tred_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1488,3 +1488,16 @@ func (rs *TredsStore) Ttl(key string) int {
}
return -2
}

func (rs *TredsStore) LongestPrefix(prefix string) (string, error) {
var res strings.Builder
key, val, found := rs.tree.Root().LongestPrefix([]byte(prefix))
if found {
res.WriteString(string(key))
res.WriteString("\n")
res.WriteString(val.(string))
res.WriteString("\n")
return res.String(), nil
}
return "", nil
}

0 comments on commit eaa41ab

Please sign in to comment.