提交 f796ee45 作者: Jeromy Johnson

Merge pull request #2699 from ipfs/use-consts-for-pin-modes

Use consts for pin modes
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
corerepo "github.com/ipfs/go-ipfs/core/corerepo" corerepo "github.com/ipfs/go-ipfs/core/corerepo"
dag "github.com/ipfs/go-ipfs/merkledag" dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path" path "github.com/ipfs/go-ipfs/path"
pin "github.com/ipfs/go-ipfs/pin"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context" context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
) )
...@@ -273,7 +274,12 @@ func pinLsKeys(args []string, typeStr string, ctx context.Context, n *core.IpfsN ...@@ -273,7 +274,12 @@ func pinLsKeys(args []string, typeStr string, ctx context.Context, n *core.IpfsN
return nil, err return nil, err
} }
pinType, pinned, err := n.Pinning.IsPinnedWithType(k, typeStr) mode, ok := pin.StringToPinMode(typeStr)
if !ok {
return nil, fmt.Errorf("Invalid pin mode '%s'", typeStr)
}
pinType, pinned, err := n.Pinning.IsPinnedWithType(k, mode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -22,8 +22,13 @@ var pinDatastoreKey = ds.NewKey("/local/pins") ...@@ -22,8 +22,13 @@ var pinDatastoreKey = ds.NewKey("/local/pins")
var emptyKey = key.B58KeyDecode("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n") var emptyKey = key.B58KeyDecode("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n")
const ( const (
linkDirect = "direct"
linkRecursive = "recursive" linkRecursive = "recursive"
linkDirect = "direct"
linkIndirect = "indirect"
linkInternal = "internal"
linkNotPinned = "not pinned"
linkAny = "any"
linkAll = "all"
) )
type PinMode int type PinMode int
...@@ -31,12 +36,42 @@ type PinMode int ...@@ -31,12 +36,42 @@ type PinMode int
const ( const (
Recursive PinMode = iota Recursive PinMode = iota
Direct Direct
Indirect
Internal
NotPinned NotPinned
Any
) )
func PinModeToString(mode PinMode) (string, bool) {
m := map[PinMode]string{
Recursive: linkRecursive,
Direct: linkDirect,
Indirect: linkIndirect,
Internal: linkInternal,
NotPinned: linkNotPinned,
Any: linkAny,
}
s, ok := m[mode]
return s, ok
}
func StringToPinMode(s string) (PinMode, bool) {
m := map[string]PinMode{
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
}
type Pinner interface { type Pinner interface {
IsPinned(key.Key) (string, bool, error) IsPinned(key.Key) (string, bool, error)
IsPinnedWithType(key.Key, string) (string, bool, error) IsPinnedWithType(key.Key, PinMode) (string, bool, error)
Pin(context.Context, *mdag.Node, bool) error Pin(context.Context, *mdag.Node, bool) error
Unpin(context.Context, key.Key, bool) error Unpin(context.Context, key.Key, bool) error
...@@ -129,7 +164,7 @@ var ErrNotPinned = fmt.Errorf("not pinned") ...@@ -129,7 +164,7 @@ var ErrNotPinned = fmt.Errorf("not pinned")
func (p *pinner) Unpin(ctx context.Context, k key.Key, recursive bool) error { func (p *pinner) Unpin(ctx context.Context, k key.Key, recursive bool) error {
p.lock.Lock() p.lock.Lock()
defer p.lock.Unlock() defer p.lock.Unlock()
reason, pinned, err := p.isPinnedWithType(k, "all") reason, pinned, err := p.isPinnedWithType(k, Any)
if err != nil { if err != nil {
return err return err
} }
...@@ -162,46 +197,47 @@ func (p *pinner) isInternalPin(key key.Key) bool { ...@@ -162,46 +197,47 @@ func (p *pinner) isInternalPin(key key.Key) bool {
func (p *pinner) IsPinned(k key.Key) (string, bool, error) { func (p *pinner) IsPinned(k key.Key) (string, bool, error) {
p.lock.RLock() p.lock.RLock()
defer p.lock.RUnlock() defer p.lock.RUnlock()
return p.isPinnedWithType(k, "all") return p.isPinnedWithType(k, Any)
} }
func (p *pinner) IsPinnedWithType(k key.Key, typeStr string) (string, bool, error) { func (p *pinner) IsPinnedWithType(k key.Key, mode PinMode) (string, bool, error) {
p.lock.RLock() p.lock.RLock()
defer p.lock.RUnlock() defer p.lock.RUnlock()
return p.isPinnedWithType(k, typeStr) return p.isPinnedWithType(k, mode)
} }
// isPinnedWithType is the implementation of IsPinnedWithType that does not lock. // isPinnedWithType is the implementation of IsPinnedWithType that does not lock.
// intended for use by other pinned methods that already take locks // intended for use by other pinned methods that already take locks
func (p *pinner) isPinnedWithType(k key.Key, typeStr string) (string, bool, error) { func (p *pinner) isPinnedWithType(k key.Key, mode PinMode) (string, bool, error) {
switch typeStr { switch mode {
case "all", "direct", "indirect", "recursive", "internal": case Any, Direct, Indirect, Recursive, Internal:
default: default:
err := fmt.Errorf("Invalid type '%s', must be one of {direct, indirect, recursive, internal, all}", typeStr) err := fmt.Errorf("Invalid Pin Mode '%d', must be one of {%d, %d, %d, %d, %d}",
mode, Direct, Indirect, Recursive, Internal, Any)
return "", false, err return "", false, err
} }
if (typeStr == "recursive" || typeStr == "all") && p.recursePin.HasKey(k) { if (mode == Recursive || mode == Any) && p.recursePin.HasKey(k) {
return "recursive", true, nil return linkRecursive, true, nil
} }
if typeStr == "recursive" { if mode == Recursive {
return "", false, nil return "", false, nil
} }
if (typeStr == "direct" || typeStr == "all") && p.directPin.HasKey(k) { if (mode == Direct || mode == Any) && p.directPin.HasKey(k) {
return "direct", true, nil return linkDirect, true, nil
} }
if typeStr == "direct" { if mode == Direct {
return "", false, nil return "", false, nil
} }
if (typeStr == "internal" || typeStr == "all") && p.isInternalPin(k) { if (mode == Internal || mode == Any) && p.isInternalPin(k) {
return "internal", true, nil return linkInternal, true, nil
} }
if typeStr == "internal" { if mode == Internal {
return "", false, nil return "", false, nil
} }
// Default is "indirect" // Default is Indirect
for _, rk := range p.recursePin.GetKeys() { for _, rk := range p.recursePin.GetKeys() {
rnd, err := p.dserv.Get(context.Background(), rk) rnd, err := p.dserv.Get(context.Background(), rk)
if err != nil { if err != nil {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论