| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use super::Pool; |
| 5 | | use sqlx::FromRow; |
| 6 | |
|
| 7 | | #[derive(Clone, Debug, FromRow)] |
| 8 | | pub struct Logged { |
| 9 | | pub idx: i64, |
| 10 | | pub object: String, |
| 11 | | pub kind: String, |
| 12 | | pub repo_id: Option<i64>, |
| 13 | | pub received_ms: i64, |
| 14 | | } |
| 15 | |
|
| 16 | |
|
| 17 | 410× | pub async fn entry_of(pool: &Pool, object: &str, kind: &str) -> sqlx::Result<Option<i64>> { |
| 18 | 410× | sqlx::query_scalar("SELECT idx FROM tlog_entries WHERE object = $1 AND kind = $2").bind(object).bind(kind).fetch_optional(pool).await |
| 19 | 410× | } |
| 20 | |
|
| 21 | 215× | pub async fn record(pool: &Pool, idx: i64, object: &str, kind: &str, repo_id: Option<i64>, received_ms: i64) -> sqlx::Result<()> { |
| 22 | 215× | sqlx::query("INSERT INTO tlog_entries (idx, object, kind, repo_id, received_ms) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING") |
| 23 | 215× | .bind(idx) |
| 24 | 215× | .bind(object) |
| 25 | 215× | .bind(kind) |
| 26 | 215× | .bind(repo_id) |
| 27 | 215× | .bind(received_ms) |
| 28 | 215× | .execute(pool) |
| 29 | 215× | .await?; |
| 30 | 215× | Ok(()) |
| 31 | 215× | } |
| 32 | |
|
| 33 | |
|
| 34 | 1× | pub async fn recent(pool: &Pool, repo_id: Option<i64>, limit: i64) -> sqlx::Result<Vec<Logged>> { |
| 35 | 1× | match repo_id { |
| 36 | 0× | Some(r) => { |
| 37 | 0× | sqlx::query_as("SELECT * FROM tlog_entries WHERE repo_id = $1 ORDER BY idx DESC LIMIT $2") |
| 38 | 0× | .bind(r) |
| 39 | 0× | .bind(limit) |
| 40 | 0× | .fetch_all(pool) |
| 41 | 0× | .await |
| 42 | | } |
| 43 | 1× | None => sqlx::query_as("SELECT * FROM tlog_entries ORDER BY idx DESC LIMIT $1").bind(limit).fetch_all(pool).await, |
| 44 | | } |
| 45 | 1× | } |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 4× | pub async fn witnessed(pool: &Pool, origin: &str) -> sqlx::Result<Option<(i64, String)>> { |
| 50 | 4× | sqlx::query_as("SELECT size, root FROM tlog_witnessed WHERE origin = $1").bind(origin).fetch_optional(pool).await |
| 51 | 4× | } |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 2× | pub async fn witness_advance(pool: &Pool, origin: &str, old: i64, size: i64, root: &str) -> sqlx::Result<bool> { |
| 56 | 2× | let now = super::now_ms(); |
| 57 | 2× | let r = if old == 0 { |
| 58 | 1× | sqlx::query( |
| 59 | 1× | "INSERT INTO tlog_witnessed (origin, size, root, updated_ms) VALUES ($1, $2, $3, $4) \ |
| 60 | 1× | ON CONFLICT (origin) DO UPDATE SET size = $2, root = $3, updated_ms = $4 WHERE tlog_witnessed.size = 0", |
| 61 | 1× | ) |
| 62 | 1× | .bind(origin) |
| 63 | 1× | .bind(size) |
| 64 | 1× | .bind(root) |
| 65 | 1× | .bind(now) |
| 66 | 1× | .execute(pool) |
| 67 | 1× | .await? |
| 68 | | } else { |
| 69 | 1× | sqlx::query("UPDATE tlog_witnessed SET size = $1, root = $2, updated_ms = $3 WHERE origin = $4 AND size = $5") |
| 70 | 1× | .bind(size) |
| 71 | 1× | .bind(root) |
| 72 | 1× | .bind(now) |
| 73 | 1× | .bind(origin) |
| 74 | 1× | .bind(old) |
| 75 | 1× | .execute(pool) |
| 76 | 1× | .await? |
| 77 | | }; |
| 78 | 2× | Ok(r.rows_affected() == 1) |
| 79 | 2× | } |
| 80 | |
|
| 81 | |
|
| 82 | 2× | pub async fn cosigned_size(pool: &Pool, name: &str) -> sqlx::Result<i64> { |
| 83 | 2× | Ok(sqlx::query_scalar("SELECT size FROM tlog_cosigned WHERE witness = $1").bind(name).fetch_optional(pool).await?.unwrap_or(0)) |
| 84 | 2× | } |
| 85 | |
|
| 86 | 2× | pub async fn set_cosigned(pool: &Pool, name: &str, size: i64) -> sqlx::Result<()> { |
| 87 | 2× | sqlx::query( |
| 88 | 2× | "INSERT INTO tlog_cosigned (witness, size, updated_ms) VALUES ($1, $2, $3) \ |
| 89 | 2× | ON CONFLICT (witness) DO UPDATE SET size = $2, updated_ms = $3", |
| 90 | 2× | ) |
| 91 | 2× | .bind(name) |
| 92 | 2× | .bind(size) |
| 93 | 2× | .bind(super::now_ms()) |
| 94 | 2× | .execute(pool) |
| 95 | 2× | .await?; |
| 96 | 2× | Ok(()) |
| 97 | 2× | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | 2× | pub async fn keep_cosigned_note(pool: &Pool, size: i64, note: &str) -> sqlx::Result<()> { |
| 102 | 2× | sqlx::query( |
| 103 | 2× | "INSERT INTO tlog_cosigned_notes (size, note, updated_ms) VALUES ($1, $2, $3) \ |
| 104 | 2× | ON CONFLICT (size) DO UPDATE SET note = $2, updated_ms = $3", |
| 105 | 2× | ) |
| 106 | 2× | .bind(size) |
| 107 | 2× | .bind(note) |
| 108 | 2× | .bind(super::now_ms()) |
| 109 | 2× | .execute(pool) |
| 110 | 2× | .await?; |
| 111 | 2× | Ok(()) |
| 112 | 2× | } |
| 113 | |
|
| 114 | |
|
| 115 | 27× | pub async fn cosigned_notes_after(pool: &Pool, index: i64, n: i64) -> sqlx::Result<Vec<(i64, String)>> { |
| 116 | 27× | sqlx::query_as("SELECT size, note FROM tlog_cosigned_notes WHERE size > $1 ORDER BY size LIMIT $2") |
| 117 | 27× | .bind(index) |
| 118 | 27× | .bind(n) |
| 119 | 27× | .fetch_all(pool) |
| 120 | 27× | .await |
| 121 | 27× | } |