-
Notifications
You must be signed in to change notification settings - Fork 66
/
dscache.go
460 lines (430 loc) · 14.4 KB
/
dscache.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package dscache
import (
"context"
"fmt"
"io/ioutil"
"strings"
"time"
flatbuffers "github.com/google/flatbuffers/go"
golog "github.com/ipfs/go-log"
"github.com/qri-io/dataset"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/dscache/dscachefb"
"github.com/qri-io/qri/dsref"
"github.com/qri-io/qri/event"
"github.com/qri-io/qri/profile"
reporef "github.com/qri-io/qri/repo/ref"
)
var (
log = golog.Logger("dscache")
// lengthOfProfileID is the expected length of a valid profileID
lengthOfProfileID = 46
// ErrNoDscache is returned when methods are called on a non-existant Dscache
ErrNoDscache = fmt.Errorf("dscache: does not exist")
// ErrInvalidProfileID is returned when an invalid profileID is given to dscache
ErrInvalidProfileID = fmt.Errorf("invalid profileID")
)
// Dscache represents an in-memory serialized dscache flatbuffer
type Dscache struct {
Filename string
Root *dscachefb.Dscache
Buffer []byte
CreateNewEnabled bool
ProfileIDToUsername map[string]string
DefaultUsername string
}
// NewDscache will construct a dscache from the given filename, or will construct an empty dscache
// that will save to the given filename. Using an empty filename will disable loading and saving
func NewDscache(ctx context.Context, fsys qfs.Filesystem, bus event.Bus, username, filename string) *Dscache {
cache := Dscache{Filename: filename}
f, err := fsys.Get(ctx, filename)
if err == nil {
// Ignore error, as dscache loading is optional
defer f.Close()
buffer, err := ioutil.ReadAll(f)
if err != nil {
log.Error(err)
} else {
root := dscachefb.GetRootAsDscache(buffer, 0)
cache = Dscache{Filename: filename, Root: root, Buffer: buffer}
}
}
cache.DefaultUsername = username
bus.SubscribeTypes(cache.handler,
event.ETDatasetNameInit,
event.ETDatasetCommitChange,
event.ETDatasetDeleteAll,
event.ETDatasetRename,
event.ETDatasetCreateLink)
return &cache
}
// IsEmpty returns whether the dscache has any constructed data in it
func (d *Dscache) IsEmpty() bool {
if d == nil {
return true
}
return d.Root == nil
}
// Assign assigns the data from one dscache to this one
func (d *Dscache) Assign(other *Dscache) error {
if d == nil {
return ErrNoDscache
}
d.Root = other.Root
d.Buffer = other.Buffer
return d.save()
}
// VerboseString is a convenience function that returns a readable string, for testing and debugging
func (d *Dscache) VerboseString(showEmpty bool) string {
if d.IsEmpty() {
return "dscache: cannot not stringify an empty dscache"
}
out := strings.Builder{}
out.WriteString("Dscache:\n")
out.WriteString(" Dscache.Users:\n")
for i := 0; i < d.Root.UsersLength(); i++ {
userAssoc := dscachefb.UserAssoc{}
d.Root.Users(&userAssoc, i)
username := userAssoc.Username()
profileID := userAssoc.ProfileID()
fmt.Fprintf(&out, " %2d) user=%s profileID=%s\n", i, username, profileID)
}
out.WriteString(" Dscache.Refs:\n")
for i := 0; i < d.Root.RefsLength(); i++ {
r := dscachefb.RefEntryInfo{}
d.Root.Refs(&r, i)
fmt.Fprintf(&out, ` %2d) initID = %s
profileID = %s
topIndex = %d
cursorIndex = %d
prettyName = %s
`, i, r.InitID(), r.ProfileID(), r.TopIndex(), r.CursorIndex(), r.PrettyName())
indent := " "
if len(r.MetaTitle()) != 0 || showEmpty {
fmt.Fprintf(&out, "%smetaTitle = %s\n", indent, r.MetaTitle())
}
if len(r.ThemeList()) != 0 || showEmpty {
fmt.Fprintf(&out, "%sthemeList = %s\n", indent, r.ThemeList())
}
if r.BodySize() != 0 || showEmpty {
fmt.Fprintf(&out, "%sbodySize = %d\n", indent, r.BodySize())
}
if r.BodyRows() != 0 || showEmpty {
fmt.Fprintf(&out, "%sbodyRows = %d\n", indent, r.BodyRows())
}
if r.CommitTime() != 0 || showEmpty {
fmt.Fprintf(&out, "%scommitTime = %d\n", indent, r.CommitTime())
}
if r.NumErrors() != 0 || showEmpty {
fmt.Fprintf(&out, "%snumErrors = %d\n", indent, r.NumErrors())
}
if len(r.HeadRef()) != 0 || showEmpty {
fmt.Fprintf(&out, "%sheadRef = %s\n", indent, r.HeadRef())
}
if len(r.FsiPath()) != 0 || showEmpty {
fmt.Fprintf(&out, "%sfsiPath = %s\n", indent, r.FsiPath())
}
}
return out.String()
}
// ListRefs returns references to each dataset in the cache
func (d *Dscache) ListRefs() ([]reporef.DatasetRef, error) {
if d.IsEmpty() {
return nil, ErrNoDscache
}
d.ensureProToUserMap()
refs := make([]reporef.DatasetRef, 0, d.Root.RefsLength())
for i := 0; i < d.Root.RefsLength(); i++ {
refCache := dscachefb.RefEntryInfo{}
d.Root.Refs(&refCache, i)
proIDStr := string(refCache.ProfileID())
profileID, err := profile.NewB58ID(proIDStr)
if err != nil {
log.Errorf("could not parse profileID %q", proIDStr)
}
username, ok := d.ProfileIDToUsername[proIDStr]
if !ok {
log.Errorf("no username associated with profileID %q", proIDStr)
}
refs = append(refs, reporef.DatasetRef{
Peername: username,
ProfileID: profileID,
Name: string(refCache.PrettyName()),
Path: string(refCache.HeadRef()),
FSIPath: string(refCache.FsiPath()),
Dataset: &dataset.Dataset{
Meta: &dataset.Meta{
Title: string(refCache.MetaTitle()),
},
Structure: &dataset.Structure{
ErrCount: int(refCache.NumErrors()),
Entries: int(refCache.BodyRows()),
Length: int(refCache.BodySize()),
},
Commit: &dataset.Commit{},
NumVersions: int(refCache.TopIndex()),
},
})
}
return refs, nil
}
// ResolveRef finds the identifier for a dataset reference
// implements dsref.Resolver interface
func (d *Dscache) ResolveRef(ctx context.Context, ref *dsref.Ref) (string, error) {
// NOTE: isEmpty is nil-callable. important b/c ResolveRef must be nil-callable
if d.IsEmpty() {
return "", dsref.ErrRefNotFound
}
vi, err := d.LookupByName(*ref)
if err != nil {
return "", dsref.ErrRefNotFound
}
ref.InitID = vi.InitID
ref.ProfileID = vi.ProfileID
if ref.Path == "" {
ref.Path = vi.Path
}
return "", nil
}
// LookupByName looks up a dataset by dsref and returns the latest VersionInfo if found
func (d *Dscache) LookupByName(ref dsref.Ref) (*dsref.VersionInfo, error) {
// Convert the username into a profileID
for i := 0; i < d.Root.UsersLength(); i++ {
userAssoc := dscachefb.UserAssoc{}
d.Root.Users(&userAssoc, i)
username := userAssoc.Username()
profileID := userAssoc.ProfileID()
if ref.Username == string(username) {
// TODO(dustmop): Switch off of profileID to a stable ID (that handle key rotations)
// based upon the Logbook creation of a user's profile.
ref.ProfileID = string(profileID)
break
}
}
if ref.ProfileID == "" {
return nil, fmt.Errorf("unknown username %q", ref.Username)
}
// Lookup the info, given the profileID/dsname
for i := 0; i < d.Root.RefsLength(); i++ {
r := dscachefb.RefEntryInfo{}
d.Root.Refs(&r, i)
if string(r.ProfileID()) == ref.ProfileID && string(r.PrettyName()) == ref.Name {
info := convertEntryToVersionInfo(&r)
return &info, nil
}
}
return nil, fmt.Errorf("dataset ref not found %s/%s", ref.Username, ref.Name)
}
func (d *Dscache) validateProfileID(profileID string) bool {
return len(profileID) == lengthOfProfileID
}
func (d *Dscache) handler(_ context.Context, e event.Event) error {
act, ok := e.Payload.(event.DsChange)
if !ok {
log.Error("dscache got an event with a payload that isn't a event.DsChange type: %v", e.Payload)
return nil
}
switch e.Type {
case event.ETDatasetNameInit:
if err := d.updateInitDataset(act); err != nil && err != ErrNoDscache {
log.Error(err)
}
case event.ETDatasetCommitChange:
if err := d.updateChangeCursor(act); err != nil && err != ErrNoDscache {
log.Error(err)
}
case event.ETDatasetDeleteAll:
if err := d.updateDeleteDataset(act); err != nil && err != ErrNoDscache {
log.Error(err)
}
case event.ETDatasetRename:
// TODO(dustmop): Handle renames
case event.ETDatasetCreateLink:
if err := d.updateCreateLink(act); err != nil && err != ErrNoDscache {
log.Error(err)
}
}
return nil
}
func (d *Dscache) updateInitDataset(act event.DsChange) error {
if d.IsEmpty() {
// Only create a new dscache if that feature is enabled. This way no one is forced to
// use dscache without opting in.
if !d.CreateNewEnabled {
return nil
}
if !d.validateProfileID(act.ProfileID) {
return ErrInvalidProfileID
}
builder := NewBuilder()
builder.AddUser(act.Username, act.ProfileID)
builder.AddDsVersionInfo(dsref.VersionInfo{
InitID: act.InitID,
ProfileID: act.ProfileID,
Name: act.PrettyName,
})
cache := builder.Build()
d.Assign(cache)
return nil
}
builder := NewBuilder()
// copy users
for i := 0; i < d.Root.UsersLength(); i++ {
up := dscachefb.UserAssoc{}
d.Root.Users(&up, i)
builder.AddUser(string(up.Username()), string(up.ProfileID()))
}
// copy ds versions
for i := 0; i < d.Root.UsersLength(); i++ {
r := dscachefb.RefEntryInfo{}
d.Root.Refs(&r, i)
builder.AddDsVersionInfoWithIndexes(convertEntryToVersionInfo(&r), int(r.TopIndex()), int(r.CursorIndex()))
}
// Add new ds version info
builder.AddDsVersionInfo(dsref.VersionInfo{
InitID: act.InitID,
ProfileID: act.ProfileID,
Name: act.PrettyName,
})
cache := builder.Build()
d.Assign(cache)
return nil
}
// Copy the entire dscache, except for the matching entry, rebuild that one to modify it
func (d *Dscache) updateChangeCursor(act event.DsChange) error {
if d.IsEmpty() {
return ErrNoDscache
}
// Flatbuffers for go do not allow mutation (for complex types like strings). So we construct
// a new flatbuffer entirely, copying the old one while replacing the entry we care to change.
builder := flatbuffers.NewBuilder(0)
users := d.copyUserAssociationList(builder)
refs := d.copyReferenceListWithReplacement(
builder,
// Function to match the entry we're looking to replace
func(r *dscachefb.RefEntryInfo) bool {
return string(r.InitID()) == act.InitID
},
// Function to replace the matching entry
func(refStartMutationFunc func(builder *flatbuffers.Builder)) {
var metaTitle flatbuffers.UOffsetT
if act.Info != nil {
metaTitle = builder.CreateString(act.Info.MetaTitle)
}
hashRef := builder.CreateString(string(act.HeadRef))
// Start building a ref object, by mutating an existing ref object.
refStartMutationFunc(builder)
// Add only the fields we want to change.
dscachefb.RefEntryInfoAddTopIndex(builder, int32(act.TopIndex))
dscachefb.RefEntryInfoAddCursorIndex(builder, int32(act.TopIndex))
if act.Info != nil {
dscachefb.RefEntryInfoAddMetaTitle(builder, metaTitle)
dscachefb.RefEntryInfoAddCommitTime(builder, act.Info.CommitTime.Unix())
dscachefb.RefEntryInfoAddBodySize(builder, int64(act.Info.BodySize))
dscachefb.RefEntryInfoAddBodyRows(builder, int32(act.Info.BodyRows))
dscachefb.RefEntryInfoAddNumErrors(builder, int32(act.Info.NumErrors))
}
dscachefb.RefEntryInfoAddHeadRef(builder, hashRef)
// Don't call RefEntryInfoEnd, that is handled by copyReferenceListWithReplacement
},
)
root, serialized := d.finishBuilding(builder, users, refs)
d.Root = root
d.Buffer = serialized
return d.save()
}
// Copy the entire dscache, except leave out the matching entry.
func (d *Dscache) updateDeleteDataset(act event.DsChange) error {
if d.IsEmpty() {
return ErrNoDscache
}
// Flatbuffers for go do not allow mutation (for complex types like strings). So we construct
// a new flatbuffer entirely, copying the old one while omitting the entry we want to remove.
builder := flatbuffers.NewBuilder(0)
users := d.copyUserAssociationList(builder)
refs := d.copyReferenceListWithReplacement(
builder,
func(r *dscachefb.RefEntryInfo) bool {
return string(r.InitID()) == act.InitID
},
// Pass a nil function, so the matching entry is not replaced, it is omitted
nil,
)
root, serialized := d.finishBuilding(builder, users, refs)
d.Root = root
d.Buffer = serialized
return d.save()
}
// Copy the entire dscache, except for the matching entry, which is copied then assigned an fsiPath
func (d *Dscache) updateCreateLink(act event.DsChange) error {
if d.IsEmpty() {
return ErrNoDscache
}
// Flatbuffers for go do not allow mutation (for complex types like strings). So we construct
// a new flatbuffer entirely, copying the old one while replacing the entry we care to change.
builder := flatbuffers.NewBuilder(0)
users := d.copyUserAssociationList(builder)
refs := d.copyReferenceListWithReplacement(
builder,
// Function to match the entry we're looking to replace
func(r *dscachefb.RefEntryInfo) bool {
if act.InitID != "" {
return string(r.InitID()) == act.InitID
}
return d.DefaultUsername == act.Username && string(r.PrettyName()) == act.PrettyName
},
// Function to replace the matching entry
func(refStartMutationFunc func(builder *flatbuffers.Builder)) {
fsiDir := builder.CreateString(string(act.Dir))
// Start building a ref object, by mutating an existing ref object.
refStartMutationFunc(builder)
// For this kind of update, only the fsiDir is modified
dscachefb.RefEntryInfoAddFsiPath(builder, fsiDir)
// Don't call RefEntryInfoEnd, that is handled by copyReferenceListWithReplacement
},
)
root, serialized := d.finishBuilding(builder, users, refs)
d.Root = root
d.Buffer = serialized
return d.save()
}
func convertEntryToVersionInfo(r *dscachefb.RefEntryInfo) dsref.VersionInfo {
return dsref.VersionInfo{
InitID: string(r.InitID()),
ProfileID: string(r.ProfileID()),
Name: string(r.PrettyName()),
Path: string(r.HeadRef()),
Published: r.Published(),
Foreign: r.Foreign(),
MetaTitle: string(r.MetaTitle()),
ThemeList: string(r.ThemeList()),
BodySize: int(r.BodySize()),
BodyRows: int(r.BodyRows()),
BodyFormat: string(r.BodyFormat()),
NumErrors: int(r.NumErrors()),
CommitTime: time.Unix(r.CommitTime(), 0),
NumVersions: int(r.NumVersions()),
FSIPath: string(r.FsiPath()),
}
}
func (d *Dscache) ensureProToUserMap() {
if d.ProfileIDToUsername != nil {
return
}
d.ProfileIDToUsername = make(map[string]string)
for i := 0; i < d.Root.UsersLength(); i++ {
userAssoc := dscachefb.UserAssoc{}
d.Root.Users(&userAssoc, i)
username := userAssoc.Username()
profileID := userAssoc.ProfileID()
d.ProfileIDToUsername[string(profileID)] = string(username)
}
}
// save writes the serialized bytes to the given filename
func (d *Dscache) save() error {
if d.Filename == "" {
log.Infof("dscache: no filename set, will not save")
return nil
}
return ioutil.WriteFile(d.Filename, d.Buffer, 0644)
}