refactor(search): give the engine Go types instead of the proto messages

This commit is contained in:
Dominik Schmidt committed 2026-11-27 14:53:19 +01:00
1 parent 46630ef1d8
commit 84d85b5660
11 files changed
+59 -55

No files matched your search

+2 -4
View File
@@ -22,7 +22,6 @@ import (
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0"
searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
searchQuery "github.com/opencloud-eu/opencloud/services/search/pkg/query"
)
@@ -65,8 +64,7 @@ func (i *serialIndex) Batch(b *bleve.Batch) error {
}
// Search executes a search request operation within the index.
// Returns a SearchIndexResponse object or an error.
func (b *Backend) Search(_ context.Context, sir *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) {
func (b *Backend) Search(_ context.Context, sir *search.IndexQuery) (*search.IndexResult, error) {
createdQuery, err := b.queryCreator.Create(sir.Query)
if err != nil {
if kql.IsValidationError(err) {
@@ -172,7 +170,7 @@ func (b *Backend) Search(_ context.Context, sir *searchService.SearchIndexReques
matches = append(matches, match)
}
return &searchService.SearchIndexResponse{
return &search.IndexResult{
Matches: matches,
TotalMatches: int32(totalMatches),
}, nil
+2 -3
View File
@@ -11,7 +11,6 @@ import (
. "github.com/onsi/gomega"
"github.com/opencloud-eu/opencloud/pkg/log"
searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/pkg/bleve"
"github.com/opencloud-eu/opencloud/services/search/pkg/content"
bleveQuery "github.com/opencloud-eu/opencloud/services/search/pkg/query/bleve"
@@ -141,10 +140,10 @@ var _ = Describe("Bleve", func() {
Describe("Update", func() {
tagHits := func(tag string) []string {
res, err := eng.Search(context.Background(), &searchService.SearchIndexRequest{Query: `tag:("` + tag + `")`})
res, err := eng.Search(context.Background(), &search.IndexQuery{Query: `tag:("` + tag + `")`})
Expect(err).ToNot(HaveOccurred())
var ids []string
for _, match := range res.GetMatches() {
for _, match := range res.Matches {
ids = append(ids, match.GetEntity().GetId().GetOpaqueId())
}
return ids
+2 -3
View File
@@ -21,7 +21,6 @@ import (
"github.com/opencloud-eu/opencloud/pkg/kql"
"github.com/opencloud-eu/opencloud/pkg/log"
searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0"
searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/pkg/mapping"
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/convert"
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/osu"
@@ -81,7 +80,7 @@ func NewBackend(ctx context.Context, name string, client *opensearchgoAPI.Client
return &Backend{index: index, client: client}, nil
}
func (b *Backend) Search(ctx context.Context, sir *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error) {
func (b *Backend) Search(ctx context.Context, sir *search.IndexQuery) (*search.IndexResult, error) {
boolQuery, err := convert.KQLToOpenSearchBoolQuery(sir.Query)
switch {
case kql.IsValidationError(err):
@@ -173,7 +172,7 @@ func (b *Backend) Search(ctx context.Context, sir *searchService.SearchIndexRequ
matches = append(matches, match)
}
return &searchService.SearchIndexResponse{
return &search.IndexResult{
Matches: matches,
TotalMatches: int32(totalMatches),
}, nil
@@ -11,7 +11,6 @@ import (
opensearchgoAPI "github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"github.com/opencloud-eu/opencloud/pkg/log"
searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/internal/opensearchtest"
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
@@ -147,9 +146,9 @@ var _ = Describe("Backend", func() {
})
tagHits := func(tag string) int {
res, err := backend.Search(context.Background(), &searchService.SearchIndexRequest{Query: `tag:("` + tag + `")`})
res, err := backend.Search(context.Background(), &search.IndexQuery{Query: `tag:("` + tag + `")`})
Expect(err).ToNot(HaveOccurred())
return len(res.GetMatches())
return len(res.Matches)
}
It("rewrites tags and favorites and keeps the extracted fields", func() {
+4 -5
View File
@@ -15,7 +15,6 @@ import (
"github.com/opencloud-eu/opencloud/pkg/log"
searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0"
searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/internal/opensearchtest"
bleveEngine "github.com/opencloud-eu/opencloud/services/search/pkg/bleve"
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch"
@@ -146,12 +145,12 @@ func badRequestAnswer(err error) []string {
return []string{"bad request"}
}
func reads(read func(*searchMessage.Match) string) func(*searchService.SearchIndexResponse) []string {
func reads(read func(*searchMessage.Match) string) func(*search.IndexResult) []string {
return readsMany(func(m *searchMessage.Match) []string { return []string{read(m)} })
}
func readsMany(read func(*searchMessage.Match) []string) func(*searchService.SearchIndexResponse) []string {
return func(resp *searchService.SearchIndexResponse) []string {
func readsMany(read func(*searchMessage.Match) []string) func(*search.IndexResult) []string {
return func(resp *search.IndexResult) []string {
if len(resp.Matches) != 1 {
return []string{fmt.Sprintf("%d matches", len(resp.Matches))}
}
@@ -170,7 +169,7 @@ func resourceID(id *searchMessage.ResourceID) string {
// ask runs a query and returns the matched names; an engine error is the
// answer "error", so a query the engine rejects still shows in the matrix.
func ask(e search.Engine, request *searchService.SearchIndexRequest) ([]string, error) {
func ask(e search.Engine, request *search.IndexQuery) ([]string, error) {
resp, err := e.Search(context.Background(), request)
if err != nil {
return []string{"error"}, err
+4 -5
View File
@@ -12,7 +12,6 @@ import (
"github.com/opencloud-eu/reva/v3/pkg/errtypes"
searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0"
searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
)
@@ -124,7 +123,7 @@ type responseCase struct {
after string
query string
reads string
read func(*searchService.SearchIndexResponse) []string
read func(*search.IndexResult) []string
want []string
engineOverrides map[string]override
@@ -288,7 +287,7 @@ var _ = Describe("Queries", func() {
Skip(e.unavailable)
}
request := &searchService.SearchIndexRequest{Query: c.query, PageSize: c.limit, Ref: c.ref}
request := &search.IndexQuery{Query: c.query, PageSize: c.limit, Ref: c.ref}
expected := override{want: c.want, wantCount: c.wantCount, wantBadRequest: c.wantBadRequest}
_, overridden := c.engineOverrides[name]
@@ -369,7 +368,7 @@ var _ = Describe("Operations", func() {
answers := make([][]string, len(c.expect))
errs := make([]error, len(c.expect))
for i, expect := range c.expect {
answers[i], errs[i] = ask(e.backend, &searchService.SearchIndexRequest{Query: expect.query})
answers[i], errs[i] = ask(e.backend, &search.IndexQuery{Query: expect.query})
recordAnswer(rows[i], name, answers[i])
}
@@ -446,7 +445,7 @@ var _ = Describe("Response", func() {
e.settle()
}
resp, err := e.backend.Search(context.Background(), &searchService.SearchIndexRequest{Query: c.query})
resp, err := e.backend.Search(context.Background(), &search.IndexQuery{Query: c.query})
if err != nil {
recordAnswer(row, name, []string{"error"})
}
@@ -4,7 +4,6 @@ import (
"fmt"
searchMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0"
searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
)
@@ -75,8 +74,8 @@ func entityGroup() responseGroup {
},
{
id: 11, query: `path:"./parent"`, reads: "TotalMatches", want: []string{"2"},
read: func(resp *searchService.SearchIndexResponse) []string {
return []string{fmt.Sprint(resp.GetTotalMatches())}
read: func(resp *search.IndexResult) []string {
return []string{fmt.Sprint(resp.TotalMatches)}
},
},
{
+13 -14
View File
@@ -7,7 +7,6 @@ package mocks
import (
"context"
"github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
mock "github.com/stretchr/testify/mock"
)
@@ -547,26 +546,26 @@ func (_c *Engine_Restore_Call) RunAndReturn(run func(id string) error) *Engine_R
}
// Search provides a mock function for the type Engine
func (_mock *Engine) Search(ctx context.Context, req *v0.SearchIndexRequest) (*v0.SearchIndexResponse, error) {
func (_mock *Engine) Search(ctx context.Context, req *search.IndexQuery) (*search.IndexResult, error) {
ret := _mock.Called(ctx, req)
if len(ret) == 0 {
panic("no return value specified for Search")
}
var r0 *v0.SearchIndexResponse
var r0 *search.IndexResult
var r1 error
if returnFunc, ok := ret.Get(0).(func(context.Context, *v0.SearchIndexRequest) (*v0.SearchIndexResponse, error)); ok {
if returnFunc, ok := ret.Get(0).(func(context.Context, *search.IndexQuery) (*search.IndexResult, error)); ok {
return returnFunc(ctx, req)
}
if returnFunc, ok := ret.Get(0).(func(context.Context, *v0.SearchIndexRequest) *v0.SearchIndexResponse); ok {
if returnFunc, ok := ret.Get(0).(func(context.Context, *search.IndexQuery) *search.IndexResult); ok {
r0 = returnFunc(ctx, req)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*v0.SearchIndexResponse)
r0 = ret.Get(0).(*search.IndexResult)
}
}
if returnFunc, ok := ret.Get(1).(func(context.Context, *v0.SearchIndexRequest) error); ok {
if returnFunc, ok := ret.Get(1).(func(context.Context, *search.IndexQuery) error); ok {
r1 = returnFunc(ctx, req)
} else {
r1 = ret.Error(1)
@@ -581,20 +580,20 @@ type Engine_Search_Call struct {
// Search is a helper method to define mock.On call
// - ctx context.Context
// - req *v0.SearchIndexRequest
// - req *search.IndexQuery
func (_e *Engine_Expecter) Search(ctx any, req any) *Engine_Search_Call {
return &Engine_Search_Call{Call: _e.mock.On("Search", ctx, req)}
}
func (_c *Engine_Search_Call) Run(run func(ctx context.Context, req *v0.SearchIndexRequest)) *Engine_Search_Call {
func (_c *Engine_Search_Call) Run(run func(ctx context.Context, req *search.IndexQuery)) *Engine_Search_Call {
_c.Call.Run(func(args mock.Arguments) {
var arg0 context.Context
if args[0] != nil {
arg0 = args[0].(context.Context)
}
var arg1 *v0.SearchIndexRequest
var arg1 *search.IndexQuery
if args[1] != nil {
arg1 = args[1].(*v0.SearchIndexRequest)
arg1 = args[1].(*search.IndexQuery)
}
run(
arg0,
@@ -604,12 +603,12 @@ func (_c *Engine_Search_Call) Run(run func(ctx context.Context, req *v0.SearchIn
return _c
}
func (_c *Engine_Search_Call) Return(searchIndexResponse *v0.SearchIndexResponse, err error) *Engine_Search_Call {
_c.Call.Return(searchIndexResponse, err)
func (_c *Engine_Search_Call) Return(indexResult *search.IndexResult, err error) *Engine_Search_Call {
_c.Call.Return(indexResult, err)
return _c
}
func (_c *Engine_Search_Call) RunAndReturn(run func(ctx context.Context, req *v0.SearchIndexRequest) (*v0.SearchIndexResponse, error)) *Engine_Search_Call {
func (_c *Engine_Search_Call) RunAndReturn(run func(ctx context.Context, req *search.IndexQuery) (*search.IndexResult, error)) *Engine_Search_Call {
_c.Call.Return(run)
return _c
}
+15 -2
View File
@@ -22,7 +22,6 @@ import (
"github.com/opencloud-eu/opencloud/pkg/log"
searchmsg "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0"
searchService "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/pkg/content"
"github.com/opencloud-eu/opencloud/services/search/pkg/mapping"
)
@@ -46,9 +45,23 @@ var ErrOptimizeUnsupported = errors.New("the search engine merges its index on i
// ErrOptimizeInProgress is returned while another optimization of the index runs
var ErrOptimizeInProgress = errors.New("the index is being optimized already")
// IndexQuery is one search against the index.
type IndexQuery struct {
Query string
Ref *searchmsg.Reference
PageSize int32
}
// IndexResult is what the engine answers: the matches of one space and how
// many there are in total.
type IndexResult struct {
Matches []*searchmsg.Match
TotalMatches int32
}
// Engine is the interface to the search engine
type Engine interface {
Search(ctx context.Context, req *searchService.SearchIndexRequest) (*searchService.SearchIndexResponse, error)
Search(ctx context.Context, req *IndexQuery) (*IndexResult, error)
DocCount() (uint64, error)
// Optimize merges the segments of the index, ErrOptimizeUnsupported if the
// engine merges on its own
+4 -4
View File
@@ -287,7 +287,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se
errg, ctx := errgroup.WithContext(ctx)
work := make(chan *provider.StorageSpace, len(spaces))
results := make(chan *searchsvc.SearchIndexResponse, len(spaces))
results := make(chan *IndexResult, len(spaces))
// Distribute work
errg.Go(func() error {
@@ -331,7 +331,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se
close(results)
}()
responses := make([]*searchsvc.SearchIndexResponse, len(spaces))
responses := make([]*IndexResult, len(spaces))
i := 0
for r := range results {
responses[i] = r
@@ -369,7 +369,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se
}, nil
}
func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, space *provider.StorageSpace, mountpointID string) (res *searchsvc.SearchIndexResponse, err error) {
func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest, space *provider.StorageSpace, mountpointID string) (res *IndexResult, err error) {
// the workers run in an errgroup, which does not recover, one failing space must not affect the others
defer func() {
if r := recover(); r != nil {
@@ -473,7 +473,7 @@ func (s *Service) searchIndex(ctx context.Context, req *searchsvc.SearchRequest,
permissions = space.GetRootInfo().GetPermissionSet()
}
searchRequest := &searchsvc.SearchIndexRequest{
searchRequest := &IndexQuery{
Query: ExpandQueryTemplate(s.queryTemplate, req.Query, space),
Ref: &searchmsg.Reference{
ResourceId: searchRootID,
+9 -9
View File
@@ -785,7 +785,7 @@ var _ = Describe("Searchprovider", func() {
Status: status.NewOK(ctx),
StorageSpaces: []*sprovider.StorageSpace{personalSpace},
}, nil)
indexClient.On("Search", mock.Anything, mock.Anything).Return(&searchsvc.SearchIndexResponse{
indexClient.On("Search", mock.Anything, mock.Anything).Return(&search.IndexResult{
TotalMatches: 1,
Matches: []*searchmsg.Match{
{
@@ -829,7 +829,7 @@ var _ = Describe("Searchprovider", func() {
Query: "Size:<10",
})
Expect(err).ToNot(HaveOccurred())
indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool {
indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool {
return req.Query == "Size:<10"
}))
})
@@ -840,7 +840,7 @@ var _ = Describe("Searchprovider", func() {
})
_, err := s.Search(ctx, &searchsvc.SearchRequest{Query: "foo"})
Expect(err).ToNot(HaveOccurred())
indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool {
indexClient.AssertCalled(GinkgoT(), "Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool {
return req.Query == `(foo) AND (NOT path:"./.space" AND NOT id:"storageid$personalspace!personalspace")`
}))
})
@@ -894,7 +894,7 @@ var _ = Describe("Searchprovider", func() {
Status: status.NewOK(ctx),
Path: "/path",
}, nil)
indexClient.On("Search", mock.Anything, mock.Anything).Return(&searchsvc.SearchIndexResponse{
indexClient.On("Search", mock.Anything, mock.Anything).Return(&search.IndexResult{
TotalMatches: 1,
Matches: []*searchmsg.Match{
{
@@ -993,7 +993,7 @@ var _ = Describe("Searchprovider", func() {
Status: status.NewOK(ctx),
StorageSpaces: []*sprovider.StorageSpace{grantSpace, mountpointSpace},
}, nil)
indexClient.On("Search", mock.Anything, mock.Anything).Return(&searchsvc.SearchIndexResponse{
indexClient.On("Search", mock.Anything, mock.Anything).Return(&search.IndexResult{
TotalMatches: 1,
Matches: []*searchmsg.Match{
{
@@ -1040,10 +1040,10 @@ var _ = Describe("Searchprovider", func() {
Status: status.NewOK(ctx),
StorageSpaces: []*sprovider.StorageSpace{personalSpace, grantSpace, mountpointSpace},
}, nil)
indexClient.On("Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool {
indexClient.On("Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool {
return req.Ref.ResourceId.OpaqueId == grantSpace.Root.SpaceId &&
req.Ref.ResourceId.SpaceId == grantSpace.Root.SpaceId
})).Return(&searchsvc.SearchIndexResponse{
})).Return(&search.IndexResult{
TotalMatches: 2,
Matches: []*searchmsg.Match{
{
@@ -1084,10 +1084,10 @@ var _ = Describe("Searchprovider", func() {
},
},
}, nil)
indexClient.On("Search", mock.Anything, mock.MatchedBy(func(req *searchsvc.SearchIndexRequest) bool {
indexClient.On("Search", mock.Anything, mock.MatchedBy(func(req *search.IndexQuery) bool {
return req.Ref.ResourceId.OpaqueId == personalSpace.Root.OpaqueId &&
req.Ref.ResourceId.SpaceId == personalSpace.Root.SpaceId
})).Return(&searchsvc.SearchIndexResponse{
})).Return(&search.IndexResult{
TotalMatches: 1,
Matches: []*searchmsg.Match{
{