github.com/thanos-io/thanos@v0.32.5/internal/cortex/chunk/cache/snappy.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package cache
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/go-kit/log"
    10  	"github.com/go-kit/log/level"
    11  	"github.com/golang/snappy"
    12  )
    13  
    14  type snappyCache struct {
    15  	next   Cache
    16  	logger log.Logger
    17  }
    18  
    19  // NewSnappy makes a new snappy encoding cache wrapper.
    20  func NewSnappy(next Cache, logger log.Logger) Cache {
    21  	return &snappyCache{
    22  		next:   next,
    23  		logger: logger,
    24  	}
    25  }
    26  
    27  func (s *snappyCache) Store(ctx context.Context, keys []string, bufs [][]byte) {
    28  	cs := make([][]byte, 0, len(bufs))
    29  	for _, buf := range bufs {
    30  		c := snappy.Encode(nil, buf)
    31  		cs = append(cs, c)
    32  	}
    33  	s.next.Store(ctx, keys, cs)
    34  }
    35  
    36  func (s *snappyCache) Fetch(ctx context.Context, keys []string) ([]string, [][]byte, []string) {
    37  	found, bufs, missing := s.next.Fetch(ctx, keys)
    38  	ds := make([][]byte, 0, len(bufs))
    39  	for _, buf := range bufs {
    40  		d, err := snappy.Decode(nil, buf)
    41  		if err != nil {
    42  			level.Error(s.logger).Log("msg", "failed to decode cache entry", "err", err)
    43  			return nil, nil, keys
    44  		}
    45  		ds = append(ds, d)
    46  	}
    47  	return found, ds, missing
    48  }
    49  
    50  func (s *snappyCache) Stop() {
    51  	s.next.Stop()
    52  }