Skip to content

Commit e81bebb

Browse files
committed
Add collection and dataobject search APIs
1 parent 560a240 commit e81bebb

File tree

10 files changed

+699
-54
lines changed

10 files changed

+699
-54
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ examples:
1717
CGO_ENABLED=0 GOOS=linux go build -o ./examples/list_dir/list_dir.out ./examples/list_dir/list_dir.go
1818
CGO_ENABLED=0 GOOS=linux go build -o ./examples/list_acls/list_acls.out ./examples/list_acls/list_acls.go
1919
CGO_ENABLED=0 GOOS=linux go build -o ./examples/list_user/list_user.out ./examples/list_user/list_user.go
20+
CGO_ENABLED=0 GOOS=linux go build -o ./examples/search/search.out ./examples/search/search.go
2021
CGO_ENABLED=0 GOOS=linux go build -o ./examples/upload/upload.out ./examples/upload/upload.go
2122
CGO_ENABLED=0 GOOS=linux go build -o ./examples/upload_parallel/upload_parallel.out ./examples/upload_parallel/upload_parallel.go
2223
CGO_ENABLED=0 GOOS=linux go build -o ./examples/download/download.out ./examples/download/download.go

examples/search/account.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
irods_authentication_scheme: "native"
2+
irods_host: "data.cyverse.org"
3+
irods_port: 1247
4+
irods_user_name: ""
5+
irods_user_password: ""
6+
irods_zone_name: "iplant"

examples/search/search.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
8+
"github.com/cyverse/go-irodsclient/config"
9+
"github.com/cyverse/go-irodsclient/fs"
10+
11+
log "github.com/sirupsen/logrus"
12+
)
13+
14+
func main() {
15+
logger := log.WithFields(log.Fields{
16+
"package": "main",
17+
"function": "main",
18+
})
19+
20+
// Parse cli parameters
21+
flag.Parse()
22+
args := flag.Args()
23+
24+
if len(args) != 1 {
25+
fmt.Fprintf(os.Stderr, "Give an iRODS path with wildcard!\n")
26+
os.Exit(1)
27+
}
28+
29+
inputPath := args[0]
30+
31+
// Read account configuration from YAML file
32+
cfg, err := config.NewConfigFromYAMLFile(config.GetDefaultConfig(), "account.yml")
33+
if err != nil {
34+
logger.Error(err)
35+
panic(err)
36+
}
37+
38+
account := cfg.ToIRODSAccount()
39+
logger.Debugf("Account : %v", account.GetRedacted())
40+
41+
// Create a file system
42+
appName := "search"
43+
filesystem, err := fs.NewFileSystemWithDefault(account, appName)
44+
if err != nil {
45+
logger.Error(err)
46+
panic(err)
47+
}
48+
49+
defer filesystem.Release()
50+
51+
entries, err := filesystem.Search(inputPath)
52+
if err != nil {
53+
logger.Error(err)
54+
panic(err)
55+
}
56+
57+
if len(entries) == 0 {
58+
fmt.Printf("Found no entries in the directory %q\n", inputPath)
59+
} else {
60+
fmt.Printf("DIR: %s\n", inputPath)
61+
for _, entry := range entries {
62+
if entry.Type == fs.FileEntry {
63+
fmt.Printf("> FILE:\t%d\t%s\t%d\n", entry.ID, entry.Path, entry.Size)
64+
} else {
65+
// dir
66+
fmt.Printf("> DIRECTORY:\t%d\t%s\n", entry.ID, entry.Path)
67+
}
68+
69+
}
70+
}
71+
}

fs/fs.go

+73
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,78 @@ func (fs *FileSystem) List(path string) ([]*Entry, error) {
270270
return fs.listEntries(collection)
271271
}
272272

273+
func (fs *FileSystem) Search(pathWildcard string) ([]*Entry, error) {
274+
conn, err := fs.metadataSession.AcquireConnection()
275+
if err != nil {
276+
return nil, err
277+
}
278+
defer fs.metadataSession.ReturnConnection(conn) //nolint
279+
280+
results := []*Entry{}
281+
282+
collEntries, err := irods_fs.SearchCollections(conn, pathWildcard)
283+
if err != nil {
284+
return nil, err
285+
}
286+
287+
for _, entry := range collEntries {
288+
results = append(results, fs.getEntryFromCollection(entry))
289+
}
290+
291+
objectEntries, err := irods_fs.SearchDataObjectsMasterReplica(conn, pathWildcard)
292+
if err != nil {
293+
return nil, err
294+
}
295+
296+
for _, entry := range objectEntries {
297+
results = append(results, fs.getEntryFromDataObject(entry))
298+
}
299+
300+
return results, nil
301+
}
302+
303+
func (fs *FileSystem) SearchDir(pathWildcard string) ([]*Entry, error) {
304+
conn, err := fs.metadataSession.AcquireConnection()
305+
if err != nil {
306+
return nil, err
307+
}
308+
defer fs.metadataSession.ReturnConnection(conn) //nolint
309+
310+
results := []*Entry{}
311+
312+
collEntries, err := irods_fs.SearchCollections(conn, pathWildcard)
313+
if err != nil {
314+
return nil, err
315+
}
316+
317+
for _, entry := range collEntries {
318+
results = append(results, fs.getEntryFromCollection(entry))
319+
}
320+
321+
return results, nil
322+
}
323+
324+
func (fs *FileSystem) SearchFile(pathWildcard string) ([]*Entry, error) {
325+
conn, err := fs.metadataSession.AcquireConnection()
326+
if err != nil {
327+
return nil, err
328+
}
329+
defer fs.metadataSession.ReturnConnection(conn) //nolint
330+
331+
results := []*Entry{}
332+
333+
objectEntries, err := irods_fs.SearchDataObjectsMasterReplica(conn, pathWildcard)
334+
if err != nil {
335+
return nil, err
336+
}
337+
338+
for _, entry := range objectEntries {
339+
results = append(results, fs.getEntryFromDataObject(entry))
340+
}
341+
342+
return results, nil
343+
}
344+
273345
// RemoveDir deletes a directory
274346
func (fs *FileSystem) RemoveDir(path string, recurse bool, force bool) error {
275347
irodsPath := util.GetCorrectIRODSPath(path)
@@ -890,6 +962,7 @@ func (fs *FileSystem) listEntries(collection *types.IRODSCollection) ([]*Entry,
890962
cachedEntries = append(cachedEntries, cachedEntry)
891963
} else {
892964
useCached = false
965+
break
893966
}
894967
}
895968
}

fs/fs_cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (fs *FileSystem) invalidateCacheForDirRemove(path string, recurse bool) {
9595
entry = fs.cache.GetEntryCache(path)
9696
}
9797

98-
// we need to expunge all negatie entry caches under irodsDestPath
98+
// we need to expunge all negative entry caches under irodsDestPath
9999
// since all sub-directories/files are also moved
100100
fs.cache.RemoveAllNegativeEntryCacheForPath(path)
101101

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/cyverse/go-irodsclient
33
go 1.18
44

55
require (
6+
github.com/dlclark/regexp2 v1.11.5
67
github.com/hashicorp/go-rootcerts v1.0.2
78
github.com/kelseyhightower/envconfig v1.4.0
89
github.com/patrickmn/go-cache v2.1.0+incompatible

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
5+
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
46
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
57
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
68
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=

0 commit comments

Comments
 (0)