-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
162 lines (140 loc) · 3.48 KB
/
cache.go
File metadata and controls
162 lines (140 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"encoding/gob"
"os"
"path/filepath"
"runtime"
"sync"
"sync/atomic"
)
// CachedFile stores parsed entries with mod time for incremental parsing
type CachedFile struct {
ModTime int64
Entries []WordIndentEntry
}
// FileCache stores all cached file data
type FileCache struct {
Version int // cache format version for invalidation
Files map[string]CachedFile
}
const cacheVersion = 1
func loadCache(dir string, strategyName string) *FileCache {
// Cache only works with word-indent strategy (uses WordIndentEntry)
if strategyName != "word-indent" {
return nil
}
cachePath := filepath.Join(dir, ".quickdup", strategyName+"-cache.gob")
file, err := os.Open(cachePath)
if err != nil {
return nil
}
defer file.Close()
var cache FileCache
decoder := gob.NewDecoder(file)
if err := decoder.Decode(&cache); err != nil {
return nil
}
// Check version
if cache.Version != cacheVersion {
return nil
}
return &cache
}
// saveCache saves the file cache to disk
func saveCache(dir string, strategyName string, files []string, fileData map[string][]Entry) {
// Cache only works with word-indent strategy (uses WordIndentEntry)
if strategyName != "word-indent" {
return
}
// Build cache from current file data
cache := FileCache{
Version: cacheVersion,
Files: make(map[string]CachedFile),
}
for _, path := range files {
entries, ok := fileData[path]
if !ok {
continue
}
info, err := os.Stat(path)
if err != nil {
continue
}
// Convert []Entry to []WordIndentEntry for serialization
concrete := make([]WordIndentEntry, len(entries))
for i, e := range entries {
concrete[i] = *e.(*WordIndentEntry)
}
cache.Files[path] = CachedFile{
ModTime: info.ModTime().UnixNano(),
Entries: concrete,
}
}
// Ensure directory exists
cacheDir := filepath.Join(dir, ".quickdup")
os.MkdirAll(cacheDir, 0755)
cachePath := filepath.Join(cacheDir, strategyName+"-cache.gob")
file, err := os.Create(cachePath)
if err != nil {
return // silently fail
}
defer file.Close()
encoder := gob.NewEncoder(file)
encoder.Encode(cache)
}
// parseFilesWithCache parses files using cache when possible
func parseFilesWithCache(files []string, cache *FileCache) (map[string][]Entry, int, int) {
numWorkers := runtime.NumCPU()
results := make(map[string][]Entry)
var mu sync.Mutex
var cacheHits atomic.Int64
var cacheMisses atomic.Int64
// Create work channel
work := make(chan string, len(files))
for _, f := range files {
work <- f
}
close(work)
// Start workers
var wg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for path := range work {
var entries []Entry
var fromCache bool
// Check cache
if cache != nil {
if cached, ok := cache.Files[path]; ok {
info, err := os.Stat(path)
if err == nil && info.ModTime().UnixNano() == cached.ModTime {
// Convert []WordIndentEntry to []Entry
entries = make([]Entry, len(cached.Entries))
for i := range cached.Entries {
entries[i] = &cached.Entries[i]
}
fromCache = true
}
}
}
// Parse if not cached
if !fromCache {
var err error
entries, err = parseFile(path)
if err != nil {
continue // skip files that fail to parse
}
cacheMisses.Add(1)
} else {
cacheHits.Add(1)
}
mu.Lock()
results[path] = entries
mu.Unlock()
}
}()
}
wg.Wait()
return results, int(cacheHits.Load()), int(cacheMisses.Load())
}