Prepare for trying multiple names

This commit is contained in:
Peter Stuifzand 2021-01-17 14:55:10 +01:00
parent 4d79405e22
commit 36dab44a24

55
file.go
View File

@ -4,16 +4,15 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"html" "html"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"time" "time"
@ -27,6 +26,8 @@ const (
BlocksDirectory = "_blocks" BlocksDirectory = "_blocks"
) )
var BlockNotFound = errors.New("block not found")
type saveMessage struct { type saveMessage struct {
p string p string
page Page page Page
@ -80,6 +81,14 @@ type FilePages struct {
index bleve.Index index bleve.Index
} }
type BlockResponse struct {
PageID string
ParentID string
Texts map[string]string
Children map[string][]string
Parents []string
}
func NewFilePages(dirname string, index bleve.Index) PagesRepository { func NewFilePages(dirname string, index bleve.Index) PagesRepository {
err := os.MkdirAll(filepath.Join(dirname, "_blocks"), 0777) err := os.MkdirAll(filepath.Join(dirname, "_blocks"), 0777)
if err != nil { if err != nil {
@ -118,18 +127,21 @@ func convertBlocksToListItems(current string, blocks BlockResponse, indent int)
return listItems return listItems
} }
func (fp *FilePages) Get(title string) Page { func (fp *FilePages) Get(name string) Page {
var sw stopwatch var sw stopwatch
sw.Start("Get " + title) sw.Start("Get " + name)
defer sw.Stop() defer sw.Stop()
blocks, err := loadBlocks(fp.dirname, title) names := []string{name}
if err != nil {
return fp.oldPagesBackend(title, blocks)
}
// Blocks based pages for _, name := range names {
return fp.blocksBackendGet(title, blocks) blocks, err := loadBlocks(fp.dirname, name)
if err != nil && errors.Is(err, BlockNotFound) {
continue
}
return fp.blocksBackendGet(name, blocks)
}
return fp.oldPagesBackend(name)
} }
func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page { func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page {
@ -160,7 +172,7 @@ func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page {
} }
} }
func (fp *FilePages) oldPagesBackend(title string, blocks BlockResponse) Page { func (fp *FilePages) oldPagesBackend(title string) Page {
name := strings.Replace(title, " ", "_", -1) name := strings.Replace(title, " ", "_", -1)
title = strings.Replace(title, "_", " ", -1) title = strings.Replace(title, "_", " ", -1)
@ -177,7 +189,6 @@ func (fp *FilePages) oldPagesBackend(title string, blocks BlockResponse) Page {
Name: name, Name: name,
Content: "", Content: "",
Refs: refs, Refs: refs,
Blocks: blocks,
} }
} }
@ -190,7 +201,6 @@ func (fp *FilePages) oldPagesBackend(title string, blocks BlockResponse) Page {
Title: title, Title: title,
Content: "", Content: "",
Refs: refs, Refs: refs,
Blocks: blocks,
} }
} }
@ -199,7 +209,6 @@ func (fp *FilePages) oldPagesBackend(title string, blocks BlockResponse) Page {
Title: title, Title: title,
Content: string(body), Content: string(body),
Refs: refs, Refs: refs,
Blocks: blocks,
} }
} }
@ -409,13 +418,6 @@ func saveBlocksFromPage(dirname string, page Page) error {
return err return err
} }
type BlockResponse struct {
PageID string
ParentID string
Texts map[string]string
Children map[string][]string
Parents []string
}
func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) { func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) {
resp := BlockResponse{ resp := BlockResponse{
@ -433,10 +435,11 @@ func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) {
block, err := loadBlock(dirname, rootBlockID) block, err := loadBlock(dirname, rootBlockID)
if err != nil { if err != nil {
return BlockResponse{}, fmt.Errorf("while loading current block (%s): %w", rootBlockID, err) return BlockResponse{}, err
} }
// NOTE: what does this do?
if rootBlockID[0] != '_' && block.Children == nil { if rootBlockID[0] != '_' && block.Children == nil {
return BlockResponse{}, fmt.Errorf("while loading current block (%s): not a block and no children", rootBlockID) return BlockResponse{}, fmt.Errorf("not a block and has no children: %w", BlockNotFound)
} }
prevID := rootBlockID prevID := rootBlockID
@ -469,7 +472,7 @@ func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) {
queue = queue[1:] queue = queue[1:]
block, err := loadBlock(dirname, current) block, err := loadBlock(dirname, current)
if err != nil { if err != nil {
return BlockResponse{}, fmt.Errorf("while loading block (%s): %w", current, err) return BlockResponse{}, err
} }
resp.Texts[current] = block.Text resp.Texts[current] = block.Text
resp.Children[current] = block.Children resp.Children[current] = block.Children
@ -482,13 +485,13 @@ func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) {
func loadBlock(dirname, blockID string) (Block, error) { func loadBlock(dirname, blockID string) (Block, error) {
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, blockID)) f, err := os.Open(filepath.Join(dirname, BlocksDirectory, blockID))
if err != nil { if err != nil {
return Block{}, err return Block{}, fmt.Errorf("%q: %w", BlockNotFound)
} }
defer f.Close() defer f.Close()
var block Block var block Block
err = json.NewDecoder(f).Decode(&block) err = json.NewDecoder(f).Decode(&block)
if err != nil { if err != nil {
return Block{}, err return Block{}, fmt.Errorf("%q: %v", err)
} }
return block, nil return block, nil
} }