|
| 1 | +// Copyright 2019-present Open Networking Foundation. |
| 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 | +package _map //nolint:golint |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "github.com/hashicorp/golang-lru" |
| 20 | + "sync" |
| 21 | +) |
| 22 | + |
| 23 | +// newCachingMap returns a decorated map that caches updates to the given map |
| 24 | +func newCachingMap(_map Map, size int) (Map, error) { |
| 25 | + cache, err := lru.New(size) |
| 26 | + if err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + cachingMap := &cachingMap{ |
| 30 | + delegatingMap: newDelegatingMap(_map), |
| 31 | + pending: make(map[string]*cachedEntry), |
| 32 | + cache: cache, |
| 33 | + } |
| 34 | + if err := cachingMap.open(); err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + return cachingMap, nil |
| 38 | +} |
| 39 | + |
| 40 | +// cachingMap is an implementation of the Map interface that caches entries |
| 41 | +type cachingMap struct { |
| 42 | + *delegatingMap |
| 43 | + cancel context.CancelFunc |
| 44 | + pending map[string]*cachedEntry |
| 45 | + cache *lru.Cache |
| 46 | + cacheVersion int64 |
| 47 | + mu sync.RWMutex |
| 48 | +} |
| 49 | + |
| 50 | +// open opens the map listeners |
| 51 | +func (m *cachingMap) open() error { |
| 52 | + ch := make(chan *Event) |
| 53 | + ctx, cancel := context.WithCancel(context.Background()) |
| 54 | + m.mu.Lock() |
| 55 | + m.cancel = cancel |
| 56 | + m.mu.Unlock() |
| 57 | + if err := m.delegatingMap.Watch(ctx, ch, WithReplay()); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + go func() { |
| 61 | + for event := range ch { |
| 62 | + m.cacheUpdate(event.Entry, event.Type == EventRemoved) |
| 63 | + } |
| 64 | + }() |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// cacheUpdate caches the given updated entry |
| 69 | +func (m *cachingMap) cacheUpdate(update *Entry, tombstone bool) { |
| 70 | + m.mu.Lock() |
| 71 | + defer m.mu.Unlock() |
| 72 | + |
| 73 | + // If the update version is less than the cache version, the cache contains |
| 74 | + // more recent updates. Ignore the update. |
| 75 | + if update.Version <= m.cacheVersion { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + // If the pending entry is newer than the update entry, the update can be ignored. |
| 80 | + // Otherwise, remove the entry from the pending cache if present. |
| 81 | + if pending, ok := m.pending[update.Key]; ok { |
| 82 | + if pending.Version > update.Version { |
| 83 | + return |
| 84 | + } |
| 85 | + delete(m.pending, update.Key) |
| 86 | + } |
| 87 | + |
| 88 | + // If the entry is a tombstone, remove it from the cache, otherwise insert it. |
| 89 | + if tombstone { |
| 90 | + m.cache.Remove(update.Key) |
| 91 | + } else { |
| 92 | + m.cache.Add(update.Key, update) |
| 93 | + } |
| 94 | + |
| 95 | + // Update the cache version. |
| 96 | + m.cacheVersion = update.Version |
| 97 | +} |
| 98 | + |
| 99 | +// cacheRead caches the given read entry |
| 100 | +func (m *cachingMap) cacheRead(read *Entry, tombstone bool) { |
| 101 | + m.mu.Lock() |
| 102 | + defer m.mu.Unlock() |
| 103 | + |
| 104 | + // If the entry version is less than the cache version, ignore the update. The entry will |
| 105 | + // have been cached as an update. |
| 106 | + if read.Version <= m.cacheVersion { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + // The pending cache contains the most recent known state for the entry. |
| 111 | + // If the read entry is newer than the pending entry for the key, update |
| 112 | + // the pending cache. |
| 113 | + if pending, ok := m.pending[read.Key]; !ok || read.Version > pending.Version { |
| 114 | + m.pending[read.Key] = &cachedEntry{ |
| 115 | + Entry: read, |
| 116 | + tombstone: tombstone, |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// getCache gets a cached entry |
| 122 | +func (m *cachingMap) getCache(key string) (*Entry, bool) { |
| 123 | + m.mu.RLock() |
| 124 | + defer m.mu.RUnlock() |
| 125 | + |
| 126 | + // The pending cache contains the most recent known states. If the entry is present |
| 127 | + // in the pending cache, return it rather than using the LRU cache. |
| 128 | + if entry, ok := m.pending[key]; ok { |
| 129 | + if entry.tombstone { |
| 130 | + return nil, true |
| 131 | + } |
| 132 | + return entry.Entry, true |
| 133 | + } |
| 134 | + |
| 135 | + // If the entry is present in the LRU cache, return it. |
| 136 | + if entry, ok := m.cache.Get(key); ok { |
| 137 | + return entry.(*Entry), true |
| 138 | + } |
| 139 | + return nil, false |
| 140 | +} |
| 141 | + |
| 142 | +func (m *cachingMap) Get(ctx context.Context, key string, opts ...GetOption) (*Entry, error) { |
| 143 | + // If the entry is already in the cache, return it |
| 144 | + if entry, ok := m.getCache(key); ok { |
| 145 | + return entry, nil |
| 146 | + } |
| 147 | + |
| 148 | + // Otherwise, fetch the entry from the underlying map |
| 149 | + entry, err := m.delegatingMap.Get(ctx, key, opts...) |
| 150 | + if err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + |
| 154 | + // Update the cache if necessary |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + m.cacheRead(entry, entry.Value == nil) |
| 159 | + return entry, nil |
| 160 | +} |
| 161 | + |
| 162 | +func (m *cachingMap) Put(ctx context.Context, key string, value []byte, opts ...PutOption) (*Entry, error) { |
| 163 | + // Put the entry in the map using the underlying map delegate |
| 164 | + entry, err := m.delegatingMap.Put(ctx, key, value, opts...) |
| 165 | + if err != nil { |
| 166 | + return nil, err |
| 167 | + } |
| 168 | + |
| 169 | + // Update the cache if necessary |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + m.cacheRead(entry, false) |
| 174 | + return entry, nil |
| 175 | +} |
| 176 | + |
| 177 | +func (m *cachingMap) Remove(ctx context.Context, key string, opts ...RemoveOption) (*Entry, error) { |
| 178 | + // Remove the entry from the map using the underlying map delegate |
| 179 | + entry, err := m.delegatingMap.Remove(ctx, key, opts...) |
| 180 | + if err != nil { |
| 181 | + return nil, err |
| 182 | + } |
| 183 | + |
| 184 | + // Update the cache if necessary |
| 185 | + if err != nil { |
| 186 | + return nil, err |
| 187 | + } |
| 188 | + m.cacheRead(entry, true) |
| 189 | + return entry, nil |
| 190 | +} |
| 191 | + |
| 192 | +func (m *cachingMap) Close(ctx context.Context) error { |
| 193 | + m.mu.Lock() |
| 194 | + if m.cancel != nil { |
| 195 | + m.cancel() |
| 196 | + } |
| 197 | + m.mu.Unlock() |
| 198 | + return m.delegatingMap.Close(ctx) |
| 199 | +} |
| 200 | + |
| 201 | +// cachedEntry is a cached entry |
| 202 | +type cachedEntry struct { |
| 203 | + *Entry |
| 204 | + tombstone bool |
| 205 | +} |
0 commit comments