MCPcopy
hub / github.com/AlistGo/alist / BuildIndex

Function BuildIndex

internal/search/build.go:33–188  ·  view source on GitHub ↗
(ctx context.Context, indexPaths, ignorePaths []string, maxDepth int, count bool)

Source from the content-addressed store, hash-verified

31}
32
33func BuildIndex(ctx context.Context, indexPaths, ignorePaths []string, maxDepth int, count bool) error {
34 var (
35 err error
36 objCount uint64 = 0
37 fi model.Obj
38 )
39 log.Infof("build index for: %+v", indexPaths)
40 log.Infof("ignore paths: %+v", ignorePaths)
41 quit := make(chan struct{}, 1)
42 if !Quit.CompareAndSwap(nil, &quit) {
43 // other goroutine is running
44 return errs.BuildIndexIsRunning
45 }
46 var (
47 indexMQ = mq.NewInMemoryMQ[ObjWithParent]()
48 running = atomic.Bool{} // current goroutine running
49 wg = &sync.WaitGroup{}
50 )
51 running.Store(true)
52 wg.Add(1)
53 go func() {
54 ticker := time.NewTicker(time.Second)
55 defer func() {
56 Quit.Store(nil)
57 wg.Done()
58 // notify walk to exit when StopIndex api called
59 running.Store(false)
60 ticker.Stop()
61 }()
62 tickCount := 0
63 for {
64 select {
65 case <-ticker.C:
66 tickCount += 1
67 if indexMQ.Len() < 1000 && tickCount != 5 {
68 continue
69 } else if tickCount >= 5 {
70 tickCount = 0
71 }
72 log.Infof("index obj count: %d", objCount)
73 indexMQ.ConsumeAll(func(messages []mq.Message[ObjWithParent]) {
74 if len(messages) != 0 {
75 log.Debugf("current index: %s", messages[len(messages)-1].Content.Parent)
76 }
77 if err = BatchIndex(ctx, utils.MustSliceConvert(messages,
78 func(src mq.Message[ObjWithParent]) ObjWithParent {
79 return src.Content
80 })); err != nil {
81 log.Errorf("build index in batch error: %+v", err)
82 } else {
83 objCount = objCount + uint64(len(messages))
84 }
85 if count {
86 WriteProgress(&model.IndexProgress{
87 ObjCount: objCount,
88 IsDone: false,
89 LastDoneTime: nil,
90 })

Callers 3

BuildIndexFunction · 0.92
UpdateIndexFunction · 0.92
UpdateFunction · 0.70

Calls 15

MustSliceConvertFunction · 0.92
GetAdminFunction · 0.92
GetStorageAndActualPathFunction · 0.92
GetFunction · 0.92
WalkFSFunction · 0.92
BatchIndexFunction · 0.85
WriteProgressFunction · 0.85
StoreMethod · 0.80
DoneMethod · 0.80
LoadMethod · 0.80
AddMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected