(ctx context.Context, n ast.Node, q string, schema []string, np *named.ParamSet)
| 48 | } |
| 49 | |
| 50 | func (c *CachedAnalyzer) analyze(ctx context.Context, n ast.Node, q string, schema []string, np *named.ParamSet) (*analysis.Analysis, bool, error) { |
| 51 | // Only cache queries for managed databases. We can't be certain the |
| 52 | // database is in an unchanged state otherwise |
| 53 | if !c.db.Managed { |
| 54 | return nil, true, nil |
| 55 | } |
| 56 | |
| 57 | dir, err := cache.AnalysisDir() |
| 58 | if err != nil { |
| 59 | return nil, true, err |
| 60 | } |
| 61 | |
| 62 | if c.configBytes == nil { |
| 63 | c.configBytes, err = json.Marshal(c.config) |
| 64 | if err != nil { |
| 65 | return nil, true, err |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Calculate cache key |
| 70 | h := fnv.New64() |
| 71 | h.Write([]byte(info.Version)) |
| 72 | h.Write(c.configBytes) |
| 73 | for _, m := range schema { |
| 74 | h.Write([]byte(m)) |
| 75 | } |
| 76 | h.Write([]byte(q)) |
| 77 | |
| 78 | key := fmt.Sprintf("%x", h.Sum(nil)) |
| 79 | path := filepath.Join(dir, key) |
| 80 | if _, err := os.Stat(path); err == nil { |
| 81 | contents, err := os.ReadFile(path) |
| 82 | if err != nil { |
| 83 | return nil, true, err |
| 84 | } |
| 85 | var a analysis.Analysis |
| 86 | if err := proto.Unmarshal(contents, &a); err != nil { |
| 87 | return nil, true, err |
| 88 | } |
| 89 | return &a, false, nil |
| 90 | } |
| 91 | |
| 92 | result, err := c.a.Analyze(ctx, n, q, schema, np) |
| 93 | |
| 94 | if err == nil { |
| 95 | contents, err := proto.Marshal(result) |
| 96 | if err != nil { |
| 97 | slog.Warn("unable to marshal analysis", "err", err) |
| 98 | return result, false, nil |
| 99 | } |
| 100 | if err := os.WriteFile(path, contents, 0644); err != nil { |
| 101 | slog.Warn("saving analysis to disk failed", "err", err) |
| 102 | return result, false, nil |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return result, false, err |
| 107 | } |
no test coverage detected