|
| 1 | +package noindex |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "path" |
| 6 | + "path/filepath" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/alist-org/alist/v3/internal/conf" |
| 11 | + "github.com/alist-org/alist/v3/internal/fs" |
| 12 | + "github.com/alist-org/alist/v3/internal/model" |
| 13 | + "github.com/alist-org/alist/v3/internal/search/searcher" |
| 14 | + "github.com/alist-org/alist/v3/internal/setting" |
| 15 | + "github.com/pkg/errors" |
| 16 | +) |
| 17 | + |
| 18 | +// maxResults caps how many matches a single live search collects, to bound the |
| 19 | +// cost of walking large storages without an index. |
| 20 | +const maxResults = 1000 |
| 21 | + |
| 22 | +// errStop aborts the walk once enough matches have been collected. |
| 23 | +var errStop = errors.New("enough results collected") |
| 24 | + |
| 25 | +// NoIndex is an index-free searcher: instead of querying a prebuilt index it |
| 26 | +// walks the filesystem under the requested parent on every search and matches |
| 27 | +// object names against the keywords. It trades query speed for not having to |
| 28 | +// build and maintain an index. See issue #9468. |
| 29 | +type NoIndex struct{} |
| 30 | + |
| 31 | +func (NoIndex) Config() searcher.Config { |
| 32 | + return config |
| 33 | +} |
| 34 | + |
| 35 | +func (NoIndex) Search(ctx context.Context, req model.SearchReq) ([]model.SearchNode, int64, error) { |
| 36 | + keywords := strings.Fields(req.Keywords) |
| 37 | + parent := req.Parent |
| 38 | + rootObj, err := fs.Get(ctx, parent, &fs.GetArgs{NoLog: true}) |
| 39 | + if err != nil { |
| 40 | + return nil, 0, errors.WithMessagef(err, "failed get dir [%s]", parent) |
| 41 | + } |
| 42 | + maxDepth := setting.GetInt(conf.MaxIndexDepth, 20) |
| 43 | + ignorePaths := conf.SlicesMap[conf.IgnorePaths] |
| 44 | + |
| 45 | + var nodes []model.SearchNode |
| 46 | + walkFn := func(reqPath string, info model.Obj) error { |
| 47 | + // the parent itself is not a search result |
| 48 | + if reqPath == parent { |
| 49 | + return nil |
| 50 | + } |
| 51 | + for _, ignore := range ignorePaths { |
| 52 | + if strings.HasPrefix(reqPath, ignore) { |
| 53 | + if info.IsDir() { |
| 54 | + return filepath.SkipDir |
| 55 | + } |
| 56 | + return nil |
| 57 | + } |
| 58 | + } |
| 59 | + // scope: 0 for all, 1 for dir, 2 for file |
| 60 | + if (req.Scope == 1 && !info.IsDir()) || (req.Scope == 2 && info.IsDir()) { |
| 61 | + return nil |
| 62 | + } |
| 63 | + if matchKeywords(info.GetName(), keywords) { |
| 64 | + nodes = append(nodes, model.SearchNode{ |
| 65 | + Parent: path.Dir(reqPath), |
| 66 | + Name: info.GetName(), |
| 67 | + IsDir: info.IsDir(), |
| 68 | + Size: info.GetSize(), |
| 69 | + }) |
| 70 | + if len(nodes) >= maxResults { |
| 71 | + return errStop |
| 72 | + } |
| 73 | + } |
| 74 | + return nil |
| 75 | + } |
| 76 | + if err := fs.WalkFS(ctx, maxDepth, parent, rootObj, walkFn); err != nil && !errors.Is(err, errStop) { |
| 77 | + return nil, 0, err |
| 78 | + } |
| 79 | + |
| 80 | + // stable ordering so pagination is consistent across repeated calls |
| 81 | + sort.Slice(nodes, func(i, j int) bool { |
| 82 | + if nodes[i].Name != nodes[j].Name { |
| 83 | + return nodes[i].Name < nodes[j].Name |
| 84 | + } |
| 85 | + return nodes[i].Parent < nodes[j].Parent |
| 86 | + }) |
| 87 | + |
| 88 | + total := int64(len(nodes)) |
| 89 | + start := (req.Page - 1) * req.PerPage |
| 90 | + if start >= len(nodes) { |
| 91 | + return []model.SearchNode{}, total, nil |
| 92 | + } |
| 93 | + end := start + req.PerPage |
| 94 | + if end > len(nodes) { |
| 95 | + end = len(nodes) |
| 96 | + } |
| 97 | + return nodes[start:end], total, nil |
| 98 | +} |
| 99 | + |
| 100 | +// matchKeywords reports whether name contains every keyword (case-insensitive), |
| 101 | +// matching the AND semantics of the indexed searchers. Empty keywords match all. |
| 102 | +func matchKeywords(name string, keywords []string) bool { |
| 103 | + lower := strings.ToLower(name) |
| 104 | + for _, kw := range keywords { |
| 105 | + if !strings.Contains(lower, strings.ToLower(kw)) { |
| 106 | + return false |
| 107 | + } |
| 108 | + } |
| 109 | + return true |
| 110 | +} |
| 111 | + |
| 112 | +func (NoIndex) Index(ctx context.Context, node model.SearchNode) error { |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func (NoIndex) BatchIndex(ctx context.Context, nodes []model.SearchNode) error { |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func (NoIndex) Get(ctx context.Context, parent string) ([]model.SearchNode, error) { |
| 121 | + return []model.SearchNode{}, nil |
| 122 | +} |
| 123 | + |
| 124 | +func (NoIndex) Del(ctx context.Context, prefix string) error { |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func (NoIndex) Release(ctx context.Context) error { |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func (NoIndex) Clear(ctx context.Context) error { |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +var _ searcher.Searcher = (*NoIndex)(nil) |
0 commit comments