|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + backend "github.com/Use-Tusk/tusk-drift-schemas/generated/go/backend" |
| 10 | + "google.golang.org/protobuf/proto" |
| 11 | +) |
| 12 | + |
| 13 | +// TraceCache manages local caching of cloud trace tests. |
| 14 | +// Cache files are stored as protobuf-serialized .bin files named by trace test ID. |
| 15 | +type TraceCache struct { |
| 16 | + cacheDir string |
| 17 | +} |
| 18 | + |
| 19 | +// NewTraceCache creates a new TraceCache for the given service ID. |
| 20 | +// The cache directory is: <os.UserCacheDir()>/tusk/<serviceID>/ |
| 21 | +func NewTraceCache(serviceID string) (*TraceCache, error) { |
| 22 | + userCacheDir, err := os.UserCacheDir() |
| 23 | + if err != nil { |
| 24 | + return nil, fmt.Errorf("failed to get user cache directory: %w", err) |
| 25 | + } |
| 26 | + |
| 27 | + cacheDir := filepath.Join(userCacheDir, "tusk", serviceID) |
| 28 | + if err := os.MkdirAll(cacheDir, 0o755); err != nil { |
| 29 | + return nil, fmt.Errorf("failed to create cache directory: %w", err) |
| 30 | + } |
| 31 | + |
| 32 | + return &TraceCache{cacheDir: cacheDir}, nil |
| 33 | +} |
| 34 | + |
| 35 | +// GetCachedIds returns the IDs of all cached trace tests by listing .bin files. |
| 36 | +func (c *TraceCache) GetCachedIds() ([]string, error) { |
| 37 | + entries, err := os.ReadDir(c.cacheDir) |
| 38 | + if err != nil { |
| 39 | + if os.IsNotExist(err) { |
| 40 | + return nil, nil |
| 41 | + } |
| 42 | + return nil, fmt.Errorf("failed to read cache directory: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + var ids []string |
| 46 | + for _, entry := range entries { |
| 47 | + if entry.IsDir() { |
| 48 | + continue |
| 49 | + } |
| 50 | + name := entry.Name() |
| 51 | + if strings.HasSuffix(name, ".bin") { |
| 52 | + ids = append(ids, strings.TrimSuffix(name, ".bin")) |
| 53 | + } |
| 54 | + } |
| 55 | + return ids, nil |
| 56 | +} |
| 57 | + |
| 58 | +// LoadTrace loads a single trace test from cache by ID. |
| 59 | +func (c *TraceCache) LoadTrace(id string) (*backend.TraceTest, error) { |
| 60 | + filePath := filepath.Join(c.cacheDir, id+".bin") |
| 61 | + data, err := os.ReadFile(filePath) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("failed to read cache file %s: %w", filePath, err) |
| 64 | + } |
| 65 | + |
| 66 | + var trace backend.TraceTest |
| 67 | + if err := proto.Unmarshal(data, &trace); err != nil { |
| 68 | + return nil, fmt.Errorf("failed to unmarshal trace %s: %w", id, err) |
| 69 | + } |
| 70 | + return &trace, nil |
| 71 | +} |
| 72 | + |
| 73 | +// LoadAllTraces loads all cached trace tests. |
| 74 | +func (c *TraceCache) LoadAllTraces() ([]*backend.TraceTest, error) { |
| 75 | + ids, err := c.GetCachedIds() |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + traces := make([]*backend.TraceTest, 0, len(ids)) |
| 81 | + for _, id := range ids { |
| 82 | + trace, err := c.LoadTrace(id) |
| 83 | + if err != nil { |
| 84 | + // Skip corrupted cache files |
| 85 | + continue |
| 86 | + } |
| 87 | + traces = append(traces, trace) |
| 88 | + } |
| 89 | + return traces, nil |
| 90 | +} |
| 91 | + |
| 92 | +// SaveTraces saves multiple trace tests to cache. |
| 93 | +func (c *TraceCache) SaveTraces(traces []*backend.TraceTest) error { |
| 94 | + for _, trace := range traces { |
| 95 | + if err := c.saveTrace(trace); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// saveTrace saves a single trace test to cache. |
| 103 | +func (c *TraceCache) saveTrace(trace *backend.TraceTest) error { |
| 104 | + data, err := proto.Marshal(trace) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("failed to marshal trace %s: %w", trace.Id, err) |
| 107 | + } |
| 108 | + |
| 109 | + filePath := filepath.Join(c.cacheDir, trace.Id+".bin") |
| 110 | + if err := os.WriteFile(filePath, data, 0o644); err != nil { |
| 111 | + return fmt.Errorf("failed to write cache file %s: %w", filePath, err) |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +// DeleteTraces removes the specified trace test IDs from cache. |
| 117 | +func (c *TraceCache) DeleteTraces(ids []string) error { |
| 118 | + for _, id := range ids { |
| 119 | + filePath := filepath.Join(c.cacheDir, id+".bin") |
| 120 | + if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) { |
| 121 | + return fmt.Errorf("failed to delete cache file %s: %w", filePath, err) |
| 122 | + } |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +// DiffIds computes which IDs need to be fetched and which need to be deleted. |
| 128 | +// Returns (toFetch, toDelete). |
| 129 | +func DiffIds(remoteIds, cachedIds []string) (toFetch, toDelete []string) { |
| 130 | + remoteSet := make(map[string]struct{}, len(remoteIds)) |
| 131 | + for _, id := range remoteIds { |
| 132 | + remoteSet[id] = struct{}{} |
| 133 | + } |
| 134 | + |
| 135 | + cachedSet := make(map[string]struct{}, len(cachedIds)) |
| 136 | + for _, id := range cachedIds { |
| 137 | + cachedSet[id] = struct{}{} |
| 138 | + } |
| 139 | + |
| 140 | + // toFetch = remoteIds - cachedIds |
| 141 | + for _, id := range remoteIds { |
| 142 | + if _, exists := cachedSet[id]; !exists { |
| 143 | + toFetch = append(toFetch, id) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // toDelete = cachedIds - remoteIds |
| 148 | + for _, id := range cachedIds { |
| 149 | + if _, exists := remoteSet[id]; !exists { |
| 150 | + toDelete = append(toDelete, id) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return toFetch, toDelete |
| 155 | +} |
0 commit comments