X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/02adf6134d262d17066dbc48d3fb85c8861c8349..8a27fe370239ecb8e50d53f46b45ed61203a35ca:/sdk/go/keepclient/block_cache.go diff --git a/sdk/go/keepclient/block_cache.go b/sdk/go/keepclient/block_cache.go index 1849fa2ce3..bac4a24fd5 100644 --- a/sdk/go/keepclient/block_cache.go +++ b/sdk/go/keepclient/block_cache.go @@ -7,6 +7,8 @@ package keepclient import ( "io" "sort" + "strconv" + "strings" "sync" "time" ) @@ -18,9 +20,8 @@ type BlockCache struct { // default size (currently 4) is used instead. MaxBlocks int - cache map[string]*cacheBlock - mtx sync.Mutex - setupOnce sync.Once + cache map[string]*cacheBlock + mtx sync.Mutex } const defaultMaxBlocks = 4 @@ -50,12 +51,34 @@ func (c *BlockCache) Sweep() { } } +// ReadAt returns data from the cache, first retrieving it from Keep if +// necessary. +func (c *BlockCache) ReadAt(kc *KeepClient, locator string, p []byte, off int) (int, error) { + buf, err := c.Get(kc, locator) + if err != nil { + return 0, err + } + if off > len(buf) { + return 0, io.ErrUnexpectedEOF + } + return copy(p, buf[off:]), nil +} + // Get returns data from the cache, first retrieving it from Keep if // necessary. func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) { - c.setupOnce.Do(c.setup) cacheKey := locator[:32] + bufsize := BLOCKSIZE + if parts := strings.SplitN(locator, "+", 3); len(parts) >= 2 { + datasize, err := strconv.ParseInt(parts[1], 10, 32) + if err == nil && datasize >= 0 { + bufsize = int(datasize) + } + } c.mtx.Lock() + if c.cache == nil { + c.cache = make(map[string]*cacheBlock) + } b, ok := c.cache[cacheKey] if !ok || b.err != nil { b = &cacheBlock{ @@ -67,7 +90,7 @@ func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) { rdr, size, _, err := kc.Get(locator) var data []byte if err == nil { - data := make([]byte, size, BLOCKSIZE) + data = make([]byte, size, bufsize) _, err = io.ReadFull(rdr, data) err2 := rdr.Close() if err == nil { @@ -93,13 +116,9 @@ func (c *BlockCache) Get(kc *KeepClient, locator string) ([]byte, error) { return b.data, b.err } -func (c *BlockCache) setup() { - c.cache = make(map[string]*cacheBlock) -} - func (c *BlockCache) Clear() { c.mtx.Lock() - c.setup() + c.cache = nil c.mtx.Unlock() }