-
Notifications
You must be signed in to change notification settings - Fork 16
/
secondary.go
118 lines (107 loc) · 2.87 KB
/
secondary.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var (
readSecondaryFile = ioutil.ReadFile
writeSecondaryFile = ioutil.WriteFile
)
var uidFromSecondaryPath = regexp.MustCompile(`secondary\.([-a-z0-9]+)`)
// Secondary backend
type Secondary struct {
Path string
}
func uidFromSecondary(note Note, path string) (string, error) {
if note.UID != "" {
return note.UID, nil
}
match := uidFromSecondaryPath.FindStringSubmatch(path)
if len(match) != 2 {
return "", errors.Errorf("Unable to extract UID from secondary=%s", path)
}
return match[1], nil
}
func (db *Secondary) deleteByUID(uid string) error {
path := fmt.Sprintf("%s.secondary.%s.%d", db.Path, uid, time.Now().UnixNano())
err := writeSecondaryFile(path, make([]byte, 0), 0644)
log.Infof("Persist (delete) via secondary path=%s err=%v", path, err)
return err
}
func (db *Secondary) list() Notes {
var notes Notes
secondaryPaths, err := filepath.Glob(fmt.Sprintf("%s.secondary.*.*", db.Path))
if err != nil {
log.Errorf("Error trying to find secondary files err=%v", err)
return notes
}
for _, path := range secondaryPaths {
var note Note
v, err := readSecondaryFile(path)
if err != nil {
log.Errorf("Error trying to read secondary file err=%v", err)
continue
}
err = note.FromBytes(v)
if err != nil {
log.Errorf("Error trying to consume secondary file err=%v", err)
continue
}
note.AheadOfPrimary = true
note.SecondaryPath = path
uid, err := uidFromSecondary(note, path)
if err == nil {
note.UID = uid
}
notes = append(notes, note)
}
// Notes are assumed to be unsorted. It's up to the caller to sort
// according to their needs.
return notes
}
func (db *Secondary) update(note Note) (Note, error) {
note, err := Persistable(note)
if err != nil {
return note, err
}
b, err := note.ToBytes()
if err != nil {
return note, errors.Wrap(err, "Aborted prior to persist attempt")
}
path := fmt.Sprintf("%s.secondary.%s.%d", db.Path, note.UID, note.Time.UnixNano())
err = writeSecondaryFile(path, b, 0644)
log.Infof("Persist via secondary path=%s err=%v", path, err)
return note, err
}
func reloadAsNeeded(db Backend, frontend, backend *messenger) {
var last time.Time
stopCh := backend.add()
for _ = range time.NewTicker(time.Second * 2).C {
select {
case <-stopCh:
backend.close(stopCh)
log.Info("Database reloader stopped, goodbye!")
return
case <-time.After(time.Millisecond):
// As you were
}
fi, err := os.Stat(db.dbFilePath())
if err != nil {
log.Errorf("Unable to stat path=%s err=%v", db.dbFilePath(), err)
continue
}
mtime := fi.ModTime()
if !last.IsZero() && mtime.After(last) {
db, err = openBoltDB(db.dbFilePath(), *secondary)
log.Infof("Database reloaded due to upstream change err=%v", err)
frontend.send("reload")
}
last = mtime
}
}