返回 DeepSeek-Reasonix
indexed_rebuild.go
根目录 / internal / history / indexed_rebuild.go
1 package history
2
3 import (
4 "context"
5 "path/filepath"
6 "strings"
7
8 "reasonix/internal/historycatalog"
9 )
10
11 // RebuildSharedCatalog closes the process-local reader, atomically replaces the
12 // disposable FTS database, and starts a fresh reader over the same roots.
13 func RebuildSharedCatalog(ctx context.Context, roots []historycatalog.Root) error {
14 return processHistoryCatalog.rebuildShared(ctx, roots)
15 }
16
17 func (m *indexedCatalogManager) rebuildShared(ctx context.Context, roots []historycatalog.Root) error {
18 m.lifecycleMu.Lock()
19 defer m.lifecycleMu.Unlock()
20 m.mu.Lock()
21 if m.roots == nil {
22 m.roots = map[string]historycatalog.Root{}
23 }
24 for _, root := range roots {
25 if strings.TrimSpace(root.Path) != "" {
26 root.Path = filepath.Clean(root.Path)
27 m.roots[root.Path] = root
28 }
29 }
30 m.closing = true
31 m.generation++
32 catalog := m.catalog
33 m.catalog = nil
34 done := make([]chan struct{}, 0, len(m.opening))
35 for generation, opening := range m.opening {
36 m.openCancel[generation]()
37 done = append(done, opening)
38 }
39 registered := make([]historycatalog.Root, 0, len(m.roots))
40 for _, root := range m.roots {
41 registered = append(registered, root)
42 }
43 rebuild := m.rebuild
44 if rebuild == nil {
45 rebuild = historycatalog.Rebuild
46 }
47 m.mu.Unlock()
48
49 var lifecycleErr error
50 if catalog != nil {
51 _ = catalog.Flush(ctx)
52 lifecycleErr = catalog.Close(ctx)
53 }
54 for _, opening := range done {
55 select {
56 case <-opening:
57 case <-ctx.Done():
58 if lifecycleErr == nil {
59 lifecycleErr = ctx.Err()
60 }
61 }
62 }
63 if lifecycleErr == nil {
64 _, lifecycleErr = rebuild(ctx, historycatalog.Options{}, registered)
65 }
66 m.mu.Lock()
67 m.closing = false
68 if ctx.Err() == nil {
69 m.startOpenLocked()
70 }
71 m.mu.Unlock()
72 return lifecycleErr
73 }
74
74 lines GO