feat: replicate threads through the observation log (#45)
feat: replicate threads through the observation log (#45) Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #45.
This commit is contained in:
+28
-2
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -187,6 +188,9 @@ func Open(path string) (*KnoxDB, error) {
|
||||
"ALTER TABLE threads ADD COLUMN retained INTEGER DEFAULT 0",
|
||||
"ALTER TABLE threads ADD COLUMN summary TEXT DEFAULT ''",
|
||||
"ALTER TABLE threads ADD COLUMN summarized_at TEXT DEFAULT ''",
|
||||
"ALTER TABLE threads ADD COLUMN uid TEXT",
|
||||
"ALTER TABLE threads ADD COLUMN hcl INTEGER",
|
||||
"ALTER TABLE threads ADD COLUMN superseded_by TEXT",
|
||||
"ALTER TABLE snippets ADD COLUMN junk INTEGER DEFAULT 0",
|
||||
"ALTER TABLE snippets ADD COLUMN kind TEXT DEFAULT 'code'",
|
||||
}
|
||||
@@ -228,6 +232,12 @@ func Open(path string) (*KnoxDB, error) {
|
||||
return nil, fmt.Errorf("create locator index: %w", err)
|
||||
}
|
||||
|
||||
// Thread replication identity index, likewise after the uid migration: a
|
||||
// fresh DB has the column from Schema, an existing one only gains it above.
|
||||
if _, err := db.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_threads_uid ON threads(uid) WHERE uid IS NOT NULL AND uid != ''`); err != nil {
|
||||
return nil, fmt.Errorf("create thread uid index: %w", err)
|
||||
}
|
||||
|
||||
// Thread idempotency index must come after the cluster_key migration (a
|
||||
// fresh DB has the column from Schema; an existing DB gets it above).
|
||||
if _, err := db.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_threads_cluster ON threads(cluster_key) WHERE cluster_key IS NOT NULL AND cluster_key != ''`); err != nil {
|
||||
@@ -1649,15 +1659,21 @@ func (k *KnoxDB) CreateThreadCluster(title, motivation, priority, tags, provenan
|
||||
// concurrent creators (thread ticker + post-pull reconcile, or two gossip
|
||||
// nodes converging on the same cluster) lose the race into a no-op instead
|
||||
// of a UNIQUE error, then pick up the winner's id.
|
||||
uid := NewThreadUID()
|
||||
res, err := k.db.Exec(
|
||||
`INSERT OR IGNORE INTO threads (title, motivation, priority, tags, provenance, cluster_key) VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
title, motivation, priority, tags, provenance, clusterKey,
|
||||
`INSERT OR IGNORE INTO threads (title, motivation, priority, tags, provenance, cluster_key, uid) VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
title, motivation, priority, tags, provenance, clusterKey, uid,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n > 0 {
|
||||
id, _ := res.LastInsertId()
|
||||
// Replicate the thread itself: titles and motivations are authored, so
|
||||
// they exist nowhere in the log until this runs.
|
||||
if err := k.EmitThread(id); err != nil {
|
||||
log.Printf("[knox] emit thread #%d: %v", id, err)
|
||||
}
|
||||
return id, true, nil
|
||||
}
|
||||
// Ignored insert: a thread with this cluster key already exists.
|
||||
@@ -1737,6 +1753,11 @@ func (k *KnoxDB) CloseThread(id int64) error {
|
||||
`UPDATE threads SET status='resolved', resolved_at=datetime('now'), updated_at=datetime('now') WHERE id=?`,
|
||||
id,
|
||||
)
|
||||
if err == nil {
|
||||
if e := k.EmitThread(id); e != nil {
|
||||
log.Printf("[knox] emit thread #%d: %v", id, e)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1777,6 +1798,11 @@ func (k *KnoxDB) UpdateThread(id int64, title, motivation, priority, tags string
|
||||
`UPDATE threads SET title=?, motivation=?, priority=?, tags=?, provenance=?, updated_at=datetime('now') WHERE id=?`,
|
||||
title, motivation, priority, tags, provenance, id,
|
||||
)
|
||||
if err == nil {
|
||||
if e := k.EmitThread(id); e != nil {
|
||||
log.Printf("[knox] emit thread #%d: %v", id, e)
|
||||
}
|
||||
}
|
||||
return true, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user