From 9de97695445f3c5e9ae42e1a37f0ed6f7af6eb8e Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Tue, 20 Oct 2020 11:14:40 +0200 Subject: [PATCH] Move implementation for case insensitive index handling to indexes --- accounts/pkg/indexer/index/cs3/non_unique.go | 18 ++++++++++++++++ accounts/pkg/indexer/index/cs3/unique.go | 18 ++++++++++++++++ accounts/pkg/indexer/index/disk/non_unique.go | 16 ++++++++++++++ accounts/pkg/indexer/index/disk/unique.go | 17 ++++++++++++++- accounts/pkg/indexer/indexer.go | 21 +------------------ 5 files changed, 69 insertions(+), 21 deletions(-) diff --git a/accounts/pkg/indexer/index/cs3/non_unique.go b/accounts/pkg/indexer/index/cs3/non_unique.go index 7b2253b366..a5ad7d4638 100644 --- a/accounts/pkg/indexer/index/cs3/non_unique.go +++ b/accounts/pkg/indexer/index/cs3/non_unique.go @@ -120,6 +120,9 @@ func (idx *NonUnique) Init() error { // Lookup exact lookup by value. func (idx *NonUnique) Lookup(v string) ([]string, error) { + if idx.caseInsensitive { + v = strings.ToLower(v) + } var matches = make([]string, 0) ctx, err := idx.getAuthenticatedContext(context.Background()) if err != nil { @@ -148,6 +151,9 @@ func (idx *NonUnique) Add(id, v string) (string, error) { if v == "" { return "", nil } + if idx.caseInsensitive { + v = strings.ToLower(v) + } ctx, err := idx.getAuthenticatedContext(context.Background()) if err != nil { return "", err @@ -174,6 +180,9 @@ func (idx *NonUnique) Remove(id string, v string) error { if v == "" { return nil } + if idx.caseInsensitive { + v = strings.ToLower(v) + } ctx, err := idx.getAuthenticatedContext(context.Background()) if err != nil { return err @@ -221,6 +230,11 @@ func (idx *NonUnique) Remove(id string, v string) error { // Update index from to . func (idx *NonUnique) Update(id, oldV, newV string) error { + if idx.caseInsensitive { + oldV = strings.ToLower(oldV) + newV = strings.ToLower(newV) + } + if err := idx.Remove(id, oldV); err != nil { return err } @@ -234,6 +248,10 @@ func (idx *NonUnique) Update(id, oldV, newV string) error { // Search allows for glob search on the index. func (idx *NonUnique) Search(pattern string) ([]string, error) { + if idx.caseInsensitive { + pattern = strings.ToLower(pattern) + } + ctx, err := idx.getAuthenticatedContext(context.Background()) if err != nil { return nil, err diff --git a/accounts/pkg/indexer/index/cs3/unique.go b/accounts/pkg/indexer/index/cs3/unique.go index 5cfa71e149..73d1bbf490 100644 --- a/accounts/pkg/indexer/index/cs3/unique.go +++ b/accounts/pkg/indexer/index/cs3/unique.go @@ -125,6 +125,9 @@ func (idx *Unique) Init() error { // Lookup exact lookup by value. func (idx *Unique) Lookup(v string) ([]string, error) { + if idx.caseInsensitive { + v = strings.ToLower(v) + } searchPath := path.Join(idx.indexRootDir, v) oldname, err := idx.resolveSymlink(searchPath) if err != nil { @@ -143,6 +146,9 @@ func (idx *Unique) Add(id, v string) (string, error) { if v == "" { return "", nil } + if idx.caseInsensitive { + v = strings.ToLower(v) + } newName := path.Join(idx.indexRootDir, v) if err := idx.createSymlink(id, newName); err != nil { if os.IsExist(err) { @@ -160,6 +166,9 @@ func (idx *Unique) Remove(id string, v string) error { if v == "" { return nil } + if idx.caseInsensitive { + v = strings.ToLower(v) + } searchPath := path.Join(idx.indexRootDir, v) _, err := idx.resolveSymlink(searchPath) if err != nil { @@ -198,6 +207,11 @@ func (idx *Unique) Remove(id string, v string) error { // Update index from to . func (idx *Unique) Update(id, oldV, newV string) error { + if idx.caseInsensitive { + oldV = strings.ToLower(oldV) + newV = strings.ToLower(newV) + } + if err := idx.Remove(id, oldV); err != nil { return err } @@ -211,6 +225,10 @@ func (idx *Unique) Update(id, oldV, newV string) error { // Search allows for glob search on the index. func (idx *Unique) Search(pattern string) ([]string, error) { + if idx.caseInsensitive { + pattern = strings.ToLower(pattern) + } + ctx := context.Background() t, err := idx.authenticate(ctx) if err != nil { diff --git a/accounts/pkg/indexer/index/disk/non_unique.go b/accounts/pkg/indexer/index/disk/non_unique.go index 565c63ec47..8f0c7d6948 100644 --- a/accounts/pkg/indexer/index/disk/non_unique.go +++ b/accounts/pkg/indexer/index/disk/non_unique.go @@ -70,6 +70,9 @@ func (idx *NonUnique) Init() error { // Lookup exact lookup by value. func (idx *NonUnique) Lookup(v string) ([]string, error) { + if idx.caseInsensitive { + v = strings.ToLower(v) + } searchPath := path.Join(idx.indexRootDir, v) fi, err := ioutil.ReadDir(searchPath) if os.IsNotExist(err) { @@ -97,6 +100,9 @@ func (idx *NonUnique) Add(id, v string) (string, error) { if v == "" { return "", nil } + if idx.caseInsensitive { + v = strings.ToLower(v) + } oldName := path.Join(idx.filesDir, id) newName := path.Join(idx.indexRootDir, v, id) @@ -118,6 +124,9 @@ func (idx *NonUnique) Remove(id string, v string) error { if v == "" { return nil } + if idx.caseInsensitive { + v = strings.ToLower(v) + } res, err := filepath.Glob(path.Join(idx.indexRootDir, "/*/", id)) if err != nil { return err @@ -147,6 +156,10 @@ func (idx *NonUnique) Remove(id string, v string) error { // Update index from to . func (idx *NonUnique) Update(id, oldV, newV string) (err error) { + if idx.caseInsensitive { + oldV = strings.ToLower(oldV) + newV = strings.ToLower(newV) + } oldDir := path.Join(idx.indexRootDir, oldV) oldPath := path.Join(oldDir, id) newDir := path.Join(idx.indexRootDir, newV) @@ -186,6 +199,9 @@ func (idx *NonUnique) Update(id, oldV, newV string) (err error) { // Search allows for glob search on the index. func (idx *NonUnique) Search(pattern string) ([]string, error) { + if idx.caseInsensitive { + pattern = strings.ToLower(pattern) + } paths, err := filepath.Glob(path.Join(idx.indexRootDir, pattern, "*")) if err != nil { return nil, err diff --git a/accounts/pkg/indexer/index/disk/unique.go b/accounts/pkg/indexer/index/disk/unique.go index ab34dc9586..7b94362236 100644 --- a/accounts/pkg/indexer/index/disk/unique.go +++ b/accounts/pkg/indexer/index/disk/unique.go @@ -83,6 +83,9 @@ func (idx *Unique) Add(id, v string) (string, error) { if v == "" { return "", nil } + if idx.caseInsensitive { + v = strings.ToLower(v) + } oldName := path.Join(idx.filesDir, id) newName := path.Join(idx.indexRootDir, v) err := os.Symlink(oldName, newName) @@ -98,18 +101,23 @@ func (idx *Unique) Remove(id string, v string) (err error) { if v == "" { return nil } + if idx.caseInsensitive { + v = strings.ToLower(v) + } searchPath := path.Join(idx.indexRootDir, v) return os.Remove(searchPath) } // Lookup exact lookup by value. func (idx *Unique) Lookup(v string) (resultPath []string, err error) { + if idx.caseInsensitive { + v = strings.ToLower(v) + } searchPath := path.Join(idx.indexRootDir, v) if err = isValidSymlink(searchPath); err != nil { if os.IsNotExist(err) { err = &idxerrs.NotFoundErr{TypeName: idx.typeName, Key: idx.indexBy, Value: v} } - return } @@ -123,6 +131,10 @@ func (idx *Unique) Lookup(v string) (resultPath []string, err error) { // Update index from to . func (idx *Unique) Update(id, oldV, newV string) (err error) { + if idx.caseInsensitive { + oldV = strings.ToLower(oldV) + newV = strings.ToLower(newV) + } oldPath := path.Join(idx.indexRootDir, oldV) if err = isValidSymlink(oldPath); err != nil { if os.IsNotExist(err) { @@ -146,6 +158,9 @@ func (idx *Unique) Update(id, oldV, newV string) (err error) { // Search allows for glob search on the index. func (idx *Unique) Search(pattern string) ([]string, error) { + if idx.caseInsensitive { + pattern = strings.ToLower(pattern) + } paths, err := filepath.Glob(path.Join(idx.indexRootDir, pattern)) if err != nil { return nil, err diff --git a/accounts/pkg/indexer/indexer.go b/accounts/pkg/indexer/indexer.go index 2803b8a268..9edbc2b0a9 100644 --- a/accounts/pkg/indexer/indexer.go +++ b/accounts/pkg/indexer/indexer.go @@ -4,7 +4,6 @@ package indexer import ( "fmt" "path" - "strings" "github.com/owncloud/ocis/accounts/pkg/config" "github.com/owncloud/ocis/accounts/pkg/indexer/errors" @@ -87,9 +86,6 @@ func (i Indexer) Add(t interface{}) ([]IdxAddResult, error) { for _, idx := range indices { pkVal := valueOf(t, fields.PKFieldName) idxByVal := valueOf(t, idx.IndexBy()) - if idx.CaseInsensitive() { - idxByVal = strings.ToLower(idxByVal) - } value, err := idx.Add(pkVal, idxByVal) if err != nil { return []IdxAddResult{}, err @@ -112,9 +108,6 @@ func (i Indexer) FindBy(t interface{}, field string, val string) ([]string, erro if fields, ok := i.indices[typeName]; ok { for _, idx := range fields.IndicesByField[field] { idxVal := val - if idx.CaseInsensitive() { - idxVal = strings.ToLower(idxVal) - } res, err := idx.Lookup(idxVal) if err != nil { if errors.IsNotFoundErr(err) { @@ -147,9 +140,6 @@ func (i Indexer) Delete(t interface{}) error { for _, idx := range indices { pkVal := valueOf(t, fields.PKFieldName) idxByVal := valueOf(t, idx.IndexBy()) - if idx.CaseInsensitive() { - idxByVal = strings.ToLower(idxByVal) - } if err := idx.Remove(pkVal, idxByVal); err != nil { return err } @@ -166,12 +156,7 @@ func (i Indexer) FindByPartial(t interface{}, field string, pattern string) ([]s resultPaths := make([]string, 0) if fields, ok := i.indices[typeName]; ok { for _, idx := range fields.IndicesByField[field] { - idxPattern := pattern - // TODO: CaseInsensitive is leaking implementation. Should be moved to the implementation instead. - if idx.CaseInsensitive() { - idxPattern = strings.ToLower(idxPattern) - } - res, err := idx.Search(idxPattern) + res, err := idx.Search(pattern) if err != nil { if errors.IsNotFoundErr(err) { continue @@ -213,10 +198,6 @@ func (i Indexer) Update(from, to interface{}) error { if oldV == newV { continue } - if idx.CaseInsensitive() { - oldV = strings.ToLower(oldV) - newV = strings.ToLower(newV) - } if oldV == "" { if _, err := idx.Add(pkVal, newV); err != nil { return err