1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-09 23:42:20 +08:00

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>
This commit is contained in:
Christian Couder
2016-01-16 14:37:04 +01:00
parent 89a6f01be0
commit b770c881a7

View File

@ -36,6 +36,7 @@ const (
type Pinner interface {
IsPinned(key.Key) (string, bool, error)
IsPinnedWithType(key.Key, string) (string, bool, error)
Pin(context.Context, *mdag.Node, bool) error
Unpin(context.Context, key.Key, bool) error
@ -126,7 +127,7 @@ func (p *pinner) Pin(ctx context.Context, node *mdag.Node, recurse bool) error {
func (p *pinner) Unpin(ctx context.Context, k key.Key, recursive bool) error {
p.lock.Lock()
defer p.lock.Unlock()
reason, pinned, err := p.isPinned(k)
reason, pinned, err := p.isPinnedWithType(k, "all")
if err != nil {
return err
}
@ -159,22 +160,46 @@ func (p *pinner) isInternalPin(key key.Key) bool {
func (p *pinner) IsPinned(k key.Key) (string, bool, error) {
p.lock.RLock()
defer p.lock.RUnlock()
return p.isPinned(k)
return p.isPinnedWithType(k, "all")
}
// isPinned is the implementation of IsPinned that does not lock.
func (p *pinner) IsPinnedWithType(k key.Key, typeStr string) (string, bool, error) {
p.lock.RLock()
defer p.lock.RUnlock()
return p.isPinnedWithType(k, typeStr)
}
// isPinnedWithType is the implementation of IsPinnedWithType that does not lock.
// intended for use by other pinned methods that already take locks
func (p *pinner) isPinned(k key.Key) (string, bool, error) {
if p.recursePin.HasKey(k) {
func (p *pinner) isPinnedWithType(k key.Key, typeStr string) (string, bool, error) {
switch typeStr {
case "all", "direct", "indirect", "recursive", "internal":
default:
err := fmt.Errorf("Invalid type '%s', must be one of {direct, indirect, recursive, internal, all}", typeStr)
return "", false, err
}
if (typeStr == "recursive" || typeStr == "all") && p.recursePin.HasKey(k) {
return "recursive", true, nil
}
if p.directPin.HasKey(k) {
return "direct", true, nil
}
if p.isInternalPin(k) {
return "internal", true, nil
if typeStr == "recursive" {
return "", false, nil
}
if (typeStr == "direct" || typeStr == "all") && p.directPin.HasKey(k) {
return "direct", true, nil
}
if typeStr == "direct" {
return "", false, nil
}
if (typeStr == "internal" || typeStr == "all") && p.isInternalPin(k) {
return "internal", true, nil
}
if typeStr == "internal" {
return "", false, nil
}
// Default is "indirect"
for _, rk := range p.recursePin.GetKeys() {
rnd, err := p.dserv.Get(context.Background(), rk)
if err != nil {