filer_search.go 3.2 KB

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