Skip to main content
Version: v0.3.0

src/sydra/storage/retention.zig

Purpose

Applies retention by deleting segment files older than a configured TTL.

Public API

pub fn apply(data_dir: std.fs.Dir, manifest: *Manifest, ttl_days: u32) !void

Behavior:

  • If ttl_days == 0, retention is disabled (keeps data forever).
  • Otherwise:
    • Computes ttl_secs = ttl_days * 24 * 3600.
    • Iterates manifest.entries and removes entries where:
      • (now_secs - entry.end_ts) > ttl_secs
    • Deletes expired segment files (deleteFile) best-effort.
    • Replaces the manifest’s in-memory entries list with only the retained entries.
apply() retention loop (from src/sydra/storage/retention.zig)
pub fn apply(data_dir: std.fs.Dir, manifest: *manifest_mod.Manifest, ttl_days: u32) !void {
if (ttl_days == 0) return; // keep forever
const now_secs: i64 = @intCast(std.time.timestamp());
const ttl_secs: i64 = @as(i64, @intCast(ttl_days)) * 24 * 3600;

var keep = std.ArrayListUnmanaged(manifest_mod.Entry){};
defer keep.deinit(manifest.alloc);

for (manifest.entries.items) |e| {
if ((now_secs - e.end_ts) > ttl_secs) {
// delete segment file best-effort
data_dir.deleteFile(e.path) catch {};
continue;
}
try keep.append(manifest.alloc, e);
}

manifest.entries.deinit(manifest.alloc);
manifest.entries = keep;
}

Time unit assumption:

  • now_secs is taken from std.time.timestamp() (seconds).
  • end_ts is compared directly against now_secs, so point timestamps must be in seconds for retention to behave as intended.

Tests

  • test "retention removes expired segments"