| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | |
| 8 | "gorm.io/gorm" |
| 9 | ) |
| 10 | |
| 11 | // Bookkeeping is the third named exemption from gate 8, beside drafts |
| 12 | // and retention (Notion import spec §14.2): run rows, heartbeats, |
| 13 | // progress, staging, clearing staged trees, and map state that changes |
| 14 | // no page. It goes through bookkeep alone, and bookkeep's transaction |
| 15 | // may name no table but the import's own: a fence on the database |
| 16 | // refuses any other, whatever the caller wrote (gate I19). |
| 17 | // |
| 18 | // A page write is never bookkeeping. It goes through Store.write and |
| 19 | // logs its event (import_store.go). |
| 20 | |
| 21 | // errLeaseLost is a run write whose lease has moved: Stop, a reclaim or |
| 22 | // a newer run took the run, and this goroutine must change nothing. |
| 23 | var errLeaseLost = errors.New("mapa: the import run's lease has moved") |
| 24 | |
| 25 | // errOutsideBookkeeping is the fence's refusal. |
| 26 | var errOutsideBookkeeping = errors.New("mapa: bookkeeping may touch " + |
| 27 | "only the import's own tables") |
| 28 | |
| 29 | // bookkeepingKey marks a context as bookkeeping's. |
| 30 | type bookkeepingKey struct{} |
| 31 | |
| 32 | // runLease names a run and the lease its writer holds. The zero lease |
| 33 | // is no run: bookkeeping outside a run (a person's act, boot, retention) |
| 34 | // checks none. |
| 35 | type runLease struct{ run, lease int64 } |
| 36 | |
| 37 | // bookkeep runs change as bookkeeping, and, when lease names a run, |
| 38 | // only while the run still holds it: every write a run makes checks |
| 39 | // its lease in the same transaction (spec §8.3). |
| 40 | func (store *Store) bookkeep(ctx context.Context, lease runLease, |
| 41 | change func(*gorm.DB) error) error { |
| 42 | ctx = context.WithValue(ctx, bookkeepingKey{}, true) |
| 43 | return store.database.WithContext(ctx).Transaction( |
| 44 | func(database *gorm.DB) error { |
| 45 | if err := holdLease(database, lease); err != nil { |
| 46 | return err |
| 47 | } |
| 48 | return change(database) |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | // holdLease checks, inside a write transaction, that the run still |
| 53 | // holds lease. The no-op update takes SQLite's write lock as it |
| 54 | // matches, so a Stop that rotates the lease waits for this transaction |
| 55 | // or makes it fail: never both. |
| 56 | func holdLease(database *gorm.DB, lease runLease) error { |
| 57 | if lease.run == 0 { |
| 58 | return nil |
| 59 | } |
| 60 | held := database.Exec("UPDATE notion_runs SET lease = lease "+ |
| 61 | "WHERE id = ? AND lease = ?", lease.run, lease.lease) |
| 62 | if held.Error != nil { |
| 63 | return held.Error |
| 64 | } |
| 65 | if held.RowsAffected != 1 { |
| 66 | return errLeaseLost |
| 67 | } |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | // fenceBookkeeping registers the fence on database, once, first in |
| 72 | // each chain, so it refuses before the statement runs. |
| 73 | func fenceBookkeeping(database *gorm.DB) { |
| 74 | callbacks := database.Callback() |
| 75 | if callbacks.Create().Get(fenceName) != nil { |
| 76 | return |
| 77 | } |
| 78 | _ = callbacks.Create().Before("*").Register(fenceName, fence) |
| 79 | _ = callbacks.Query().Before("*").Register(fenceName, fence) |
| 80 | _ = callbacks.Update().Before("*").Register(fenceName, fence) |
| 81 | _ = callbacks.Delete().Before("*").Register(fenceName, fence) |
| 82 | _ = callbacks.Row().Before("*").Register(fenceName, fence) |
| 83 | _ = callbacks.Raw().Before("*").Register(fenceName, fence) |
| 84 | } |
| 85 | |
| 86 | const fenceName = "mapa:bookkeeping_fence" |
| 87 | |
| 88 | // fence refuses, inside bookkeeping, any statement whose table is not |
| 89 | // the import's. A raw statement names no table to the fence and is |
| 90 | // refused too, except holdLease's, which is the fence's own check. |
| 91 | func fence(database *gorm.DB) { |
| 92 | statement := database.Statement |
| 93 | if statement.Context == nil || |
| 94 | statement.Context.Value(bookkeepingKey{}) == nil { |
| 95 | return |
| 96 | } |
| 97 | table := statement.Table |
| 98 | if table == "" && strings.HasPrefix(statement.SQL.String(), |
| 99 | "UPDATE notion_runs SET lease = lease ") { |
| 100 | return |
| 101 | } |
| 102 | if !strings.HasPrefix(table, "notion_") { |
| 103 | _ = database.AddError(errOutsideBookkeeping) |
| 104 | } |
| 105 | } |
| 106 | |