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

Support concurrency / improved performance for multiple queries on same JSON payload #237

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
fix: add missing functions to Query struct
samsullivan committed Jan 11, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 52965f398a2e3b6586b8d84752060017992b3647
6 changes: 0 additions & 6 deletions compiler.go
Original file line number Diff line number Diff line change
@@ -50,12 +50,6 @@ func (c *Code) RunWithContext(ctx context.Context, v any, values ...any) Iter {
return c.ConcurrentRunWithContext(ctx, PrepareData(v), normalized...)
}

// PrepareData is not safe for use in goroutines, since it writes to any maps
// with numeric values; supports ConcurrentRun and ConcurrentRunWithContext.
func PrepareData(v any) PreparedData {
return normalizeNumbers(v)
}

// ConcurrentRun supports reusing data across goroutines, as long as it is
// prepared with PrepareData.
func (c *Code) ConcurrentRun(v PreparedData, values ...any) Iter {
6 changes: 6 additions & 0 deletions normalize.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,12 @@ import (
"strings"
)

// PrepareData is not safe for use in goroutines, since it writes to any maps
// with numeric values; supports ConcurrentRun and ConcurrentRunWithContext.
func PrepareData(v any) PreparedData {
return normalizeNumbers(v)
}

func normalizeNumber(v json.Number) any {
if i, err := v.Int64(); err == nil && math.MinInt <= i && i <= math.MaxInt {
return int(i)
15 changes: 15 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
@@ -34,6 +34,21 @@ func (e *Query) RunWithContext(ctx context.Context, v any) Iter {
return code.RunWithContext(ctx, v)
}

// ConcurrentRun supports reusing data across goroutines, as long as it is
// prepared with PrepareData.
func (e *Query) ConcurrentRun(v PreparedData) Iter {
return e.ConcurrentRunWithContext(context.Background(), v)
}

// ConcurrentRunWithContext runs the code concurrently with context.
func (e *Query) ConcurrentRunWithContext(ctx context.Context, v PreparedData) Iter {
code, err := Compile(e)
if err != nil {
return NewIter(err)
}
return code.ConcurrentRunWithContext(ctx, v)
}

func (e *Query) String() string {
var s strings.Builder
e.writeTo(&s)