From ee8d7f70984156a26c0cb82d29d89de3bd0f1cf7 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Fri, 17 Aug 2018 16:53:40 +0200 Subject: [PATCH] IPFSHttp: Add "size-only=true" flag to repo/stat calls This will make them very fast. Fixes #280 License: MIT Signed-off-by: Hector Sanjuan --- ipfsconn/ipfshttp/ipfshttp.go | 5 ++--- test/ipfs_mock.go | 7 ++++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ipfsconn/ipfshttp/ipfshttp.go b/ipfsconn/ipfshttp/ipfshttp.go index 2474484a2..4a46b7a45 100644 --- a/ipfsconn/ipfshttp/ipfshttp.go +++ b/ipfsconn/ipfshttp/ipfshttp.go @@ -97,7 +97,6 @@ type ipfsIDResp struct { type ipfsRepoStatResp struct { RepoSize uint64 StorageMax uint64 - NumObjects uint64 } type ipfsAddResp struct { @@ -924,7 +923,7 @@ func getConfigValue(path []string, cfg map[string]interface{}) (interface{}, err func (ipfs *Connector) FreeSpace() (uint64, error) { ctx, cancel := context.WithTimeout(ipfs.ctx, ipfs.config.IPFSRequestTimeout) defer cancel() - res, err := ipfs.postCtx(ctx, "repo/stat", "", nil) + res, err := ipfs.postCtx(ctx, "repo/stat?size-only=true", "", nil) if err != nil { logger.Error(err) return 0, err @@ -944,7 +943,7 @@ func (ipfs *Connector) FreeSpace() (uint64, error) { func (ipfs *Connector) RepoSize() (uint64, error) { ctx, cancel := context.WithTimeout(ipfs.ctx, ipfs.config.IPFSRequestTimeout) defer cancel() - res, err := ipfs.postCtx(ctx, "repo/stat", "", nil) + res, err := ipfs.postCtx(ctx, "repo/stat?size-only=true", "", nil) if err != nil { logger.Error(err) return 0, err diff --git a/test/ipfs_mock.go b/test/ipfs_mock.go index 0fe7a36e7..6c5515910 100644 --- a/test/ipfs_mock.go +++ b/test/ipfs_mock.go @@ -288,10 +288,15 @@ func (m *IpfsMock) handler(w http.ResponseWriter, r *http.Request) { } w.Write(data) case "repo/stat": + sizeOnly := r.URL.Query().Get("size-only") len := len(m.pinMap.List()) + numObjs := uint64(len) + if sizeOnly == "true" { + numObjs = 0 + } resp := mockRepoStatResp{ RepoSize: uint64(len) * 1000, - NumObjects: uint64(len), + NumObjects: numObjs, StorageMax: 10000000000, //10 GB } j, _ := json.Marshal(resp)