github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/nbs/manifest_cache.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package nbs
    23  
    24  import (
    25  	"container/list"
    26  	"fmt"
    27  	"time"
    28  
    29  	"sync"
    30  )
    31  
    32  func newManifestCache(maxSize uint64) *manifestCache {
    33  	return &manifestCache{
    34  		maxSize: maxSize,
    35  		cache:   map[string]manifestCacheEntry{},
    36  		mu:      &sync.Mutex{},
    37  	}
    38  }
    39  
    40  type manifestCacheEntry struct {
    41  	lruEntry *list.Element
    42  	contents manifestContents
    43  	t        time.Time
    44  }
    45  
    46  type manifestCache struct {
    47  	totalSize uint64
    48  	maxSize   uint64
    49  	mu        *sync.Mutex
    50  	lru       list.List
    51  	cache     map[string]manifestCacheEntry
    52  }
    53  
    54  // Get() checks the searches the cache for an entry. If it exists, it moves it's
    55  // lru entry to the back of the queue and returns (value, true). Otherwise, it
    56  // returns (nil, false).
    57  func (mc *manifestCache) Get(db string) (contents manifestContents, t time.Time, present bool) {
    58  	mc.mu.Lock()
    59  	defer mc.mu.Unlock()
    60  
    61  	if entry, ok := mc.entry(db); ok {
    62  		contents, t, present = entry.contents, entry.t, true
    63  	}
    64  	return
    65  }
    66  
    67  // entry() checks if the value is in the cache. If not in the cache, it returns an
    68  // empty manifestCacheEntry and false. It it is in the cache, it moves it to
    69  // to the back of lru and returns the entry and true.
    70  func (mc *manifestCache) entry(key string) (manifestCacheEntry, bool) {
    71  	entry, ok := mc.cache[key]
    72  	if !ok {
    73  		return manifestCacheEntry{}, false
    74  	}
    75  	mc.lru.MoveToBack(entry.lruEntry)
    76  	return entry, true
    77  }
    78  
    79  // Put inserts |contents| into the cache with the key |db|, replacing any
    80  // currently cached value. Put() will add this element to the cache at the
    81  // back of the queue as long it's size does not exceed maxSize. If the
    82  // addition of this entry causes the size of the cache to exceed maxSize, the
    83  // necessary entries at the front of the queue will be deleted in order to
    84  // keep the total cache size below maxSize. |t| must be *prior* to initiating
    85  // the call which read/wrote |contents|.
    86  func (mc *manifestCache) Put(db string, contents manifestContents, t time.Time) error {
    87  	mc.mu.Lock()
    88  	defer mc.mu.Unlock()
    89  
    90  	if entry, ok := mc.entry(db); ok {
    91  		mc.totalSize -= entry.contents.size()
    92  		mc.lru.Remove(entry.lruEntry)
    93  		delete(mc.cache, db)
    94  	}
    95  
    96  	if contents.size() <= mc.maxSize {
    97  		newEl := mc.lru.PushBack(db)
    98  		ce := manifestCacheEntry{lruEntry: newEl, contents: contents, t: t}
    99  		mc.cache[db] = ce
   100  		mc.totalSize += ce.contents.size()
   101  		for el := mc.lru.Front(); el != nil && mc.totalSize > mc.maxSize; {
   102  			key1 := el.Value.(string)
   103  			ce, ok := mc.cache[key1]
   104  			if !ok {
   105  				return fmt.Errorf("manifestCache is missing expected value for %s", key1)
   106  			}
   107  			next := el.Next()
   108  			delete(mc.cache, key1)
   109  			mc.totalSize -= ce.contents.size()
   110  			mc.lru.Remove(el)
   111  			el = next
   112  		}
   113  	}
   114  
   115  	return nil
   116  }