提交 01498b89 作者: Steven Allen

commands/repo: add the provider record information to stat

Useful for debugging repo usage.
上级 d778b84f
......@@ -195,6 +195,8 @@ Version string The repo version.
if !sizeOnly {
fmt.Fprintf(wtr, "NumObjects:\t%d\n", stat.NumObjects)
fmt.Fprintf(wtr, "NumValues:\t%d\n", stat.NumValues)
fmt.Fprintf(wtr, "NumProviders:\t%d\n", stat.NumProviders)
}
printSize("RepoSize", stat.RepoSize)
......
package corerepo
import (
"context"
"fmt"
"math"
context "context"
"strings"
"github.com/ipfs/go-ipfs/core"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
humanize "github.com/dustin/go-humanize"
dsq "github.com/ipfs/go-datastore/query"
)
// SizeStat wraps information about the repository size and its limit.
......@@ -21,45 +22,60 @@ type SizeStat struct {
// Stat wraps information about the objects stored on disk.
type Stat struct {
SizeStat
// Number of blocks. Called "objects" for historical reasons".
NumObjects uint64
RepoPath string
Version string
// Number of provider records.
NumProviders uint64
// Total number of values in the datastore.
NumValues uint64
RepoPath string
Version string
}
// NoLimit represents the value for unlimited storage
const NoLimit uint64 = math.MaxUint64
// RepoStat returns a *Stat object with all the fields set.
func RepoStat(ctx context.Context, n *core.IpfsNode) (Stat, error) {
sizeStat, err := RepoSize(ctx, n)
func RepoStat(ctx context.Context, n *core.IpfsNode) (stat Stat, err error) {
stat.Version = fmt.Sprintf("fs-repo@%d", fsrepo.RepoVersion)
stat.RepoPath, err = fsrepo.BestKnownPath()
if err != nil {
return Stat{}, err
return stat, err
}
allKeys, err := n.Blockstore.AllKeysChan(ctx)
stat.SizeStat, err = RepoSize(ctx, n)
if err != nil {
return Stat{}, err
}
count := uint64(0)
for range allKeys {
count++
return stat, err
}
path, err := fsrepo.BestKnownPath()
// count the records.
query, err := n.Repo.Datastore().Query(dsq.Query{KeysOnly: true})
if err != nil {
return Stat{}, err
return stat, err
}
defer query.Close()
return Stat{
SizeStat: SizeStat{
RepoSize: sizeStat.RepoSize,
StorageMax: sizeStat.StorageMax,
},
NumObjects: count,
RepoPath: path,
Version: fmt.Sprintf("fs-repo@%d", fsrepo.RepoVersion),
}, nil
for {
if err := ctx.Err(); err != nil {
return stat, err
}
res, ok := query.NextSync()
if !ok {
return stat, nil
}
if res.Error != nil {
return stat, res.Error
}
stat.NumValues++
if strings.HasPrefix(res.Key, "/blocks/") {
stat.NumObjects++
} else if strings.HasPrefix(res.Key, "/providers/") {
stat.NumProviders++
}
}
}
// RepoSize returns a *Stat object with the RepoSize and StorageMax fields set.
......
......@@ -112,6 +112,7 @@ require (
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac // indirect
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect
google.golang.org/appengine v1.4.0 // indirect
gopkg.in/cheggaaa/pb.v1 v1.0.28
gotest.tools/gotestsum v0.3.4
)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论