╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const hash = xit.hash; 5 const mrg = xit.merge; 6 const obj = xit.object; 7 const rf = xit.ref; 8 const pch = @import("patch.zig"); 9 10 pub const User = @import("event/User.zig"); 11 pub const Repo = @import("event/Repo.zig"); 12 pub const Fork = @import("event/Fork.zig"); 13 pub const Issue = @import("event/Issue.zig"); 14 pub const Discussion = @import("event/Discussion.zig"); 15 pub const Comment = @import("event/Comment.zig"); 16 pub const Attachment = @import("event/Attachment.zig"); 17 pub const Patch = @import("event/Patch.zig"); 18 pub const PatchRev = @import("event/PatchRev.zig"); 19 20 // the most bytes an event's serialized form may hold 21 pub const max_event_size: usize = 100 * 1024; 22 23 // the branch haxy events are committed to before being consumed 24 pub const events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/events" }; 25 26 pub const history_key = "haxy/history"; 27 pub const last_object_id_key = "haxy/last-object-id"; 28 29 // options + db type for the admin repo 30 pub const admin_repo_opts: rp.RepoOpts(.xit) = .{}; 31 pub const AdminDB = rp.Repo(.xit, admin_repo_opts).DB; 32 33 // the xitdb type events are stored in. it only depends on the hash kind, so 34 // this matches the db of any xit repo using that hash kind. 35 pub fn EventDB(comptime hash_kind: hash.HashKind) type { 36 return rp.Repo(.xit, .{ .hash = hash_kind }).DB; 37 } 38 39 // the email between a commit author line's angle brackets, or null 40 pub fn authorEmail(author_line: []const u8) ?[]const u8 { 41 const open_bracket = std.mem.indexOfScalar(u8, author_line, '<') orelse return null; 42 const close_bracket = std.mem.indexOfScalarPos(u8, author_line, open_bracket + 1, '>') orelse return null; 43 return author_line[open_bracket + 1 .. close_bracket]; 44 } 45 46 // build a `T` by copying its fields, by name, out of `source` 47 pub fn project(comptime T: type, source: anytype) T { 48 var result: T = undefined; 49 inline for (std.meta.fields(T)) |field| { 50 @field(result, field.name) = @field(source, field.name); 51 } 52 return result; 53 } 54 55 pub const EventKind = enum { 56 user, 57 repo, 58 fork, 59 issue, 60 discuss, 61 comment, 62 attach, 63 patchrev, 64 patch, 65 }; 66 67 pub const RepoRole = enum { 68 admin, 69 repo, 70 fork, 71 72 fn allows(self: RepoRole, kind: EventKind) bool { 73 return switch (self) { 74 .admin => switch (kind) { 75 .user, .repo, .fork => true, 76 else => false, 77 }, 78 .repo => switch (kind) { 79 .issue, .discuss, .comment, .attach, .patchrev, .patch => true, 80 else => false, 81 }, 82 .fork => switch (kind) { 83 .patchrev, .patch => true, 84 else => false, 85 }, 86 }; 87 } 88 }; 89 90 // every logical event's kind and update-ordered active/removed id sets 91 pub const event_index_key = "event-id->kind"; 92 pub const active_event_id_set_key = "active-event-id-set"; 93 pub const removed_event_id_set_key = "removed-event-id-set"; 94 const event_order_key = "event-order"; 95 96 pub fn titleValid(title: []const u8) bool { 97 return std.mem.trim(u8, title, " \t\r\n").len != 0; 98 } 99 100 // a null payload removes the record 101 pub const Event = union(EventKind) { 102 user: ?User, 103 repo: ?Repo, 104 fork: ?Fork, 105 issue: ?Issue, 106 discuss: ?Discussion, 107 comment: ?Comment, 108 attach: ?Attachment, 109 patchrev: ?PatchRev, 110 patch: ?Patch, 111 }; 112 113 // a file committed alongside its event 114 pub const Blob = struct { 115 name: []const u8, 116 size: u64, 117 reader: *std.Io.Reader, 118 }; 119 120 // an entry in the fresh tree carried by one event commit 121 pub const EventTreeEntry = union(enum) { 122 blob: Blob, 123 tree: struct { 124 name: []const u8, 125 oid: []const u8, 126 }, 127 }; 128 129 pub const HostKind = enum { 130 local, 131 server, 132 }; 133 134 // who a commit is attributed to 135 pub const CommitAuthor = struct { 136 name: []const u8, 137 email: []const u8, 138 }; 139 140 pub const event_id_size: usize = 16; 141 142 pub fn parseEventId(id: []const u8) ![event_id_size]u8 { 143 var bytes: [event_id_size]u8 = undefined; 144 _ = try std.fmt.hexToBytes(&bytes, id); 145 if (!std.mem.eql(u8, id, &std.fmt.bytesToHex(bytes, .lower))) return error.InvalidEventId; 146 return bytes; 147 } 148 149 pub const EventWithId = struct { 150 id: [event_id_size * 2]u8, 151 event: Event, 152 timestamp: u64 = 0, // not serialized, because it comes from the commit timestamp 153 author: CommitAuthor, // not serialized, because it goes in the commit author line 154 tree_entries: []const EventTreeEntry = &.{}, // not serialized, because they go in the commit tree 155 156 pub fn jsonStringify(self: EventWithId, jw: anytype) !void { 157 try jw.beginObject(); 158 try jw.objectField("id"); 159 try jw.write(self.id); 160 try jw.objectField("kind"); 161 try jw.write(@tagName(self.event)); 162 try jw.objectField("data"); 163 switch (self.event) { 164 inline else => |event_maybe| try jw.write(event_maybe), 165 } 166 try jw.endObject(); 167 } 168 169 fn fromString(arena: *std.heap.ArenaAllocator, message: []const u8) !EventWithId { 170 const JsonEvent = struct { 171 id: [event_id_size * 2]u8, 172 kind: EventKind, 173 data: ?std.json.Value = null, 174 }; 175 const json_event = try std.json.parseFromSliceLeaky(JsonEvent, arena.allocator(), message, .{ .ignore_unknown_fields = true }); 176 return .{ 177 .id = json_event.id, 178 // unused when reading; the author lives in the commit 179 .author = .{ .name = "", .email = "" }, 180 .event = switch (json_event.kind) { 181 .user => .{ 182 .user = if (json_event.data) |value| 183 try std.json.parseFromValueLeaky(User, arena.allocator(), value, .{ .ignore_unknown_fields = true }) 184 else 185 null, 186 }, 187 .repo => .{ 188 .repo = if (json_event.data) |value| 189 try std.json.parseFromValueLeaky(Repo, arena.allocator(), value, .{ .ignore_unknown_fields = true }) 190 else 191 null, 192 }, 193 .fork => .{ 194 .fork = if (json_event.data) |value| 195 try std.json.parseFromValueLeaky(Fork, arena.allocator(), value, .{ .ignore_unknown_fields = true }) 196 else 197 null, 198 }, 199 .issue => .{ 200 .issue = if (json_event.data) |value| 201 try std.json.parseFromValueLeaky(Issue, arena.allocator(), value, .{ .ignore_unknown_fields = true }) 202 else 203 null, 204 }, 205 .discuss => .{ 206 .discuss = if (json_event.data) |value| 207 try std.json.parseFromValueLeaky(Discussion, arena.allocator(), value, .{ .ignore_unknown_fields = true }) 208 else 209 null, 210 }, 211 .comment => .{ 212 .comment = if (json_event.data) |value| 213 try std.json.parseFromValueLeaky(Comment, arena.allocator(), value, .{ .ignore_unknown_fields = true }) 214 else 215 null, 216 }, 217 .attach => .{ 218 .attach = if (json_event.data) |value| 219 try std.json.parseFromValueLeaky(Attachment, arena.allocator(), value, .{ .ignore_unknown_fields = true }) 220 else 221 null, 222 }, 223 .patch => .{ 224 .patch = if (json_event.data) |value| 225 try std.json.parseFromValueLeaky(Patch, arena.allocator(), value, .{ .ignore_unknown_fields = true }) 226 else 227 null, 228 }, 229 .patchrev => .{ 230 .patchrev = if (json_event.data) |value| 231 try std.json.parseFromValueLeaky(PatchRev, arena.allocator(), value, .{ .ignore_unknown_fields = true }) 232 else 233 null, 234 }, 235 }, 236 }; 237 } 238 239 pub fn randomId(random: std.Random) [event_id_size]u8 { 240 var id_bytes: [event_id_size]u8 = undefined; 241 random.bytes(&id_bytes); 242 return id_bytes; 243 } 244 }; 245 246 // remove an event by emitting a null payload 247 pub fn remove( 248 host_kind: HostKind, 249 comptime role: RepoRole, 250 comptime repo_kind: rp.RepoKind, 251 comptime repo_opts: rp.RepoOpts(repo_kind), 252 io: std.Io, 253 allocator: std.mem.Allocator, 254 repo: *rp.Repo(repo_kind, repo_opts), 255 id: *const [event_id_size]u8, 256 kind: EventKind, 257 author: CommitAuthor, 258 ) !void { 259 const DB = EventDB(repo_opts.hash); 260 var event_db_maybe: ?LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 261 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 262 const moment = (if (event_db_maybe) |*event_db| 263 currentMomentFromDb(repo_opts.hash, event_db.db) 264 else if (repo_kind == .git) 265 return error.EventNotFound 266 else 267 currentMoment(repo_opts, repo)) catch return error.EventNotFound; 268 const kind_map_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, event_index_key)) orelse return error.EventNotFound; 269 const kind_map = try DB.HashMap(.read_only).init(kind_map_cursor); 270 const stored_kind = (try readEventKind(repo_opts.hash, kind_map, id)) orelse return error.EventNotFound; 271 if (stored_kind != kind) return error.EventNotFound; 272 if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 273 event_db_maybe = null; 274 275 const event: Event = switch (kind) { 276 .user => .{ .user = null }, 277 .repo => .{ .repo = null }, 278 .fork => .{ .fork = null }, 279 .issue => .{ .issue = null }, 280 .discuss => .{ .discuss = null }, 281 .comment => .{ .comment = null }, 282 .attach => .{ .attach = null }, 283 .patchrev => .{ .patchrev = null }, 284 .patch => .{ .patch = null }, 285 }; 286 try consume(host_kind, role, repo_kind, repo_opts, io, allocator, repo, events_ref, &.{.{ 287 .id = std.fmt.bytesToHex(id.*, .lower), 288 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 289 .author = author, 290 .event = event, 291 }}); 292 } 293 294 pub const undo_action = "haxy/events"; 295 pub const merge_undo_action = "haxy/merge-events"; 296 297 // index that tracks the events consumed in a given haxy moment 298 pub const moment_index_to_event_id_set_key = "moment-index->event-id-set"; 299 300 // a haxy moment's own position in the haxy history 301 pub const moment_index_key = "moment-index"; 302 303 // a moment's key in the set map 304 fn momentIndexKey(comptime hash_kind: hash.HashKind, moment_index: u64) hash.HashInt(hash_kind) { 305 var buffer: [@sizeOf(u64)]u8 = undefined; 306 std.mem.writeInt(u64, &buffer, moment_index, .big); 307 return hash.hashInt(hash_kind, &buffer); 308 } 309 310 // the ids of the events a moment wrote, newest first, or null when it wrote none 311 pub fn momentEventIds( 312 comptime DB: type, 313 comptime hash_kind: hash.HashKind, 314 haxy_moment: DB.HashMap(.read_only), 315 moment_index: u64, 316 ) !?DB.SortedSet(.read_only) { 317 const sets_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, moment_index_to_event_id_set_key)) orelse return null; 318 const sets = try DB.HashMap(.read_only).init(sets_cursor); 319 const ids_cursor = try sets.getCursor(momentIndexKey(hash_kind, moment_index)) orelse return null; 320 return try DB.SortedSet(.read_only).init(ids_cursor); 321 } 322 323 // commit `events` (if any) as JSON commit messages on `ref`, then consume the 324 // events on `ref` into the db the repo's views read: the repo's own db for a 325 // xit repo, or the standalone event db next to a git repo. for a xit repo the 326 // commits and the consume run in one transaction, which also saves the 327 // mergeability of the patches the events touched. 328 pub fn consume( 329 host_kind: HostKind, 330 comptime role: RepoRole, 331 comptime repo_kind: rp.RepoKind, 332 comptime repo_opts: rp.RepoOpts(repo_kind), 333 io: std.Io, 334 allocator: std.mem.Allocator, 335 repo: *rp.Repo(repo_kind, repo_opts), 336 ref: rf.Ref, 337 events: []const EventWithId, 338 ) !void { 339 for (events) |event| { 340 if (!role.allows(std.meta.activeTag(event.event))) return error.EventKindNotAllowed; 341 } 342 343 var resolved_remote_name: ?[]u8 = null; 344 defer if (resolved_remote_name) |name| allocator.free(name); 345 346 // a branch that only exists on a remote is consumed from its remote-tracking 347 // ref, while commits go on the local branch, continuing from the remote tip 348 // so a cloned repo's local branch stays connected to the server history 349 const resolved_ref: rf.Ref, const first_parent_oids: ?[1][hash.hexLen(repo_opts.hash)]u8 = blk: { 350 if (null != try repo.readRef(io, ref)) break :blk .{ ref, null }; 351 var remotes = try repo.listRemotes(io, allocator); 352 defer remotes.deinit(); 353 for (remotes.sections.keys()) |remote_name| { 354 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = ref.name }; 355 if (try repo.readRef(io, remote_ref)) |oid| { 356 if (events.len > 0) break :blk .{ ref, .{oid} }; 357 const name = try allocator.dupe(u8, remote_name); 358 resolved_remote_name = name; 359 break :blk .{ rf.Ref{ .kind = .{ .remote = name }, .name = ref.name }, null }; 360 } 361 } 362 if (events.len > 0) break :blk .{ ref, null }; 363 // the branch is gone (or never existed): drop any db from a previous 364 // sync rather than serving its issues indefinitely 365 if (repo_kind == .git) try LocalEventDB(repo_opts.hash).delete(io, repo.core.repo_dir); 366 return; 367 }; 368 369 switch (repo_kind) { 370 .git => { 371 try commitEvents(.git, repo_opts, .{ .core = &repo.core, .extra = .{} }, io, allocator, ref, events, first_parent_oids); 372 373 var event_db = try LocalEventDB(repo_opts.hash).open(io, allocator, repo.core.repo_dir); 374 defer event_db.deinit(io, allocator); 375 376 try event_db.consume(repo_kind, repo_opts, io, allocator, repo, resolved_ref); 377 }, 378 .xit => { 379 const DB = rp.Repo(.xit, repo_opts).DB; 380 const State = rp.Repo(.xit, repo_opts).State; 381 var checks: pch.MergeCheckInputs(repo_opts) = .{}; 382 defer checks.deinit(io, allocator); 383 if (role == .repo and host_kind == .server) { 384 checks.prepare(io, allocator, repo, events) catch |err| { 385 std.log.warn("failed to prepare mergeability: {s}", .{@errorName(err)}); 386 }; 387 } 388 389 const Ctx = struct { 390 core: *rp.Repo(.xit, repo_opts).Core, 391 io: std.Io, 392 allocator: std.mem.Allocator, 393 ref: rf.Ref, 394 events: []const EventWithId, 395 checks: *pch.MergeCheckInputs(repo_opts), 396 first_parent_oids: ?[1][hash.hexLen(repo_opts.hash)]u8, 397 398 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 399 var moment = try DB.HashMap(.read_write).init(cursor.*); 400 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 401 402 try commitEvents(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, ctx.first_parent_oids); 403 if (!try consumeInTransaction(role, .xit, repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, ctx.ref)) return error.CancelTransaction; 404 _ = try ctx.checks.update(state, ctx.io, ctx.allocator); 405 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action_kind = undo_action } }); 406 } 407 }; 408 409 try repo.core.db_file.lock(io, .exclusive); 410 defer repo.core.db_file.unlock(io); 411 412 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 413 history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 414 .core = &repo.core, 415 .io = io, 416 .allocator = allocator, 417 .ref = resolved_ref, 418 .events = events, 419 .checks = &checks, 420 .first_parent_oids = first_parent_oids, 421 }) catch |err| switch (err) { 422 error.CancelTransaction => return, 423 else => |e| return e, 424 }; 425 }, 426 } 427 428 // consuming an existing ref does not provide the set of changed patches 429 // explicit patch writes already saved their checks in the event transaction 430 if (role == .repo and repo_kind == .xit and host_kind == .server and events.len == 0) { 431 pch.refreshOpenMergeability(repo_opts, io, allocator, repo, null, null); 432 } 433 } 434 435 // merge the remote events ref into the local events ref without touching head, 436 // the index, or the worktree 437 pub fn mergeEvents( 438 comptime repo_kind: rp.RepoKind, 439 comptime repo_opts: rp.RepoOpts(repo_kind), 440 io: std.Io, 441 allocator: std.mem.Allocator, 442 repo: *rp.Repo(repo_kind, repo_opts), 443 remote_ref: rf.Ref, 444 ) !void { 445 switch (repo_kind) { 446 .git => _ = try mergeEventsInTransaction(.git, repo_opts, .{ .core = &repo.core, .extra = .{} }, io, allocator, remote_ref), 447 .xit => { 448 const DB = rp.Repo(.xit, repo_opts).DB; 449 const State = rp.Repo(.xit, repo_opts).State; 450 451 const Ctx = struct { 452 core: *rp.Repo(.xit, repo_opts).Core, 453 io: std.Io, 454 allocator: std.mem.Allocator, 455 remote_ref: rf.Ref, 456 457 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 458 var moment = try DB.HashMap(.read_write).init(cursor.*); 459 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 460 if (!try mergeEventsInTransaction(.xit, repo_opts, state, ctx.io, ctx.allocator, ctx.remote_ref)) return error.CancelTransaction; 461 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action_kind = merge_undo_action } }); 462 } 463 }; 464 465 try repo.core.db_file.lock(io, .exclusive); 466 defer repo.core.db_file.unlock(io); 467 468 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 469 history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 470 .core = &repo.core, 471 .io = io, 472 .allocator = allocator, 473 .remote_ref = remote_ref, 474 }) catch |err| switch (err) { 475 error.CancelTransaction => {}, 476 else => |e| return e, 477 }; 478 }, 479 } 480 } 481 482 fn mergeEventsInTransaction( 483 comptime repo_kind: rp.RepoKind, 484 comptime repo_opts: rp.RepoOpts(repo_kind), 485 state: rp.Repo(repo_kind, repo_opts).State(.read_write), 486 io: std.Io, 487 allocator: std.mem.Allocator, 488 remote_ref: rf.Ref, 489 ) !bool { 490 const remote_oid = (try rf.readRecur(repo_kind, repo_opts, state.readOnly(), io, .{ .ref = remote_ref })) orelse return false; 491 const local_oid_maybe = try rf.readRecur(repo_kind, repo_opts, state.readOnly(), io, .{ .ref = events_ref }); 492 493 var ref_path_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 494 const ref_path = try events_ref.toPath(&ref_path_buffer); 495 const local_oid = local_oid_maybe orelse { 496 try rf.write(repo_kind, repo_opts, state, io, ref_path, .{ .oid = &remote_oid }); 497 return true; 498 }; 499 if (std.mem.eql(u8, &local_oid, &remote_oid)) return false; 500 501 const ancestor_maybe: ?[hash.hexLen(repo_opts.hash)]u8 = mrg.commonAncestor(repo_kind, repo_opts, state.readOnly(), io, allocator, &local_oid, &remote_oid) catch |err| switch (err) { 502 error.NoCommonAncestor => null, 503 else => return err, 504 }; 505 if (ancestor_maybe) |ancestor| { 506 if (std.mem.eql(u8, &ancestor, &remote_oid)) return false; 507 if (std.mem.eql(u8, &ancestor, &local_oid)) { 508 try rf.write(repo_kind, repo_opts, state, io, ref_path, .{ .oid = &remote_oid }); 509 return true; 510 } 511 } 512 513 const parent_oids = [_][hash.hexLen(repo_opts.hash)]u8{ local_oid, remote_oid }; 514 var tree = try obj.Tree.init(allocator); 515 defer tree.deinit(); 516 _ = try obj.writeCommit(repo_kind, repo_opts, state, io, allocator, .{ 517 .message = "merge events", 518 .parent_oids = &parent_oids, 519 }, &tree, events_ref); 520 return true; 521 } 522 523 // the file an attach event carries: the one entry in its commit's tree. the 524 // name and oid are allocated in `arena`. 525 fn attachedFile( 526 comptime repo_kind: rp.RepoKind, 527 comptime repo_opts: rp.RepoOpts(repo_kind), 528 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 529 io: std.Io, 530 allocator: std.mem.Allocator, 531 arena: *std.heap.ArenaAllocator, 532 tree_oid: *const [hash.hexLen(repo_opts.hash)]u8, 533 ) !struct { name: []const u8, oid: []const u8 } { 534 var tree_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, tree_oid); 535 defer tree_object.deinit(); 536 537 const entries = switch (tree_object.content) { 538 .tree => |tree| tree.entries, 539 else => return error.InvalidAttachment, 540 }; 541 if (entries.count() != 1) return error.InvalidAttachment; 542 const entry = entries.values()[0]; 543 if (entry.isTree()) return error.InvalidAttachment; 544 const name = entries.keys()[0]; 545 if (!Attachment.nameValid(name)) return error.InvalidAttachment; 546 547 const oid = std.fmt.bytesToHex(entry.oid, .lower); 548 var object_reader = try obj.ObjectReader(repo_kind, repo_opts).init(state, io, allocator, &oid); 549 defer object_reader.deinit(); 550 if (object_reader.header().kind != .blob) return error.InvalidAttachment; 551 552 return .{ 553 .name = try arena.allocator().dupe(u8, name), 554 .oid = try arena.allocator().dupe(u8, &oid), 555 }; 556 } 557 558 fn commitIdentity(line: []const u8) ![]const u8 { 559 const close = std.mem.indexOfScalar(u8, line, '>') orelse return error.AuthorNotFound; 560 return line[0 .. close + 1]; 561 } 562 563 // commit each event as a JSON commit message on `ref` through `state`, so a 564 // xit repo can write them inside an already-open transaction 565 pub fn commitEvents( 566 comptime repo_kind: rp.RepoKind, 567 comptime repo_opts: rp.RepoOpts(repo_kind), 568 state: rp.Repo(repo_kind, repo_opts).State(.read_write), 569 io: std.Io, 570 allocator: std.mem.Allocator, 571 ref: rf.Ref, 572 events: []const EventWithId, 573 first_parent_oids: ?[1][hash.hexLen(repo_opts.hash)]u8, 574 ) !void { 575 var parent_oids: ?[]const [hash.hexLen(repo_opts.hash)]u8 = if (first_parent_oids) |*oids| oids else null; 576 577 var json: std.Io.Writer.Allocating = .init(allocator); 578 defer json.deinit(); 579 580 for (events) |event| { 581 json.clearRetainingCapacity(); 582 try std.json.Stringify.value(event, .{}, &json.writer); 583 if (json.written().len > max_event_size) return error.EventTooLarge; 584 const author = try std.fmt.allocPrint(allocator, "{s} <{s}>", .{ event.author.name, event.author.email }); 585 defer allocator.free(author); 586 587 // every event gets a fresh tree 588 var tree = try obj.Tree.init(allocator); 589 defer tree.deinit(); 590 for (event.tree_entries) |entry| { 591 switch (entry) { 592 .blob => |blob| { 593 var oid_bytes = [_]u8{0} ** hash.byteLen(repo_opts.hash); 594 try obj.writeObject(repo_kind, repo_opts, state, io, allocator, blob.reader, .{ .kind = .blob, .size = blob.size }, &oid_bytes); 595 try tree.addBlobEntry(.{ .content = .{ .unix_permission = 0o644, .object_type = .regular_file } }, blob.name, &oid_bytes); 596 }, 597 .tree => |nested| { 598 var oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 599 _ = try std.fmt.hexToBytes(&oid_bytes, nested.oid); 600 if (!std.mem.eql(u8, nested.oid, &std.fmt.bytesToHex(oid_bytes, .lower))) return error.InvalidTreeObject; 601 try tree.addTreeEntry(nested.name, &oid_bytes); 602 }, 603 } 604 } 605 606 _ = try obj.writeCommit(repo_kind, repo_opts, state, io, allocator, .{ 607 .author = author, 608 .message = json.written(), 609 .timestamp = event.timestamp, 610 .parent_oids = parent_oids, 611 .allow_empty = true, 612 }, &tree, ref); 613 // later events parent on the ref's new tip 614 parent_oids = null; 615 } 616 } 617 618 // a standalone event db holding events consumed from a local repo's events 619 // branch. it lives inside the repo dir but is not part of the repo's own data, 620 // so viewing a repo never mutates it. 621 pub fn LocalEventDB(comptime hash_kind: hash.HashKind) type { 622 return struct { 623 file: std.Io.File, 624 buffer: *std.Io.Writer.Allocating, 625 db: *EventDB(hash_kind), 626 627 const db_name = "haxy"; 628 const Self = @This(); 629 630 // open the db in `repo_dir` for writing, creating it if it doesn't exist 631 pub fn open(io: std.Io, allocator: std.mem.Allocator, repo_dir: std.Io.Dir) !Self { 632 const file = try repo_dir.createFile(io, db_name, .{ .truncate = false, .read = true, .lock = .exclusive }); 633 return fromFile(io, allocator, file); 634 } 635 636 // open the db in `repo_dir` for reading, or null if it doesn't exist 637 pub fn openReadOnly(io: std.Io, allocator: std.mem.Allocator, repo_dir: std.Io.Dir) !?Self { 638 const file = repo_dir.openFile(io, db_name, .{ .mode = .read_write, .lock = .shared }) catch |err| switch (err) { 639 error.FileNotFound => return null, 640 else => |e| return e, 641 }; 642 return try fromFile(io, allocator, file); 643 } 644 645 // remove the db from `repo_dir`, so a repo whose events branch 646 // disappeared doesn't keep showing stale issues 647 pub fn delete(io: std.Io, repo_dir: std.Io.Dir) !void { 648 repo_dir.deleteFile(io, db_name) catch |err| switch (err) { 649 error.FileNotFound => {}, 650 else => |e| return e, 651 }; 652 } 653 654 fn fromFile(io: std.Io, allocator: std.mem.Allocator, file: std.Io.File) !Self { 655 errdefer file.close(io); 656 657 const buffer = try allocator.create(std.Io.Writer.Allocating); 658 errdefer allocator.destroy(buffer); 659 buffer.* = std.Io.Writer.Allocating.init(allocator); 660 errdefer buffer.deinit(); 661 662 const db = try allocator.create(EventDB(hash_kind)); 663 errdefer allocator.destroy(db); 664 db.* = try EventDB(hash_kind).init(.{ .io = io, .file = file, .buffer = buffer }); 665 666 return .{ .file = file, .buffer = buffer, .db = db }; 667 } 668 669 pub fn deinit(self: *Self, io: std.Io, allocator: std.mem.Allocator) void { 670 self.file.close(io); 671 self.buffer.deinit(); 672 allocator.destroy(self.buffer); 673 allocator.destroy(self.db); 674 } 675 676 // consume the events on `repo`'s `ref` into this db. the db must come 677 // from `open`, whose handle holds the exclusive lock guarding the 678 // transaction. 679 pub fn consume( 680 self: *Self, 681 comptime repo_kind: rp.RepoKind, 682 comptime repo_opts: rp.RepoOpts(repo_kind), 683 io: std.Io, 684 allocator: std.mem.Allocator, 685 repo: *rp.Repo(repo_kind, repo_opts), 686 ref: rf.Ref, 687 ) !void { 688 comptime if (repo_opts.hash != hash_kind) @compileError("the repo must use the db's hash kind"); 689 const DB = EventDB(hash_kind); 690 691 const Ctx = struct { 692 repo: *rp.Repo(repo_kind, repo_opts), 693 db: *DB, 694 io: std.Io, 695 allocator: std.mem.Allocator, 696 ref: rf.Ref, 697 698 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 699 var moment = try DB.HashMap(.read_write).init(cursor.*); 700 const state = rp.Repo(repo_kind, repo_opts).State(.read_write){ .core = &ctx.repo.core, .extra = .{} }; 701 if (!try consumeInTransaction(.repo, repo_kind, repo_opts, state, ctx.db, &moment, ctx.io, ctx.allocator, ctx.ref)) return error.CancelTransaction; 702 } 703 }; 704 705 const history = try DB.ArrayList(.read_write).init(self.db.rootCursor()); 706 history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 707 .repo = repo, 708 .db = self.db, 709 .io = io, 710 .allocator = allocator, 711 .ref = ref, 712 }) catch |err| switch (err) { 713 error.CancelTransaction => {}, 714 else => |e| return e, 715 }; 716 } 717 }; 718 } 719 720 // consume the events on `ref` into the db `moment` writes to, returning false 721 // if there was nothing to do. events are read from `state`'s repo, which 722 // needn't be the repo backing the db — that's how a local (possibly 723 // git-backed) repo's events land in a standalone db. 724 pub fn consumeInTransaction( 725 comptime role: RepoRole, 726 comptime repo_kind: rp.RepoKind, 727 comptime repo_opts: rp.RepoOpts(repo_kind), 728 state: rp.Repo(repo_kind, repo_opts).State(.read_write), 729 db: *EventDB(repo_opts.hash), 730 moment: *EventDB(repo_opts.hash).HashMap(.read_write), 731 io: std.Io, 732 allocator: std.mem.Allocator, 733 ref: rf.Ref, 734 ) !bool { 735 const DB = EventDB(repo_opts.hash); 736 const read_state = state.readOnly(); 737 738 // the last_object_id represents the object id that was last consumed 739 var last_object_id_maybe: ?[hash.byteLen(repo_opts.hash)]u8 = null; 740 if (try moment.getCursor(hash.hashInt(repo_opts.hash, last_object_id_key))) |last_object_id_cursor| { 741 var last_object_id_buffer: [hash.byteLen(repo_opts.hash)]u8 = undefined; 742 _ = try last_object_id_cursor.readBytes(&last_object_id_buffer); 743 last_object_id_maybe = last_object_id_buffer; 744 } 745 746 // the tip was already consumed, so there is nothing to do 747 if (last_object_id_maybe) |*last_object_id| { 748 const tip_hex = (try rf.readRecur(repo_kind, repo_opts, read_state, io, .{ .ref = ref })) orelse return error.OidNotFound; 749 var tip: [hash.byteLen(repo_opts.hash)]u8 = undefined; 750 _ = try std.fmt.hexToBytes(&tip, &tip_hex); 751 if (std.mem.eql(u8, last_object_id, &tip)) return false; 752 } 753 754 // the list with all of haxy's state, including materialized views. 755 // the reason it is a list is so we can keep every previous haxy 756 // state, making it easy to revert to an older state if the user 757 // rebases some of the past commits. 758 const haxy_history_cursor = try moment.putCursor(hash.hashInt(repo_opts.hash, history_key)); 759 const haxy_history = try DB.ArrayList(.read_write).init(haxy_history_cursor); 760 761 // add a new item to the haxy history list created above. 762 // we call the item haxy_moments. it is a map of object id to haxy moment. 763 // in other words, it maps each object id to a hash map containing the 764 // state that the database was in when that object id was consumed. 765 var haxy_moments_cursor = try haxy_history.appendCursor(); 766 // use the previous haxy_moments as the basis for this one if it exists 767 if (try haxy_history.getCursor(-2)) |last_haxy_moments_cursor| { 768 try haxy_moments_cursor.write(.{ .slot = last_haxy_moments_cursor.slot() }); 769 } 770 var haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 771 772 var commit_iter = try CommitIterator(repo_kind, repo_opts).init(read_state, io, allocator, haxy_moments.readOnly(), ref); 773 defer commit_iter.deinit(io, allocator); 774 775 // if there are no events to process, look at the oid at HEAD and update 776 // the last_object_id to point to it. this is important in situations 777 // where we do a merge and then force-push to remove the merge. see the 778 // merge test for an example. 779 if (commit_iter.newest_object_id == null) { 780 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, read_state, io, .{ .ref = ref })) orelse return error.OidNotFound; 781 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 782 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 783 784 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &head_oid)) orelse return error.CursorNotFound; 785 const haxy_moment = try DB.HashMap(.read_only).init(haxy_moment_cursor); 786 787 const moment_index_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, moment_index_key)) orelse return error.CursorNotFound; 788 const moment_index = try moment_index_cursor.readUint(); 789 790 try haxy_history.slice(moment_index + 1); 791 try moment.put(hash.hashInt(repo_opts.hash, last_object_id_key), .{ .bytes = &head_oid }); 792 return true; 793 } 794 795 // if this branch was rebased and force pushed, we need to detect that and 796 // properly revert the haxy state to the last valid state. we detect this 797 // by simply asking if the last event is a descendent of the last event 798 // we consumed. if it isn't, then we know the haxy state needs to be reverted. 799 if (last_object_id_maybe) |*last_object_id| { 800 var is_rebased = false; 801 const newest_object_id = commit_iter.newest_object_id orelse return error.OidNotFound; 802 803 _ = mrg.getDescendent( 804 repo_kind, 805 repo_opts, 806 read_state, 807 io, 808 allocator, 809 &std.fmt.bytesToHex(last_object_id, .lower), 810 &std.fmt.bytesToHex(&newest_object_id, .lower), 811 ) catch |err| switch (err) { 812 error.DescendentNotFound => is_rebased = true, 813 else => |e| return e, 814 }; 815 816 if (is_rebased) { 817 const oldest_object_id = commit_iter.oldest_object_id orelse return error.OidNotFound; 818 var oldest_object = try obj.Object(repo_kind, repo_opts).init(read_state, io, allocator, &std.fmt.bytesToHex(oldest_object_id, .lower)); 819 defer oldest_object.deinit(); 820 821 const parent_oids = oldest_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 822 switch (parent_oids.len) { 823 0 => { 824 // the branch was rebased all the way to the very beginning. 825 // we have a repo event with no parent, which means it is now 826 // the very first event. all we need to do is set the haxy 827 // history list to be empty and make a new haxy_moments map 828 // to work with. 829 830 try haxy_history.slice(0); 831 haxy_moments_cursor = try haxy_history.appendCursor(); 832 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 833 834 last_object_id_maybe = null; 835 }, 836 1 => { 837 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 838 _ = try std.fmt.hexToBytes(&oid, &parent_oids[0]); 839 840 const old_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 841 const old_moment = try DB.HashMap(.read_only).init(old_moment_cursor); 842 843 const old_moment_index_cursor = try old_moment.getCursor(hash.hashInt(repo_opts.hash, moment_index_key)) orelse return error.CursorNotFound; 844 const old_moment_index = try old_moment_index_cursor.readUint(); 845 846 // resize the haxy history list so we truncate all the 847 // moments that were created after the parent_oid was consumed 848 try haxy_history.slice(old_moment_index + 1); 849 850 // make a new haxy moment and set its initial value to the last haxy moment 851 haxy_moments_cursor = try haxy_history.appendCursor(); 852 const old_haxy_moments_cursor = try haxy_history.getCursor(old_moment_index) orelse return error.CursorNotFound; 853 try haxy_moments_cursor.write(.{ .slot = old_haxy_moments_cursor.slot() }); 854 haxy_moments = try DB.HashMap(.read_write).init(haxy_moments_cursor); 855 856 last_object_id_maybe = oid; 857 }, 858 else => return error.UnexpectedParentCount, 859 } 860 } 861 } 862 863 while (try commit_iter.next()) |repo_event_oid| { 864 var commit_object = try obj.Object(repo_kind, repo_opts).init(read_state, io, allocator, &std.fmt.bytesToHex(repo_event_oid, .lower)); 865 defer commit_object.deinit(); 866 867 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 868 869 // create a moment for this object id 870 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event_oid)); 871 872 // if there was a previous object id, set this haxy moment's initial value to it. 873 // this efficiently "clones" the map so we make further modifications based on it. 874 if (parent_oids.len > 0) { 875 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 876 _ = try std.fmt.hexToBytes(&first_parent_oid, &parent_oids[0]); 877 878 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 879 try haxy_moment_cursor.write(.{ .slot = first_parent_haxy_moment_cursor.slot() }); 880 } 881 882 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 883 884 // graph generation gives every event a causal order independent of 885 // commit timestamps. concurrent events tie-break by id in their index. 886 var event_order: u64 = 1; 887 for (parent_oids) |*parent_oid| { 888 var parent_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 889 _ = try std.fmt.hexToBytes(&parent_id, parent_oid); 890 const parent_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &parent_id)) orelse return error.CursorNotFound; 891 const parent = try DB.HashMap(.read_only).init(parent_cursor); 892 const parent_order_cursor = try parent.getCursor(hash.hashInt(repo_opts.hash, event_order_key)) orelse return error.CursorNotFound; 893 const parent_order = try parent_order_cursor.readUint(); 894 event_order = @max(event_order, parent_order +| 1); 895 } 896 try haxy_moment.put(hash.hashInt(repo_opts.hash, event_order_key), .{ .uint = event_order }); 897 898 // merge changes from every parent after the first parent. the common 899 // ancestor is the baseline, so a later parent only contributes records 900 // it changed relative to the merge base. only the record maps are 901 // merged; every other view is derived from them, and each kind's 902 // `consume` re-derives its own as the winning records are written. 903 if (parent_oids.len > 1) { 904 for (parent_oids[1..]) |*parent_oid| { 905 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 906 _ = try std.fmt.hexToBytes(&oid, parent_oid); 907 908 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 909 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 910 911 const baseline_oid_hex = try mrg.commonAncestor(repo_kind, repo_opts, read_state, io, allocator, &parent_oids[0], parent_oid); 912 var baseline_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 913 _ = try std.fmt.hexToBytes(&baseline_oid, &baseline_oid_hex); 914 const baseline_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &baseline_oid)) orelse return error.CursorNotFound; 915 const baseline_haxy_moment = try DB.HashMap(.read_only).init(baseline_haxy_moment_cursor); 916 917 inline for (std.meta.fields(Event)) |field| { 918 const T = @typeInfo(field.type).optional.child; 919 try merge(T, DB, repo_opts.hash, allocator, haxy_moment, parent_haxy_moment, baseline_haxy_moment); 920 } 921 } 922 } 923 924 // associate this moment with the index it will first appear at in the 925 // haxy history list. this will be important later so we can truncate 926 // that list if the user ever rebases starting at this object id. 927 const moment_index = try haxy_history.count() - 1; 928 try haxy_moment.put(hash.hashInt(repo_opts.hash, moment_index_key), .{ .uint = moment_index }); 929 930 // consume the event unless it's a merge commit 931 if (parent_oids.len <= 1) { 932 var arena = std.heap.ArenaAllocator.init(allocator); 933 defer arena.deinit(); 934 935 // read the message from the commit 936 var message: std.ArrayList(u8) = .empty; 937 try commit_object.readMessage(arena.allocator(), &message, .limited(max_event_size)); 938 939 const event_with_id = try EventWithId.fromString(&arena, message.items); 940 if (!role.allows(std.meta.activeTag(event_with_id.event))) return error.EventKindNotAllowed; 941 942 const current_event_id = try parseEventId(&event_with_id.id); 943 944 // associate the transaction with the events it writes. this is 945 // used by the undo tab to link the events that were affected 946 // by a given transaction. 947 { 948 const sets = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, moment_index_to_event_id_set_key))); 949 const ids = try DB.SortedSet(.read_write).init(try sets.putCursor(momentIndexKey(repo_opts.hash, moment_index))); 950 try ids.put(&orderKeyDesc(event_order, &current_event_id)); 951 } 952 953 // wrap the payload into the record `consume` stores, with its 954 // commit-derived fields; on update, `consume` preserves the 955 // existing record's 956 switch (event_with_id.event) { 957 .user => |event_maybe| { 958 const record_maybe: ?User.Record = if (event_maybe) |event| .{ .event = event, .created_order = event_order, .updated_order = event_order } else null; 959 try User.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, record_maybe, &arena, &repo_event_oid); 960 }, 961 .repo => |event_maybe| { 962 const record_maybe: ?Repo.Record = if (event_maybe) |event| .{ .event = event, .created_order = event_order, .updated_order = event_order } else null; 963 try Repo.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, record_maybe, &arena, &repo_event_oid); 964 }, 965 .fork => |event_maybe| { 966 const record_maybe: ?Fork.Record = if (event_maybe) |event| .{ .event = event, .created_order = event_order, .updated_order = event_order } else null; 967 try Fork.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, record_maybe, &arena, &repo_event_oid); 968 }, 969 .issue => |event_maybe| { 970 const record_maybe: ?Issue.Record = if (event_maybe) |event| .{ 971 .event = event, 972 .author_email = authorEmail(commit_object.content.commit.metadata.author orelse ""), 973 .created_order = event_order, 974 .updated_order = event_order, 975 } else null; 976 try Issue.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, record_maybe, &arena, &repo_event_oid); 977 }, 978 .discuss => |event_maybe| { 979 const record_maybe: ?Discussion.Record = if (event_maybe) |event| .{ 980 .event = event, 981 .author_email = authorEmail(commit_object.content.commit.metadata.author orelse ""), 982 .created_order = event_order, 983 .updated_order = event_order, 984 } else null; 985 try Discussion.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, record_maybe, &arena, &repo_event_oid); 986 }, 987 .comment => |event_maybe| { 988 const record_maybe: ?Comment.Record = if (event_maybe) |event| .{ 989 .event = event, 990 .author_email = authorEmail(commit_object.content.commit.metadata.author orelse ""), 991 .created_order = event_order, 992 .updated_order = event_order, 993 } else null; 994 try Comment.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, record_maybe, &arena, &repo_event_oid); 995 }, 996 .attach => |event_maybe| { 997 const record_maybe: ?Attachment.Record = if (event_maybe) |event| blk: { 998 const entry = try attachedFile(repo_kind, repo_opts, read_state, io, allocator, &arena, &commit_object.content.commit.tree); 999 break :blk .{ 1000 .event = event, 1001 .author_email = authorEmail(commit_object.content.commit.metadata.author orelse ""), 1002 .created_order = event_order, 1003 .updated_order = event_order, 1004 .name = entry.name, 1005 .blob_oid = entry.oid, 1006 }; 1007 } else null; 1008 try Attachment.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, record_maybe, &arena, &repo_event_oid); 1009 }, 1010 .patchrev => |event_maybe| { 1011 const record_maybe: ?PatchRev.Record = if (event_maybe) |event| blk: { 1012 const trees = try PatchRev.readTrees(repo_kind, repo_opts, read_state, io, allocator, &commit_object.content.commit.tree); 1013 const existing_maybe = try PatchRev.readById(DB, repo_opts.hash, haxy_moment.readOnly(), &arena, &current_event_id); 1014 const event_oid = std.fmt.bytesToHex(repo_event_oid, .lower); 1015 1016 var patch_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1017 if (existing_maybe) |existing| { 1018 if (existing.patch_oid.len != patch_oid.len) return error.InvalidPatch; 1019 @memcpy(&patch_oid, existing.patch_oid); 1020 } else { 1021 const author = try commitIdentity(commit_object.content.commit.metadata.author orelse return error.AuthorNotFound); 1022 const committer = try commitIdentity(commit_object.content.commit.metadata.committer orelse author); 1023 patch_oid = try PatchRev.writeSquashCommit( 1024 repo_kind, 1025 repo_opts, 1026 state, 1027 io, 1028 allocator, 1029 event, 1030 &trees.head, 1031 author, 1032 committer, 1033 commit_object.content.commit.metadata.timestamp, 1034 ); 1035 } 1036 1037 break :blk .{ 1038 .event = event, 1039 .author_email = authorEmail(commit_object.content.commit.metadata.author orelse ""), 1040 .created_order = event_order, 1041 .updated_order = event_order, 1042 .event_oid = &event_oid, 1043 .base_tree_oid = &trees.base, 1044 .head_tree_oid = &trees.head, 1045 .patch_oid = &patch_oid, 1046 .commit_count = PatchRev.commitCount(repo_kind, repo_opts, read_state, io, allocator, event.base_oid, event.source_oid) catch null, 1047 }; 1048 } else null; 1049 try PatchRev.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, record_maybe, &arena, &repo_event_oid); 1050 }, 1051 .patch => |event_maybe| { 1052 const record_maybe: ?Patch.Record = if (event_maybe) |event| .{ 1053 .event = event, 1054 .author_email = authorEmail(commit_object.content.commit.metadata.author orelse ""), 1055 .created_order = event_order, 1056 .updated_order = event_order, 1057 } else null; 1058 try Patch.consume(DB, repo_opts.hash, haxy_moment, &current_event_id, record_maybe, &arena, &repo_event_oid); 1059 }, 1060 } 1061 } 1062 1063 // the current object id is now the last one 1064 last_object_id_maybe = repo_event_oid; 1065 1066 // prevent any of the data created above from being mutated by future iterations of this loop 1067 try db.freeze(); 1068 } 1069 1070 if (last_object_id_maybe) |*last_object_id| { 1071 try moment.put(hash.hashInt(repo_opts.hash, last_object_id_key), .{ .bytes = last_object_id }); 1072 } 1073 1074 return true; 1075 } 1076 1077 // the conflicted records of one kind, keyed by the same order key its id set 1078 // uses, so this doubles as the index the conflicts view lists and counts. every 1079 // value under an entry is a slot reference to a structure that already exists: 1080 // 1081 // <order key> -> conflict entry 1082 // conflicted-fields the conflicting field names, space separated 1083 // base-record the merge base's whole record 1084 // their-record the merged-in side's whole record 1085 // their-field->oid that side's field name to commit map 1086 // 1087 // our own value is the live record and our commit is in the kind's oid map, so 1088 // neither is repeated here. 1089 pub const conflicted_fields_key = "conflicted-fields"; 1090 pub const base_record_key = "base-record"; 1091 pub const their_record_key = "their-record"; 1092 pub const their_field_to_oid_key = "their-field->oid"; 1093 1094 // record the commit against every field this event changes. a merge passes no 1095 // oid, since each field's winner may come from a different side. 1096 pub fn writeOid( 1097 comptime T: type, 1098 comptime DB: type, 1099 comptime hash_kind: hash.HashKind, 1100 id_to_field_to_oid: DB.HashMap(.read_write), 1101 record_key: hash.HashInt(hash_kind), 1102 existing_maybe: ?T, 1103 event: T, 1104 event_oid: ?[]const u8, 1105 ) !void { 1106 const oid = event_oid orelse return; 1107 1108 // created only once a field actually changes 1109 var field_oids: ?DB.SortedMap(.read_write) = null; 1110 1111 inline for (std.meta.fields(T)) |field| { 1112 const changed = if (existing_maybe) |existing| 1113 !fieldEqual(field.type, @field(existing, field.name), @field(event, field.name)) 1114 else 1115 true; 1116 if (changed) { 1117 if (field_oids == null) { 1118 field_oids = try DB.SortedMap(.read_write).init(try id_to_field_to_oid.putCursor(record_key)); 1119 } 1120 if (field_oids) |map| try map.put(field.name, .{ .bytes = oid }); 1121 } 1122 } 1123 } 1124 1125 // the whole field list, space separated, is the longest it can get 1126 fn conflictedFieldsMaxLen(comptime T: type) usize { 1127 var len: usize = 0; 1128 for (std.meta.fields(T)) |field| len += field.name.len + 1; 1129 return len; 1130 } 1131 1132 pub const MergePolicy = enum { 1133 field_conflicts, 1134 target_wins, 1135 }; 1136 1137 // what a three-way merge did with a field 1138 pub const FieldMerge = enum { 1139 // the target's value stands, because neither side changed it, both made the 1140 // same change, or only the target changed it 1141 kept, 1142 parent, 1143 conflicted, 1144 }; 1145 1146 // three-way merge each field. one only the parent changed takes the parent's 1147 // value; one both sides changed differently keeps the target's. a missing 1148 // baseline makes every differing field a conflict. 1149 fn mergeFields( 1150 comptime T: type, 1151 baseline_maybe: ?T, 1152 target: T, 1153 parent: T, 1154 outcome: *[std.meta.fields(T).len]FieldMerge, 1155 ) T { 1156 var merged = target; 1157 1158 inline for (std.meta.fields(T), 0..) |field, i| { 1159 const target_value = @field(target, field.name); 1160 const parent_value = @field(parent, field.name); 1161 1162 if (!fieldEqual(field.type, target_value, parent_value)) { 1163 outcome[i] = if (baseline_maybe) |baseline| blk: { 1164 const baseline_value = @field(baseline, field.name); 1165 if (fieldEqual(field.type, target_value, baseline_value)) { 1166 @field(merged, field.name) = parent_value; 1167 break :blk .parent; 1168 } 1169 // only the target changed it 1170 if (fieldEqual(field.type, parent_value, baseline_value)) break :blk .kept; 1171 break :blk .conflicted; 1172 } else .conflicted; 1173 } 1174 } 1175 1176 return merged; 1177 } 1178 1179 // three-way merge of one kind's records from a merge parent. kinds that expose 1180 // conflicts merge field by field; the others take one whole record. the result 1181 // goes through the kind's `consume`, which re-derives the indexes from it. a 1182 // removal carries over unless the other side changed the record, so a merge 1183 // never fails. 1184 pub fn merge( 1185 comptime T: type, 1186 comptime DB: type, 1187 comptime hash_kind: hash.HashKind, 1188 allocator: std.mem.Allocator, 1189 haxy_moment: DB.HashMap(.read_write), 1190 parent_moment: DB.HashMap(.read_only), 1191 baseline_moment: DB.HashMap(.read_only), 1192 ) !void { 1193 const parent_set_cursor = try parent_moment.getCursor(hash.hashInt(hash_kind, T.all_id_set_key)) orelse return; 1194 const parent_set = try DB.SortedSet(.read_only).init(parent_set_cursor); 1195 1196 const parent_records_cursor = try parent_moment.getCursor(hash.hashInt(hash_kind, T.record_map_key)) orelse return; 1197 const parent_records = try DB.HashMap(.read_only).init(parent_records_cursor); 1198 1199 var baseline_records: ?DB.HashMap(.read_only) = null; 1200 if (try baseline_moment.getCursor(hash.hashInt(hash_kind, T.record_map_key))) |cursor| { 1201 baseline_records = try DB.HashMap(.read_only).init(cursor); 1202 } 1203 1204 var parent_conflicts: ?DB.SortedMap(.read_only) = null; 1205 var baseline_conflicts: ?DB.SortedMap(.read_only) = null; 1206 var parent_id_to_field_to_oid: ?DB.HashMap(.read_only) = null; 1207 switch (comptime T.merge_policy) { 1208 .field_conflicts => { 1209 if (try parent_moment.getCursor(hash.hashInt(hash_kind, T.conflicts_key))) |cursor| { 1210 parent_conflicts = try DB.SortedMap(.read_only).init(cursor); 1211 } 1212 if (try baseline_moment.getCursor(hash.hashInt(hash_kind, T.conflicts_key))) |cursor| { 1213 baseline_conflicts = try DB.SortedMap(.read_only).init(cursor); 1214 } 1215 if (try parent_moment.getCursor(hash.hashInt(hash_kind, T.id_to_field_to_oid_key))) |cursor| { 1216 parent_id_to_field_to_oid = try DB.HashMap(.read_only).init(cursor); 1217 } 1218 }, 1219 .target_wins => {}, 1220 } 1221 1222 var arena = std.heap.ArenaAllocator.init(allocator); 1223 defer arena.deinit(); 1224 1225 var iter = try parent_set.iteratorFromIndex(0); 1226 while (try iter.next()) |kv_pair_cursor| { 1227 const event_id = try readOrderKeyId(DB, kv_pair_cursor); 1228 const record_key = hash.hashInt(hash_kind, &event_id); 1229 1230 _ = arena.reset(.retain_capacity); 1231 1232 const parent_record_cursor = try parent_records.getCursor(record_key) orelse continue; 1233 const parent_slot = parent_record_cursor.slot(); 1234 const baseline_slot = if (baseline_records) |map| try map.getSlot(record_key) else null; 1235 1236 // the parent left this record alone 1237 if (baseline_slot) |slot| { 1238 if (slot.eql(parent_slot)) continue; 1239 } 1240 1241 const parent_record = try read(T.Record, DB, hash_kind, &arena, try DB.HashMap(.read_only).init(parent_record_cursor)); 1242 1243 // re-derived every iteration, since `consume` writes through its own 1244 // handles 1245 const target_records_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, T.record_map_key)); 1246 const target_records = try DB.HashMap(.read_write).init(target_records_cursor); 1247 1248 // a record the target doesn't have arrives wholesale from the parent 1249 var outcome: [std.meta.fields(T).len]FieldMerge = @splat(.parent); 1250 var merged = parent_record; 1251 1252 if (try target_records.getCursor(record_key)) |target_record_cursor| { 1253 // both sides share the change 1254 if (target_record_cursor.slot().eql(parent_slot)) continue; 1255 1256 const target_record = try read(T.Record, DB, hash_kind, &arena, try DB.HashMap(.read_only).init(target_record_cursor)); 1257 1258 var baseline_record: ?T.Record = null; 1259 if (baseline_records) |map| { 1260 if (try map.getCursor(record_key)) |baseline_record_cursor| { 1261 baseline_record = try read(T.Record, DB, hash_kind, &arena, try DB.HashMap(.read_only).init(baseline_record_cursor)); 1262 } 1263 } 1264 1265 switch (comptime T.merge_policy) { 1266 .field_conflicts => { 1267 outcome = @splat(.kept); 1268 merged = target_record; 1269 merged.event = mergeFields(T, if (baseline_record) |baseline| baseline.event else null, target_record.event, parent_record.event, &outcome); 1270 merged.updated_order = @max(target_record.updated_order, parent_record.updated_order); 1271 if (comptime @hasDecl(T, "resolveMerge")) { 1272 T.resolveMerge(target_record.event, parent_record.event, &merged.event, &outcome); 1273 } 1274 1275 // merge the removed state separately from serialized fields. removal 1276 // carries over an unchanged record, while an edit or restoration 1277 // keeps it active. 1278 merged.removed = if (target_record.removed == parent_record.removed) 1279 target_record.removed 1280 else if (baseline_record) |baseline| blk: { 1281 const active = if (!target_record.removed) target_record else parent_record; 1282 if (active.removed != baseline.removed) break :blk false; 1283 if (!fieldsEqual(T, active.event, baseline.event)) break :blk false; 1284 break :blk true; 1285 } else false; 1286 }, 1287 .target_wins => { 1288 // an uncontested parent change carries over. if both sides 1289 // changed the record, the target wins as a whole, except that 1290 // an edit or restoration wins over a removal. 1291 const target_changed = if (baseline_slot) |slot| 1292 !target_record_cursor.slot().eql(slot) 1293 else 1294 true; 1295 merged = if (!target_changed) 1296 parent_record 1297 else if (target_record.removed != parent_record.removed) 1298 if (target_record.removed) parent_record else target_record 1299 else 1300 target_record; 1301 }, 1302 } 1303 } 1304 1305 // the index is unique, so a key the target gave to a different record 1306 // leaves this one out rather than stranding that one behind a name it 1307 // no longer owns 1308 if (!merged.removed and @hasDecl(T, "name_index_key")) { 1309 const index_key = try merged.indexKey(arena.allocator()); 1310 const name_index_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, T.name_index_key)); 1311 const name_index = try DB.HashMap(.read_write).init(name_index_cursor); 1312 if (try name_index.getCursor(hash.hashInt(hash_kind, index_key))) |owner_cursor| { 1313 var owner_id: [event_id_size]u8 = undefined; 1314 _ = try owner_cursor.readBytes(&owner_id); 1315 if (!std.mem.eql(u8, &owner_id, &event_id)) continue; 1316 } 1317 } 1318 1319 // `consume` sets no oids, since each field's winner may come from 1320 // a different side 1321 try T.consume(DB, hash_kind, haxy_moment, &event_id, merged, &arena, null); 1322 1323 switch (comptime T.merge_policy) { 1324 .field_conflicts => {}, 1325 .target_wins => continue, 1326 } 1327 1328 // removed records do not carry field conflicts 1329 if (merged.removed) continue; 1330 1331 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, T.conflicts_key)); 1332 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 1333 const id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, T.id_to_field_to_oid_key)); 1334 const id_to_field_to_oid = try DB.HashMap(.read_write).init(id_to_field_to_oid_cursor); 1335 const merged_key = orderKeyDesc(merged.created_order, &event_id); 1336 1337 var parent_field_oids: ?DB.SortedMap(.read_only) = null; 1338 if (parent_id_to_field_to_oid) |map| { 1339 if (try map.getCursor(record_key)) |field_oids_cursor| { 1340 parent_field_oids = try DB.SortedMap(.read_only).init(field_oids_cursor); 1341 } 1342 } 1343 // created only once a field actually comes from the parent 1344 var field_oids: ?DB.SortedMap(.read_write) = null; 1345 1346 // the fields both sides changed differently, space separated 1347 var conflicted_fields: [conflictedFieldsMaxLen(T)]u8 = undefined; 1348 var conflicted_len: usize = 0; 1349 1350 inline for (std.meta.fields(T), 0..) |field, i| { 1351 switch (outcome[i]) { 1352 // the target's value stands, so its oid does too 1353 .kept => {}, 1354 // the parent set this value, so its commit takes the field. a 1355 // parent with no oid took its own value from a merge, leaving 1356 // the field unattributed rather than crediting the target's 1357 // commit for a value it no longer holds. 1358 .parent => { 1359 if (field_oids == null) { 1360 field_oids = try DB.SortedMap(.read_write).init(try id_to_field_to_oid.putCursor(record_key)); 1361 } 1362 const map = field_oids orelse unreachable; 1363 const parent_oid = if (parent_field_oids) |parent_map| try parent_map.getCursor(field.name) else null; 1364 if (parent_oid) |oid_cursor| { 1365 try map.put(field.name, .{ .slot = oid_cursor.slot() }); 1366 } else { 1367 _ = try map.remove(field.name); 1368 } 1369 }, 1370 .conflicted => { 1371 if (conflicted_len > 0) { 1372 conflicted_fields[conflicted_len] = ' '; 1373 conflicted_len += 1; 1374 } 1375 @memcpy(conflicted_fields[conflicted_len..][0..field.name.len], field.name); 1376 conflicted_len += field.name.len; 1377 }, 1378 } 1379 } 1380 1381 if (conflicted_len > 0) { 1382 // each side's whole record is stored by reference, so the view can 1383 // show any of them or take theirs wholesale 1384 _ = try conflicts.remove(&merged_key); 1385 const conflict = try DB.HashMap(.read_write).init(try conflicts.putCursor(&merged_key)); 1386 try conflict.put(hash.hashInt(hash_kind, conflicted_fields_key), .{ .bytes = conflicted_fields[0..conflicted_len] }); 1387 try conflict.put(hash.hashInt(hash_kind, base_record_key), .{ .slot = baseline_slot }); 1388 try conflict.put(hash.hashInt(hash_kind, their_record_key), .{ .slot = parent_slot }); 1389 if (parent_field_oids) |map| { 1390 try conflict.put(hash.hashInt(hash_kind, their_field_to_oid_key), .{ .slot = map.slot() }); 1391 } 1392 continue; 1393 } 1394 1395 // created_order never changes, so the parent keys this record's conflict 1396 // identically, and it carries over when the target has none, unless the 1397 // parent's entry is unchanged from the baseline, meaning the target 1398 // inherited it too and resolving it removed it here. 1399 if (parent_conflicts) |map| { 1400 if (null == try conflicts.getCursor(&merged_key)) { 1401 if (try map.getSlot(&merged_key)) |conflict_slot| { 1402 const baseline_conflict_slot = if (baseline_conflicts) |baseline_map| try baseline_map.getSlot(&merged_key) else null; 1403 const resolved = if (baseline_conflict_slot) |slot| slot.eql(conflict_slot) else false; 1404 if (!resolved) try conflicts.put(&merged_key, .{ .slot = conflict_slot }); 1405 } 1406 } 1407 } 1408 } 1409 } 1410 1411 pub fn readFromRepo( 1412 comptime T: type, 1413 comptime repo_kind: rp.RepoKind, 1414 comptime repo_opts: rp.RepoOpts(repo_kind), 1415 io: std.Io, 1416 allocator: std.mem.Allocator, 1417 arena: *std.heap.ArenaAllocator, 1418 repo: *rp.Repo(repo_kind, repo_opts), 1419 id: *const [event_id_size]u8, 1420 ) !?T.Record { 1421 var event_db_maybe: ?LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 1422 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 1423 const moment = (if (event_db_maybe) |*event_db| 1424 currentMomentFromDb(repo_opts.hash, event_db.db) 1425 else if (repo_kind == .git) 1426 return null 1427 else 1428 currentMoment(repo_opts, repo)) catch return null; 1429 return T.readById(EventDB(repo_opts.hash), repo_opts.hash, moment, arena, id); 1430 } 1431 1432 pub fn currentMoment( 1433 comptime repo_opts: rp.RepoOpts(.xit), 1434 repo: *rp.Repo(.xit, repo_opts), 1435 ) !rp.Repo(.xit, repo_opts).DB.HashMap(.read_only) { 1436 return currentMomentFromDb(repo_opts.hash, &repo.core.db); 1437 } 1438 1439 pub fn currentMomentFromDb( 1440 comptime hash_kind: hash.HashKind, 1441 db: *EventDB(hash_kind), 1442 ) !EventDB(hash_kind).HashMap(.read_only) { 1443 const DB = EventDB(hash_kind); 1444 const history = try DB.ArrayList(.read_only).init(db.rootCursor().readOnly()); 1445 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 1446 const moment = try DB.HashMap(.read_only).init(moment_cursor); 1447 return try currentMomentFromRepoMoment(hash_kind, moment); 1448 } 1449 1450 pub fn currentMomentFromRepoMoment( 1451 comptime hash_kind: hash.HashKind, 1452 moment: EventDB(hash_kind).HashMap(.read_only), 1453 ) !EventDB(hash_kind).HashMap(.read_only) { 1454 const DB = EventDB(hash_kind); 1455 const last_object_id_cursor = try moment.getCursor(hash.hashInt(hash_kind, last_object_id_key)) orelse return error.NotFound; 1456 var last_object_id: [hash.byteLen(hash_kind)]u8 = undefined; 1457 _ = try last_object_id_cursor.readBytes(&last_object_id); 1458 const haxy_history_cursor = try moment.getCursor(hash.hashInt(hash_kind, history_key)) orelse return error.NotFound; 1459 const haxy_history = try DB.ArrayList(.read_only).init(haxy_history_cursor); 1460 const haxy_moments_cursor = try haxy_history.getCursor(-1) orelse return error.NotFound; 1461 const haxy_moments = try DB.HashMap(.read_only).init(haxy_moments_cursor); 1462 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(hash_kind, &last_object_id)) orelse return error.NotFound; 1463 return try DB.HashMap(.read_only).init(haxy_moment_cursor); 1464 } 1465 1466 // build a sorted-set key from an order and event id. big-endian order makes 1467 // byte order match numeric order; the event id breaks ties. 1468 pub fn orderKey(order: u64, event_id: *const [event_id_size]u8) [@sizeOf(u64) + event_id_size]u8 { 1469 var key: [@sizeOf(u64) + event_id_size]u8 = undefined; 1470 std.mem.writeInt(u64, key[0..@sizeOf(u64)], order, .big); 1471 @memcpy(key[@sizeOf(u64)..], event_id); 1472 return key; 1473 } 1474 1475 pub fn orderKeyDesc(order: u64, event_id: *const [event_id_size]u8) [@sizeOf(u64) + event_id_size]u8 { 1476 return orderKey(std.math.maxInt(u64) - order, event_id); 1477 } 1478 1479 pub fn removedRecord( 1480 comptime T: type, 1481 comptime DB: type, 1482 comptime hash_kind: hash.HashKind, 1483 haxy_moment: DB.HashMap(.read_only), 1484 existing_maybe: ?T, 1485 ) !T { 1486 var record = existing_maybe orelse return error.EventNotFound; 1487 record.removed = true; 1488 const order_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, event_order_key)) orelse return error.CursorNotFound; 1489 record.updated_order = try order_cursor.readUint(); 1490 return record; 1491 } 1492 1493 // add or refresh one entry in the global logical-event index 1494 pub fn indexEvent( 1495 comptime DB: type, 1496 comptime hash_kind: hash.HashKind, 1497 haxy_moment: DB.HashMap(.read_write), 1498 event_id: *const [event_id_size]u8, 1499 kind: EventKind, 1500 existing_maybe: anytype, 1501 record: anytype, 1502 ) !void { 1503 const event_key = hash.hashInt(hash_kind, event_id); 1504 const kind_map_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, event_index_key)); 1505 const kind_map = try DB.HashMap(.read_write).init(kind_map_cursor); 1506 try kind_map.put(event_key, .{ .bytes = @tagName(kind) }); 1507 1508 const active_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, active_event_id_set_key)); 1509 const active = try DB.SortedSet(.read_write).init(active_cursor); 1510 const removed_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, removed_event_id_set_key)); 1511 const removed_set = try DB.SortedSet(.read_write).init(removed_cursor); 1512 1513 if (existing_maybe) |existing| { 1514 const old_key = orderKeyDesc(existing.updated_order, event_id); 1515 _ = try active.remove(&old_key); 1516 _ = try removed_set.remove(&old_key); 1517 } 1518 1519 const order_key = orderKeyDesc(record.updated_order, event_id); 1520 if (record.removed) { 1521 try removed_set.put(&order_key); 1522 } else { 1523 try active.put(&order_key); 1524 } 1525 } 1526 1527 pub fn touchThread( 1528 comptime DB: type, 1529 comptime hash_kind: hash.HashKind, 1530 haxy_moment: DB.HashMap(.read_write), 1531 thread_id: *const [event_id_size]u8, 1532 order: u64, 1533 arena: *std.heap.ArenaAllocator, 1534 ) !void { 1535 const kind_map_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, event_index_key)) orelse return; 1536 const kind_map = try DB.HashMap(.read_only).init(kind_map_cursor); 1537 const kind = (try readEventKind(hash_kind, kind_map, thread_id)) orelse return; 1538 switch (kind) { 1539 .discuss => try Discussion.touch(DB, hash_kind, haxy_moment, thread_id, order, arena), 1540 else => {}, 1541 } 1542 } 1543 1544 pub fn readEventKind( 1545 comptime hash_kind: hash.HashKind, 1546 kind_map: anytype, 1547 id: *const [event_id_size]u8, 1548 ) !?EventKind { 1549 const cursor = try kind_map.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 1550 var buffer: [64]u8 = undefined; 1551 return std.meta.stringToEnum(EventKind, try cursor.readBytes(&buffer)) orelse error.InvalidEventKind; 1552 } 1553 1554 // the email of the event's author, or null when the id names no authored event 1555 // of this kind 1556 pub fn readAuthorEmail( 1557 comptime repo_kind: rp.RepoKind, 1558 comptime repo_opts: rp.RepoOpts(repo_kind), 1559 io: std.Io, 1560 allocator: std.mem.Allocator, 1561 arena: *std.heap.ArenaAllocator, 1562 repo: *rp.Repo(repo_kind, repo_opts), 1563 kind: EventKind, 1564 id: *const [event_id_size]u8, 1565 ) !?[]const u8 { 1566 const record_map_key = switch (kind) { 1567 .issue => Issue.record_map_key, 1568 .discuss => Discussion.record_map_key, 1569 .comment => Comment.record_map_key, 1570 .attach => Attachment.record_map_key, 1571 .patchrev => PatchRev.record_map_key, 1572 .patch => Patch.record_map_key, 1573 .user, .repo, .fork => return null, 1574 }; 1575 1576 const DB = EventDB(repo_opts.hash); 1577 var event_db_maybe: ?LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 1578 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 1579 const moment = (if (event_db_maybe) |*event_db| 1580 currentMomentFromDb(repo_opts.hash, event_db.db) 1581 else if (repo_kind == .git) 1582 return null 1583 else 1584 currentMoment(repo_opts, repo)) catch return null; 1585 1586 const records_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, record_map_key))) orelse return null; 1587 const records = try DB.HashMap(.read_only).init(records_cursor); 1588 const record_cursor = (try records.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return null; 1589 const record = try DB.HashMap(.read_only).init(record_cursor); 1590 return try readField(?[]const u8, DB, repo_opts.hash, arena, try record.getCursor(hash.hashInt(repo_opts.hash, "author_email"))); 1591 } 1592 1593 // the event id embedded in a set entry's order key 1594 pub fn readOrderKeyId(comptime DB: type, kv_pair_cursor: DB.Cursor(.read_only)) ![event_id_size]u8 { 1595 var cursor = kv_pair_cursor; 1596 const kv_pair = try cursor.readKeyValuePair(); 1597 var order_key: [@sizeOf(u64) + event_id_size]u8 = undefined; 1598 _ = try kv_pair.key_cursor.readBytes(&order_key); 1599 return order_key[@sizeOf(u64)..][0..event_id_size].*; 1600 } 1601 1602 // split a pushed "<owner>/<repo>" path into its two components, or null if it 1603 // isn't exactly two non-empty segments. 1604 pub fn parseOwnerRepoPath(path: []const u8) ?struct { owner: []const u8, name: []const u8 } { 1605 var it = std.mem.splitScalar(u8, path, '/'); 1606 const owner = it.next() orelse return null; 1607 const name = it.next() orelse return null; 1608 if (it.next() != null) return null; 1609 if (owner.len == 0 or name.len == 0) return null; 1610 1611 return .{ .owner = owner, .name = name }; 1612 } 1613 1614 pub const CreateRepoOptions = struct { 1615 read_access: Repo.Access = .private, 1616 }; 1617 1618 // resolve a pushed `<owner>/<repo>` to the hex event id that names its on-disk 1619 // directory under the repos dir 1620 pub fn resolveOrCreateRepo( 1621 io: std.Io, 1622 allocator: std.mem.Allocator, 1623 admin_repo_path: []const u8, 1624 owner_name: []const u8, 1625 repo_name: []const u8, 1626 create_options_maybe: ?CreateRepoOptions, 1627 ) !?[event_id_size * 2]u8 { 1628 var repo = rp.Repo(.xit, admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 1629 error.RepoNotFound => return null, 1630 else => |e| return e, 1631 }; 1632 defer repo.deinit(io, allocator); 1633 1634 var arena = std.heap.ArenaAllocator.init(allocator); 1635 defer arena.deinit(); 1636 1637 const moment = try currentMoment(admin_repo_opts, &repo); 1638 1639 // an already-registered repo reuses its event id, so a re-push (or a clone) 1640 // lands in the same repo 1641 if (try Repo.readByOwnerAndName(AdminDB, admin_repo_opts.hash, moment, &arena, owner_name, repo_name)) |found| { 1642 return std.fmt.bytesToHex(found.event_id, .lower); 1643 } 1644 1645 const create_options = create_options_maybe orelse return null; 1646 1647 // the new repo is owned by the named user, read from the name index; an 1648 // unknown owner can't own a repo, so the push is rejected 1649 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(admin_repo_opts.hash, "name->user-id")) orelse return null; 1650 const name_to_user_id = try AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 1651 const owner_id_cursor = try name_to_user_id.getCursor(hash.hashInt(admin_repo_opts.hash, owner_name)) orelse return null; 1652 var owner_user_id: [event_id_size]u8 = undefined; 1653 _ = try owner_id_cursor.readBytes(&owner_user_id); 1654 1655 const owner = (try User.readById(AdminDB, admin_repo_opts.hash, moment, &arena, &owner_user_id)) orelse unreachable; 1656 1657 var id_bytes: [event_id_size]u8 = undefined; 1658 io.random(&id_bytes); 1659 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1660 1661 try consume(.server, .admin, .xit, admin_repo_opts, io, allocator, &repo, events_ref, &[_]EventWithId{.{ 1662 .id = event_id_hex, 1663 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1664 .author = .{ .name = owner.event.name, .email = owner.event.email }, 1665 .event = .{ .repo = .{ 1666 .user_id = &owner_user_id, 1667 .name = repo_name, 1668 .description = "", 1669 .read_access = create_options.read_access, 1670 } }, 1671 }}); 1672 1673 return event_id_hex; 1674 } 1675 1676 // 1677 // reading from xitdb 1678 // 1679 1680 pub fn read( 1681 comptime T: type, 1682 comptime DB: type, 1683 comptime hash_kind: hash.HashKind, 1684 arena: *std.heap.ArenaAllocator, 1685 map: DB.HashMap(.read_only), 1686 ) !T { 1687 var event: T = undefined; 1688 switch (@typeInfo(T)) { 1689 .@"struct" => |struct_info| { 1690 inline for (struct_info.fields) |field| { 1691 const cursor = try map.getCursor(hash.hashInt(hash_kind, field.name)); 1692 @field(event, field.name) = try readField(field.type, DB, hash_kind, arena, cursor); 1693 } 1694 }, 1695 else => @compileError("read expects a struct"), 1696 } 1697 return event; 1698 } 1699 1700 fn readField( 1701 comptime Field: type, 1702 comptime DB: type, 1703 comptime hash_kind: hash.HashKind, 1704 arena: *std.heap.ArenaAllocator, 1705 cursor_maybe: ?DB.Cursor(.read_only), 1706 ) !Field { 1707 if (@typeInfo(Field) == .optional) { 1708 // a missing key or a null union payload is null 1709 const cursor = cursor_maybe orelse return null; 1710 if (cursor.slot().tag == .none) return null; 1711 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 1712 } 1713 const cursor = cursor_maybe orelse return error.NotFound; 1714 switch (@typeInfo(Field)) { 1715 .pointer => |pointer_info| { 1716 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.is_const) { 1717 @compileError("unsupported read field type: " ++ @typeName(Field)); 1718 } 1719 return try cursor.readBytesAlloc(arena.allocator(), null); 1720 }, 1721 .array => |array_info| { 1722 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 1723 var bytes: Field = undefined; 1724 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 1725 return bytes; 1726 }, 1727 .bool => { 1728 var buffer: [5]u8 = undefined; 1729 const bytes = try cursor.readBytesObject(&buffer); 1730 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 1731 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 1732 if (std.mem.eql(u8, bytes.value, "true")) return true; 1733 if (std.mem.eql(u8, bytes.value, "false")) return false; 1734 return error.InvalidBool; 1735 }, 1736 .int => |int_info| return switch (int_info.signedness) { 1737 .unsigned => @intCast(try cursor.readUint()), 1738 .signed => @intCast(try cursor.readInt()), 1739 }, 1740 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 1741 .@"union" => |union_info| { 1742 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 1743 switch (cursor.slot().tag) { 1744 .bytes, .short_bytes => { 1745 const name = try cursor.readBytesAlloc(arena.allocator(), null); 1746 inline for (union_info.fields) |variant| { 1747 if (variant.type == void and std.mem.eql(u8, name, variant.name)) { 1748 return @unionInit(Field, variant.name, {}); 1749 } 1750 } 1751 return error.InvalidUnion; 1752 }, 1753 .hash_map => { 1754 const variants = try DB.HashMap(.read_only).init(cursor); 1755 var iter = try variants.iterator(); 1756 const entry = (try iter.next()) orelse return error.InvalidUnion; 1757 const pair = try entry.readKeyValuePair(); 1758 if (try iter.next() != null) return error.InvalidUnion; 1759 inline for (union_info.fields) |variant| { 1760 if (variant.type != void and pair.hash == hash.hashInt(hash_kind, variant.name)) { 1761 return @unionInit(Field, variant.name, try readField(variant.type, DB, hash_kind, arena, pair.value_cursor)); 1762 } 1763 } 1764 return error.InvalidUnion; 1765 }, 1766 else => return error.InvalidUnion, 1767 } 1768 }, 1769 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 1770 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 1771 } 1772 } 1773 1774 // whether two events carry the same data 1775 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 1776 switch (@typeInfo(T)) { 1777 .@"struct" => |struct_info| { 1778 inline for (struct_info.fields) |field| { 1779 if (!fieldEqual(field.type, @field(a, field.name), @field(b, field.name))) return false; 1780 } 1781 }, 1782 else => @compileError("fieldsEqual expects a struct"), 1783 } 1784 1785 return true; 1786 } 1787 1788 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 1789 switch (@typeInfo(Field)) { 1790 .pointer => |pointer_info| { 1791 if (pointer_info.size == .slice and pointer_info.child == u8) { 1792 return std.mem.eql(u8, a, b); 1793 } else { 1794 @compileError("unsupported field type: " ++ @typeName(Field)); 1795 } 1796 }, 1797 .array => |array_info| { 1798 if (array_info.child == u8) { 1799 return std.mem.eql(u8, &a, &b); 1800 } else { 1801 @compileError("unsupported field type: " ++ @typeName(Field)); 1802 } 1803 }, 1804 .void => return true, 1805 .bool, .int, .@"enum" => return a == b, 1806 .@"union" => { 1807 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1808 return switch (a) { 1809 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 1810 }; 1811 }, 1812 .@"struct" => return fieldsEqual(Field, a, b), 1813 .optional => |optional_info| { 1814 if (a) |a_value| { 1815 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 1816 return false; 1817 } 1818 return b == null; 1819 }, 1820 else => @compileError("unsupported field type: " ++ @typeName(Field)), 1821 } 1822 } 1823 1824 // 1825 // writing to xitdb 1826 // 1827 1828 pub fn upsert( 1829 comptime T: type, 1830 comptime DB: type, 1831 comptime hash_kind: hash.HashKind, 1832 map: DB.HashMap(.read_write), 1833 event: T, 1834 ) !void { 1835 switch (@typeInfo(T)) { 1836 .@"struct" => |struct_info| { 1837 inline for (struct_info.fields) |field| { 1838 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1839 } 1840 }, 1841 else => @compileError("upsert expects a struct"), 1842 } 1843 } 1844 1845 fn upsertField( 1846 comptime DB: type, 1847 comptime hash_kind: hash.HashKind, 1848 map: DB.HashMap(.read_write), 1849 comptime field_name: []const u8, 1850 comptime Field: type, 1851 value: Field, 1852 ) !void { 1853 const key = hash.hashInt(hash_kind, field_name); 1854 1855 switch (@typeInfo(Field)) { 1856 .pointer => |pointer_info| { 1857 if (pointer_info.size == .slice and pointer_info.child == u8) { 1858 try upsertBytes(DB, hash_kind, map, key, value); 1859 } else { 1860 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1861 } 1862 }, 1863 .array => |array_info| { 1864 if (array_info.child == u8) { 1865 try upsertBytes(DB, hash_kind, map, key, &value); 1866 } else { 1867 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1868 } 1869 }, 1870 .int => |int_info| switch (int_info.signedness) { 1871 .unsigned => { 1872 if (try map.getCursor(key)) |value_cursor| { 1873 if (try value_cursor.readUint() == value) { 1874 return; 1875 } 1876 } 1877 1878 try map.put(key, .{ .uint = value }); 1879 }, 1880 .signed => { 1881 if (try map.getCursor(key)) |value_cursor| { 1882 if (try value_cursor.readInt() == value) { 1883 return; 1884 } 1885 } 1886 1887 try map.put(key, .{ .int = value }); 1888 }, 1889 }, 1890 .bool => { 1891 const bytes = if (value) "true" else "false"; 1892 if (try map.getCursor(key)) |value_cursor| { 1893 var buffer: [5]u8 = undefined; 1894 const existing = try value_cursor.readBytesObject(&buffer); 1895 if (existing.format_tag) |format_tag| { 1896 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1897 return; 1898 } 1899 } 1900 } 1901 1902 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1903 }, 1904 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1905 .@"union" => switch (value) { 1906 inline else => |payload, tag| { 1907 if (@TypeOf(payload) == void) { 1908 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 1909 } else { 1910 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 1911 if (try map.getCursor(key)) |cursor| { 1912 switch (cursor.slot().tag) { 1913 .hash_map => { 1914 const variants = try DB.HashMap(.read_only).init(cursor); 1915 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 1916 }, 1917 else => try map.put(key, .{ .slot = null }), 1918 } 1919 } 1920 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 1921 if (@typeInfo(@TypeOf(payload)) == .optional) { 1922 // keep the variant even when its payload is null 1923 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 1924 } 1925 if (try variants.getCursor(tag_key)) |cursor| { 1926 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 1927 } 1928 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 1929 } 1930 }, 1931 }, 1932 .optional => |optional_info| { 1933 // a missing key is null 1934 if (value) |child| { 1935 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 1936 } else { 1937 _ = try map.remove(key); 1938 } 1939 }, 1940 .@"struct" => try upsert( 1941 Field, 1942 DB, 1943 hash_kind, 1944 try DB.HashMap(.read_write).init(try map.putCursor(key)), 1945 value, 1946 ), 1947 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1948 } 1949 } 1950 1951 fn upsertBytes( 1952 comptime DB: type, 1953 comptime hash_kind: hash.HashKind, 1954 map: DB.HashMap(.read_write), 1955 key: hash.HashInt(hash_kind), 1956 value: []const u8, 1957 ) !void { 1958 var existing_cursor_maybe = try map.getCursor(key); 1959 if (existing_cursor_maybe) |*existing_cursor| { 1960 if (try bytesEqual(DB, existing_cursor, value)) { 1961 return; 1962 } 1963 } 1964 1965 try map.put(key, .{ .bytes = value }); 1966 } 1967 1968 fn bytesEqual( 1969 comptime DB: type, 1970 cursor: *DB.Cursor(.read_only), 1971 value: []const u8, 1972 ) !bool { 1973 switch (cursor.slot().tag) { 1974 .bytes, .short_bytes => {}, 1975 else => return false, 1976 } 1977 var read_buffer: [1024]u8 = undefined; 1978 var reader = try cursor.reader(&read_buffer); 1979 if (reader.size != value.len) { 1980 return false; 1981 } 1982 1983 var chunk_buffer: [1024]u8 = undefined; 1984 var offset: usize = 0; 1985 while (offset < value.len) { 1986 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1987 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1988 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1989 return false; 1990 } 1991 offset += chunk_len; 1992 } 1993 1994 return true; 1995 } 1996 1997 // 1998 // CommitIterator 1999 // 2000 // differs from xit's ObjectIterator in a few ways: 2001 // 2002 // 1. iterates parents first, rather than children first 2003 // 2. `next` only returns the object id, not the full Object 2004 // 3. stores its temporary state in a file on disk to avoid OOMs 2005 // 2006 2007 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2008 return struct { 2009 const DB = EventDB(repo_opts.hash); 2010 // a plain file is used for the scratch db because each write outside 2011 // a transaction is synced anyway, so buffering would gain nothing 2012 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2013 const db_name = "haxy-repo-events.db"; 2014 2015 repo_dir: std.Io.Dir, 2016 db_file: std.Io.File, 2017 db: *TempDB, 2018 // map of each object id to its children 2019 parent_to_children: TempDB.HashMap(.read_write), 2020 // tracking pending parents is necessary because a child can only 2021 // be returned after all of its unconsumed parents have been returned 2022 child_to_pending_parents: TempDB.HashMap(.read_write), 2023 // queue of object ids that are ready to be returned from `next` 2024 ready_queue: TempDB.ArrayList(.read_write), 2025 ready_queue_index: u64, 2026 // the object id at the tip of this branch 2027 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2028 // the object id furthest back in the history that hasn't been consumed 2029 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2030 2031 pub fn init( 2032 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2033 io: std.Io, 2034 allocator: std.mem.Allocator, 2035 consumed_object_ids: DB.HashMap(.read_only), 2036 ref: rf.Ref, 2037 ) !CommitIterator(repo_kind, repo_opts) { 2038 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2039 errdefer { 2040 db_file.close(io); 2041 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2042 } 2043 2044 // cursors point at the db, so it needs a stable address 2045 const db_ptr = try allocator.create(TempDB); 2046 errdefer allocator.destroy(db_ptr); 2047 // the db is scratch state that gets deleted after iteration, so 2048 // there is nothing worth fsyncing 2049 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2050 2051 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2052 2053 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2054 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2055 2056 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2057 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2058 2059 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2060 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2061 2062 var self = CommitIterator(repo_kind, repo_opts){ 2063 .repo_dir = state.core.repo_dir, 2064 .db_file = db_file, 2065 .db = db_ptr, 2066 .parent_to_children = parent_to_children, 2067 .child_to_pending_parents = child_to_pending_parents, 2068 .ready_queue = ready_queue, 2069 .ready_queue_index = 0, 2070 .newest_object_id = null, 2071 .oldest_object_id = null, 2072 }; 2073 errdefer self.deinit(io, allocator); 2074 2075 try self.collect(state, io, allocator, consumed_object_ids, ref); 2076 2077 return self; 2078 } 2079 2080 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2081 self.db_file.close(io); 2082 self.repo_dir.deleteFile(io, db_name) catch {}; 2083 allocator.destroy(self.db); 2084 } 2085 2086 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2087 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2088 2089 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2090 self.ready_queue_index += 1; 2091 2092 try self.enqueueChildren(&oid); 2093 return oid; 2094 } 2095 2096 fn collect( 2097 self: *CommitIterator(repo_kind, repo_opts), 2098 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2099 io: std.Io, 2100 allocator: std.mem.Allocator, 2101 consumed_object_ids: DB.HashMap(.read_only), 2102 ref: rf.Ref, 2103 ) !void { 2104 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2105 2106 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2107 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2108 var walk_queue_index: u64 = 0; 2109 2110 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2111 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2112 2113 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2114 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2115 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2116 try walk_queue.append(.{ .bytes = &head_oid }); 2117 2118 while (walk_queue_index < try walk_queue.count()) { 2119 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2120 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2121 2122 walk_queue_index += 1; 2123 2124 // if we've seen this object id, skip it 2125 if (null != try seen_object_ids.getCursor(oid_int)) { 2126 continue; 2127 } 2128 2129 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2130 2131 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2132 defer commit_object.deinit(); 2133 2134 // if this object id has already been consumed, skip it 2135 if (null != try consumed_object_ids.getCursor(oid_int)) { 2136 continue; 2137 } 2138 2139 if (self.newest_object_id == null) self.newest_object_id = oid; 2140 self.oldest_object_id = oid; 2141 2142 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2143 var pending_parent_count: u64 = 0; 2144 for (parent_oids) |*parent_oid| { 2145 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2146 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2147 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2148 2149 // if this object id has already been consumed, skip it 2150 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2151 continue; 2152 } 2153 2154 // if this object id hasn't already been seen, add it to the walk queue 2155 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2156 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2157 } 2158 2159 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2160 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2161 try children.put(oid_int, .{ .bytes = &oid }); 2162 pending_parent_count += 1; 2163 } 2164 2165 if (pending_parent_count == 0) { 2166 try self.ready_queue.append(.{ .bytes = &oid }); 2167 } else { 2168 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2169 } 2170 } 2171 } 2172 2173 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2174 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2175 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2176 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2177 var children_iter = try children.iterator(); 2178 2179 while (try children_iter.next()) |*child_cursor| { 2180 const kv_pair = try child_cursor.readKeyValuePair(); 2181 const child_oid_int = kv_pair.hash; 2182 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2183 const pending_parent_count = try pending_parent_cursor.readUint(); 2184 2185 if (pending_parent_count <= 1) { 2186 _ = try self.child_to_pending_parents.remove(child_oid_int); 2187 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2188 try self.ready_queue.append(.{ .bytes = &child_oid }); 2189 } else { 2190 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2191 } 2192 } 2193 } 2194 2195 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2196 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2197 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2198 _ = try oid_cursor.readBytes(&oid); 2199 return oid; 2200 } 2201 }; 2202 } 2203