Move block code to BlockRepository

single-block-api
Peter Stuifzand 3 years ago
parent f086d9d9f9
commit 764c67a707

@ -0,0 +1,158 @@
/*
* Wiki - A wiki with editor
* Copyright (c) 2021 Peter Stuifzand
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"sync"
)
const BlocksDirectory = "_blocks"
type FileBlockRepository struct {
dirname string
rwLock sync.RWMutex
}
type Block struct {
Text string
Children []string
Parent string
}
func NewBlockRepo(dirname string) (*FileBlockRepository, error) {
err := os.MkdirAll(filepath.Join(dirname, BlocksDirectory), 0777)
if err != nil {
return nil, err
}
return &FileBlockRepository{
dirname: dirname,
}, nil
}
func (blockRepo *FileBlockRepository) GetBlock(blockID string) (Block, error) {
blockRepo.rwLock.RLock()
defer blockRepo.rwLock.RUnlock()
return loadBlock(blockRepo.dirname, blockID)
}
func (blockRepo *FileBlockRepository) SaveBlock(blockID string, block Block) error {
blockRepo.rwLock.Lock()
defer blockRepo.rwLock.Unlock()
return saveBlock(blockRepo.dirname, blockID, block)
}
func (blockRepo *FileBlockRepository) GetBlocks(rootBlockID string) (BlockResponse, error) {
resp := BlockResponse{
rootBlockID,
"",
nil,
nil,
nil,
}
resp.Texts = make(map[string]string)
resp.Children = make(map[string][]string)
queue := []string{rootBlockID}
block, err := blockRepo.GetBlock(rootBlockID)
if err != nil {
return BlockResponse{}, err
}
// NOTE: what does this do?
if rootBlockID[0] != '_' && block.Children == nil {
return BlockResponse{}, fmt.Errorf("not a block and has no children: %w", BlockNotFound)
}
prevID := rootBlockID
parentID := block.Parent
for parentID != "root" {
parent, err := blockRepo.GetBlock(parentID)
if err != nil {
return BlockResponse{}, fmt.Errorf("while loading current parent block (%s->%s): %w", prevID, parentID, err)
}
resp.Texts[parentID] = parent.Text
resp.Children[parentID] = parent.Children
resp.ParentID = parentID
resp.Parents = append(resp.Parents, parentID)
prevID = parentID
parentID = parent.Parent
}
if parentID == "root" {
resp.ParentID = "root"
}
for {
if len(queue) == 0 {
break
}
current := queue[0]
queue = queue[1:]
block, err := blockRepo.GetBlock(current)
if err != nil {
return BlockResponse{}, err
}
resp.Texts[current] = block.Text
resp.Children[current] = block.Children
queue = append(queue, block.Children...)
}
return resp, nil
}
func loadBlock(dirname, blockID string) (Block, error) {
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, blockID))
if err != nil {
return Block{}, fmt.Errorf("%q: %w", blockID, BlockNotFound)
}
defer f.Close()
var block Block
err = json.NewDecoder(f).Decode(&block)
if err != nil {
return Block{}, fmt.Errorf("%q: %v", blockID, err)
}
return block, nil
}
func saveBlock(dirname, blockID string, block Block) error {
log.Printf("Writing to %q\n", blockID)
f, err := os.OpenFile(filepath.Join(dirname, BlocksDirectory, blockID), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("while saving block %s: %w", blockID, err)
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
err = enc.Encode(&block)
if err != nil {
return fmt.Errorf("while encoding block %s: %w", blockID, err)
}
return nil
}

@ -40,7 +40,6 @@ import (
const (
DocumentsFile = "_documents.json"
LinksFile = "_links.json"
BlocksDirectory = "_blocks"
)
var BlockNotFound = errors.New("block not found")
@ -52,12 +51,6 @@ type saveMessage struct {
author string
}
type Block struct {
Text string
Children []string
Parent string
}
// ListItemV2 is way to convert from old structure to new structure
type ListItemV2 struct {
ID ID
@ -95,6 +88,8 @@ type FilePages struct {
dirname string
saveC chan saveMessage
index bleve.Index
blockRepo BlockRepository
}
type BlockResponse struct {
@ -105,13 +100,18 @@ type BlockResponse struct {
Parents []string
}
func NewFilePages(dirname string, index bleve.Index) PagesRepository {
err := os.MkdirAll(filepath.Join(dirname, "_blocks"), 0777)
func NewFilePages(dirname string, index bleve.Index) *FilePages {
blockRepo, err := NewBlockRepo(dirname)
if err != nil {
log.Fatalln(err)
log.Fatal(err)
}
fp := &FilePages{dirname, make(chan saveMessage), index}
fp := &FilePages{
dirname: dirname,
saveC: make(chan saveMessage),
index: index,
blockRepo: blockRepo,
}
go func() {
for msg := range fp.saveC {
@ -174,7 +174,7 @@ func (fp *FilePages) Get(name string) Page {
}
for _, name := range names {
blocks, err := loadBlocks(fp.dirname, name)
blocks, err := fp.blockRepo.GetBlocks(name)
if err != nil && errors.Is(err, BlockNotFound) {
continue
}
@ -289,7 +289,7 @@ func (fp *FilePages) save(msg saveMessage) error {
}
sw.Start("create blocks")
err := saveBlocksFromPage(fp.dirname, page)
err := fp.saveBlocksFromPage(fp.dirname, page)
if err != nil {
log.Println(err)
}
@ -333,7 +333,7 @@ func (fp *FilePages) save(msg saveMessage) error {
sw.Stop()
sw.Start("index")
searchObjects, err := createSearchObjects(page.Name)
searchObjects, err := createSearchObjects(fp, page.Name)
if err != nil {
return fmt.Errorf("while creating search object %s: %w", page.Name, err)
}
@ -368,7 +368,7 @@ func saveWithNewIDs(dirname string, listItems []ListItemV2, pageName string) ([]
return newListItems, nil
}
func saveBlocksFromPage(dirname string, page Page) error {
func (fp* FilePages) saveBlocksFromPage(dirname string, page Page) error {
log.Printf("Processing: %q\n", page.Name)
var listItems []ListItem
err := json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems)
@ -386,7 +386,7 @@ func saveBlocksFromPage(dirname string, page Page) error {
prevList := make(map[string]ListItem)
root := "root"
parentBlock, err := loadBlock(dirname, page.Name)
parentBlock, err := fp.blockRepo.GetBlock(page.Name)
if err != nil {
log.Println(err)
} else {
@ -446,7 +446,7 @@ func saveBlocksFromPage(dirname string, page Page) error {
prev = &listItems[i]
}
// TODO: found out if this is still necessary
// TODO: find out if this is still necessary
log.Printf("Loading parent block: %q", rootParentID)
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, rootParentID))
if err == nil {
@ -485,83 +485,6 @@ func saveBlocksFromPage(dirname string, page Page) error {
return err
}
func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) {
resp := BlockResponse{
rootBlockID,
"",
nil,
nil,
nil,
}
resp.Texts = make(map[string]string)
resp.Children = make(map[string][]string)
queue := []string{rootBlockID}
block, err := loadBlock(dirname, rootBlockID)
if err != nil {
return BlockResponse{}, err
}
// NOTE: what does this do?
if rootBlockID[0] != '_' && block.Children == nil {
return BlockResponse{}, fmt.Errorf("not a block and has no children: %w", BlockNotFound)
}
prevID := rootBlockID
parentID := block.Parent
for parentID != "root" {
parent, err := loadBlock(dirname, parentID)
if err != nil {
return BlockResponse{}, fmt.Errorf("while loading current parent block (%s->%s): %w", prevID, parentID, err)
}
resp.Texts[parentID] = parent.Text
resp.Children[parentID] = parent.Children
resp.ParentID = parentID
resp.Parents = append(resp.Parents, parentID)
prevID = parentID
parentID = parent.Parent
}
if parentID == "root" {
resp.ParentID = "root"
}
for {
if len(queue) == 0 {
break
}
current := queue[0]
queue = queue[1:]
block, err := loadBlock(dirname, current)
if err != nil {
return BlockResponse{}, err
}
resp.Texts[current] = block.Text
resp.Children[current] = block.Children
queue = append(queue, block.Children...)
}
return resp, nil
}
func loadBlock(dirname, blockID string) (Block, error) {
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, blockID))
if err != nil {
return Block{}, fmt.Errorf("%q: %w", blockID, BlockNotFound)
}
defer f.Close()
var block Block
err = json.NewDecoder(f).Decode(&block)
if err != nil {
return Block{}, fmt.Errorf("%q: %v", blockID, err)
}
return block, nil
}
func saveLinksIncremental(dirname, title string) error {
type Document struct {
Title string `json:"title"`

@ -52,8 +52,14 @@ var (
authKey = authorizedKey("authorizedKey")
)
type BlockRepository interface {
GetBlock(id string) (Block, error)
GetBlocks(rootBlockID string) (BlockResponse, error)
SaveBlock(id string, block Block) error
}
var (
mp PagesRepository
mp PagesRepository
port = flag.Int("port", 8080, "listen port")
baseurl = flag.String("baseurl", "", "baseurl")
@ -1106,6 +1112,7 @@ func main() {
}
}
}))
http.HandleFunc("/api/block/update", wrapAuth(func(w http.ResponseWriter, r *http.Request) {}))
http.HandleFunc("/api/block/append", wrapAuth(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
@ -1287,7 +1294,7 @@ func createSearchIndex(dataDir, indexName string) (bleve.Index, error) {
}
for _, page := range pages {
searchObjects, err := createSearchObjects(page.Name)
searchObjects, err := createSearchObjects(fp, page.Name)
if err != nil {
log.Println(err)
continue

@ -115,7 +115,7 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
for _, page := range pages {
err = saveBlocksFromPage("data", page)
err = mp.saveBlocksFromPage("data", page)
if err != nil {
log.Printf("error while processing blocks from page %s: %v", page.Name, err)
continue
@ -168,7 +168,7 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
for _, page := range pages {
searchObjects, err := createSearchObjects(page.Name)
searchObjects, err := createSearchObjects(mp, page.Name)
if err != nil {
log.Printf("error while creating search object %s: %v", page.Title, err)
continue
@ -246,8 +246,8 @@ func (p pageBlock) Type() string {
return "block"
}
func createSearchObjects(rootBlockID string) ([]pageBlock, error) {
blocks, err := loadBlocks("data", rootBlockID)
func createSearchObjects(fp *FilePages, rootBlockID string) ([]pageBlock, error) {
blocks, err := fp.blockRepo.GetBlocks(rootBlockID)
if err != nil {
return nil, err
}

Loading…
Cancel
Save