From 328f2892e01056b809851e5338ef184df5033906 Mon Sep 17 00:00:00 2001 From: David Schlachter Date: Wed, 7 Jan 2026 22:53:13 -0500 Subject: Much faster initial setup --- go.mod | 5 ++++- go.sum | 2 ++ main.go | 5 +++++ setup.go | 16 +++++++++++++--- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 310a6aa..61d6704 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module davidschlachter.com/french-wiktionary-flashcards go 1.24.1 -require github.com/mattn/go-sqlite3 v1.14.33 +require ( + github.com/goccy/go-json v0.10.5 + github.com/mattn/go-sqlite3 v1.14.33 +) diff --git a/go.sum b/go.sum index 3de9741..720c2c7 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/mattn/go-sqlite3 v1.14.33 h1:A5blZ5ulQo2AtayQ9/limgHEkFreKj1Dv226a1K73s0= github.com/mattn/go-sqlite3 v1.14.33/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/main.go b/main.go index f1598bf..7306df2 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,11 @@ func main() { log.Fatalf("creating table: %s", err) } + _, err = db.Exec("PRAGMA synchronous = OFF;") + if err != nil { + log.Fatalf("setting risky writes: %s", err) + } + row := db.QueryRow(`SELECT count(*) as count from words`) var count int err = row.Scan(&count) diff --git a/setup.go b/setup.go index 47005e1..49aedd4 100644 --- a/setup.go +++ b/setup.go @@ -3,12 +3,13 @@ package main import ( "bufio" "database/sql" - "encoding/json" "fmt" "html/template" "log" "os" "strings" + + "github.com/goccy/go-json" ) type rawDictionaryEntry struct { @@ -65,10 +66,15 @@ func readDictionary(db *sql.DB) error { panic(err) } + tx, err := db.Begin() + if err != nil { + return fmt.Errorf("starting transaction: %w", err) + } + // Set up a prepared statement - stmt, err := db.Prepare("insert into words(word, definition) values(?, ?)") + stmt, err := tx.Prepare("insert into words(word, definition) values(?, ?)") if err != nil { - log.Fatal(err) + return fmt.Errorf("preparing statement: %w", err) } defer stmt.Close() @@ -141,6 +147,10 @@ func readDictionary(db *sql.DB) error { return fmt.Errorf("scanning: %w", err) } + if err := tx.Commit(); err != nil { + return fmt.Errorf("committing: %w", err) + } + log.Printf("prepared %d dictionary entries", wordsAdded) return nil -- cgit v1.2.3