pinconv.go 3.52 KB
Newer Older
1
// Package pinconv converts pins between the dag-based ldpinner and the
Andrew Gillis's avatar
Andrew Gillis committed
2 3 4 5 6 7 8 9
// datastore-based dspinner.  Once conversion is complete, the pins from the
// source pinner are removed.
package pinconv

import (
	"context"
	"fmt"

10 11 12 13 14 15
	cid "gitlab.dms3.io/dms3/go-cid"
	ds "gitlab.dms3.io/dms3/go-datastore"
	dms3pinner "gitlab.dms3.io/dms3/go-dms3-pinner"
	"gitlab.dms3.io/dms3/go-dms3-pinner/dspinner"
	"gitlab.dms3.io/dms3/go-dms3-pinner/ldpinner"
	ld "gitlab.dms3.io/dms3/go-ld-format"
Andrew Gillis's avatar
Andrew Gillis committed
16 17
)

18
// ConvertPinsFromLDToDS converts pins stored in mdag based storage to pins
Andrew Gillis's avatar
Andrew Gillis committed
19 20 21 22 23
// stores in the datastore.  Returns a dspinner loaded with the converted pins,
// and a count of the recursive and direct pins converted.
//
// After pins are stored in datastore, the root pin key is deleted to unlink
// the pin data in the DAGService.
24 25
func ConvertPinsFromLDToDS(ctx context.Context, dstore ds.Datastore, dserv ld.DAGService, internal ld.DAGService) (dms3pinner.Pinner, int, error) {
	const ldPinPath = "/local/pins"
Andrew Gillis's avatar
Andrew Gillis committed
26 27 28 29 30 31

	dsPinner, err := dspinner.New(ctx, dstore, dserv)
	if err != nil {
		return nil, 0, err
	}

32 33 34 35
	var convCount int
	keyChan := make(chan cid.Cid)

	go func() {
36
		err = ldpinner.LoadKeys(ctx, dstore, dserv, internal, true, keyChan)
37 38 39
		close(keyChan)
	}()
	for key := range keyChan {
40
		dsPinner.PinWithMode(key, dms3pinner.Recursive)
41
		convCount++
Andrew Gillis's avatar
Andrew Gillis committed
42
	}
43 44
	if err != nil {
		return nil, 0, fmt.Errorf("cannot load recursive keys: %s", err)
Andrew Gillis's avatar
Andrew Gillis committed
45 46
	}

47 48
	keyChan = make(chan cid.Cid)
	go func() {
49
		err = ldpinner.LoadKeys(ctx, dstore, dserv, internal, false, keyChan)
50 51 52
		close(keyChan)
	}()
	for key := range keyChan {
53
		dsPinner.PinWithMode(key, dms3pinner.Direct)
54
		convCount++
Andrew Gillis's avatar
Andrew Gillis committed
55
	}
56 57
	if err != nil {
		return nil, 0, fmt.Errorf("cannot load direct keys: %s", err)
Andrew Gillis's avatar
Andrew Gillis committed
58 59 60 61 62 63 64 65
	}

	err = dsPinner.Flush(ctx)
	if err != nil {
		return nil, 0, err
	}

	// Delete root mdag key from datastore to remove old pin storage.
66 67
	ldPinDatastoreKey := ds.NewKey(ldPinPath)
	if err = dstore.Delete(ldPinDatastoreKey); err != nil {
Andrew Gillis's avatar
Andrew Gillis committed
68 69
		return nil, 0, fmt.Errorf("cannot delete old pin state: %v", err)
	}
70
	if err = dstore.Sync(ldPinDatastoreKey); err != nil {
Andrew Gillis's avatar
Andrew Gillis committed
71 72 73 74 75 76
		return nil, 0, fmt.Errorf("cannot sync old pin state: %v", err)
	}

	return dsPinner, convCount, nil
}

77 78 79
// ConvertPinsFromDSToLD converts the pins stored in the datastore by
// dspinner, into pins stored in the given internal DAGService by ldpinner.
// Returns an ldpinner loaded with the converted pins, and a count of the
Andrew Gillis's avatar
Andrew Gillis committed
80 81 82 83
// recursive and direct pins converted.
//
// After the pins are stored in the DAGService, the pins and their indexes are
// removed from the dspinner.
84
func ConvertPinsFromDSToLD(ctx context.Context, dstore ds.Datastore, dserv ld.DAGService, internal ld.DAGService) (dms3pinner.Pinner, int, error) {
Andrew Gillis's avatar
Andrew Gillis committed
85 86 87 88 89
	dsPinner, err := dspinner.New(ctx, dstore, dserv)
	if err != nil {
		return nil, 0, err
	}

90
	ldPinner, err := ldpinner.New(dstore, dserv, internal)
Andrew Gillis's avatar
Andrew Gillis committed
91 92 93 94 95 96 97 98 99
	if err != nil {
		return nil, 0, err
	}

	cids, err := dsPinner.RecursiveKeys(ctx)
	if err != nil {
		return nil, 0, err
	}
	for i := range cids {
100 101
		ldPinner.PinWithMode(cids[i], dms3pinner.Recursive)
		dsPinner.RemovePinWithMode(cids[i], dms3pinner.Recursive)
Andrew Gillis's avatar
Andrew Gillis committed
102 103 104 105 106 107 108 109
	}
	convCount := len(cids)

	cids, err = dsPinner.DirectKeys(ctx)
	if err != nil {
		return nil, 0, err
	}
	for i := range cids {
110 111
		ldPinner.PinWithMode(cids[i], dms3pinner.Direct)
		dsPinner.RemovePinWithMode(cids[i], dms3pinner.Direct)
Andrew Gillis's avatar
Andrew Gillis committed
112 113 114
	}
	convCount += len(cids)

115 116
	// Save the ldpinner pins
	err = ldPinner.Flush(ctx)
Andrew Gillis's avatar
Andrew Gillis committed
117 118 119 120 121 122 123 124 125
	if err != nil {
		return nil, 0, err
	}

	err = dsPinner.Flush(ctx)
	if err != nil {
		return nil, 0, err
	}

126
	return ldPinner, convCount, nil
Andrew Gillis's avatar
Andrew Gillis committed
127
}