-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move event methods into dedicated engine struct for better reusability.
implement recursive search item indexing.
- Loading branch information
Showing
4 changed files
with
187 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package search | ||
|
||
import ( | ||
"context" | ||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" | ||
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" | ||
"github.com/cs3org/reva/v2/pkg/storage/utils/walker" | ||
"github.com/cs3org/reva/v2/pkg/storagespace" | ||
"github.com/cs3org/reva/v2/pkg/utils" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/log" | ||
searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0" | ||
"github.com/owncloud/ocis/v2/services/search/pkg/content" | ||
"github.com/owncloud/ocis/v2/services/search/pkg/engine" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
type EngineHandler struct { | ||
logger log.Logger | ||
engine engine.Engine | ||
gateway gateway.GatewayAPIClient | ||
extractor content.Extractor | ||
secret string | ||
} | ||
|
||
// NewProvider creates a new Provider instance. | ||
func NewEngineHandler(gw gateway.GatewayAPIClient, eng engine.Engine, extractor content.Extractor, logger log.Logger, secret string) *EngineHandler { | ||
return &EngineHandler{ | ||
gateway: gw, | ||
engine: eng, | ||
secret: secret, | ||
logger: logger, | ||
extractor: extractor, | ||
} | ||
} | ||
|
||
func (eh *EngineHandler) trashItem(rid *provider.ResourceId) { | ||
err := eh.engine.Delete(storagespace.FormatResourceID(*rid)) | ||
if err != nil { | ||
eh.logger.Error().Err(err).Interface("Id", rid).Msg("failed to remove item from index") | ||
} | ||
} | ||
|
||
func (eh *EngineHandler) upsertItem(ref *provider.Reference, uid *user.UserId) { | ||
ctx, stat, path := eh.resInfo(uid, ref) | ||
if ctx == nil || stat == nil || path == "" { | ||
return | ||
} | ||
|
||
doc, err := eh.extractor.Extract(ctx, stat.Info) | ||
if err != nil { | ||
eh.logger.Error().Err(err).Msg("failed to extract resource content") | ||
return | ||
} | ||
|
||
r := engine.Resource{ | ||
ID: storagespace.FormatResourceID(*stat.Info.Id), | ||
RootID: storagespace.FormatResourceID(provider.ResourceId{ | ||
StorageId: stat.Info.Id.StorageId, | ||
OpaqueId: stat.Info.Id.SpaceId, | ||
SpaceId: stat.Info.Id.SpaceId, | ||
}), | ||
Path: utils.MakeRelativePath(path), | ||
Type: uint64(stat.Info.Type), | ||
Document: doc, | ||
} | ||
|
||
if parentId := stat.GetInfo().GetParentId(); parentId != nil { | ||
r.ParentID = storagespace.FormatResourceID(*parentId) | ||
} | ||
|
||
if err = eh.engine.Upsert(r.ID, r); err != nil { | ||
eh.logger.Error().Err(err).Msg("error adding updating the resource in the index") | ||
} else { | ||
logDocCount(eh.engine, eh.logger) | ||
} | ||
} | ||
|
||
func (eh *EngineHandler) restoreItem(ref *provider.Reference, uid *user.UserId) { | ||
ctx, stat, path := eh.resInfo(uid, ref) | ||
if ctx == nil || stat == nil || path == "" { | ||
return | ||
} | ||
|
||
if err := eh.engine.Restore(storagespace.FormatResourceID(*stat.Info.Id)); err != nil { | ||
eh.logger.Error().Err(err).Msg("failed to restore the changed resource in the index") | ||
} | ||
} | ||
|
||
func (eh *EngineHandler) moveItem(ref *provider.Reference, uid *user.UserId) { | ||
ctx, stat, path := eh.resInfo(uid, ref) | ||
if ctx == nil || stat == nil || path == "" { | ||
return | ||
} | ||
|
||
if err := eh.engine.Move(storagespace.FormatResourceID(*stat.GetInfo().GetId()), storagespace.FormatResourceID(*stat.GetInfo().GetParentId()), path); err != nil { | ||
eh.logger.Error().Err(err).Msg("failed to move the changed resource in the index") | ||
} | ||
} | ||
|
||
func (eh *EngineHandler) resInfo(uid *user.UserId, ref *provider.Reference) (context.Context, *provider.StatResponse, string) { | ||
ownerCtx, err := getAuthContext(&user.User{Id: uid}, eh.gateway, eh.secret, eh.logger) | ||
if err != nil { | ||
return nil, nil, "" | ||
} | ||
|
||
statRes, err := statResource(ownerCtx, ref, eh.gateway, eh.logger) | ||
if err != nil { | ||
return nil, nil, "" | ||
} | ||
|
||
r, err := ResolveReference(ownerCtx, ref, statRes.GetInfo(), eh.gateway) | ||
if err != nil { | ||
return nil, nil, "" | ||
} | ||
|
||
return ownerCtx, statRes, r.GetPath() | ||
} | ||
|
||
func (eh *EngineHandler) indexSpace(entry *provider.Reference, uId *user.UserId) { | ||
ownerCtx, err := getAuthContext(&user.User{Id: uId}, eh.gateway, eh.secret, eh.logger) | ||
if err != nil { | ||
return | ||
} | ||
|
||
rootId := &provider.ResourceId{ | ||
StorageId: entry.GetResourceId().GetStorageId(), | ||
SpaceId: entry.GetResourceId().GetSpaceId(), | ||
} | ||
|
||
w := walker.NewWalker(eh.gateway) | ||
err = w.Walk(ownerCtx, rootId, func(wd string, info *provider.ResourceInfo, err error) error { | ||
if err != nil { | ||
eh.logger.Error().Err(err).Msg("error walking the tree") | ||
return err | ||
} | ||
|
||
if info == nil { | ||
return nil | ||
} | ||
|
||
ref := &provider.Reference{ | ||
Path: utils.MakeRelativePath(filepath.Join(wd, info.Path)), | ||
ResourceId: rootId, | ||
} | ||
eh.logger.Debug().Str("path", ref.Path).Msg("Walking tree") | ||
|
||
searchRes, err := eh.engine.Search(ownerCtx, &searchsvc.SearchIndexRequest{ | ||
Query: "+ID:" + storagespace.FormatResourceID(*info.Id) + ` +Mtime:>="` + utils.TSToTime(info.Mtime).Format(time.RFC3339Nano) + `"`, | ||
}) | ||
|
||
if err == nil && len(searchRes.Matches) >= 1 { | ||
if info.Type == provider.ResourceType_RESOURCE_TYPE_CONTAINER { | ||
eh.logger.Debug().Str("path", ref.Path).Msg("subtree hasn't changed. Skipping.") | ||
return filepath.SkipDir | ||
} | ||
eh.logger.Debug().Str("path", ref.Path).Msg("element hasn't changed. Skipping.") | ||
return nil | ||
} | ||
|
||
eh.upsertItem(ref, info.Owner) | ||
|
||
return nil | ||
}) | ||
|
||
logDocCount(eh.engine, eh.logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters