Migrate categories

This commit is contained in:
Timur Ismagilov 2024-09-07 22:36:35 +03:00
parent 98469b3c49
commit b9b1476505
2 changed files with 16 additions and 13 deletions

View File

@ -2,7 +2,7 @@ package categories
import ( import (
"encoding/json" "encoding/json"
"log" "log/slog"
"os" "os"
"slices" "slices"
"sort" "sort"
@ -16,12 +16,11 @@ var categoryToHyphae = map[string]*categoryNode{}
var hyphaToCategories = map[string]*hyphaNode{} var hyphaToCategories = map[string]*hyphaNode{}
// Init initializes the category system. Call it after the Structure is initialized. This function might terminate the program in case of a bad mood or filesystem faults. // Init initializes the category system. Call it after the Structure is initialized. This function might terminate the program in case of a bad mood or filesystem faults.
func Init() { func Init() error {
var ( record, err := readCategoriesFromDisk()
record, err = readCategoriesFromDisk()
)
if err != nil { if err != nil {
log.Fatalln(err) slog.Error("Failed to read categories from disk", "err", err)
return err
} }
for _, cat := range record.Categories { for _, cat := range record.Categories {
@ -46,7 +45,8 @@ func Init() {
} }
} }
log.Println("Found", len(categoryToHyphae), "categories") slog.Info("Indexed categories", "n", len(categoryToHyphae))
return nil
} }
type categoryNode struct { type categoryNode struct {
@ -123,9 +123,7 @@ func readCategoriesFromDisk() (catFileRecord, error) {
var fileMutex sync.Mutex var fileMutex sync.Mutex
func saveToDisk() { func saveToDisk() {
var ( var record catFileRecord
record catFileRecord
)
for name, node := range categoryToHyphae { for name, node := range categoryToHyphae {
record.Categories = append(record.Categories, catRecord{ record.Categories = append(record.Categories, catRecord{
Name: name, Name: name,
@ -134,13 +132,16 @@ func saveToDisk() {
} }
data, err := json.MarshalIndent(record, "", "\t") data, err := json.MarshalIndent(record, "", "\t")
if err != nil { if err != nil {
log.Fatalln(err) // Better fail now, than later slog.Error("Failed to marshal categories record", "err", err)
os.Exit(1) // Better fail now, than later
} }
// TODO: make the data safer somehow?? Back it up before overwriting? // TODO: make the data safer somehow?? Back it up before overwriting?
fileMutex.Lock() fileMutex.Lock()
err = os.WriteFile(files.CategoriesJSON(), data, 0666) err = os.WriteFile(files.CategoriesJSON(), data, 0666)
if err != nil { if err != nil {
log.Fatalln(err) slog.Error("Failed to write categories.json", "err", err)
os.Exit(1)
} }
fileMutex.Unlock() fileMutex.Unlock()
} }

View File

@ -53,7 +53,9 @@ func main() {
migration.MigrateRocketsMaybe() migration.MigrateRocketsMaybe()
migration.MigrateHeadingsMaybe() migration.MigrateHeadingsMaybe()
shroom.SetHeaderLinks() shroom.SetHeaderLinks()
categories.Init() if err := categories.Init(); err != nil {
os.Exit(1)
}
if err := interwiki.Init(); err != nil { if err := interwiki.Init(); err != nil {
os.Exit(1) os.Exit(1)
} }