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 search dsl #72

Merged
merged 1 commit into from
Sep 28, 2023
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
14 changes: 11 additions & 3 deletions internal/app/subsystems/aio/store/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"

"github.com/resonatehq/resonate/internal/aio"
Expand Down Expand Up @@ -92,11 +93,12 @@ const (
promises
WHERE
($1::int IS NULL OR sort_id < $1) AND
(state & $2 != 0)
state & $2 != 0 AND
id LIKE $3
ORDER BY
sort_id DESC
LIMIT
$3`
$4`

PROMISE_INSERT_STATEMENT = `
INSERT INTO promises
Expand Down Expand Up @@ -533,14 +535,20 @@ func (w *PostgresStoreWorker) readPromise(tx *sql.Tx, cmd *types.ReadPromiseComm
}

func (w *PostgresStoreWorker) searchPromises(tx *sql.Tx, cmd *types.SearchPromisesCommand) (*types.Result, error) {
util.Assert(cmd.Q != "", "query cannot be empty")
util.Assert(cmd.States != nil, "states cannot be empty")

// convert query
query := strings.ReplaceAll(cmd.Q, "*", "%")

// convert list of state to bit mask
mask := 0
for _, state := range cmd.States {
mask = mask | int(state)
}

// select
rows, err := tx.Query(PROMISE_SEARCH_STATEMENT, cmd.SortId, mask, cmd.Limit)
rows, err := tx.Query(PROMISE_SEARCH_STATEMENT, cmd.SortId, mask, query, cmd.Limit)
if err != nil {
return nil, err
}
Expand Down
12 changes: 10 additions & 2 deletions internal/app/subsystems/aio/store/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/json"
"os"
"strings"
"time"

"github.com/resonatehq/resonate/internal/aio"
Expand Down Expand Up @@ -84,7 +85,8 @@ const (
promises
WHERE
(? IS NULL OR sort_id < ?) AND
(state & ? != 0)
state & ? != 0 AND
id LIKE ?
ORDER BY
sort_id DESC
LIMIT
Expand Down Expand Up @@ -508,14 +510,20 @@ func (w *SqliteStoreWorker) readPromise(tx *sql.Tx, cmd *types.ReadPromiseComman
}

func (w *SqliteStoreWorker) searchPromises(tx *sql.Tx, cmd *types.SearchPromisesCommand) (*types.Result, error) {
util.Assert(cmd.Q != "", "query cannot be empty")
util.Assert(cmd.States != nil, "states cannot be empty")

// convert query
query := strings.ReplaceAll(cmd.Q, "*", "%")

// convert list of state to bit mask
mask := 0
for _, state := range cmd.States {
mask = mask | int(state)
}

// select
rows, err := tx.Query(PROMISE_SEARCH_STATEMENT, cmd.SortId, cmd.SortId, mask, cmd.Limit)
rows, err := tx.Query(PROMISE_SEARCH_STATEMENT, cmd.SortId, cmd.SortId, mask, query, cmd.Limit)
if err != nil {
return nil, err
}
Expand Down
242 changes: 241 additions & 1 deletion internal/app/subsystems/aio/store/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,247 @@ var TestCases = []*testCase{
},
},
{
name: "SearchPromises",
name: "SearchPromisesById",
commands: []*types.Command{
{
Kind: types.StoreCreatePromise,
CreatePromise: &types.CreatePromiseCommand{
Id: "foo.a",
Timeout: 2,
Param: promise.Value{
Headers: map[string]string{},
Data: []byte{},
},
Tags: map[string]string{},
CreatedOn: 1,
},
},
{
Kind: types.StoreCreatePromise,
CreatePromise: &types.CreatePromiseCommand{
Id: "foo.b",
Timeout: 2,
Param: promise.Value{
Headers: map[string]string{},
Data: []byte{},
},
Tags: map[string]string{},
CreatedOn: 1,
},
},
{
Kind: types.StoreCreatePromise,
CreatePromise: &types.CreatePromiseCommand{
Id: "a.bar",
Timeout: 2,
Param: promise.Value{
Headers: map[string]string{},
Data: []byte{},
},
Tags: map[string]string{},
CreatedOn: 1,
},
},
{
Kind: types.StoreCreatePromise,
CreatePromise: &types.CreatePromiseCommand{
Id: "b.bar",
Timeout: 2,
Param: promise.Value{
Headers: map[string]string{},
Data: []byte{},
},
Tags: map[string]string{},
CreatedOn: 1,
},
},
{
Kind: types.StoreSearchPromises,
SearchPromises: &types.SearchPromisesCommand{
Q: "foo.*",
States: []promise.State{
promise.Pending,
},
Limit: 2,
},
},
{
Kind: types.StoreSearchPromises,
SearchPromises: &types.SearchPromisesCommand{
Q: "*.bar",
States: []promise.State{
promise.Pending,
},
Limit: 2,
},
},
{
Kind: types.StoreSearchPromises,
SearchPromises: &types.SearchPromisesCommand{
Q: "*",
States: []promise.State{
promise.Pending,
},
Limit: 2,
},
},
{
Kind: types.StoreSearchPromises,
SearchPromises: &types.SearchPromisesCommand{
Q: "*",
States: []promise.State{
promise.Pending,
},
Limit: 2,
SortId: int64ToPointer(3),
},
},
},
expected: []*types.Result{
{
Kind: types.StoreCreatePromise,
CreatePromise: &types.AlterPromisesResult{
RowsAffected: 1,
},
},
{
Kind: types.StoreCreatePromise,
CreatePromise: &types.AlterPromisesResult{
RowsAffected: 1,
},
},
{
Kind: types.StoreCreatePromise,
CreatePromise: &types.AlterPromisesResult{
RowsAffected: 1,
},
},
{
Kind: types.StoreCreatePromise,
CreatePromise: &types.AlterPromisesResult{
RowsAffected: 1,
},
},
{
Kind: types.StoreSearchPromises,
SearchPromises: &types.QueryPromisesResult{
RowsReturned: 2,
LastSortId: 1,
Records: []*promise.PromiseRecord{
{
Id: "foo.b",
State: 1,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
CreatedOn: int64ToPointer(1),
Tags: []byte("{}"),
SortId: 2,
},
{
Id: "foo.a",
State: 1,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
CreatedOn: int64ToPointer(1),
Tags: []byte("{}"),
SortId: 1,
},
},
},
},
{
Kind: types.StoreSearchPromises,
SearchPromises: &types.QueryPromisesResult{
RowsReturned: 2,
LastSortId: 3,
Records: []*promise.PromiseRecord{
{
Id: "b.bar",
State: 1,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
CreatedOn: int64ToPointer(1),
Tags: []byte("{}"),
SortId: 4,
},
{
Id: "a.bar",
State: 1,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
CreatedOn: int64ToPointer(1),
Tags: []byte("{}"),
SortId: 3,
},
},
},
},
{
Kind: types.StoreSearchPromises,
SearchPromises: &types.QueryPromisesResult{
RowsReturned: 2,
LastSortId: 3,
Records: []*promise.PromiseRecord{
{
Id: "b.bar",
State: 1,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
CreatedOn: int64ToPointer(1),
Tags: []byte("{}"),
SortId: 4,
},
{
Id: "a.bar",
State: 1,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
CreatedOn: int64ToPointer(1),
Tags: []byte("{}"),
SortId: 3,
},
},
},
},
{
Kind: types.StoreSearchPromises,
SearchPromises: &types.QueryPromisesResult{
RowsReturned: 2,
LastSortId: 1,
Records: []*promise.PromiseRecord{
{
Id: "foo.b",
State: 1,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
CreatedOn: int64ToPointer(1),
Tags: []byte("{}"),
SortId: 2,
},
{
Id: "foo.a",
State: 1,
ParamHeaders: []byte("{}"),
ParamData: []byte{},
Timeout: 2,
CreatedOn: int64ToPointer(1),
Tags: []byte("{}"),
SortId: 1,
},
},
},
},
},
},
{
name: "SearchPromisesByState",
commands: []*types.Command{
{
Kind: types.StoreCreatePromise,
Expand Down
10 changes: 9 additions & 1 deletion test/dst/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ func (g *Generator) GenerateSearchPromises(r *rand.Rand, t int64) *types.Request
limit := r.Intn(10)
states := []promise.State{}

var query string
switch r.Intn(2) {
case 0:
query = fmt.Sprintf("*%d", r.Intn(10))
default:
query = fmt.Sprintf("%d*", r.Intn(10))
}

for i := 0; i < r.Intn(5); i++ {
switch r.Intn(5) {
case 0:
Expand All @@ -143,7 +151,7 @@ func (g *Generator) GenerateSearchPromises(r *rand.Rand, t int64) *types.Request
return &types.Request{
Kind: types.SearchPromises,
SearchPromises: &types.SearchPromisesRequest{
Q: "*",
Q: query,
States: states,
Limit: limit,
},
Expand Down
18 changes: 13 additions & 5 deletions test/dst/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import (
"fmt"
"regexp"
"strings"

"github.com/resonatehq/resonate/internal/kernel/types"
"github.com/resonatehq/resonate/pkg/promise"
Expand Down Expand Up @@ -59,19 +61,25 @@
}

func (m *Model) ValidateSearchPromises(req *types.Request, res *types.Response) error {
states := map[promise.State]bool{}
for _, state := range req.SearchPromises.States {
states[state] = true
}

if res.SearchPromises.Cursor != nil {
m.addCursor(&types.Request{
Kind: types.SearchPromises,
SearchPromises: res.SearchPromises.Cursor.Next,
})
}

regex := regexp.MustCompile(fmt.Sprintf("^%s$", strings.ReplaceAll(req.SearchPromises.Q, "*", ".*")))

states := map[promise.State]bool{}
for _, state := range req.SearchPromises.States {
states[state] = true
}

for _, p := range res.SearchPromises.Promises {
if !regex.MatchString(p.Id) {
return fmt.Errorf("promise id '%s' does not match search query '%s'", p.Id, req.SearchPromises.Q)
}

Check warning on line 81 in test/dst/model.go

View check run for this annotation

Codecov / codecov/patch

test/dst/model.go#L80-L81

Added lines #L80 - L81 were not covered by tests

if _, ok := states[p.State]; !ok {
return fmt.Errorf("unexpected state %s, searched for %s", p.State, req.SearchPromises.States)
}
Expand Down