-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.go
More file actions
332 lines (282 loc) · 8.59 KB
/
operations.go
File metadata and controls
332 lines (282 loc) · 8.59 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package granular
import (
"errors"
"fmt"
"io"
"path/filepath"
"github.com/spf13/afero"
)
var (
// ErrCacheMiss is returned when a cache key is not found.
ErrCacheMiss = errors.New("cache miss")
// ErrInvalidKey is returned when a key is invalid.
ErrInvalidKey = errors.New("invalid key")
)
// Result represents the result of a cache operation.
type Result struct {
// Path contains the path to the cached file.
Path string
// Metadata contains additional information about the cached result.
Metadata map[string]string
}
// Get retrieves a cached result for the given key.
// It returns the result, a boolean indicating whether the key was found,
// and an error if one occurred.
func (c *Cache) Get(key Key) (*Result, bool, error) {
c.mu.RLock()
defer c.mu.RUnlock()
// Validate the key
if len(key.Inputs) == 0 {
return nil, false, fmt.Errorf("%w: key has no inputs", ErrInvalidKey)
}
// Compute the key hash
keyHash, err := c.computeKeyHash(key)
if err != nil {
return nil, false, fmt.Errorf("failed to compute key hash: %w", err)
}
// Check if the manifest exists on disk
manifestFile := c.manifestPath(keyHash)
exists, err := afero.Exists(c.fs, manifestFile)
if err != nil {
return nil, false, fmt.Errorf("failed to check manifest existence: %w", err)
}
if !exists {
return nil, false, nil
}
// Load the manifest
manifest, err := c.loadManifest(keyHash)
if err != nil {
return nil, false, fmt.Errorf("failed to load manifest: %w", err)
}
// Create the result
// Use the first output file as the Path
path := ""
if len(manifest.OutputFiles) > 0 {
objectDir := c.objectPath(keyHash)
path = filepath.Join(objectDir, filepath.Base(manifest.OutputFiles[0]))
}
// Create metadata from OutputMeta (preferred) or OutputData (fallback)
metadata := make(map[string]string)
if len(manifest.OutputMeta) > 0 {
// Use OutputMeta if available
for k, v := range manifest.OutputMeta {
metadata[k] = v
}
} else {
// Fallback to OutputData for backward compatibility
for k, v := range manifest.OutputData {
metadata[k] = string(v)
}
}
result := &Result{
Path: path,
Metadata: metadata,
}
// Load any output data from files
if len(manifest.OutputFiles) > 0 {
objectDir := c.objectPath(keyHash)
// Check if all output files exist
for _, file := range manifest.OutputFiles {
outputPath := filepath.Join(objectDir, filepath.Base(file))
exists, err := afero.Exists(c.fs, outputPath)
if err != nil {
return nil, false, fmt.Errorf("failed to check output file existence: %w", err)
}
if !exists {
return nil, false, fmt.Errorf("output file %s not found in cache", file)
}
}
}
return result, true, nil
}
// Store stores a result in the cache for the given key.
func (c *Cache) Store(key Key, result Result) error {
c.mu.Lock()
defer c.mu.Unlock()
// Validate the key
if len(key.Inputs) == 0 {
return fmt.Errorf("%w: key has no inputs", ErrInvalidKey)
}
// Compute the key hash
keyHash, err := c.computeKeyHash(key)
if err != nil {
return fmt.Errorf("failed to compute key hash: %w", err)
}
// Create input descriptions
inputDescs := make([]string, len(key.Inputs))
for i, input := range key.Inputs {
inputDescs[i] = input.String()
}
// Extract file path from result
var files []string
if result.Path != "" {
files = append(files, result.Path)
}
// Convert metadata to output data and output meta
outputData := make(map[string][]byte)
outputMeta := make(map[string]string)
for k, v := range result.Metadata {
outputData[k] = []byte(v)
outputMeta[k] = v
}
// Create the manifest
manifest := &Manifest{
KeyHash: keyHash,
InputDescs: inputDescs,
ExtraData: key.Extra,
OutputFiles: files,
OutputData: outputData,
OutputMeta: outputMeta,
CreatedAt: c.now(),
AccessedAt: c.now(),
Description: "",
}
// Compute the output hash
outputHash, err := c.computeOutputHash(files, outputData, outputMeta)
if err != nil {
return fmt.Errorf("failed to compute output hash: %w", err)
}
manifest.OutputHash = outputHash
// Create the object directory
objectDir := c.objectPath(keyHash)
if err := c.fs.MkdirAll(objectDir, 0o755); err != nil {
return fmt.Errorf("failed to create object directory: %w", err)
}
// Copy output files to the cache
for _, file := range files {
exists, err := afero.Exists(c.fs, file)
if err != nil {
return fmt.Errorf("failed to check output file existence: %w", err)
}
if !exists {
return fmt.Errorf("output file %s does not exist", file)
}
// Copy the file to the cache
destPath := filepath.Join(objectDir, filepath.Base(file))
if err := c.copyFile(file, destPath); err != nil {
return fmt.Errorf("failed to copy output file %s: %w", file, err)
}
}
// Save the manifest
if err := c.saveManifest(manifest); err != nil {
return fmt.Errorf("failed to save manifest: %w", err)
}
return nil
}
// copyFile copies a file from src to dst using the cache's filesystem.
func (c *Cache) copyFile(src, dst string) error {
// Open the source file
srcFile, err := c.fs.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
// Create the destination file
dstFile, err := c.fs.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
// Get a buffer from the pool
bufPtr := bufferPool.Get().(*[]byte)
buffer := *bufPtr
defer bufferPool.Put(bufPtr)
// Copy the file
_, err = io.CopyBuffer(dstFile, srcFile, buffer)
return err
}
// GetFile retrieves a cached file for the given key and filename.
// It returns the path to the file, a boolean indicating whether the key was found,
// and an error if one occurred.
func (c *Cache) GetFile(key Key, filename string) (string, bool, error) {
// Get the cache entry
result, hit, err := c.Get(key)
if err != nil {
return "", false, err
}
if !hit {
return "", false, nil
}
if filepath.Base(result.Path) == filepath.Base(filename) {
return result.Path, true, nil
}
return "", false, fmt.Errorf("file %s not found in cache entry", filename)
}
// manifestPath returns the path to the manifest file for the given key hash.
func (c *Cache) manifestPath(keyHash string) string {
// Use first 2 characters as directory name for better distribution
prefix := keyHash[:2]
return filepath.Join(c.manifestDir(), prefix, keyHash+".json")
}
// objectPath returns the path to the object directory for the given key hash.
func (c *Cache) objectPath(keyHash string) string {
// Use first 2 characters as directory name for better distribution
prefix := keyHash[:2]
return filepath.Join(c.objectsDir(), prefix, keyHash)
}
// GetData retrieves cached data for the given key and data key.
// It returns the data, a boolean indicating whether the key was found,
// and an error if one occurred.
func (c *Cache) GetData(key Key, dataKey string) ([]byte, bool, error) {
// Get the cache entry
result, hit, err := c.Get(key)
if err != nil {
return nil, false, err
}
if !hit {
return nil, false, nil
}
// Check if the data is in the metadata
if value, ok := result.Metadata[dataKey]; ok {
return []byte(value), true, nil
}
return nil, false, fmt.Errorf("data key %s not found in cache entry", dataKey)
}
// Clear removes all entries from the cache.
func (c *Cache) Clear() error {
c.mu.Lock()
defer c.mu.Unlock()
// Remove the cache directories
if err := c.fs.RemoveAll(c.manifestDir()); err != nil {
return fmt.Errorf("failed to remove manifests directory: %w", err)
}
if err := c.fs.RemoveAll(c.objectsDir()); err != nil {
return fmt.Errorf("failed to remove objects directory: %w", err)
}
// Recreate the directories
if err := c.fs.MkdirAll(c.manifestDir(), 0o755); err != nil {
return fmt.Errorf("failed to create manifests directory: %w", err)
}
if err := c.fs.MkdirAll(c.objectsDir(), 0o755); err != nil {
return fmt.Errorf("failed to create objects directory: %w", err)
}
return nil
}
// Remove removes a specific entry from the cache.
func (c *Cache) Remove(keyHash string) error {
c.mu.Lock()
defer c.mu.Unlock()
// Remove the manifest file
manifestFile := c.manifestPath(keyHash)
exists, err := afero.Exists(c.fs, manifestFile)
if err != nil {
return fmt.Errorf("failed to check manifest file existence: %w", err)
}
if exists {
if err := c.fs.Remove(manifestFile); err != nil {
return fmt.Errorf("failed to remove manifest file: %w", err)
}
}
// Remove the object directory
objectDir := c.objectPath(keyHash)
exists, err = afero.Exists(c.fs, objectDir)
if err != nil {
return fmt.Errorf("failed to check object directory existence: %w", err)
}
if exists {
if err := c.fs.RemoveAll(objectDir); err != nil {
return fmt.Errorf("failed to remove object directory: %w", err)
}
}
return nil
}