Skip to content

Commit 813b719

Browse files
committed
post filter search result
1 parent e81bebb commit 813b719

File tree

6 files changed

+46
-25
lines changed

6 files changed

+46
-25
lines changed

examples/search/search.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func main() {
4848

4949
defer filesystem.Release()
5050

51-
entries, err := filesystem.Search(inputPath)
51+
entries, err := filesystem.SearchUnixWildcard(inputPath)
5252
if err != nil {
5353
logger.Error(err)
5454
panic(err)

fs/fs.go

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

273-
func (fs *FileSystem) Search(pathWildcard string) ([]*Entry, error) {
273+
func (fs *FileSystem) SearchUnixWildcard(pathUnixWildcard string) ([]*Entry, error) {
274274
conn, err := fs.metadataSession.AcquireConnection()
275275
if err != nil {
276276
return nil, err
@@ -279,7 +279,7 @@ func (fs *FileSystem) Search(pathWildcard string) ([]*Entry, error) {
279279

280280
results := []*Entry{}
281281

282-
collEntries, err := irods_fs.SearchCollections(conn, pathWildcard)
282+
collEntries, err := irods_fs.SearchCollectionsUnixWildcard(conn, pathUnixWildcard)
283283
if err != nil {
284284
return nil, err
285285
}
@@ -288,7 +288,7 @@ func (fs *FileSystem) Search(pathWildcard string) ([]*Entry, error) {
288288
results = append(results, fs.getEntryFromCollection(entry))
289289
}
290290

291-
objectEntries, err := irods_fs.SearchDataObjectsMasterReplica(conn, pathWildcard)
291+
objectEntries, err := irods_fs.SearchDataObjectsMasterReplicaUnixWildcard(conn, pathUnixWildcard)
292292
if err != nil {
293293
return nil, err
294294
}
@@ -309,7 +309,7 @@ func (fs *FileSystem) SearchDir(pathWildcard string) ([]*Entry, error) {
309309

310310
results := []*Entry{}
311311

312-
collEntries, err := irods_fs.SearchCollections(conn, pathWildcard)
312+
collEntries, err := irods_fs.SearchCollectionsUnixWildcard(conn, pathWildcard)
313313
if err != nil {
314314
return nil, err
315315
}
@@ -330,7 +330,7 @@ func (fs *FileSystem) SearchFile(pathWildcard string) ([]*Entry, error) {
330330

331331
results := []*Entry{}
332332

333-
objectEntries, err := irods_fs.SearchDataObjectsMasterReplica(conn, pathWildcard)
333+
objectEntries, err := irods_fs.SearchDataObjectsMasterReplicaUnixWildcard(conn, pathWildcard)
334334
if err != nil {
335335
return nil, err
336336
}

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/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964
67
github.com/dlclark/regexp2 v1.11.5
78
github.com/hashicorp/go-rootcerts v1.0.2
89
github.com/kelseyhightower/envconfig v1.4.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
2+
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
13
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
35
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

irods/fs/collection.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/cyverse/go-irodsclient/irods/message"
1111
"github.com/cyverse/go-irodsclient/irods/types"
1212
"github.com/cyverse/go-irodsclient/irods/util"
13+
"github.com/danwakefield/fnmatch"
1314
"golang.org/x/xerrors"
1415
)
1516

@@ -725,8 +726,8 @@ func ListSubCollections(conn *connection.IRODSConnection, path string) ([]*types
725726
return collections, nil
726727
}
727728

728-
// SearchCollections searches collections using wildcard
729-
func SearchCollections(conn *connection.IRODSConnection, pathWildcard string) ([]*types.IRODSCollection, error) {
729+
// SearchCollectionsUnixWildcard searches collections using unix-style wildcard
730+
func SearchCollectionsUnixWildcard(conn *connection.IRODSConnection, pathUnixWildcard string) ([]*types.IRODSCollection, error) {
730731
if conn == nil || !conn.IsConnected() {
731732
return nil, xerrors.Errorf("connection is nil or disconnected")
732733
}
@@ -736,7 +737,7 @@ func SearchCollections(conn *connection.IRODSConnection, pathWildcard string) ([
736737
metrics.IncreaseCounterForList(1)
737738
}
738739

739-
pathWildcard = util.UnixWildcardsToSQLWildcards(pathWildcard)
740+
pathUnixWildcard = util.UnixWildcardsToSQLWildcards(pathUnixWildcard)
740741

741742
// lock the connection
742743
conn.Lock()
@@ -755,7 +756,7 @@ func SearchCollections(conn *connection.IRODSConnection, pathWildcard string) ([
755756
query.AddSelect(common.ICAT_COLUMN_COLL_CREATE_TIME, 1)
756757
query.AddSelect(common.ICAT_COLUMN_COLL_MODIFY_TIME, 1)
757758

758-
query.AddLikeStringCondition(common.ICAT_COLUMN_COLL_NAME, pathWildcard)
759+
query.AddLikeStringCondition(common.ICAT_COLUMN_COLL_NAME, pathUnixWildcard)
759760

760761
queryResult := message.IRODSMessageQueryResponse{}
761762
err := conn.Request(query, &queryResult, nil)
@@ -843,7 +844,13 @@ func SearchCollections(conn *connection.IRODSConnection, pathWildcard string) ([
843844
}
844845
}
845846

846-
collections = append(collections, pagenatedCollections...)
847+
// Filter results by original unix wildcard, since the SQL wildcards
848+
// are less strict (e.g. a unix wildcard range is converted to a generic wildcards in SQL).
849+
for _, pagenatedCollection := range pagenatedCollections {
850+
if fnmatch.Match(pathUnixWildcard, pagenatedCollection.Path, fnmatch.FNM_PATHNAME) {
851+
collections = append(collections, pagenatedCollection)
852+
}
853+
}
847854

848855
continueIndex = queryResult.ContinueIndex
849856
if continueIndex == 0 {

irods/fs/data_object.go

+25-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/cyverse/go-irodsclient/irods/message"
1515
"github.com/cyverse/go-irodsclient/irods/types"
1616
"github.com/cyverse/go-irodsclient/irods/util"
17+
"github.com/danwakefield/fnmatch"
1718
"golang.org/x/xerrors"
1819
)
1920

@@ -1274,8 +1275,8 @@ func ListDataObjectsMasterReplica(conn *connection.IRODSConnection, collection *
12741275
return mergedDataObjects, nil
12751276
}
12761277

1277-
// SearchDataObjects searches data objects in the given collection using wildcard
1278-
func SearchDataObjects(conn *connection.IRODSConnection, pathWildcard string) ([]*types.IRODSDataObject, error) {
1278+
// SearchDataObjectsUnixWildcard searches data objects in the given collection using unix-style wildcard
1279+
func SearchDataObjectsUnixWildcard(conn *connection.IRODSConnection, pathUnixWildcard string) ([]*types.IRODSDataObject, error) {
12791280
if conn == nil || !conn.IsConnected() {
12801281
return nil, xerrors.Errorf("connection is nil or disconnected")
12811282
}
@@ -1285,10 +1286,10 @@ func SearchDataObjects(conn *connection.IRODSConnection, pathWildcard string) ([
12851286
metrics.IncreaseCounterForList(1)
12861287
}
12871288

1288-
pathWildcard = util.UnixWildcardsToSQLWildcards(pathWildcard)
1289+
pathUnixWildcard = util.UnixWildcardsToSQLWildcards(pathUnixWildcard)
12891290
// we don't use util.GetBasename() and util.GetDir() as wildcard may have '\' in the path
1290-
basenameSqlWildcard := path.Base(pathWildcard)
1291-
dirnameSqlWildcard := path.Dir(pathWildcard)
1291+
basenameSqlWildcard := path.Base(pathUnixWildcard)
1292+
dirnameSqlWildcard := path.Dir(pathUnixWildcard)
12921293

12931294
// lock the connection
12941295
conn.Lock()
@@ -1413,7 +1414,6 @@ func SearchDataObjects(conn *connection.IRODSConnection, pathWildcard string) ([
14131414
}
14141415
case int(common.ICAT_COLUMN_COLL_NAME):
14151416
pagenatedDataObjectCollectionNames[row] = value
1416-
14171417
if len(pagenatedDataObjects[row].Name) > 0 {
14181418
pagenatedDataObjects[row].Path = util.MakeIRODSPath(value, pagenatedDataObjects[row].Name)
14191419
}
@@ -1465,7 +1465,13 @@ func SearchDataObjects(conn *connection.IRODSConnection, pathWildcard string) ([
14651465
}
14661466
}
14671467

1468-
dataObjects = append(dataObjects, pagenatedDataObjects...)
1468+
// Filter results by original unix wildcard, since the SQL wildcards
1469+
// are less strict (e.g. a unix wildcard range is converted to a generic wildcards in SQL).
1470+
for _, pagenatedDataObject := range pagenatedDataObjects {
1471+
if fnmatch.Match(pathUnixWildcard, pagenatedDataObject.Path, fnmatch.FNM_PATHNAME) {
1472+
dataObjects = append(dataObjects, pagenatedDataObject)
1473+
}
1474+
}
14691475

14701476
continueIndex = queryResult.ContinueIndex
14711477
if continueIndex == 0 {
@@ -1495,8 +1501,8 @@ func SearchDataObjects(conn *connection.IRODSConnection, pathWildcard string) ([
14951501
return mergedDataObjects, nil
14961502
}
14971503

1498-
// SearchDataObjectsMasterReplica searches data objects in the given collection using wildcard, returns only master replica
1499-
func SearchDataObjectsMasterReplica(conn *connection.IRODSConnection, pathWildcard string) ([]*types.IRODSDataObject, error) {
1504+
// SearchDataObjectsMasterReplicaUnixWildcard searches data objects in the given collection using unix-style wildcard, returns only master replica
1505+
func SearchDataObjectsMasterReplicaUnixWildcard(conn *connection.IRODSConnection, pathUnixWildcard string) ([]*types.IRODSDataObject, error) {
15001506
if conn == nil || !conn.IsConnected() {
15011507
return nil, xerrors.Errorf("connection is nil or disconnected")
15021508
}
@@ -1506,10 +1512,10 @@ func SearchDataObjectsMasterReplica(conn *connection.IRODSConnection, pathWildca
15061512
metrics.IncreaseCounterForList(1)
15071513
}
15081514

1509-
pathWildcard = util.UnixWildcardsToSQLWildcards(pathWildcard)
1515+
pathUnixWildcard = util.UnixWildcardsToSQLWildcards(pathUnixWildcard)
15101516
// we don't use util.GetBasename() and util.GetDir() as wildcard may have '\' in the path
1511-
basenameSqlWildcard := path.Base(pathWildcard)
1512-
dirnameSqlWildcard := path.Dir(pathWildcard)
1517+
basenameSqlWildcard := path.Base(pathUnixWildcard)
1518+
dirnameSqlWildcard := path.Dir(pathUnixWildcard)
15131519

15141520
// lock the connection
15151521
conn.Lock()
@@ -1635,7 +1641,6 @@ func SearchDataObjectsMasterReplica(conn *connection.IRODSConnection, pathWildca
16351641
}
16361642
case int(common.ICAT_COLUMN_COLL_NAME):
16371643
pagenatedDataObjectCollectionNames[row] = value
1638-
16391644
if len(pagenatedDataObjects[row].Name) > 0 {
16401645
pagenatedDataObjects[row].Path = util.MakeIRODSPath(value, pagenatedDataObjects[row].Name)
16411646
}
@@ -1687,7 +1692,13 @@ func SearchDataObjectsMasterReplica(conn *connection.IRODSConnection, pathWildca
16871692
}
16881693
}
16891694

1690-
dataObjects = append(dataObjects, pagenatedDataObjects...)
1695+
// Filter results by original unix wildcard, since the SQL wildcards
1696+
// are less strict (e.g. a unix wildcard range is converted to a generic wildcards in SQL).
1697+
for _, pagenatedDataObject := range pagenatedDataObjects {
1698+
if fnmatch.Match(pathUnixWildcard, pagenatedDataObject.Path, fnmatch.FNM_PATHNAME) {
1699+
dataObjects = append(dataObjects, pagenatedDataObject)
1700+
}
1701+
}
16911702

16921703
continueIndex = queryResult.ContinueIndex
16931704
if continueIndex == 0 {

0 commit comments

Comments
 (0)