This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
forked from MichaelMure/go-ipfs-pinner
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpin.go
1031 lines (908 loc) · 24.3 KB
/
pin.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package dspinner implements structures and methods to keep track of
// which objects a user wants to keep stored locally. This implementation
// stores pin data in a datastore.
package dspinner
import (
"context"
"errors"
"fmt"
"path"
"sync"
"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-merkledag/dagutils"
"github.com/polydawn/refmt/cbor"
"github.com/polydawn/refmt/obj/atlas"
ipfspinner "github.com/ipfs/go-ipfs-pinner"
"github.com/ipfs/go-ipfs-pinner/dsindex"
)
const (
basePath = "/pins"
pinKeyPath = "/pins/pin"
indexKeyPath = "/pins/index"
dirtyKeyPath = "/pins/state/dirty"
)
var (
log logging.StandardLogger = logging.Logger("pin")
linkDirect, linkRecursive string
pinCidDIndexPath string
pinCidRIndexPath string
pinNameIndexPath string
dirtyKey = ds.NewKey(dirtyKeyPath)
pinAtl atlas.Atlas
)
func init() {
directStr, ok := ipfspinner.ModeToString(ipfspinner.Direct)
if !ok {
panic("could not find Direct pin enum")
}
linkDirect = directStr
recursiveStr, ok := ipfspinner.ModeToString(ipfspinner.Recursive)
if !ok {
panic("could not find Recursive pin enum")
}
linkRecursive = recursiveStr
pinCidRIndexPath = path.Join(indexKeyPath, "cidRindex")
pinCidDIndexPath = path.Join(indexKeyPath, "cidDindex")
pinNameIndexPath = path.Join(indexKeyPath, "nameIndex")
pinAtl = atlas.MustBuild(
atlas.BuildEntry(pin{}).StructMap().
AddField("Cid", atlas.StructMapEntry{SerialName: "cid"}).
AddField("Metadata", atlas.StructMapEntry{SerialName: "metadata", OmitEmpty: true}).
AddField("Mode", atlas.StructMapEntry{SerialName: "mode"}).
AddField("Name", atlas.StructMapEntry{SerialName: "name", OmitEmpty: true}).
Complete(),
atlas.BuildEntry(cid.Cid{}).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(func(live cid.Cid) ([]byte, error) { return live.MarshalBinary() })).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(func(serializable []byte) (cid.Cid, error) {
c := cid.Cid{}
err := c.UnmarshalBinary(serializable)
if err != nil {
return cid.Cid{}, err
}
return c, nil
})).Complete(),
)
pinAtl = pinAtl.WithMapMorphism(atlas.MapMorphism{KeySortMode: atlas.KeySortMode_Strings})
}
// pinner implements the Pinner interface
type pinner struct {
autoSync bool
lock sync.RWMutex
dserv ipld.DAGService
dstore ds.Datastore
cidDIndex dsindex.Indexer
cidRIndex dsindex.Indexer
nameIndex dsindex.Indexer
clean int64
dirty int64
}
var _ ipfspinner.Pinner = (*pinner)(nil)
type pin struct {
Id string
Cid cid.Cid
Metadata map[string]interface{}
Mode ipfspinner.Mode
Name string
}
func (p *pin) dsKey() ds.Key {
return ds.NewKey(path.Join(pinKeyPath, p.Id))
}
func newPin(c cid.Cid, mode ipfspinner.Mode, name string) *pin {
return &pin{
Id: path.Base(ds.RandomKey().String()),
Cid: c,
Name: name,
Mode: mode,
}
}
type syncDAGService interface {
ipld.DAGService
Sync() error
}
// New creates a new pinner and loads its keysets from the given datastore. If
// there is no data present in the datastore, then an empty pinner is returned.
//
// By default, changes are automatically flushed to the datastore. This can be
// disabled by calling SetAutosync(false), which will require that Flush be
// called explicitly.
func New(ctx context.Context, dstore ds.Datastore, dserv ipld.DAGService) (*pinner, error) {
p := &pinner{
autoSync: true,
cidDIndex: dsindex.New(dstore, ds.NewKey(pinCidDIndexPath)),
cidRIndex: dsindex.New(dstore, ds.NewKey(pinCidRIndexPath)),
nameIndex: dsindex.New(dstore, ds.NewKey(pinNameIndexPath)),
dserv: dserv,
dstore: dstore,
}
data, err := dstore.Get(ctx, dirtyKey)
if err != nil {
if err == ds.ErrNotFound {
return p, nil
}
return nil, fmt.Errorf("cannot load dirty flag: %v", err)
}
if data[0] == 1 {
p.dirty = 1
err = p.rebuildIndexes(ctx)
if err != nil {
return nil, fmt.Errorf("cannot rebuild indexes: %v", err)
}
}
return p, nil
}
// SetAutosync allows auto-syncing to be enabled or disabled during runtime.
// This may be used to turn off autosync before doing many repeated pinning
// operations, and then turn it on after. Returns the previous value.
func (p *pinner) SetAutosync(auto bool) bool {
p.lock.Lock()
defer p.lock.Unlock()
p.autoSync, auto = auto, p.autoSync
return auto
}
// Pin the given node, optionally recursive
func (p *pinner) Pin(ctx context.Context, node ipld.Node, recurse bool) error {
err := p.dserv.Add(ctx, node)
if err != nil {
return err
}
if recurse {
return p.doPinRecursive(ctx, node.Cid(), true)
} else {
return p.doPinDirect(ctx, node.Cid())
}
}
func (p *pinner) doPinRecursive(ctx context.Context, c cid.Cid, fetch bool) error {
cidKey := c.KeyString()
p.lock.Lock()
defer p.lock.Unlock()
found, err := p.cidRIndex.HasAny(ctx, cidKey)
if err != nil {
return err
}
if found {
return nil
}
dirtyBefore := p.dirty
if fetch {
// temporary unlock to fetch the entire graph
p.lock.Unlock()
// Fetch graph starting at node identified by cid
err = merkledag.FetchGraph(ctx, c, p.dserv)
p.lock.Lock()
if err != nil {
return err
}
}
// If autosyncing, sync dag service before making any change to pins
err = p.flushDagService(ctx, false)
if err != nil {
return err
}
// Only look again if something has changed.
if p.dirty != dirtyBefore {
found, err = p.cidRIndex.HasAny(ctx, cidKey)
if err != nil {
return err
}
if found {
return nil
}
}
// TODO: remove this to support multiple pins per CID
found, err = p.cidDIndex.HasAny(ctx, cidKey)
if err != nil {
return err
}
if found {
_, err = p.removePinsForCid(ctx, c, ipfspinner.Direct)
if err != nil {
return err
}
}
_, err = p.addPin(ctx, c, ipfspinner.Recursive, "")
if err != nil {
return err
}
return p.flushPins(ctx, false)
}
func (p *pinner) doPinDirect(ctx context.Context, c cid.Cid) error {
cidKey := c.KeyString()
p.lock.Lock()
defer p.lock.Unlock()
found, err := p.cidRIndex.HasAny(ctx, cidKey)
if err != nil {
return err
}
if found {
return fmt.Errorf("%s already pinned recursively", c.String())
}
_, err = p.addPin(ctx, c, ipfspinner.Direct, "")
if err != nil {
return err
}
return p.flushPins(ctx, false)
}
func (p *pinner) addPin(ctx context.Context, c cid.Cid, mode ipfspinner.Mode, name string) (string, error) {
// Create new pin and store in datastore
pp := newPin(c, mode, name)
// Serialize pin
pinData, err := encodePin(pp)
if err != nil {
return "", fmt.Errorf("could not encode pin: %v", err)
}
p.setDirty(ctx)
// Store the pin
err = p.dstore.Put(ctx, pp.dsKey(), pinData)
if err != nil {
return "", err
}
// Store CID index
switch mode {
case ipfspinner.Recursive:
err = p.cidRIndex.Add(ctx, c.KeyString(), pp.Id)
case ipfspinner.Direct:
err = p.cidDIndex.Add(ctx, c.KeyString(), pp.Id)
default:
panic("pin mode must be recursive or direct")
}
if err != nil {
return "", fmt.Errorf("could not add pin cid index: %v", err)
}
if name != "" {
// Store name index
err = p.nameIndex.Add(ctx, name, pp.Id)
if err != nil {
if mode == ipfspinner.Recursive {
e := p.cidRIndex.Delete(ctx, c.KeyString(), pp.Id)
if e != nil {
log.Errorf("error deleting index: %s", e)
}
} else {
e := p.cidDIndex.Delete(ctx, c.KeyString(), pp.Id)
if e != nil {
log.Errorf("error deleting index: %s", e)
}
}
return "", fmt.Errorf("could not add pin name index: %v", err)
}
}
return pp.Id, nil
}
func (p *pinner) removePin(ctx context.Context, pp *pin) error {
p.setDirty(ctx)
var err error
// Remove cid index from datastore
if pp.Mode == ipfspinner.Recursive {
err = p.cidRIndex.Delete(ctx, pp.Cid.KeyString(), pp.Id)
} else {
err = p.cidDIndex.Delete(ctx, pp.Cid.KeyString(), pp.Id)
}
if err != nil {
return err
}
if pp.Name != "" {
// Remove name index from datastore
err = p.nameIndex.Delete(ctx, pp.Name, pp.Id)
if err != nil {
return err
}
}
// The pin is removed last so that an incomplete remove is detected by a
// pin that has a missing index.
err = p.dstore.Delete(ctx, pp.dsKey())
if err != nil {
return err
}
return nil
}
// Unpin a given key
func (p *pinner) Unpin(ctx context.Context, c cid.Cid, recursive bool) error {
cidKey := c.KeyString()
p.lock.Lock()
defer p.lock.Unlock()
// TODO: use Ls() to lookup pins when new pinning API available
/*
matchSpec := map[string][]string {
"cid": []string{c.String}
}
matches := p.Ls(matchSpec)
*/
has, err := p.cidRIndex.HasAny(ctx, cidKey)
if err != nil {
return err
}
if has {
if !recursive {
return fmt.Errorf("%s is pinned recursively", c.String())
}
} else {
has, err = p.cidDIndex.HasAny(ctx, cidKey)
if err != nil {
return err
}
if !has {
return ipfspinner.ErrNotPinned
}
}
removed, err := p.removePinsForCid(ctx, c, ipfspinner.Any)
if err != nil {
return err
}
if !removed {
return nil
}
return p.flushPins(ctx, false)
}
// IsPinned returns whether or not the given key is pinned
// and an explanation of why its pinned
func (p *pinner) IsPinned(ctx context.Context, c cid.Cid) (string, bool, error) {
p.lock.RLock()
defer p.lock.RUnlock()
return p.isPinnedWithType(ctx, c, ipfspinner.Any)
}
// IsPinnedWithType returns whether or not the given cid is pinned with the
// given pin type, as well as returning the type of pin its pinned with.
func (p *pinner) IsPinnedWithType(ctx context.Context, c cid.Cid, mode ipfspinner.Mode) (string, bool, error) {
p.lock.RLock()
defer p.lock.RUnlock()
return p.isPinnedWithType(ctx, c, mode)
}
func (p *pinner) isPinnedWithType(ctx context.Context, c cid.Cid, mode ipfspinner.Mode) (string, bool, error) {
cidKey := c.KeyString()
switch mode {
case ipfspinner.Recursive:
has, err := p.cidRIndex.HasAny(ctx, cidKey)
if err != nil {
return "", false, err
}
if has {
return linkRecursive, true, nil
}
return "", false, nil
case ipfspinner.Direct:
has, err := p.cidDIndex.HasAny(ctx, cidKey)
if err != nil {
return "", false, err
}
if has {
return linkDirect, true, nil
}
return "", false, nil
case ipfspinner.Internal:
return "", false, nil
case ipfspinner.Indirect:
case ipfspinner.Any:
has, err := p.cidRIndex.HasAny(ctx, cidKey)
if err != nil {
return "", false, err
}
if has {
return linkRecursive, true, nil
}
has, err = p.cidDIndex.HasAny(ctx, cidKey)
if err != nil {
return "", false, err
}
if has {
return linkDirect, true, nil
}
default:
err := fmt.Errorf(
"invalid Pin Mode '%d', must be one of {%d, %d, %d, %d, %d}",
mode, ipfspinner.Direct, ipfspinner.Indirect, ipfspinner.Recursive,
ipfspinner.Internal, ipfspinner.Any)
return "", false, err
}
// Default is Indirect
visitedSet := cid.NewSet()
// No index for given CID, so search children of all recursive pinned CIDs
var has bool
var rc cid.Cid
var e error
err := p.cidRIndex.ForEach(ctx, "", func(key, value string) bool {
rc, e = cid.Cast([]byte(key))
if e != nil {
return false
}
has, e = hasChild(ctx, p.dserv, rc, c, visitedSet.Visit)
if e != nil {
return false
}
if has {
return false
}
return true
})
if err != nil {
return "", false, err
}
if e != nil {
return "", false, e
}
if has {
return rc.String(), true, nil
}
return "", false, nil
}
// CheckIfPinned checks if a set of keys are pinned, more efficient than
// calling IsPinned for each key, returns the pinned status of cid(s)
//
// TODO: If a CID is pinned by multiple pins, should they all be reported?
func (p *pinner) CheckIfPinned(ctx context.Context, cids ...cid.Cid) ([]ipfspinner.Pinned, error) {
pinned := make([]ipfspinner.Pinned, 0, len(cids))
toCheck := cid.NewSet()
p.lock.RLock()
defer p.lock.RUnlock()
// First check for non-Indirect pins directly
for _, c := range cids {
cidKey := c.KeyString()
has, err := p.cidRIndex.HasAny(ctx, cidKey)
if err != nil {
return nil, err
}
if has {
pinned = append(pinned, ipfspinner.Pinned{Key: c, Mode: ipfspinner.Recursive})
} else {
has, err = p.cidDIndex.HasAny(ctx, cidKey)
if err != nil {
return nil, err
}
if has {
pinned = append(pinned, ipfspinner.Pinned{Key: c, Mode: ipfspinner.Direct})
} else {
toCheck.Add(c)
}
}
}
var e error
visited := cid.NewSet()
err := p.cidRIndex.ForEach(ctx, "", func(key, value string) bool {
var rk cid.Cid
rk, e = cid.Cast([]byte(key))
if e != nil {
return false
}
e = merkledag.Walk(ctx, merkledag.GetLinksWithDAG(p.dserv), rk, func(c cid.Cid) bool {
if toCheck.Len() == 0 || !visited.Visit(c) {
return false
}
if toCheck.Has(c) {
pinned = append(pinned, ipfspinner.Pinned{Key: c, Mode: ipfspinner.Indirect, Via: rk})
toCheck.Remove(c)
}
return true
}, merkledag.Concurrent())
if e != nil {
return false
}
return toCheck.Len() > 0
})
if err != nil {
return nil, err
}
if e != nil {
return nil, e
}
// Anything left in toCheck is not pinned
for _, k := range toCheck.Keys() {
pinned = append(pinned, ipfspinner.Pinned{Key: k, Mode: ipfspinner.NotPinned})
}
return pinned, nil
}
// removePinsForCid removes all pins for a cid that has the specified mode.
// Returns true if any pins, and all corresponding CID index entries, were
// removed. Otherwise, returns false.
func (p *pinner) removePinsForCid(ctx context.Context, c cid.Cid, mode ipfspinner.Mode) (bool, error) {
// Search for pins by CID
var ids []string
var err error
cidKey := c.KeyString()
switch mode {
case ipfspinner.Recursive:
ids, err = p.cidRIndex.Search(ctx, cidKey)
case ipfspinner.Direct:
ids, err = p.cidDIndex.Search(ctx, cidKey)
case ipfspinner.Any:
ids, err = p.cidRIndex.Search(ctx, cidKey)
if err != nil {
return false, err
}
dIds, err := p.cidDIndex.Search(ctx, cidKey)
if err != nil {
return false, err
}
if len(dIds) != 0 {
ids = append(ids, dIds...)
}
}
if err != nil {
return false, err
}
var removed bool
// Remove the pin with the requested mode
for _, pid := range ids {
var pp *pin
pp, err = p.loadPin(ctx, pid)
if err != nil {
if err == ds.ErrNotFound {
p.setDirty(ctx)
// Fix index; remove index for pin that does not exist
switch mode {
case ipfspinner.Recursive:
_, err = p.cidRIndex.DeleteKey(ctx, cidKey)
if err != nil {
return false, fmt.Errorf("error deleting index: %s", err)
}
case ipfspinner.Direct:
_, err = p.cidDIndex.DeleteKey(ctx, cidKey)
if err != nil {
return false, fmt.Errorf("error deleting index: %s", err)
}
case ipfspinner.Any:
_, err = p.cidRIndex.DeleteKey(ctx, cidKey)
if err != nil {
return false, fmt.Errorf("error deleting index: %s", err)
}
_, err = p.cidDIndex.DeleteKey(ctx, cidKey)
if err != nil {
return false, fmt.Errorf("error deleting index: %s", err)
}
}
if err = p.flushPins(ctx, true); err != nil {
return false, err
}
// Mark this as removed since it removed an index, which is
// what prevents determines if an item is pinned.
removed = true
log.Error("found CID index with missing pin")
continue
}
return false, err
}
if mode == ipfspinner.Any || pp.Mode == mode {
err = p.removePin(ctx, pp)
if err != nil {
return false, err
}
removed = true
}
}
return removed, nil
}
// loadPin loads a single pin from the datastore.
func (p *pinner) loadPin(ctx context.Context, pid string) (*pin, error) {
pinData, err := p.dstore.Get(ctx, ds.NewKey(path.Join(pinKeyPath, pid)))
if err != nil {
return nil, err
}
return decodePin(pid, pinData)
}
// DirectKeys returns a slice containing the directly pinned keys
func (p *pinner) DirectKeys(ctx context.Context) ([]cid.Cid, error) {
p.lock.RLock()
defer p.lock.RUnlock()
cidSet := cid.NewSet()
var e error
err := p.cidDIndex.ForEach(ctx, "", func(key, value string) bool {
var c cid.Cid
c, e = cid.Cast([]byte(key))
if e != nil {
return false
}
cidSet.Add(c)
return true
})
if err != nil {
return nil, err
}
if e != nil {
return nil, e
}
return cidSet.Keys(), nil
}
// RecursiveKeys returns a slice containing the recursively pinned keys
func (p *pinner) RecursiveKeys(ctx context.Context) ([]cid.Cid, error) {
p.lock.RLock()
defer p.lock.RUnlock()
cidSet := cid.NewSet()
var e error
err := p.cidRIndex.ForEach(ctx, "", func(key, value string) bool {
var c cid.Cid
c, e = cid.Cast([]byte(key))
if e != nil {
return false
}
cidSet.Add(c)
return true
})
if err != nil {
return nil, err
}
if e != nil {
return nil, e
}
return cidSet.Keys(), nil
}
// InternalPins returns all cids kept pinned for the internal state of the
// pinner
func (p *pinner) InternalPins(ctx context.Context) ([]cid.Cid, error) {
return nil, nil
}
// Update updates a recursive pin from one cid to another. This is equivalent
// to pinning the new one and unpinning the old one.
//
// TODO: This will not work when multiple pins are supported
func (p *pinner) Update(ctx context.Context, from, to cid.Cid, unpin bool) error {
p.lock.Lock()
defer p.lock.Unlock()
found, err := p.cidRIndex.HasAny(ctx, from.KeyString())
if err != nil {
return err
}
if !found {
return errors.New("'from' cid was not recursively pinned already")
}
// If `from` already recursively pinned and `to` is the same, then all done
if from == to {
return nil
}
// Check if the `to` cid is already recursively pinned
found, err = p.cidRIndex.HasAny(ctx, to.KeyString())
if err != nil {
return err
}
if found {
return errors.New("'to' cid was already recursively pinned")
}
// Temporarily unlock while we fetch the differences.
p.lock.Unlock()
err = dagutils.DiffEnumerate(ctx, p.dserv, from, to)
p.lock.Lock()
if err != nil {
return err
}
_, err = p.addPin(ctx, to, ipfspinner.Recursive, "")
if err != nil {
return err
}
if unpin {
_, err = p.removePinsForCid(ctx, from, ipfspinner.Recursive)
if err != nil {
return err
}
}
return p.flushPins(ctx, false)
}
func (p *pinner) flushDagService(ctx context.Context, force bool) error {
if !p.autoSync && !force {
return nil
}
if syncDServ, ok := p.dserv.(syncDAGService); ok {
if err := syncDServ.Sync(); err != nil {
return fmt.Errorf("cannot sync pinned data: %v", err)
}
}
return nil
}
func (p *pinner) flushPins(ctx context.Context, force bool) error {
if !p.autoSync && !force {
return nil
}
if err := p.dstore.Sync(ctx, ds.NewKey(basePath)); err != nil {
return fmt.Errorf("cannot sync pin state: %v", err)
}
p.setClean(ctx)
return nil
}
// Flush encodes and writes pinner keysets to the datastore
func (p *pinner) Flush(ctx context.Context) error {
p.lock.Lock()
defer p.lock.Unlock()
err := p.flushDagService(ctx, true)
if err != nil {
return err
}
return p.flushPins(ctx, true)
}
// PinWithMode allows the user to have fine grained control over pin
// counts
func (p *pinner) PinWithMode(ctx context.Context, c cid.Cid, mode ipfspinner.Mode) error {
// TODO: remove his to support multiple pins per CID
switch mode {
case ipfspinner.Recursive:
return p.doPinRecursive(ctx, c, false)
case ipfspinner.Direct:
return p.doPinDirect(ctx, c)
default:
return fmt.Errorf("unrecognized pin mode")
}
}
// hasChild recursively looks for a Cid among the children of a root Cid.
// The visit function can be used to shortcut already-visited branches.
func hasChild(ctx context.Context, ng ipld.NodeGetter, root cid.Cid, child cid.Cid, visit func(cid.Cid) bool) (bool, error) {
links, err := ipld.GetLinks(ctx, ng, root)
if err != nil {
return false, err
}
for _, lnk := range links {
c := lnk.Cid
if lnk.Cid.Equals(child) {
return true, nil
}
if visit(c) {
has, err := hasChild(ctx, ng, c, child, visit)
if err != nil {
return false, err
}
if has {
return has, nil
}
}
}
return false, nil
}
func encodePin(p *pin) ([]byte, error) {
b, err := cbor.MarshalAtlased(p, pinAtl)
if err != nil {
return nil, err
}
return b, nil
}
func decodePin(pid string, data []byte) (*pin, error) {
p := &pin{Id: pid}
err := cbor.UnmarshalAtlased(cbor.DecodeOptions{}, data, p, pinAtl)
if err != nil {
return nil, err
}
return p, nil
}
// setDirty updates the dirty counter and saves a dirty state in the datastore
// if the state was previously clean
func (p *pinner) setDirty(ctx context.Context) {
wasClean := p.dirty == p.clean
p.dirty++
if !wasClean {
return // do not save; was already dirty
}
data := []byte{1}
err := p.dstore.Put(ctx, dirtyKey, data)
if err != nil {
log.Errorf("failed to set pin dirty flag: %s", err)
return
}
err = p.dstore.Sync(ctx, dirtyKey)
if err != nil {
log.Errorf("failed to sync pin dirty flag: %s", err)
}
}
// setClean saves a clean state value in the datastore if the state was
// previously dirty
func (p *pinner) setClean(ctx context.Context) {
if p.dirty == p.clean {
return // already clean
}
data := []byte{0}
err := p.dstore.Put(ctx, dirtyKey, data)
if err != nil {
log.Errorf("failed to set clear dirty flag: %s", err)
return
}
if err = p.dstore.Sync(ctx, dirtyKey); err != nil {
log.Errorf("failed to sync cleared pin dirty flag: %s", err)
return
}
p.clean = p.dirty // set clean
}
// sync datastore after every 50 cid repairs
const syncRepairFrequency = 50
// rebuildIndexes uses the stored pins to rebuild secondary indexes. This
// resolves any discrepancy between secondary indexes and pins that could
// result from a program termination between saving the two.
func (p *pinner) rebuildIndexes(ctx context.Context) error {
// Load all pins from the datastore.
q := query.Query{
Prefix: pinKeyPath,
}
results, err := p.dstore.Query(ctx, q)
if err != nil {
return err
}
defer results.Close()
var checkedCount, repairedCount int
// Iterate all pins and check if the corresponding recursive or direct
// index is missing. If the index is missing then create the index.
for r := range results.Next() {
if ctx.Err() != nil {
return ctx.Err()
}
if r.Error != nil {
return fmt.Errorf("cannot read index: %v", r.Error)
}
ent := r.Entry
pp, err := decodePin(path.Base(ent.Key), ent.Value)
if err != nil {
return err
}
indexKey := pp.Cid.KeyString()
var indexer, staleIndexer dsindex.Indexer
var idxrName, staleIdxrName string
if pp.Mode == ipfspinner.Recursive {
indexer = p.cidRIndex
staleIndexer = p.cidDIndex
idxrName = linkRecursive
staleIdxrName = linkDirect
} else if pp.Mode == ipfspinner.Direct {
indexer = p.cidDIndex
staleIndexer = p.cidRIndex
idxrName = linkDirect
staleIdxrName = linkRecursive
} else {
log.Error("unrecognized pin mode:", pp.Mode)
continue
}
// Remove any stale index from unused indexer
ok, err := staleIndexer.HasValue(ctx, indexKey, pp.Id)
if err != nil {
return err
}
if ok {
// Delete any stale index
log.Errorf("deleting stale %s pin index for cid %v", staleIdxrName, pp.Cid.String())
if err = staleIndexer.Delete(ctx, indexKey, pp.Id); err != nil {
return err
}
}
// Check that the indexer indexes this pin
ok, err = indexer.HasValue(ctx, indexKey, pp.Id)
if err != nil {
return err
}
var repaired bool
if !ok {
// Do not rebuild if index has an old value with leading slash
ok, err = indexer.HasValue(ctx, indexKey, "/"+pp.Id)
if err != nil {
return err
}
if !ok {
log.Errorf("repairing %s pin index for cid: %s", idxrName, pp.Cid.String())
// There was no index found for this pin. This was either an
// incomplete add or and incomplete delete of a pin. Either
// way, restore the index to complete the add or to undo the
// incomplete delete.
if err = indexer.Add(ctx, indexKey, pp.Id); err != nil {
return err