Commit b770c881 authored by Christian Couder's avatar Christian Couder

pin/pin: replace isPinned() with isPinnedWithType()

It is more generic to be able to pass a pin type argument.

License: MIT
Signed-off-by: default avatarChristian Couder <chriscool@tuxfamily.org>
parent 89a6f01b
......@@ -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) {
if typeStr == "recursive" {
return "", false, nil
}
if (typeStr == "direct" || typeStr == "all") && p.directPin.HasKey(k) {
return "direct", true, nil
}
if p.isInternalPin(k) {
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 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment