(ctx context.Context, indexPaths, ignorePaths []string, maxDepth int, count bool)
| 31 | } |
| 32 | |
| 33 | func 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 | }) |
no test coverage detected