Problem: block repo did not serialize writes
Solution: serialize writes
This commit is contained in:
parent
1acd0e5f0f
commit
19183da0f8
43
block.go
43
block.go
|
@ -25,23 +25,56 @@ import (
|
|||
)
|
||||
|
||||
type BlockRepository interface {
|
||||
Save(block Block) error
|
||||
Save(id string, block Block) error
|
||||
Load(id string) (Block, error)
|
||||
}
|
||||
|
||||
type blockSaveMessage struct {
|
||||
id string
|
||||
block Block
|
||||
}
|
||||
|
||||
type blockRepo struct {
|
||||
dirname string
|
||||
saveC chan blockSaveMessage
|
||||
errC chan error
|
||||
}
|
||||
|
||||
func (br *blockRepo) Save(id string, block Block) error {
|
||||
f, err := os.OpenFile(filepath.Join(br.dirname, BlocksDirectory, id), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
defer f.Close()
|
||||
func saveBlock(dirname, id string, block Block) error {
|
||||
f, err := os.OpenFile(filepath.Join(dirname, BlocksDirectory, id), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
enc := json.NewEncoder(f)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(&block)
|
||||
err = enc.Encode(&block)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewBlockRepo(dirname string) BlockRepository {
|
||||
saveC := make(chan blockSaveMessage, 1)
|
||||
errC := make(chan error)
|
||||
go func() {
|
||||
for msg := range saveC {
|
||||
err := saveBlock(dirname, msg.id, msg.block)
|
||||
errC <- err
|
||||
}
|
||||
}()
|
||||
return &blockRepo{
|
||||
dirname: dirname,
|
||||
saveC: saveC,
|
||||
errC: errC,
|
||||
}
|
||||
}
|
||||
|
||||
func (br *blockRepo) Save(id string, block Block) error {
|
||||
br.saveC <- blockSaveMessage{id, block}
|
||||
err := <-br.errC
|
||||
return err
|
||||
}
|
||||
|
||||
func (br *blockRepo) Load(id string) (Block, error) {
|
||||
|
|
6
main.go
6
main.go
|
@ -1064,6 +1064,7 @@ func main() {
|
|||
}
|
||||
|
||||
mp = NewFilePages(dataDir, searchIndex)
|
||||
repo := NewBlockRepo("data")
|
||||
|
||||
http.Handle("/auth/", &authHandler{})
|
||||
http.HandleFunc("/api/block/view", wrapAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -1081,7 +1082,6 @@ func main() {
|
|||
|
||||
id := r.URL.Query().Get("id")
|
||||
|
||||
repo := blockRepo{dirname: "data"}
|
||||
block, err := repo.Load(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
|
@ -1165,7 +1165,6 @@ func main() {
|
|||
http.Error(w, "missing id", 400)
|
||||
return
|
||||
}
|
||||
repo := blockRepo{dirname: "data"}
|
||||
block, err := repo.Load(id)
|
||||
block.Text = r.Form.Get("text")
|
||||
err = repo.Save(id, block)
|
||||
|
@ -1196,9 +1195,6 @@ func main() {
|
|||
http.Error(w, "missing id", 400)
|
||||
return
|
||||
}
|
||||
repo := blockRepo{
|
||||
dirname: "data",
|
||||
}
|
||||
newBlock := Block{
|
||||
Text: r.Form.Get("text"),
|
||||
Children: []string{},
|
||||
|
|
Loading…
Reference in New Issue
Block a user