Commit 46455352 authored by starlord's avatar starlord
Browse files

format cache code


Former-commit-id: 99ff6affb78c09941a59c53183427a14b8ff00d1
parent 6ac9c1a0
Loading
Loading
Loading
Loading
+25 −15
Original line number Diff line number Diff line
@@ -37,12 +37,22 @@ public:
    Cache(int64_t capacity_gb, uint64_t cache_max_count);
    ~Cache() = default;

    int64_t usage() const { return usage_; }
    int64_t capacity() const { return capacity_; } //unit: BYTE
    int64_t usage() const {
        return usage_;
    }

    int64_t capacity() const {
        return capacity_;
    } //unit: BYTE
    void set_capacity(int64_t capacity); //unit: BYTE

    double freemem_percent() const { return freemem_percent_; };
    void set_freemem_percent(double percent) { freemem_percent_ = percent; }
    double freemem_percent() const {
        return freemem_percent_;
    }

    void set_freemem_percent(double percent) {
        freemem_percent_ = percent;
    }

    size_t size() const;
    bool exists(const std::string &key);
@@ -64,8 +74,8 @@ private:
    mutable std::mutex mutex_;
};

}   // cache
}   // milvus
}   // zilliz
} // namespace cache
} // namespace milvus
} // namespace zilliz

#include "cache/Cache.inl"
+31 −22
Original line number Diff line number Diff line
@@ -33,7 +33,8 @@ Cache<ItemObj>::Cache(int64_t capacity, uint64_t cache_max_count)
}

template<typename ItemObj>
void Cache<ItemObj>::set_capacity(int64_t capacity) {
void
Cache<ItemObj>::set_capacity(int64_t capacity) {
    if (capacity > 0) {
        capacity_ = capacity;
        free_memory();
@@ -41,19 +42,22 @@ void Cache<ItemObj>::set_capacity(int64_t capacity) {
}

template<typename ItemObj>
size_t Cache<ItemObj>::size() const {
size_t
Cache<ItemObj>::size() const {
    std::lock_guard<std::mutex> lock(mutex_);
    return lru_.size();
}

template<typename ItemObj>
bool Cache<ItemObj>::exists(const std::string& key) {
bool
Cache<ItemObj>::exists(const std::string &key) {
    std::lock_guard<std::mutex> lock(mutex_);
    return lru_.exists(key);
}

template<typename ItemObj>
ItemObj Cache<ItemObj>::get(const std::string& key) {
ItemObj
Cache<ItemObj>::get(const std::string &key) {
    std::lock_guard<std::mutex> lock(mutex_);
    if (!lru_.exists(key)) {
        return nullptr;
@@ -63,7 +67,8 @@ ItemObj Cache<ItemObj>::get(const std::string& key) {
}

template<typename ItemObj>
void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) {
void
Cache<ItemObj>::insert(const std::string &key, const ItemObj &item) {
    if (item == nullptr) {
        return;
    }
@@ -107,7 +112,8 @@ void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) {
}

template<typename ItemObj>
void Cache<ItemObj>::erase(const std::string& key) {
void
Cache<ItemObj>::erase(const std::string &key) {
    std::lock_guard<std::mutex> lock(mutex_);
    if (!lru_.exists(key)) {
        return;
@@ -122,7 +128,8 @@ void Cache<ItemObj>::erase(const std::string& key) {
}

template<typename ItemObj>
void Cache<ItemObj>::clear() {
void
Cache<ItemObj>::clear() {
    std::lock_guard<std::mutex> lock(mutex_);
    lru_.clear();
    usage_ = 0;
@@ -131,7 +138,8 @@ void Cache<ItemObj>::clear() {

/* free memory space when CACHE occupation exceed its capacity */
template<typename ItemObj>
void Cache<ItemObj>::free_memory() {
void
Cache<ItemObj>::free_memory() {
    if (usage_ <= capacity_) return;

    int64_t threshhold = capacity_ * freemem_percent_;
@@ -167,7 +175,8 @@ void Cache<ItemObj>::free_memory() {
}

template<typename ItemObj>
void Cache<ItemObj>::print() {
void
Cache<ItemObj>::print() {
    size_t cache_count = 0;
    {
        std::lock_guard<std::mutex> lock(mutex_);
@@ -179,7 +188,7 @@ void Cache<ItemObj>::print() {
    SERVER_LOG_DEBUG << "[Cache capacity]: " << capacity_ << " bytes";
}

}   // cache
}   // milvus
}   // zilliz
} // namespace cache
} // namespace milvus
} // namespace zilliz
+14 −12
Original line number Diff line number Diff line
@@ -22,6 +22,9 @@
#include "utils/Log.h"
#include "metrics/Metrics.h"

#include <string>
#include <memory>

namespace zilliz {
namespace milvus {
namespace cache {
@@ -56,9 +59,8 @@ protected:
    CachePtr cache_;
};


}
}
}
} // namespace cache
} // namespace milvus
} // namespace zilliz

#include "cache/CacheMgr.inl"
+34 −24
Original line number Diff line number Diff line
@@ -30,7 +30,8 @@ CacheMgr<ItemObj>::~CacheMgr() {
}

template<typename ItemObj>
uint64_t CacheMgr<ItemObj>::ItemCount() const {
uint64_t
CacheMgr<ItemObj>::ItemCount() const {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return 0;
@@ -40,7 +41,8 @@ uint64_t CacheMgr<ItemObj>::ItemCount() const {
}

template<typename ItemObj>
bool CacheMgr<ItemObj>::ItemExists(const std::string& key) {
bool
CacheMgr<ItemObj>::ItemExists(const std::string &key) {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return false;
@@ -50,7 +52,8 @@ bool CacheMgr<ItemObj>::ItemExists(const std::string& key) {
}

template<typename ItemObj>
ItemObj CacheMgr<ItemObj>::GetItem(const std::string& key) {
ItemObj
CacheMgr<ItemObj>::GetItem(const std::string &key) {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return nullptr;
@@ -60,7 +63,8 @@ ItemObj CacheMgr<ItemObj>::GetItem(const std::string& key) {
}

template<typename ItemObj>
void CacheMgr<ItemObj>::InsertItem(const std::string& key, const ItemObj& data) {
void
CacheMgr<ItemObj>::InsertItem(const std::string &key, const ItemObj &data) {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return;
@@ -71,7 +75,8 @@ void CacheMgr<ItemObj>::InsertItem(const std::string& key, const ItemObj& data)
}

template<typename ItemObj>
void CacheMgr<ItemObj>::EraseItem(const std::string& key) {
void
CacheMgr<ItemObj>::EraseItem(const std::string &key) {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return;
@@ -82,7 +87,8 @@ void CacheMgr<ItemObj>::EraseItem(const std::string& key) {
}

template<typename ItemObj>
void CacheMgr<ItemObj>::PrintInfo() {
void
CacheMgr<ItemObj>::PrintInfo() {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return;
@@ -92,7 +98,8 @@ void CacheMgr<ItemObj>::PrintInfo() {
}

template<typename ItemObj>
void CacheMgr<ItemObj>::ClearCache() {
void
CacheMgr<ItemObj>::ClearCache() {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return;
@@ -102,7 +109,8 @@ void CacheMgr<ItemObj>::ClearCache() {
}

template<typename ItemObj>
int64_t CacheMgr<ItemObj>::CacheUsage() const {
int64_t
CacheMgr<ItemObj>::CacheUsage() const {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return 0;
@@ -112,7 +120,8 @@ int64_t CacheMgr<ItemObj>::CacheUsage() const {
}

template<typename ItemObj>
int64_t CacheMgr<ItemObj>::CacheCapacity() const {
int64_t
CacheMgr<ItemObj>::CacheCapacity() const {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return 0;
@@ -122,7 +131,8 @@ int64_t CacheMgr<ItemObj>::CacheCapacity() const {
}

template<typename ItemObj>
void CacheMgr<ItemObj>::SetCapacity(int64_t capacity) {
void
CacheMgr<ItemObj>::SetCapacity(int64_t capacity) {
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return;
@@ -130,6 +140,6 @@ void CacheMgr<ItemObj>::SetCapacity(int64_t capacity) {
    cache_->set_capacity(capacity);
}

}
}
}
} // namespace cache
} // namespace milvus
} // namespace zilliz
+14 −10
Original line number Diff line number Diff line
@@ -16,10 +16,12 @@
// under the License.


#include "CpuCacheMgr.h"
#include "cache/CpuCacheMgr.h"
#include "server/Config.h"
#include "utils/Log.h"

#include <utility>

namespace zilliz {
namespace milvus {
namespace cache {
@@ -53,12 +55,14 @@ CpuCacheMgr::CpuCacheMgr() {
    }
}

CpuCacheMgr* CpuCacheMgr::GetInstance() {
CpuCacheMgr *
CpuCacheMgr::GetInstance() {
    static CpuCacheMgr s_mgr;
    return &s_mgr;
}

engine::VecIndexPtr CpuCacheMgr::GetIndex(const std::string& key) {
engine::VecIndexPtr
CpuCacheMgr::GetIndex(const std::string &key) {
    DataObjPtr obj = GetItem(key);
    if (obj != nullptr) {
        return obj->data();
@@ -67,6 +71,6 @@ engine::VecIndexPtr CpuCacheMgr::GetIndex(const std::string& key) {
    return nullptr;
}

}
}
}
 No newline at end of file
} // namespace cache
} // namespace milvus
} // namespace zilliz
Loading