(logger *slog.Logger, dir string, meta *BlockMeta)
| 273 | } |
| 274 | |
| 275 | func writeMetaFile(logger *slog.Logger, dir string, meta *BlockMeta) (int64, error) { |
| 276 | meta.Version = metaVersion1 |
| 277 | |
| 278 | // Make any changes to the file appear atomic. |
| 279 | path := filepath.Join(dir, metaFilename) |
| 280 | tmp := path + ".tmp" |
| 281 | defer func() { |
| 282 | if err := os.RemoveAll(tmp); err != nil { |
| 283 | logger.Error("remove tmp file", "err", err.Error()) |
| 284 | } |
| 285 | }() |
| 286 | |
| 287 | f, err := os.Create(tmp) |
| 288 | if err != nil { |
| 289 | return 0, err |
| 290 | } |
| 291 | |
| 292 | jsonMeta, err := json.MarshalIndent(meta, "", "\t") |
| 293 | if err != nil { |
| 294 | return 0, errors.Join(err, f.Close()) |
| 295 | } |
| 296 | |
| 297 | n, err := f.Write(jsonMeta) |
| 298 | if err != nil { |
| 299 | return 0, errors.Join(err, f.Close()) |
| 300 | } |
| 301 | |
| 302 | // Force the kernel to persist the file on disk to avoid data loss if the host crashes. |
| 303 | if err := f.Sync(); err != nil { |
| 304 | return 0, errors.Join(err, f.Close()) |
| 305 | } |
| 306 | if err := f.Close(); err != nil { |
| 307 | return 0, err |
| 308 | } |
| 309 | return int64(n), fileutil.Replace(tmp, path) |
| 310 | } |
| 311 | |
| 312 | // Block represents a directory of time series data covering a continuous time range. |
| 313 | type Block struct { |
searching dependent graphs…