Skip to content

Commit

Permalink
Write relations to pre-allocated slices (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenh authored Dec 6, 2024
1 parent 5e70cdd commit c60a31d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.3
require (
github.com/Masterminds/semver/v3 v3.3.1
github.com/aserto-dev/aserto-grpc v0.2.6
github.com/aserto-dev/azm v0.2.2-0.20241204064621-830edfe39225
github.com/aserto-dev/azm v0.2.2-0.20241206200521-5da97f2bc092
github.com/aserto-dev/errors v0.0.11
github.com/aserto-dev/go-directory v0.33.1
github.com/bufbuild/protovalidate-go v0.7.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYW
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
github.com/aserto-dev/aserto-grpc v0.2.6 h1:h64MYALF5zLm2sSKcLEtyXyrJvZSxfqTOmQ1j/J44kI=
github.com/aserto-dev/aserto-grpc v0.2.6/go.mod h1:Vki74KINVfnwtJ8QGzRm+xHNjsJ2KUWFtXhezJK9DEg=
github.com/aserto-dev/azm v0.2.2-0.20241204064621-830edfe39225 h1:zAg4NhpKfAC50dsVPLo1XjAAEdJ9j7om0SnItyqs5CM=
github.com/aserto-dev/azm v0.2.2-0.20241204064621-830edfe39225/go.mod h1:2/cPbPXJST9swni5fE6SaI960ahAdiaj7rLhjTXF4ak=
github.com/aserto-dev/azm v0.2.2-0.20241206200521-5da97f2bc092 h1:EPHo8BZdkfLecd8XCd4d80ZkWUd3/Kp1mi5jiEF2SzA=
github.com/aserto-dev/azm v0.2.2-0.20241206200521-5da97f2bc092/go.mod h1:2/cPbPXJST9swni5fE6SaI960ahAdiaj7rLhjTXF4ak=
github.com/aserto-dev/errors v0.0.11 h1:CXo+Uwmh09doG2HvL1SC8Fnne8f9VPrGyEQPtogAfyY=
github.com/aserto-dev/errors v0.0.11/go.mod h1:T1YQOtcxpgBriPTn5HXJkD/QukYz5YojYOIzGMo0ybM=
github.com/aserto-dev/go-directory v0.33.1 h1:jLyzMRu5Omw5KJ2cmqnGZZbrF091HFHnt9awhqZhSq8=
Expand Down
17 changes: 12 additions & 5 deletions pkg/bdb/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,17 @@ func Scan[T any, M Message[T]](ctx context.Context, tx *bolt.Tx, path Path, keyF
return results, nil
}

func ScanWithFilter[T any, M Message[T]](ctx context.Context, tx *bolt.Tx, path Path, keyFilter string, valueFilter func(M) bool) ([]M, error) {
func ScanWithFilter[T any, M Message[T]](
ctx context.Context,
tx *bolt.Tx,
path Path,
keyFilter string,
valueFilter func(M) bool,
out *[]M,
) error {
b, err := SetBucket(tx, path)
if err != nil {
return nil, errors.Wrapf(ErrPathNotFound, "path [%s]", path)
return errors.Wrapf(ErrPathNotFound, "path [%s]", path)
}

c := b.Cursor()
Expand All @@ -204,20 +211,20 @@ func ScanWithFilter[T any, M Message[T]](ctx context.Context, tx *bolt.Tx, path

prefix := []byte(keyFilter)

var results []M
results := *out

for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
m, err := unmarshal[T, M](v)
if err != nil {
return nil, err
return err
}

if valueFilter(m) {
results = append(results, m)
}
}

return results, nil
return nil
}

func KeyPrefixExists[T any, M Message[T]](ctx context.Context, tx *bolt.Tx, path Path, keyFilter string) (bool, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/ds/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func (i *check) Exec(ctx context.Context, tx *bolt.Tx, mc *cache.Cache) (*dsr3.C
}

func getRelations(ctx context.Context, tx *bolt.Tx) graph.RelationReader {
return func(r *dsc3.Relation) ([]*dsc3.Relation, error) {
return func(r *dsc3.Relation, out *[]*dsc3.Relation) error {
path, keyFilter, valueFilter := Relation(r).Filter()

return bdb.ScanWithFilter[dsc3.Relation](ctx, tx, path, keyFilter, valueFilter)
return bdb.ScanWithFilter[dsc3.Relation](ctx, tx, path, keyFilter, valueFilter, out)
}
}

Expand Down

0 comments on commit c60a31d

Please sign in to comment.