mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-23 21:47:52 +08:00
a few more comments
This commit is contained in:
@ -19,6 +19,9 @@ func NewBlock(data []byte) *Block {
|
|||||||
return &Block{Data: data, Multihash: u.Hash(data)}
|
return &Block{Data: data, Multihash: u.Hash(data)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBlockWithHash creates a new block when the hash of the data
|
||||||
|
// is already known, this is used to save time in situations where
|
||||||
|
// we are able to be confident that the data is correct
|
||||||
func NewBlockWithHash(data []byte, h mh.Multihash) (*Block, error) {
|
func NewBlockWithHash(data []byte, h mh.Multihash) (*Block, error) {
|
||||||
if u.Debug {
|
if u.Debug {
|
||||||
chk := u.Hash(data)
|
chk := u.Hash(data)
|
||||||
|
@ -48,6 +48,7 @@ type Link struct {
|
|||||||
Node *Node
|
Node *Node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MakeLink creates a link to the given node
|
||||||
func MakeLink(n *Node) (*Link, error) {
|
func MakeLink(n *Node) (*Link, error) {
|
||||||
s, err := n.Size()
|
s, err := n.Size()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -64,6 +65,7 @@ func MakeLink(n *Node) (*Link, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNode returns the MDAG Node that this link points to
|
||||||
func (l *Link) GetNode(serv DAGService) (*Node, error) {
|
func (l *Link) GetNode(serv DAGService) (*Node, error) {
|
||||||
if l.Node != nil {
|
if l.Node != nil {
|
||||||
return l.Node, nil
|
return l.Node, nil
|
||||||
@ -98,6 +100,7 @@ func (n *Node) AddNodeLinkClean(name string, that *Node) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove a link on this node by the given name
|
||||||
func (n *Node) RemoveNodeLink(name string) error {
|
func (n *Node) RemoveNodeLink(name string) error {
|
||||||
for i, l := range n.Links {
|
for i, l := range n.Links {
|
||||||
if l.Name == name {
|
if l.Name == name {
|
||||||
@ -196,6 +199,7 @@ func (n *dagService) Add(nd *Node) (u.Key, error) {
|
|||||||
return n.Blocks.AddBlock(b)
|
return n.Blocks.AddBlock(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddRecursive adds the given node and all child nodes to the BlockService
|
||||||
func (n *dagService) AddRecursive(nd *Node) error {
|
func (n *dagService) AddRecursive(nd *Node) error {
|
||||||
_, err := n.Add(nd)
|
_, err := n.Add(nd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -230,6 +234,7 @@ func (n *dagService) Get(k u.Key) (*Node, error) {
|
|||||||
return Decoded(b.Data)
|
return Decoded(b.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove deletes the given node and all of its children from the BlockService
|
||||||
func (n *dagService) Remove(nd *Node) error {
|
func (n *dagService) Remove(nd *Node) error {
|
||||||
for _, l := range nd.Links {
|
for _, l := range nd.Links {
|
||||||
if l.Node != nil {
|
if l.Node != nil {
|
||||||
@ -243,6 +248,8 @@ func (n *dagService) Remove(nd *Node) error {
|
|||||||
return n.Blocks.DeleteBlock(k)
|
return n.Blocks.DeleteBlock(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FetchGraph asynchronously fetches all nodes that are children of the given
|
||||||
|
// node, and returns a channel that may be waited upon for the fetch to complete
|
||||||
func FetchGraph(ctx context.Context, root *Node, serv DAGService) chan struct{} {
|
func FetchGraph(ctx context.Context, root *Node, serv DAGService) chan struct{} {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
@ -26,6 +26,7 @@ func NewRoutingPublisher(route routing.IpfsRouting) Publisher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Publish implements Publisher. Accepts a keypair and a value,
|
// Publish implements Publisher. Accepts a keypair and a value,
|
||||||
|
// and publishes it out to the routing system
|
||||||
func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error {
|
func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error {
|
||||||
log.Debugf("namesys: Publish %s", value)
|
log.Debugf("namesys: Publish %s", value)
|
||||||
|
|
||||||
|
@ -274,6 +274,7 @@ func (p *peer) VerifyAndSetPubKey(pk ic.PubKey) error {
|
|||||||
panic("invariant violated: unexpected key mismatch")
|
panic("invariant violated: unexpected key mismatch")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Updates this peer with information from another peer instance
|
||||||
func (p *peer) Update(other Peer) error {
|
func (p *peer) Update(other Peer) error {
|
||||||
if !p.ID().Equal(other.ID()) {
|
if !p.ID().Equal(other.ID()) {
|
||||||
return errors.New("peer ids do not match")
|
return errors.New("peer ids do not match")
|
||||||
|
15
pin/pin.go
15
pin/pin.go
@ -1,9 +1,6 @@
|
|||||||
package pin
|
package pin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
||||||
//ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
|
|
||||||
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
@ -36,12 +33,14 @@ type Pinner interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ManualPinner is for manually editing the pin structure
|
// ManualPinner is for manually editing the pin structure
|
||||||
// Use with care
|
// Use with care! If used improperly, garbage collection
|
||||||
|
// may not be successful
|
||||||
type ManualPinner interface {
|
type ManualPinner interface {
|
||||||
PinWithMode(util.Key, PinMode)
|
PinWithMode(util.Key, PinMode)
|
||||||
Pinner
|
Pinner
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pinner implements the Pinner interface
|
||||||
type pinner struct {
|
type pinner struct {
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
recursePin set.BlockSet
|
recursePin set.BlockSet
|
||||||
@ -51,6 +50,7 @@ type pinner struct {
|
|||||||
dstore ds.Datastore
|
dstore ds.Datastore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewPinner creates a new pinner using the given datastore as a backend
|
||||||
func NewPinner(dstore ds.Datastore, serv mdag.DAGService) Pinner {
|
func NewPinner(dstore ds.Datastore, serv mdag.DAGService) Pinner {
|
||||||
|
|
||||||
// Load set from given datastore...
|
// Load set from given datastore...
|
||||||
@ -70,6 +70,7 @@ func NewPinner(dstore ds.Datastore, serv mdag.DAGService) Pinner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pin the given node, optionally recursive
|
||||||
func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
|
func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
defer p.lock.Unlock()
|
defer p.lock.Unlock()
|
||||||
@ -95,6 +96,7 @@ func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unpin a given key with optional recursive unpinning
|
||||||
func (p *pinner) Unpin(k util.Key, recurse bool) error {
|
func (p *pinner) Unpin(k util.Key, recurse bool) error {
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
defer p.lock.Unlock()
|
defer p.lock.Unlock()
|
||||||
@ -158,6 +160,7 @@ func (p *pinner) pinLinks(node *mdag.Node) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsPinned returns whether or not the given key is pinned
|
||||||
func (p *pinner) IsPinned(key util.Key) bool {
|
func (p *pinner) IsPinned(key util.Key) bool {
|
||||||
p.lock.RLock()
|
p.lock.RLock()
|
||||||
defer p.lock.RUnlock()
|
defer p.lock.RUnlock()
|
||||||
@ -166,6 +169,7 @@ func (p *pinner) IsPinned(key util.Key) bool {
|
|||||||
p.indirPin.HasKey(key)
|
p.indirPin.HasKey(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadPinner loads a pinner and its keysets from the given datastore
|
||||||
func LoadPinner(d ds.Datastore, dserv mdag.DAGService) (Pinner, error) {
|
func LoadPinner(d ds.Datastore, dserv mdag.DAGService) (Pinner, error) {
|
||||||
p := new(pinner)
|
p := new(pinner)
|
||||||
|
|
||||||
@ -200,6 +204,7 @@ func LoadPinner(d ds.Datastore, dserv mdag.DAGService) (Pinner, error) {
|
|||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush encodes and writes pinner keysets to the datastore
|
||||||
func (p *pinner) Flush() error {
|
func (p *pinner) Flush() error {
|
||||||
p.lock.RLock()
|
p.lock.RLock()
|
||||||
defer p.lock.RUnlock()
|
defer p.lock.RUnlock()
|
||||||
@ -244,6 +249,8 @@ func loadSet(d ds.Datastore, k ds.Key, val interface{}) error {
|
|||||||
return json.Unmarshal(bf, val)
|
return json.Unmarshal(bf, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PinWithMode is a method on ManualPinners, allowing the user to have fine
|
||||||
|
// grained control over pin counts
|
||||||
func (p *pinner) PinWithMode(k util.Key, mode PinMode) {
|
func (p *pinner) PinWithMode(k util.Key, mode PinMode) {
|
||||||
switch mode {
|
switch mode {
|
||||||
case Recursive:
|
case Recursive:
|
||||||
|
Reference in New Issue
Block a user