filer_search.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package filer
  2. import (
  3. "context"
  4. "github.com/seaweedfs/seaweedfs/weed/util"
  5. "math"
  6. "path/filepath"
  7. "strings"
  8. )
  9. func splitPattern(pattern string) (prefix string, restPattern string) {
  10. position := strings.Index(pattern, "*")
  11. if position >= 0 {
  12. return pattern[:position], pattern[position:]
  13. }
  14. position = strings.Index(pattern, "?")
  15. if position >= 0 {
  16. return pattern[:position], pattern[position:]
  17. }
  18. return "", restPattern
  19. }
  20. // For now, prefix and namePattern are mutually exclusive
  21. func (f *Filer) ListDirectoryEntries(ctx context.Context, p util.FullPath, startFileName string, inclusive bool, limit int64, prefix string, namePattern string, namePatternExclude string) (entries []*Entry, hasMore bool, err error) {
  22. if limit > math.MaxInt32-1 {
  23. limit = math.MaxInt32 - 1
  24. }
  25. _, err = f.StreamListDirectoryEntries(ctx, p, startFileName, inclusive, limit+1, prefix, namePattern, namePatternExclude, func(entry *Entry) bool {
  26. entries = append(entries, entry)
  27. return true
  28. })
  29. hasMore = int64(len(entries)) >= limit+1
  30. if hasMore {
  31. entries = entries[:limit]
  32. }
  33. return entries, hasMore, err
  34. }
  35. // For now, prefix and namePattern are mutually exclusive
  36. func (f *Filer) StreamListDirectoryEntries(ctx context.Context, p util.FullPath, startFileName string, inclusive bool, limit int64, prefix string, namePattern string, namePatternExclude string, eachEntryFunc ListEachEntryFunc) (lastFileName string, err error) {
  37. if strings.HasSuffix(string(p), "/") && len(p) > 1 {
  38. p = p[0 : len(p)-1]
  39. }
  40. prefixInNamePattern, restNamePattern := splitPattern(namePattern)
  41. if prefixInNamePattern != "" {
  42. prefix = prefixInNamePattern
  43. }
  44. var missedCount int64
  45. missedCount, lastFileName, err = f.doListPatternMatchedEntries(ctx, p, startFileName, inclusive, limit, prefix, restNamePattern, namePatternExclude, eachEntryFunc)
  46. for missedCount > 0 && err == nil {
  47. missedCount, lastFileName, err = f.doListPatternMatchedEntries(ctx, p, lastFileName, false, missedCount, prefix, restNamePattern, namePatternExclude, eachEntryFunc)
  48. }
  49. return
  50. }
  51. func (f *Filer) doListPatternMatchedEntries(ctx context.Context, p util.FullPath, startFileName string, inclusive bool, limit int64, prefix, restNamePattern string, namePatternExclude string, eachEntryFunc ListEachEntryFunc) (missedCount int64, lastFileName string, err error) {
  52. if len(restNamePattern) == 0 && len(namePatternExclude) == 0 {
  53. lastFileName, err = f.doListValidEntries(ctx, p, startFileName, inclusive, limit, prefix, eachEntryFunc)
  54. return 0, lastFileName, err
  55. }
  56. lastFileName, err = f.doListValidEntries(ctx, p, startFileName, inclusive, limit, prefix, func(entry *Entry) bool {
  57. nameToTest := entry.Name()
  58. if len(namePatternExclude) > 0 {
  59. if matched, matchErr := filepath.Match(namePatternExclude, nameToTest); matchErr == nil && matched {
  60. missedCount++
  61. return true
  62. }
  63. }
  64. if len(restNamePattern) > 0 {
  65. if matched, matchErr := filepath.Match(restNamePattern, nameToTest[len(prefix):]); matchErr == nil && !matched {
  66. missedCount++
  67. return true
  68. }
  69. }
  70. if !eachEntryFunc(entry) {
  71. return false
  72. }
  73. return true
  74. })
  75. if err != nil {
  76. return
  77. }
  78. return
  79. }
  80. func (f *Filer) doListValidEntries(ctx context.Context, p util.FullPath, startFileName string, inclusive bool, limit int64, prefix string, eachEntryFunc ListEachEntryFunc) (lastFileName string, err error) {
  81. var expiredCount int64
  82. expiredCount, lastFileName, err = f.doListDirectoryEntries(ctx, p, startFileName, inclusive, limit, prefix, eachEntryFunc)
  83. for expiredCount > 0 && err == nil {
  84. expiredCount, lastFileName, err = f.doListDirectoryEntries(ctx, p, lastFileName, false, expiredCount, prefix, eachEntryFunc)
  85. }
  86. return
  87. }