/
database.go
185 lines (152 loc) · 3.94 KB
/
database.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package database
import (
"context"
"github.com/minkezhang/truffle/api/go/database/utils"
"github.com/minkezhang/truffle/client/mal"
"github.com/minkezhang/truffle/client/truffle"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
cpb "github.com/minkezhang/truffle/api/go/config"
dpb "github.com/minkezhang/truffle/api/go/database"
)
type DB struct {
truffle *truffle.C
config *cpb.Config
}
func New(db *truffle.C, config *cpb.Config) *DB {
return &DB{
truffle: db,
config: config,
}
}
func (db *DB) Add(ctx context.Context, epb *dpb.Entry) (*dpb.Entry, error) {
return db.truffle.Add(ctx, epb)
}
type GetOpts struct {
APIs []dpb.API
}
func (db *DB) Get(ctx context.Context, id *dpb.LinkedID, opts interface{}) (*dpb.Entry, error) {
query, ok := opts.(GetOpts)
if !ok {
return nil, status.Errorf(codes.InvalidArgument, "invalid search opts provided")
}
apis := map[dpb.API]bool{}
for _, api := range query.APIs {
apis[api] = true
}
closed := map[string]bool{}
open := []*dpb.LinkedID{id}
epb := &dpb.Entry{}
for len(open) > 0 {
id, open = open[0], open[1:]
if closed[utils.ID(id)] {
continue
}
closed[utils.ID(id)] = true
if f := map[dpb.API]func(context.Context, *dpb.LinkedID, interface{}) (*dpb.Entry, error){
dpb.API_API_TRUFFLE: db.truffle.Get,
dpb.API_API_MAL: mal.New(db.config.GetMal()).Get,
}[id.GetApi()]; f != nil {
if !apis[id.GetApi()] {
continue
}
fpb, err := f(ctx, id, nil)
if err != nil {
// We only consider user DB lookup errors to be
// fatal.
if id.GetApi() == dpb.API_API_TRUFFLE {
return nil, err
} else {
return epb, nil
}
}
// Do not override already populated important fields.
if epb.GetId() != nil {
fpb.Id = nil
}
if epb.GetScore() != 0 {
fpb.Score = 0
}
if epb.GetEtag() != nil {
fpb.Etag = nil
}
proto.Merge(epb, fpb)
open = append(open, fpb.GetLinkedIds()...)
} else {
return nil, status.Errorf(codes.InvalidArgument, "unsupported API specified for Get()")
}
}
return utils.Clean(epb), nil
}
func (db *DB) Put(ctx context.Context, epb *dpb.Entry) (*dpb.Entry, error) {
return db.truffle.Put(ctx, epb)
}
func (db *DB) Delete(ctx context.Context, id *dpb.LinkedID) (*dpb.Entry, error) {
return db.truffle.Delete(ctx, id)
}
type SearchOpts struct {
Title string
Corpus dpb.Corpus
APIs []dpb.API
// MAL contains MAL-specific API options.
MAL mal.SearchOpts
}
func (db *DB) Search(ctx context.Context, opts interface{}) ([]*dpb.Entry, error) {
query, ok := opts.(SearchOpts)
if !ok {
return nil, status.Errorf(codes.InvalidArgument, "invalid search opts provided")
}
apis := map[dpb.API]bool{}
for _, api := range query.APIs {
apis[api] = true
}
var candidates []*dpb.Entry
if apis[dpb.API_API_TRUFFLE] {
if cs, err := db.truffle.Search(
ctx,
truffle.SearchOpts{
Title: query.Title,
Corpus: query.Corpus,
},
); err != nil {
return nil, err
} else {
candidates = append(candidates, cs...)
}
}
duplicates := map[string]bool{}
for _, epb := range candidates {
for _, id := range epb.GetLinkedIds() {
duplicates[utils.ID(id)] = true
}
}
if apis[dpb.API_API_MAL] {
if cs, err := mal.New(db.config.GetMal()).Search(
ctx,
mal.SearchOpts{
Title: query.Title,
Corpus: query.Corpus,
},
); err != nil {
// Only return fatal if MAL is the only API being
// used; otherwise continue.
if len(apis) == 1 {
return nil, err
}
} else {
candidates = append(candidates, cs...)
}
}
var unique []*dpb.Entry
for _, epb := range candidates {
// Skip reporting entries which are already accounted for in the
// user DB (if we are returning user DB results).
if apis[dpb.API_API_TRUFFLE] && duplicates[utils.ID(epb.GetId())] {
continue
}
unique = append(unique, epb)
}
return unique, nil
}
func Marshal(db *DB) ([]byte, error) { return truffle.Marshal(db.truffle) }