-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
299 lines (265 loc) · 8.83 KB
/
collection.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package lib
import (
"context"
"errors"
"fmt"
"strings"
"github.com/affix-io/affix/base"
"github.com/affix-io/affix/base/params"
"github.com/affix-io/affix/dscache/build"
"github.com/affix-io/affix/dsref"
qhttp "github.com/affix-io/affix/lib/http"
"github.com/affix-io/affix/profile"
reporef "github.com/affix-io/affix/repo/ref"
)
// CollectionMethods lists a user's datasets. Datasets in a collection consist
// of datasets the user has created and other datasets the user has pulled.
//
// Collections are local. The same user's collection on one affix node will
// often be different from another node, depending on what datasets have been
// created, pushed, or pulled to that node
type CollectionMethods struct {
d dispatcher
}
// Name returns the name of this method group
func (m CollectionMethods) Name() string {
return "collection"
}
// Attributes defines attributes for each method
func (m CollectionMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"list": {Endpoint: qhttp.AEList, HTTPVerb: "POST"},
"listrawrefs": {Endpoint: qhttp.DenyHTTP},
"get": {Endpoint: qhttp.AECollectionGet, HTTPVerb: "POST"},
}
}
// ErrListWarning is a warning that can occur while listing
var ErrListWarning = base.ErrUnlistableReferences
// CollectionListParams defines parameters for listing a user's collection
type CollectionListParams struct {
params.List
Username string `json:"username,omitempty"`
Public bool `json:"public,omitempty"`
Term string `json:"term,omitempty"`
}
// SetNonZeroDefaults sets OrderBy to "created" if it's value is empty
func (p *CollectionListParams) SetNonZeroDefaults() {
if len(p.OrderBy) == 0 {
p.List = p.List.WithOrderBy("created")
}
if p.Offset < 0 {
p.Offset = 0
}
if p.Limit <= 0 {
p.Limit = params.DefaultListLimit
}
}
// List gets the reflist for either the local repo or a peer
func (m CollectionMethods) List(ctx context.Context, p *CollectionListParams) ([]dsref.VersionInfo, Cursor, error) {
got, cur, err := m.d.Dispatch(ctx, dispatchMethodName(m, "list"), p)
if res, ok := got.([]dsref.VersionInfo); ok {
return res, cur, err
}
return nil, nil, dispatchReturnError(got, err)
}
// ListRawRefs gets the list of raw references as string
func (m CollectionMethods) ListRawRefs(ctx context.Context, p *EmptyParams) (string, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "listrawrefs"), p)
if res, ok := got.(string); ok {
return res, err
}
return "", dispatchReturnError(got, err)
}
// CollectionGetParams defines parameters for looking up the head of a dataset from the collection
type CollectionGetParams struct {
Ref string `json:"ref"`
InitID string `json:"initID"`
}
// Validate returns an error if CollectionGetParams fields are in an invalid state
func (p *CollectionGetParams) Validate() error {
if p.Ref == "" && p.InitID == "" {
return fmt.Errorf("either ref or initID are required")
}
return nil
}
// Get gets the head of a dataset as a VersionInfo from the collection
func (m CollectionMethods) Get(ctx context.Context, p *CollectionGetParams) (*dsref.VersionInfo, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "get"), p)
if res, ok := got.(*dsref.VersionInfo); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// collectionImpl holds the method implementations for CollectionMethods
type collectionImpl struct{}
// List gets the reflist for either the local repo or a peer
func (collectionImpl) List(scope scope, p *CollectionListParams) ([]dsref.VersionInfo, Cursor, error) {
if s := scope.CollectionSet(); s != nil {
id := scope.ActiveProfile().ID
if p.Username != "" {
pro, err := getProfile(scope.Context(), scope.Profiles(), "", p.Username)
if err != nil {
return nil, nil, err
}
id = pro.ID
}
infos, err := s.List(scope.ctx, id, p.List)
if err != nil {
return nil, nil, err
}
// Create a cursor that points to the next page of results
// A cursor is simply the current input params to this method, tweaked such that
// they get the next page of results
p.Offset += p.Limit
cur := scope.MakeCursor(len(infos), p)
return infos, cur, nil
}
// TODO(dustmop): When List is converted to use scope, get the ProfileID from
// the scope if the user is authorized to only view their own datasets, as opposed
// to the full collection that exists in this node's repository.
restrictPid := ""
// ensure valid limit value
if p.Limit <= 0 {
p.Limit = 25
}
// ensure valid offset value
if p.Offset < 0 {
p.Offset = 0
}
reqProfile := scope.Repo().Profiles().Owner(scope.Context())
listProfile, err := getProfile(scope.Context(), scope.Repo().Profiles(), reqProfile.ID.Encode(), p.Username)
if err != nil {
return nil, nil, err
}
// If the list operation leads to a warning, store it in this var
var listWarning error
var infos []dsref.VersionInfo
if scope.UseDscache() {
c := scope.Dscache()
if c.IsEmpty() {
log.Infof("building dscache from repo's logbook, profile, and dsref")
built, err := build.DscacheFromRepo(scope.Context(), scope.Repo())
if err != nil {
return nil, nil, err
}
err = c.Assign(built)
if err != nil {
log.Error(err)
}
}
refs, err := c.ListRefs()
if err != nil {
return nil, nil, err
}
// Filter references so that only with a matching name are returned
if p.Term != "" {
matched := make([]reporef.DatasetRef, len(refs))
count := 0
for _, ref := range refs {
if strings.Contains(ref.AliasString(), p.Term) {
matched[count] = ref
count++
}
}
refs = matched[:count]
}
// Filter references by skipping to the correct offset
if p.Offset > len(refs) {
refs = []reporef.DatasetRef{}
} else {
refs = refs[p.Offset:]
}
// Filter references by limiting how many are returned
if p.Limit < len(refs) {
refs = refs[:p.Limit]
}
// Convert old style DatasetRef list to VersionInfo list.
// TODO(dustmop): Remove this and convert lower-level functions to return []VersionInfo.
infos = make([]dsref.VersionInfo, len(refs))
for i, r := range refs {
infos[i] = reporef.ConvertToVersionInfo(&r)
}
} else if listProfile.Peername == "" || reqProfile.Peername == listProfile.Peername {
infos, err = base.ListDatasets(scope.Context(), scope.Repo(), p.Term, restrictPid, p.Offset, p.Limit, p.Public, true)
if errors.Is(err, ErrListWarning) {
// This warning can happen when there's conflicts between usernames and
// profileIDs. This type of conflict should not break listing functionality.
// Instead, store the warning and treat it as non-fatal.
listWarning = err
err = nil
}
} else {
return nil, nil, fmt.Errorf("listing datasets on a peer is not implemented")
}
if err != nil {
return nil, nil, err
}
// Create a cursor that points to the next page of results
// A cursor is simply the current input params to this method, tweaked such that
// they get the next page of results
p.Offset += p.Limit
cur := scope.MakeCursor(len(infos), p)
if listWarning != nil {
// If there was a warning listing the datasets, we should still return the list
// itself. The caller should handle this warning by simply printing it, but this
// shouldn't break the `list` functionality.
return infos, cur, listWarning
}
return infos, cur, nil
}
func getProfile(ctx context.Context, pros profile.Store, idStr, peername string) (pro *profile.Profile, err error) {
if idStr == "" {
// TODO(b5): we're handling the "me" keyword here, should be handled as part of
// request scope construction
if peername == "me" {
return pros.Owner(ctx), nil
}
return profile.ResolveUsername(ctx, pros, peername)
}
id, err := profile.IDB58Decode(idStr)
if err != nil {
log.Debugw("decoding profile ID", "err", err)
return nil, err
}
return pros.GetProfile(ctx, id)
}
// ListRawRefs gets the list of raw references as string
func (collectionImpl) ListRawRefs(scope scope, p *EmptyParams) (string, error) {
text := ""
if scope.UseDscache() {
c := scope.Dscache()
if c == nil || c.IsEmpty() {
return "", fmt.Errorf("repo: dscache not found")
}
text = c.VerboseString(true)
return text, nil
}
return base.RawDatasetRefs(scope.Context(), scope.ActiveProfile().ID, scope.CollectionSet())
}
// Get gets the head of a dataset as a VersionInfo from the collection
func (collectionImpl) Get(scope scope, p *CollectionGetParams) (*dsref.VersionInfo, error) {
s := scope.CollectionSet()
if s == nil {
return nil, fmt.Errorf("no collection")
}
var err error
ref := dsref.Ref{
InitID: p.InitID,
}
if ref.InitID != "" {
_, err = scope.ResolveReference(scope.Context(), &ref)
if err != nil {
return nil, err
}
} else {
ref, _, err = scope.ParseAndResolveRef(scope.Context(), p.Ref)
if err != nil {
return nil, err
}
}
id, err := profile.IDB58Decode(ref.ProfileID)
if err != nil {
return nil, err
}
return s.Get(scope.Context(), id, ref.InitID)
}