lru_cache.h 4.5 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2022.09.02
//

#pragma once

#include <functional>
#include <unordered_set>

namespace common {

L
Longda Feng 已提交
22
template <typename Key, typename Value, typename Hash = std::hash<Key>, typename Pred = std::equal_to<Key>>
羽飞's avatar
羽飞 已提交
23 24 25 26
class LruCache {

  class ListNode {
  public:
L
Longda Feng 已提交
27
    Key key_;
羽飞's avatar
羽飞 已提交
28 29 30 31 32 33 34 35 36 37 38 39
    Value value_;

    ListNode *prev_ = nullptr;
    ListNode *next_ = nullptr;

  public:
    ListNode(const Key &key, const Value &value) : key_(key), value_(value)
    {}
  };

  class PListNodeHasher {
  public:
L
Longda Feng 已提交
40 41
    size_t operator()(ListNode *node) const
    {
羽飞's avatar
羽飞 已提交
42
      if (node == nullptr) {
L
Longda Feng 已提交
43
        return 0;
羽飞's avatar
羽飞 已提交
44 45 46 47 48 49 50 51 52 53
      }
      return hasher_(node->key_);
    }

  private:
    Hash hasher_;
  };

  class PListNodePredicator {
  public:
L
Longda Feng 已提交
54 55
    bool operator()(ListNode *const node1, ListNode *const node2) const
    {
羽飞's avatar
羽飞 已提交
56
      if (node1 == node2) {
L
Longda Feng 已提交
57
        return true;
羽飞's avatar
羽飞 已提交
58 59 60
      }

      if (node1 == nullptr || node2 == nullptr) {
L
Longda Feng 已提交
61
        return false;
羽飞's avatar
羽飞 已提交
62 63 64 65
      }

      return pred_(node1->key_, node2->key_);
    }
L
Longda Feng 已提交
66

羽飞's avatar
羽飞 已提交
67 68 69 70
  private:
    Pred pred_;
  };

L
Longda Feng 已提交
71
public:
羽飞's avatar
羽飞 已提交
72 73 74
  LruCache(size_t reserve = 0)
  {
    if (reserve > 0) {
L
Longda Feng 已提交
75
      searcher_.reserve(reserve);
羽飞's avatar
羽飞 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    }
  }

  ~LruCache()
  {
    destroy();
  }

  void destroy()
  {
    for (ListNode *node : searcher_) {
      delete node;
    }
    searcher_.clear();

    lru_front_ = nullptr;
L
Longda Feng 已提交
92
    lru_tail_ = nullptr;
羽飞's avatar
羽飞 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  }

  size_t count() const
  {
    return searcher_.size();
  }

  bool get(const Key &key, Value &value)
  {
    auto iter = searcher_.find((ListNode *)&key);
    if (iter == searcher_.end()) {
      return false;
    }

    lru_touch(*iter);
    value = (*iter)->value_;
    return true;
  }

  void put(const Key &key, const Value &value)
  {
    auto iter = searcher_.find((ListNode *)&key);
    if (iter != searcher_.end()) {
L
Longda Feng 已提交
116
      ListNode *ln = *iter;
羽飞's avatar
羽飞 已提交
117 118
      ln->value_ = value;
      lru_touch(ln);
L
Longda Feng 已提交
119
      return;
羽飞's avatar
羽飞 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    }

    ListNode *ln = new ListNode(key, value);
    lru_push(ln);
  }

  void remove(const Key &key)
  {
    auto iter = searcher_.find((ListNode *)&key);
    if (iter != searcher_.end()) {
      lru_remove(*iter);
    }
  }

  void pop(Value *&value)
  {
    // TODO
    value = nullptr;
  }

L
Longda Feng 已提交
140
  void foreach (std::function<bool(const Key &, const Value &)> func)
羽飞's avatar
羽飞 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  {
    for (ListNode *node = lru_front_; node != nullptr; node = node->next_) {
      bool ret = func(node->key_, node->value_);
      if (!ret) {
        break;
      }
    }
  }

  void foreach_reverse(std::function<bool(const Key &, const Value &)> func)
  {
    for (ListNode *node = lru_tail_; node != nullptr; node = node->prev_) {
      bool ret = func(node->key_, node->value_);
      if (!ret) {
        break;
      }
    }
  }

private:
  void lru_touch(ListNode *node)
  {
    // move node to front
    if (nullptr == node->prev_) {
      return;
    }

    node->prev_->next_ = node->next_;
L
Longda Feng 已提交
169

羽飞's avatar
羽飞 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    if (node->next_ != nullptr) {
      node->next_->prev_ = node->prev_;
    } else {
      lru_tail_ = node->prev_;
    }

    node->prev_ = nullptr;
    node->next_ = lru_front_;
    if (lru_front_ != nullptr) {
      lru_front_->prev_ = node;
    }
    lru_front_ = node;
  }

  void lru_push(ListNode *node)
  {
    // push front
    if (nullptr == lru_tail_) {
      lru_tail_ = node;
    }

    node->prev_ = nullptr;
    node->next_ = lru_front_;
    if (lru_front_ != nullptr) {
      lru_front_->prev_ = node;
    }

    lru_front_ = node;
    searcher_.insert(node);
  }

  void lru_remove(ListNode *node)
  {
    if (node->prev_ != nullptr) {
      node->prev_->next_ = node->next_;
    }

    if (node->next_ != nullptr) {
      node->next_->prev_ = node->prev_;
    }

    if (lru_front_ == node) {
      lru_front_ = node->next_;
    }
    if (lru_tail_ == node) {
      lru_tail_ = node->prev_;
    }

    searcher_.erase(node);
219
    delete node;
羽飞's avatar
羽飞 已提交
220 221 222 223
  }

private:
  using SearchType = std::unordered_set<ListNode *, PListNodeHasher, PListNodePredicator>;
L
Longda Feng 已提交
224 225 226
  SearchType searcher_;
  ListNode *lru_front_ = nullptr;
  ListNode *lru_tail_ = nullptr;
羽飞's avatar
羽飞 已提交
227 228
};

L
Longda Feng 已提交
229
}  // namespace common