-
Notifications
You must be signed in to change notification settings - Fork 1
/
retriever.go
109 lines (96 loc) · 2.72 KB
/
retriever.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
package storage
import (
"fmt"
indexfield "github.com/xichen2020/eventdb/index/field"
"github.com/xichen2020/eventdb/index/segment"
"github.com/xichen2020/eventdb/persist"
xerrors "github.com/m3db/m3/src/x/errors"
)
// updatingSegmentRetriever retrieves fields from persistent storage, and updates
// the corresponding segment with the retrieved fields so they no longer need to
// be retrieved for future queries.
type updatingSegmentRetriever struct {
namespace []byte
dbSegment *dbSegment
retriever persist.FieldRetriever
}
func newUpdatingSegmentRetriever(
namespace []byte,
dbSegment *dbSegment,
retriever persist.FieldRetriever,
) *updatingSegmentRetriever {
return &updatingSegmentRetriever{
namespace: namespace,
dbSegment: dbSegment,
retriever: retriever,
}
}
func (r *updatingSegmentRetriever) Retrieve(
seg segment.ImmutableSegment,
toRetrieve []persist.RetrieveFieldOptions,
) ([]indexfield.DocsField, error) {
if len(toRetrieve) == 0 {
return nil, nil
}
// NB: If no error, len(retrieved) == len(toRetrieve).
retrieved, err := r.retrieve(seg.Metadata(), toRetrieve)
if err != nil {
return nil, err
}
if err := r.update(seg, retrieved, toRetrieve); err != nil {
for i := range retrieved {
if retrieved[i] != nil {
retrieved[i].Close()
retrieved[i] = nil
}
}
return nil, err
}
return retrieved, nil
}
func (r *updatingSegmentRetriever) retrieve(
segmentMeta segment.Metadata,
toRetrieve []persist.RetrieveFieldOptions,
) ([]indexfield.DocsField, error) {
res := make([]indexfield.DocsField, 0, len(toRetrieve))
for _, retrieveOpts := range toRetrieve {
// NB(xichen): This assumes that the loaded field is never nil if err == nil.
retrieved, err := r.retriever.RetrieveField(r.namespace, segmentMeta, retrieveOpts)
if err != nil {
for i := range res {
res[i].Close()
res[i] = nil
}
return nil, err
}
res = append(res, retrieved)
}
return res, nil
}
// Precondition: len(retrieved) == len(retrieveOpts).
// Postcondition: If no error, `fields` contains and owns the fields loaded for `metas`,
// and should be closed when processing is done.
func (r *updatingSegmentRetriever) update(
seg segment.ImmutableSegment,
retrieved []indexfield.DocsField,
retrieveOptsList []persist.RetrieveFieldOptions,
) error {
var (
multiErr xerrors.MultiError
updated bool
)
for i, retrieveOpts := range retrieveOptsList {
f, exists := seg.FieldAt(retrieveOpts.FieldPath)
if !exists {
err := fmt.Errorf("field %v loaded but does not exist in the segment", retrieveOpts.FieldPath)
multiErr = multiErr.Add(err)
continue
}
f.Insert(retrieved[i])
updated = true
}
if updated {
r.dbSegment.setLoadedStatus(segmentPartiallyLoaded)
}
return multiErr.FinalError()
}