Skip to content

Commit

Permalink
fix logic
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Jul 8, 2022
1 parent 7626649 commit b183d96
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 45 deletions.
36 changes: 19 additions & 17 deletions pkg/storage/utils/indexer/index/autoincrement.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,35 @@ func (idx *Autoincrement) Lookup(v string) ([]string, error) {

// LookupCtx retieves multiple exact values and allows passing in a context
func (idx *Autoincrement) LookupCtx(ctx context.Context, values ...string) ([]string, error) {
var allValues []string
var err error
var allValues map[string]struct{}
if len(values) != 1 {
allValues, err = idx.storage.ReadDir(context.Background(), path.Join("/", idx.indexRootDir))
// prefetch all values with one request
entries, err := idx.storage.ReadDir(context.Background(), path.Join("/", idx.indexRootDir))
if err != nil {
return nil, err
}
} else {
allValues = values
// convert known values to set
for _, e := range entries {
allValues[path.Base(e)] = struct{}{}
}
}

valueSet := make(map[string]struct{}, len(allValues))
for _, v := range allValues {
valueSet[path.Base(v)] = struct{}{}
// convert requestet values to set
valueSet := make(map[string]struct{}, len(values))
for _, v := range values {
valueSet[v] = struct{}{}
}

var matches = []string{}

for _, v := range values {
if _, ok := valueSet[v]; ok {
oldname, err := idx.storage.ResolveSymlink(context.Background(), path.Join("/", idx.indexRootDir, v))
if err != nil {
continue
}
matches = append(matches, oldname)
for v := range valueSet {
if _, ok := allValues[v]; ok || len(allValues) == 0 {
oldname, err := idx.storage.ResolveSymlink(context.Background(), path.Join("/", idx.indexRootDir, v))
if err != nil {
continue
}
matches = append(matches, oldname)
}
}
}

if len(matches) == 0 {
var v string
Expand Down
37 changes: 24 additions & 13 deletions pkg/storage/utils/indexer/index/non_unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,39 @@ func (idx *NonUnique) Lookup(v string) ([]string, error) {

// LookupCtx retieves multiple exact values and allows passing in a context
func (idx *NonUnique) LookupCtx(ctx context.Context, values ...string) ([]string, error) {
allValues, err := idx.storage.ReadDir(context.Background(), path.Join("/", idx.indexRootDir))
// prefetch all values with one request
entries, err := idx.storage.ReadDir(context.Background(), path.Join("/", idx.indexRootDir))
if err != nil {
return nil, err
}
// convert known values to set
allValues := make(map[string]struct{}, len(entries))
for _, e := range entries {
allValues[path.Base(e)] = struct{}{}
}

valueSet := make(map[string]struct{}, len(allValues))
for _, v := range allValues {
if idx.caseInsensitive {
valueSet[strings.ToLower(path.Base(v))] = struct{}{}
} else {
valueSet[path.Base(v)] = struct{}{}
// convert requestet values to set
valueSet := make(map[string]struct{}, len(values))
if idx.caseInsensitive {
for _, v := range values {
valueSet[strings.ToLower(v)] = struct{}{}
}
} else {
for _, v := range values {
valueSet[v] = struct{}{}
}
}

var matches = map[string]struct{}{}
for v := range valueSet {
children, err := idx.storage.ReadDir(context.Background(), path.Join("/", idx.indexRootDir, v))
if err != nil {
continue
}
for _, c := range children {
matches[path.Base(c)] = struct{}{}
if _, ok := allValues[v]; ok {
children, err := idx.storage.ReadDir(context.Background(), path.Join("/", idx.indexRootDir, v))
if err != nil {
continue
}
for _, c := range children {
matches[path.Base(c)] = struct{}{}
}
}
}

Expand Down
37 changes: 22 additions & 15 deletions pkg/storage/utils/indexer/index/unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,40 @@ func (idx *Unique) Lookup(v string) ([]string, error) {

// LookupCtx retieves multiple exact values and allows passing in a context
func (idx *Unique) LookupCtx(ctx context.Context, values ...string) ([]string, error) {
var allValues []string
var err error
var allValues map[string]struct{}
if len(values) != 1 {
allValues, err = idx.storage.ReadDir(context.Background(), path.Join("/", idx.indexRootDir))
// prefetch all values with one request
entries, err := idx.storage.ReadDir(context.Background(), path.Join("/", idx.indexRootDir))
if err != nil {
return nil, err
}
} else {
allValues = values
// convert known values to set
for _, e := range entries {
allValues[path.Base(e)] = struct{}{}
}
}

valueSet := make(map[string]struct{}, len(allValues))
for _, v := range allValues {
if idx.caseInsensitive {
valueSet[strings.ToLower(path.Base(v))] = struct{}{}
} else {
valueSet[path.Base(v)] = struct{}{}
// convert requestet values to set
valueSet := make(map[string]struct{}, len(values))
if idx.caseInsensitive {
for _, v := range values {
valueSet[strings.ToLower(v)] = struct{}{}
}
} else {
for _, v := range values {
valueSet[v] = struct{}{}
}
}

var matches = make([]string, 0)
for v := range valueSet {
oldname, err := idx.storage.ResolveSymlink(context.Background(), path.Join(idx.indexRootDir, v))
if err != nil {
continue
if _, ok := allValues[v]; ok || len(allValues) == 0 {
oldname, err := idx.storage.ResolveSymlink(context.Background(), path.Join(idx.indexRootDir, v))
if err != nil {
continue
}
matches = append(matches, oldname)
}
matches = append(matches, oldname)
}

if len(matches) == 0 {
Expand Down

0 comments on commit b183d96

Please sign in to comment.