-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Lingrino/search
Search functions
- Loading branch information
Showing
21 changed files
with
368 additions
and
18 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
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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,113 @@ | ||
package vaku | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// folderSearchWorkerInput takes input/output channels for input to the job | ||
type folderSearchWorkerInput struct { | ||
inputsC <-chan *PathInput | ||
searchS string | ||
resultsC chan<- *folderSearchWorkerOutput | ||
} | ||
|
||
// folderSearchWorkerOutput is the data returned by a FolderSearch worker | ||
type folderSearchWorkerOutput struct { | ||
path string | ||
match bool | ||
err error | ||
} | ||
|
||
// FolderSearch takes in a PathInput and a search string. It then calls FolderList() on that path | ||
// and concurrently runs PathSearch() on every returned path in the list. It returns a list of paths | ||
// at which the search string was found. Note that running this function against a path that is not | ||
// a folder will produce an error, to search a single path you should use PathSearch() instead. | ||
func (c *Client) FolderSearch(i *PathInput, s string) ([]string, error) { | ||
var err error | ||
var output []string | ||
|
||
// Init the path to get mount info | ||
c.InitPathInput(i) | ||
|
||
// Get all of the paths to search | ||
list, err := c.FolderList(&PathInput{ | ||
Path: i.Path, | ||
TrimPathPrefix: true, | ||
}) | ||
if err != nil { | ||
return output, errors.Wrapf(err, "failed to list folder at %s", i.Path) | ||
} | ||
|
||
// Concurrency channels for workers | ||
inputsC := make(chan *PathInput, len(list)) | ||
resultsC := make(chan *folderSearchWorkerOutput, len(list)) | ||
|
||
// Spawn workers equal to MaxConcurrency | ||
for w := 1; w <= MaxConcurrency; w++ { | ||
go c.folderSearchWorker(&folderSearchWorkerInput{ | ||
inputsC: inputsC, | ||
searchS: s, | ||
resultsC: resultsC, | ||
}) | ||
} | ||
|
||
// Add all paths to search to the path channel | ||
for _, p := range list { | ||
inputsC <- &PathInput{ | ||
Path: c.PathJoin(i.Path, p), | ||
mountPath: i.mountPath, | ||
mountlessPath: i.mountlessPath, | ||
mountVersion: i.mountVersion, | ||
} | ||
} | ||
close(inputsC) | ||
|
||
// Empty the results channel into output | ||
for j := 0; j < len(list); j++ { | ||
o := <-resultsC | ||
if o.err != nil { | ||
err = errors.Wrapf(o.err, "Failed to search path %s", o.path) | ||
} | ||
if o.match { | ||
if i.TrimPathPrefix { | ||
outputPath := c.PathJoin(strings.TrimPrefix(o.path, i.Path)) | ||
output = append(output, outputPath) | ||
} else { | ||
output = append(output, o.path) | ||
} | ||
} | ||
} | ||
|
||
sort.Strings(output) | ||
|
||
return output, err | ||
} | ||
|
||
// folderSearchWorker does the work of searching a single path | ||
func (c *Client) folderSearchWorker(i *folderSearchWorkerInput) { | ||
for { | ||
input, more := <-i.inputsC | ||
if more { | ||
match, err := c.PathSearch(input, i.searchS) | ||
output := &folderSearchWorkerOutput{ | ||
err: err, | ||
path: input.Path, | ||
match: false, | ||
} | ||
if err != nil { | ||
output.err = errors.Wrapf(err, "Failed to search path %s", input.Path) | ||
i.resultsC <- output | ||
continue | ||
} | ||
if match { | ||
output.match = true | ||
} | ||
i.resultsC <- output | ||
} else { | ||
return | ||
} | ||
} | ||
} |
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,109 @@ | ||
package vaku_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type TestFolderSearchData struct { | ||
inputPath *vaku.PathInput | ||
inputSearch string | ||
output []string | ||
outputErr bool | ||
} | ||
|
||
func TestFolderSearch(t *testing.T) { | ||
t.Parallel() | ||
c := clientInitForTests(t) | ||
|
||
tests := map[int]TestFolderSearchData{ | ||
1: { | ||
inputPath: vaku.NewPathInput("secretv1/test"), | ||
inputSearch: "ba", | ||
output: []string{ | ||
"fizz", | ||
"foo", | ||
"value", | ||
}, | ||
outputErr: false, | ||
}, | ||
2: { | ||
inputPath: vaku.NewPathInput("secretv2/test"), | ||
inputSearch: "ba", | ||
output: []string{ | ||
"fizz", | ||
"foo", | ||
"value", | ||
}, | ||
outputErr: false, | ||
}, | ||
3: { | ||
inputPath: vaku.NewPathInput("secretv1/test/inner"), | ||
inputSearch: "rs1mCRB5", | ||
output: []string{ | ||
"again/inner/UCrt6sZT", | ||
}, | ||
outputErr: false, | ||
}, | ||
4: { | ||
inputPath: vaku.NewPathInput("secretv2/test/inner"), | ||
inputSearch: "rs1mCRB5", | ||
output: []string{ | ||
"again/inner/UCrt6sZT", | ||
}, | ||
outputErr: false, | ||
}, | ||
5: { | ||
inputPath: &vaku.PathInput{ | ||
Path: "secretv1/test", | ||
TrimPathPrefix: false, | ||
}, | ||
inputSearch: "VbJ", | ||
output: []string{ | ||
"secretv1/test/HToOeKKD", | ||
}, | ||
outputErr: false, | ||
}, | ||
6: { | ||
inputPath: &vaku.PathInput{ | ||
Path: "secretv2/test", | ||
TrimPathPrefix: false, | ||
}, | ||
inputSearch: "VbJ", | ||
output: []string{ | ||
"secretv2/test/HToOeKKD", | ||
}, | ||
outputErr: false, | ||
}, | ||
7: { | ||
inputPath: vaku.NewPathInput("secretv1/doesnotexist"), | ||
inputSearch: "foo", | ||
output: nil, | ||
outputErr: true, | ||
}, | ||
8: { | ||
inputPath: vaku.NewPathInput("secretv2/doesnotexist"), | ||
inputSearch: "foo", | ||
output: nil, | ||
outputErr: true, | ||
}, | ||
9: { | ||
inputPath: vaku.NewPathInput("secretdoesnotexist/test"), | ||
inputSearch: "foo", | ||
output: nil, | ||
outputErr: true, | ||
}, | ||
} | ||
|
||
for _, d := range tests { | ||
o, e := c.FolderSearch(d.inputPath, d.inputSearch) | ||
assert.Equal(t, d.output, o) | ||
if d.outputErr { | ||
assert.Error(t, e) | ||
} else { | ||
assert.NoError(t, e) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"testing" | ||
|
||
"github.com/Lingrino/vaku/vaku" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
Oops, something went wrong.