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
/
pin.go
182 lines (155 loc) · 5.48 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
// Package pin implements structures and methods to keep track of
// which objects a user wants to keep stored locally.
package pin
import (
"context"
"fmt"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
)
const (
linkRecursive = "recursive"
linkDirect = "direct"
linkIndirect = "indirect"
linkInternal = "internal"
linkNotPinned = "not pinned"
linkAny = "any"
linkAll = "all"
)
// Mode allows to specify different types of pin (recursive, direct etc.).
// See the Pin Modes constants for a full list.
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.Mode
type Mode int
// Pin Modes
const (
// Recursive pins pin the target cids along with any reachable children.
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.Recursive
Recursive Mode = iota
// Direct pins pin just the target cid.
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.Direct
Direct
// Indirect pins are cids who have some ancestor pinned recursively.
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.Indirect
Indirect
// Internal pins are cids used to keep the internal state of the pinner.
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.Internal
Internal
// NotPinned
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.NotPinned
NotPinned
// Any refers to any pinned cid
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.Any
Any
)
// ModeToString returns a human-readable name for the Mode.
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.ModeToString
func ModeToString(mode Mode) (string, bool) {
m := map[Mode]string{
Recursive: linkRecursive,
Direct: linkDirect,
Indirect: linkIndirect,
Internal: linkInternal,
NotPinned: linkNotPinned,
Any: linkAny,
}
s, ok := m[mode]
return s, ok
}
// StringToMode parses the result of ModeToString() back to a Mode.
// It returns a boolean which is set to false if the mode is unknown.
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.StringToMode
func StringToMode(s string) (Mode, bool) {
m := map[string]Mode{
linkRecursive: Recursive,
linkDirect: Direct,
linkIndirect: Indirect,
linkInternal: Internal,
linkNotPinned: NotPinned,
linkAny: Any,
linkAll: Any, // "all" and "any" means the same thing
}
mode, ok := m[s]
return mode, ok
}
// ErrNotPinned is returned when trying to unpin items that are not pinned.
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.ErrNotPinned
var ErrNotPinned = fmt.Errorf("not pinned or pinned indirectly")
// A Pinner provides the necessary methods to keep track of Nodes which are
// to be kept locally, according to a pin mode. In practice, a Pinner is in
// in charge of keeping the list of items from the local storage that should
// not be garbage-collected.
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.Pinner
type Pinner interface {
// IsPinned returns whether or not the given cid is pinned
// and an explanation of why its pinned
IsPinned(ctx context.Context, c cid.Cid) (string, bool, error)
// 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.
IsPinnedWithType(ctx context.Context, c cid.Cid, mode Mode) (string, bool, error)
// Pin the given node, optionally recursively.
// Pin will make sure that the given node and its children if recursive is set
// are stored locally.
Pin(ctx context.Context, node ipld.Node, recursive bool) error
// Unpin the given cid. If recursive is true, removes either a recursive or
// a direct pin. If recursive is false, only removes a direct pin.
// If the pin doesn't exist, return ErrNotPinned
Unpin(ctx context.Context, cid cid.Cid, recursive bool) error
// Update updates a recursive pin from one cid to another
// this is more efficient than simply pinning the new one and unpinning the
// old one
Update(ctx context.Context, from, to cid.Cid, unpin bool) error
// Check if a set of keys are pinned, more efficient than
// calling IsPinned for each key
CheckIfPinned(ctx context.Context, cids ...cid.Cid) ([]Pinned, error)
// PinWithMode is for manually editing the pin structure. Use with
// care! If used improperly, garbage collection may not be
// successful.
PinWithMode(context.Context, cid.Cid, Mode) error
// Flush writes the pin state to the backing datastore
Flush(ctx context.Context) error
// DirectKeys returns all directly pinned cids
DirectKeys(ctx context.Context) ([]cid.Cid, error)
// RecursiveKeys returns all recursively pinned cids
RecursiveKeys(ctx context.Context) ([]cid.Cid, error)
// InternalPins returns all cids kept pinned for the internal state of the
// pinner
InternalPins(ctx context.Context) ([]cid.Cid, error)
}
// Pinned represents CID which has been pinned with a pinning strategy.
// The Via field allows to identify the pinning parent of this CID, in the
// case that the item is not pinned directly (but rather pinned recursively
// by some ascendant).
//
// Deprecated: use github.com/ipfs/boxo/pinning/pinner.Pinned
type Pinned struct {
Key cid.Cid
Mode Mode
Via cid.Cid
}
// Pinned returns whether or not the given cid is pinned
func (p Pinned) Pinned() bool {
return p.Mode != NotPinned
}
// String Returns pin status as string
func (p Pinned) String() string {
switch p.Mode {
case NotPinned:
return "not pinned"
case Indirect:
return fmt.Sprintf("pinned via %s", p.Via)
default:
modeStr, _ := ModeToString(p.Mode)
return fmt.Sprintf("pinned: %s", modeStr)
}
}