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

Getrange #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ SELECT CLUSTER_COUNTKEYSINSLOT(<key_id>)
SELECT DBSIZE()
```

### GETRANGE

```sql
SELECT GETRANGE('key-id',<start>,<end>)
```

### LLEN

```sql
Expand Down
2 changes: 1 addition & 1 deletion commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
| GETBIT | 🚧
| GETDEL | 🚧
| GETEX | 🚧
| GETRANGE | 🚧
| GETRANGE | ✅ [`GETRANGE`](https://github.com/augmentable-dev/reqlite/tree/main/internal/redis/getrange)
| GETSET | 🚧
| HDEL | 🚧
| HELLO | 🚧
Expand Down
41 changes: 41 additions & 0 deletions internal/redis/getrange/getrange.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package getrange

import (
"context"
"fmt"

"github.com/go-redis/redis/v8"
"go.riyazali.net/sqlite"
)

type getrange struct {
rdb *redis.Client
}

func (f *getrange) Args() int { return -1 }
func (f *getrange) Deterministic() bool { return false }
func (f *getrange) Apply(ctx *sqlite.Context, values ...sqlite.Value) {
var (
key string
start int64
end int64
)

if len(values) >= 3 {
key = values[0].Text()
start = values[1].Int64()
end = values[2].Int64()
} else {
ctx.ResultError(fmt.Errorf("must supply (key,start,end) to getrange"))
return
}

result := f.rdb.GetRange(context.TODO(), key, start, end)

ctx.ResultText(fmt.Sprintf("%+q", result))
}

// New returns a sqlite function for reading the contents of a file
func New(rdb *redis.Client) sqlite.Function {
return &getrange{rdb}
}
69 changes: 69 additions & 0 deletions internal/redis/getrange/getrange_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package getrange_test

import (
"testing"

getrange "github.com/augmentable-dev/reqlite/internal/redis/getrange"
_ "github.com/augmentable-dev/reqlite/internal/sqlite"
"github.com/go-redis/redismock/v8"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"go.riyazali.net/sqlite"
)

func TestGetrange(t *testing.T) {
rdb, mock := redismock.NewClientMock()

sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) {
if err := api.CreateFunction("getrange", getrange.New(rdb)); err != nil {
return sqlite.SQLITE_ERROR, err
}
return sqlite.SQLITE_OK, nil
})

mock.ExpectGetRange("my-key", -4, -1).SetVal("key")
db, err := sqlx.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()

row := db.QueryRow("SELECT getrange('my-key',-4,-1)")
err = row.Err()
if err != nil {
t.Fatal(err)
}

var s string
err = row.Scan(&s)
if err != nil {
t.Fatal(err)
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
}
func TestGetrangeErr(t *testing.T) {
rdb, _ := redismock.NewClientMock()

sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) {
if err := api.CreateFunction("getrange", getrange.New(rdb)); err != nil {
return sqlite.SQLITE_ERROR, err
}
return sqlite.SQLITE_OK, nil
})

db, err := sqlx.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
var s string
row := db.QueryRow("SELECT getrange()")
err = row.Scan(&s)
if err == nil && s != "must supply (key,start,end) to getrange" {
t.Fatal("no error returned when no input provided")
}

}
5 changes: 5 additions & 0 deletions pkg/ext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/augmentable-dev/reqlite/internal/redis/dbsize"
"github.com/augmentable-dev/reqlite/internal/redis/dump"
"github.com/augmentable-dev/reqlite/internal/redis/echo"
"github.com/augmentable-dev/reqlite/internal/redis/getrange"
"github.com/augmentable-dev/reqlite/internal/redis/hgetall"
"github.com/augmentable-dev/reqlite/internal/redis/llen"
"github.com/augmentable-dev/reqlite/internal/redis/lrange"
Expand Down Expand Up @@ -98,6 +99,10 @@ func init() {
return sqlite.SQLITE_ERROR, err
}

if err := api.CreateFunction("getrange", getrange.New(rdb)); err != nil {
return sqlite.SQLITE_ERROR, err
}

if err := api.CreateFunction("llen", llen.New(rdb)); err != nil {
return sqlite.SQLITE_ERROR, err
}
Expand Down