pin/pin: replace isPinned() with isPinnedWithType()
It is more generic to be able to pass a pin type argument. License: MIT Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Christian Couder committed
Jan 16, 2016 at 14:37 UTC
b770c881a790dd860070de8e4e980fd835471a72
1 file changed
+32
-7
pin/pin.go
+32
-7
@@ -36,6 +36,7 @@ const (
36
37
type Pinner interface {
38
IsPinned(key.Key) (string, bool, error)
39
+ IsPinnedWithType(key.Key, string) (string, bool, error)
40
Pin(context.Context, *mdag.Node, bool) error
41
Unpin(context.Context, key.Key, bool) error
42
@@ -126,7 +127,7 @@ func (p *pinner) Pin(ctx context.Context, node *mdag.Node, recurse bool) error {
127
func (p *pinner) Unpin(ctx context.Context, k key.Key, recursive bool) error {
128
p.lock.Lock()
129
defer p.lock.Unlock()
129
- reason, pinned, err := p.isPinned(k)
130
+ reason, pinned, err := p.isPinnedWithType(k, "all")
131
if err != nil {
132
return err
133
}
@@ -159,22 +160,46 @@ func (p *pinner) isInternalPin(key key.Key) bool {
160
func (p *pinner) IsPinned(k key.Key) (string, bool, error) {
161
p.lock.RLock()
162
defer p.lock.RUnlock()
162
- return p.isPinned(k)
163
+ return p.isPinnedWithType(k, "all")
164
}
165
165
-// isPinned is the implementation of IsPinned that does not lock.
166
+func (p *pinner) IsPinnedWithType(k key.Key, typeStr string) (string, bool, error) {
167
+ p.lock.RLock()
168
+ defer p.lock.RUnlock()
169
+ return p.isPinnedWithType(k, typeStr)
170
+}
171
+
172
+// isPinnedWithType is the implementation of IsPinnedWithType that does not lock.
173
// intended for use by other pinned methods that already take locks
167
-func (p *pinner) isPinned(k key.Key) (string, bool, error) {
168
- if p.recursePin.HasKey(k) {
174
+func (p *pinner) isPinnedWithType(k key.Key, typeStr string) (string, bool, error) {
175
+ switch typeStr {
176
+ case "all", "direct", "indirect", "recursive", "internal":
177
+ default:
178
+ err := fmt.Errorf("Invalid type '%s', must be one of {direct, indirect, recursive, internal, all}", typeStr)
179
+ return "", false, err
180
+ }
181
+ if (typeStr == "recursive" || typeStr == "all") && p.recursePin.HasKey(k) {
182
return "recursive", true, nil
183
}
171
- if p.directPin.HasKey(k) {
184
+ if typeStr == "recursive" {
185
+ return "", false, nil
186
+ }
187
+
188
+ if (typeStr == "direct" || typeStr == "all") && p.directPin.HasKey(k) {
189
return "direct", true, nil
190
}
174
- if p.isInternalPin(k) {
191
+ if typeStr == "direct" {
192
+ return "", false, nil
193
+ }
194
+
195
+ if (typeStr == "internal" || typeStr == "all") && p.isInternalPin(k) {
196
return "internal", true, nil
197
}
198
+ if typeStr == "internal" {
199
+ return "", false, nil
200
+ }
201
202
+ // Default is "indirect"
203
for _, rk := range p.recursePin.GetKeys() {
204
rnd, err := p.dserv.Get(context.Background(), rk)
205
if err != nil {