-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
49 lines (42 loc) · 962 Bytes
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"github.com/atmatto/atylar"
"path/filepath"
)
type Storage struct {
Root string
UserStores map[string]atylar.Store
Reads chan NoteRead
Writes chan NoteWrite
}
func (s *Storage) LoadAll(usernames []string) error {
var err error
for _, u := range usernames {
s.UserStores[u], err = atylar.New(filepath.Join(s.Root, u))
if err != nil {
return fmt.Errorf("failed to initialize note storage for user \"%s\": %v", u, err)
}
}
return nil
}
func InitStorage(path string) Storage {
return Storage{Root: path, UserStores: make(map[string]atylar.Store)}
}
func (db *Database) StartStorageWorker() {
s := &db.storage
s.Reads = make(chan NoteRead)
s.Writes = make(chan NoteWrite)
go func() {
for {
select {
case read := <-s.Reads:
str, err := read.Execute(db)
read.resp <- NoteReadResp{str, err}
case write := <-s.Writes:
err := write.Execute(db)
write.resp <- err
}
}
}()
}