implement the new GetSize methods for the filestore blockservices
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Aug 6, 2018 at 22:12 UTC
eac8072320bc483b53f83e7cea7b297c2ea34fb7
2 files changed
+28
filestore/filestore.go
+16
@@ -154,6 +154,22 @@ func (f *Filestore) Get(c *cid.Cid) (blocks.Block, error) {
154
return f.fm.Get(c)
155
}
156
157
+// GetSize returns the size of the requested block. It may return ErrNotFound
158
+// when the block is not stored.
159
+func (f *Filestore) GetSize(c *cid.Cid) (int, error) {
160
+ size, err := f.bs.GetSize(c)
161
+ switch err {
162
+ default:
163
+ return -1, err
164
+ case nil:
165
+ return size, nil
166
+ case blockstore.ErrNotFound:
167
+ // try filestore
168
+ }
169
+
170
+ return f.fm.GetSize(c)
171
+}
172
+
173
// Has returns true if the block with the given Cid is
174
// stored in the Filestore.
175
func (f *Filestore) Has(c *cid.Cid) (bool, error) {
filestore/fsrefstore.go
+12
@@ -122,6 +122,18 @@ func (f *FileManager) Get(c *cid.Cid) (blocks.Block, error) {
122
return blocks.NewBlockWithCid(out, c)
123
}
124
125
+// GetSize gets the size of the block from the datastore.
126
+//
127
+// This method may successfully return the size even if returning the block
128
+// would fail because the associated file is no longer available.
129
+func (f *FileManager) GetSize(c *cid.Cid) (int, error) {
130
+ dobj, err := f.getDataObj(c)
131
+ if err != nil {
132
+ return -1, err
133
+ }
134
+ return int(dobj.GetSize_()), nil
135
+}
136
+
137
func (f *FileManager) readDataObj(c *cid.Cid, d *pb.DataObj) ([]byte, error) {
138
if IsURL(d.GetFilePath()) {
139
return f.readURLDataObj(c, d)