12447: Remove .Bytes()
[arvados.git] / sdk / go / keepclient / block_cache.go
index 88617ea2d3222b06b7368e96a95ada893ad056fa..1849fa2ce3f15f0387e000ceae4e07e14ca3eed7 100644 (file)
@@ -5,7 +5,7 @@
 package keepclient
 
 import (
-       "io/ioutil"
+       "io"
        "sort"
        "sync"
        "time"
@@ -29,7 +29,7 @@ const defaultMaxBlocks = 4
 // there are no more than MaxBlocks left.
 func (c *BlockCache) Sweep() {
        max := c.MaxBlocks
-       if max < defaultMaxBlocks {
+       if max == 0 {
                max = defaultMaxBlocks
        }
        c.mtx.Lock()
@@ -64,10 +64,11 @@ func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) {
                }
                c.cache[cacheKey] = b
                go func() {
-                       rdr, _, _, err := kc.Get(locator)
+                       rdr, size, _, err := kc.Get(locator)
                        var data []byte
                        if err == nil {
-                               data, err = ioutil.ReadAll(rdr)
+                               data := make([]byte, size, BLOCKSIZE)
+                               _, err = io.ReadFull(rdr, data)
                                err2 := rdr.Close()
                                if err == nil {
                                        err = err2
@@ -96,6 +97,12 @@ func (c *BlockCache) setup() {
        c.cache = make(map[string]*cacheBlock)
 }
 
+func (c *BlockCache) Clear() {
+       c.mtx.Lock()
+       c.setup()
+       c.mtx.Unlock()
+}
+
 type timeSlice []time.Time
 
 func (ts timeSlice) Len() int { return len(ts) }