/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of 
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * 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 FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#ifndef METADEF_CXX_GRAPH_CACHE_POLICY_AGING_POLICY_LRU_K_H
#define METADEF_CXX_GRAPH_CACHE_POLICY_AGING_POLICY_LRU_K_H
#include "graph/cache_policy/aging_policy.h"
#include "graph/cache_policy/policy_register.h"

namespace ge {
class AgingPolicyLruK : public AgingPolicy {
 public:
  AgingPolicyLruK() : depth_(kDefaultCacheQueueDepth) {}
  explicit AgingPolicyLruK(size_t depth) : depth_(depth) {}
  AgingPolicyLruK(size_t k_times, size_t depth) : k_times_(k_times), depth_(depth) {}
  ~AgingPolicyLruK() override = default;

  void SetCachedAgingDepth(size_t depth) override {
    depth_ = depth;
  }
  bool IsReadyToAddCache(const CacheHashKey hash_key, const CacheDescPtr &cache_desc) override {
    return IsCacheDescAppearKTimes(hash_key, cache_desc);
  }
  std::vector<CacheItemId> DoAging(const CacheState &cache_state) const override;

 private:
  bool IsCacheDescAppearKTimes(const CacheHashKey hash_key, const CacheDescPtr &cache_desc);
 private:
  size_t k_times_ = 2U;
  size_t depth_;
  // todo 历史缓存队列的老化
  std::mutex hash_2_cache_descs_and_count_mu_;
  std::unordered_map<CacheHashKey, std::vector<std::pair<const CacheDescPtr, size_t>>> hash_2_cache_descs_and_count_;
};
REGISTER_AGING_POLICY_CREATOR(AgingPolicyType::AGING_POLICY_LRU_K,
                              []() { return std::make_shared<AgingPolicyLruK>(); });
}
#endif  // METADEF_CXX_GRAPH_CACHE_POLICY_AGING_POLICY_LRU_K_H