/
fetch.go
157 lines (128 loc) · 2.95 KB
/
fetch.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package protavobolt
import (
bolt "github.com/coreos/bbolt"
"github.com/jmalloc/protavo/src/protavo/driver"
"github.com/jmalloc/protavo/src/protavo/filter"
"github.com/jmalloc/protavo/src/protavobolt/internal/database"
)
// executeFetch calls fn for each document that matches f.
func executeFetch(
tx *bolt.Tx,
ns string,
f *filter.Filter,
fn driver.FetchFunc,
) error {
s, ok, err := database.OpenStore(tx, ns)
if !ok || err != nil {
return err
}
return selectStrategy(s, f).Fetch(fn)
}
// applyFetch executes the side-effects of a fetch operation.
func applyFetch(
s *database.Store,
id string,
rec *database.Record,
fn driver.FetchFunc,
) (bool, error) {
c, err := s.GetContent(id)
if err != nil {
return false, err
}
doc, err := newDocument(id, rec, c)
if err != nil {
return false, err
}
return fn(doc)
}
// Fetch is the implementation of the "no-op" strategy for fetching.
func (*noop) Fetch(fn driver.FetchFunc) error {
return nil
}
// Fetch is the implementation of the "scan records" strategy for fetching.
func (qs *scanRecords) Fetch(fn driver.FetchFunc) error {
cur := qs.store.Records.Cursor()
for k, v := cur.First(); k != nil; k, v = cur.Next() {
rec, err := database.UnmarshalRecord(v)
if err != nil {
return err
}
id := string(k)
if !isFilterSatisfiedByRecord(qs.filter, id, rec) {
continue
}
ok, err := applyFetch(qs.store, id, rec, fn)
if !ok || err != nil {
return err
}
}
return nil
}
// Fetch is the implementation of the "use document ID first" strategy for
// fetching.
func (qs *useIDFirst) Fetch(fn driver.FetchFunc) error {
for id := range qs.conds.ExtractIsOneOf().Values {
rec, exists, err := qs.store.TryGetRecord(id)
if err != nil {
return err
}
if !exists {
continue
}
if !qs.conds.AreSatisfiedBy(id, rec) {
continue
}
ok, err := applyFetch(qs.store, id, rec, fn)
if !ok || err != nil {
return err
}
}
return nil
}
// Fetch is the implementation of the "use unique key first" strategy for
// fetching.
func (qs *useUniqueKeyFirst) Fetch(fn driver.FetchFunc) error {
for key := range qs.conds.ExtractHasUniqueKeyIn().Values {
k, err := qs.store.GetKey(key)
if err != nil {
return err
}
id, ok := k.GetUniqueDocumentID()
if !ok {
continue
}
rec, err := qs.store.GetRecord(id)
if err != nil {
return err
}
if !qs.conds.AreSatisfiedBy(id, rec) {
continue
}
ok, err = applyFetch(qs.store, id, rec, fn)
if !ok || err != nil {
return err
}
}
return nil
}
// Fetch is the implementation of the "use keys first" strategy for fetching.
func (qs *useKeysFirst) Fetch(fn driver.FetchFunc) error {
ids, err := qs.findDocumentIDs()
if err != nil {
return err
}
for id := range ids {
rec, err := qs.store.GetRecord(id)
if err != nil {
return err
}
if !qs.conds.AreSatisfiedBy(id, rec) {
continue
}
ok, err := applyFetch(qs.store, id, rec, fn)
if !ok || err != nil {
return err
}
}
return nil
}