github.com/thanos-io/thanos@v0.32.5/pkg/store/cache/cachekey/cachekey.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package cachekey
     5  
     6  import (
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  var (
    14  	ErrInvalidBucketCacheKeyFormat = errors.New("key has invalid format")
    15  	ErrInvalidBucketCacheKeyVerb   = errors.New("key has invalid verb")
    16  	ErrParseKeyInt                 = errors.New("failed to parse integer in key")
    17  )
    18  
    19  // VerbType is the type of operation whose result has been stored in the caching bucket's cache.
    20  type VerbType string
    21  
    22  const (
    23  	ExistsVerb        VerbType = "exists"
    24  	ContentVerb       VerbType = "content"
    25  	IterVerb          VerbType = "iter"
    26  	IterRecursiveVerb VerbType = "iter-recursive"
    27  	AttributesVerb    VerbType = "attrs"
    28  	SubrangeVerb      VerbType = "subrange"
    29  )
    30  
    31  type BucketCacheKey struct {
    32  	Verb  VerbType
    33  	Name  string
    34  	Start int64
    35  	End   int64
    36  }
    37  
    38  // String returns the string representation of BucketCacheKey.
    39  func (ck BucketCacheKey) String() string {
    40  	if ck.Start == 0 && ck.End == 0 {
    41  		return string(ck.Verb) + ":" + ck.Name
    42  	}
    43  
    44  	return strings.Join([]string{string(ck.Verb), ck.Name, strconv.FormatInt(ck.Start, 10), strconv.FormatInt(ck.End, 10)}, ":")
    45  }
    46  
    47  // IsValidVerb checks if the VerbType matches the predefined verbs.
    48  func IsValidVerb(v VerbType) bool {
    49  	switch v {
    50  	case
    51  		ExistsVerb,
    52  		ContentVerb,
    53  		IterVerb,
    54  		IterRecursiveVerb,
    55  		AttributesVerb,
    56  		SubrangeVerb:
    57  		return true
    58  	}
    59  	return false
    60  }
    61  
    62  // ParseBucketCacheKey parses a string and returns BucketCacheKey.
    63  func ParseBucketCacheKey(key string) (BucketCacheKey, error) {
    64  	ck := BucketCacheKey{}
    65  	slice := strings.Split(key, ":")
    66  	if len(slice) < 2 {
    67  		return ck, ErrInvalidBucketCacheKeyFormat
    68  	}
    69  
    70  	verb := VerbType(slice[0])
    71  	if !IsValidVerb(verb) {
    72  		return BucketCacheKey{}, ErrInvalidBucketCacheKeyVerb
    73  	}
    74  
    75  	if verb == SubrangeVerb {
    76  		if len(slice) != 4 {
    77  			return BucketCacheKey{}, ErrInvalidBucketCacheKeyFormat
    78  		}
    79  
    80  		start, err := strconv.ParseInt(slice[2], 10, 64)
    81  		if err != nil {
    82  			return BucketCacheKey{}, ErrParseKeyInt
    83  		}
    84  
    85  		end, err := strconv.ParseInt(slice[3], 10, 64)
    86  		if err != nil {
    87  			return BucketCacheKey{}, ErrParseKeyInt
    88  		}
    89  
    90  		ck.Start = start
    91  		ck.End = end
    92  	} else {
    93  		if len(slice) != 2 {
    94  			return BucketCacheKey{}, ErrInvalidBucketCacheKeyFormat
    95  		}
    96  	}
    97  
    98  	ck.Verb = verb
    99  	ck.Name = slice[1]
   100  	return ck, nil
   101  }