mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 09:34:03 +08:00
docs for mfs system method impls
License: MIT Signed-off-by: ForrestWeston <forrest@protocol.ai>
This commit is contained in:
@ -41,19 +41,19 @@ const (
|
|||||||
TDir
|
TDir
|
||||||
)
|
)
|
||||||
|
|
||||||
// FSNode represents any node (directory, root, or file) in the mfs filesystem
|
// FSNode represents any node (directory, root, or file) in the mfs filesystem.
|
||||||
type FSNode interface {
|
type FSNode interface {
|
||||||
GetNode() (node.Node, error)
|
GetNode() (node.Node, error)
|
||||||
Flush() error
|
Flush() error
|
||||||
Type() NodeType
|
Type() NodeType
|
||||||
}
|
}
|
||||||
|
|
||||||
// Root represents the root of a filesystem tree
|
// Root represents the root of a filesystem tree.
|
||||||
type Root struct {
|
type Root struct {
|
||||||
// node is the merkledag root
|
// node is the merkledag root.
|
||||||
node *dag.ProtoNode
|
node *dag.ProtoNode
|
||||||
|
|
||||||
// val represents the node. It can either be a File or a Directory
|
// val represents the node. It can either be a File or a Directory.
|
||||||
val FSNode
|
val FSNode
|
||||||
|
|
||||||
repub *Republisher
|
repub *Republisher
|
||||||
@ -63,9 +63,10 @@ type Root struct {
|
|||||||
Type string
|
Type string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PubFunc is the function used by the `publish()` method.
|
||||||
type PubFunc func(context.Context, *cid.Cid) error
|
type PubFunc func(context.Context, *cid.Cid) error
|
||||||
|
|
||||||
// newRoot creates a new Root and starts up a republisher routine for it
|
// NewRoot creates a new Root and starts up a republisher routine for it.
|
||||||
func NewRoot(parent context.Context, ds dag.DAGService, node *dag.ProtoNode, pf PubFunc) (*Root, error) {
|
func NewRoot(parent context.Context, ds dag.DAGService, node *dag.ProtoNode, pf PubFunc) (*Root, error) {
|
||||||
|
|
||||||
var repub *Republisher
|
var repub *Republisher
|
||||||
@ -107,10 +108,13 @@ func NewRoot(parent context.Context, ds dag.DAGService, node *dag.ProtoNode, pf
|
|||||||
return root, nil
|
return root, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetValue returns the value of Root.
|
||||||
func (kr *Root) GetValue() FSNode {
|
func (kr *Root) GetValue() FSNode {
|
||||||
return kr.val
|
return kr.val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush signals that an update has occurred since the last publish,
|
||||||
|
// and updates the Root republisher.
|
||||||
func (kr *Root) Flush() error {
|
func (kr *Root) Flush() error {
|
||||||
nd, err := kr.GetValue().GetNode()
|
nd, err := kr.GetValue().GetNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -154,7 +158,7 @@ func (kr *Root) FlushMemFree(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// closeChild implements the childCloser interface, and signals to the publisher that
|
// closeChild implements the childCloser interface, and signals to the publisher that
|
||||||
// there are changes ready to be published
|
// there are changes ready to be published.
|
||||||
func (kr *Root) closeChild(name string, nd node.Node, sync bool) error {
|
func (kr *Root) closeChild(name string, nd node.Node, sync bool) error {
|
||||||
c, err := kr.dserv.Add(nd)
|
c, err := kr.dserv.Add(nd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -181,7 +185,7 @@ func (kr *Root) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Republisher manages when to publish a given entry
|
// Republisher manages when to publish a given entry.
|
||||||
type Republisher struct {
|
type Republisher struct {
|
||||||
TimeoutLong time.Duration
|
TimeoutLong time.Duration
|
||||||
TimeoutShort time.Duration
|
TimeoutShort time.Duration
|
||||||
@ -198,7 +202,7 @@ type Republisher struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewRepublisher creates a new Republisher object to republish the given root
|
// NewRepublisher creates a new Republisher object to republish the given root
|
||||||
// using the given short and long time intervals
|
// using the given short and long time intervals.
|
||||||
func NewRepublisher(ctx context.Context, pf PubFunc, tshort, tlong time.Duration) *Republisher {
|
func NewRepublisher(ctx context.Context, pf PubFunc, tshort, tlong time.Duration) *Republisher {
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
return &Republisher{
|
return &Republisher{
|
||||||
@ -218,6 +222,8 @@ func (p *Republisher) setVal(c *cid.Cid) {
|
|||||||
p.val = c
|
p.val = c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WaitPub Returns immediately if `lastpub` value is consistent with the
|
||||||
|
// current value `val`, else will block until `val` has been published.
|
||||||
func (p *Republisher) WaitPub() {
|
func (p *Republisher) WaitPub() {
|
||||||
p.lk.Lock()
|
p.lk.Lock()
|
||||||
consistent := p.lastpub == p.val
|
consistent := p.lastpub == p.val
|
||||||
@ -239,7 +245,7 @@ func (p *Republisher) Close() error {
|
|||||||
|
|
||||||
// Touch signals that an update has occurred since the last publish.
|
// Touch signals that an update has occurred since the last publish.
|
||||||
// Multiple consecutive touches may extend the time period before
|
// Multiple consecutive touches may extend the time period before
|
||||||
// the next Publish occurs in order to more efficiently batch updates
|
// the next Publish occurs in order to more efficiently batch updates.
|
||||||
func (np *Republisher) Update(c *cid.Cid) {
|
func (np *Republisher) Update(c *cid.Cid) {
|
||||||
np.setVal(c)
|
np.setVal(c)
|
||||||
select {
|
select {
|
||||||
@ -248,7 +254,7 @@ func (np *Republisher) Update(c *cid.Cid) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run is the main republisher loop
|
// Run is the main republisher loop.
|
||||||
func (np *Republisher) Run() {
|
func (np *Republisher) Run() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -284,6 +290,7 @@ func (np *Republisher) Run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// publish calls the `PubFunc`.
|
||||||
func (np *Republisher) publish(ctx context.Context) error {
|
func (np *Republisher) publish(ctx context.Context) error {
|
||||||
np.lk.Lock()
|
np.lk.Lock()
|
||||||
topub := np.val
|
topub := np.val
|
||||||
|
Reference in New Issue
Block a user