-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: sitan liu <[email protected]>
- Loading branch information
Showing
10 changed files
with
377 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** Copyright 2020-2022 Alibaba Group Holding Limited. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#ifndef MODULES_FUSE_CACHE_MANAGER_MANAGER_H_ | ||
#define MODULES_FUSE_CACHE_MANAGER_MANAGER_H_ | ||
#include <iostream> | ||
#include <list> | ||
#include <memory> | ||
#include <type_traits> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
#include "arrow/buffer.h" | ||
#include "common/util/logging.h" | ||
#include "common/util/status.h" | ||
namespace vineyard { | ||
namespace fuse { | ||
|
||
namespace cache_manager { | ||
template <typename K, typename V> | ||
struct KeyValue { | ||
using KeyType = K; | ||
using ValType = std::shared_ptr<V>; | ||
const KeyType key; | ||
ValType value; | ||
KeyValue(KeyType k, ValType v) : key(k), value(v) { ; } | ||
KeyValue(const KeyValue<K, V>& kv) : key(kv.key), value(kv.value) { ; } | ||
}; | ||
|
||
template <typename KeyValue> | ||
class CacheManager { | ||
private: | ||
std::list<KeyValue> myList; | ||
std::unordered_map<typename KeyValue::KeyType, | ||
typename std::list<KeyValue>::iterator> | ||
myMap; | ||
size_t capacityBytes; | ||
size_t curBytes; | ||
void popToNBytes(size_t n); | ||
bool WithInCapacity(size_t data); | ||
|
||
public: | ||
explicit CacheManager(size_t capacity); | ||
CacheManager(); | ||
void resize(size_t capacity); | ||
Status put(const typename KeyValue::KeyType& key, | ||
typename KeyValue::ValType val); | ||
typename KeyValue::ValType get(const typename KeyValue::KeyType& key); | ||
bool has(const typename KeyValue::KeyType& key); | ||
std::list<KeyValue> getLinkedList(); | ||
typename KeyValue::ValType operator[](const typename KeyValue::KeyType& key); | ||
size_t getCurBytes(); | ||
size_t getCapacityBytes(); | ||
void destroy(); | ||
}; | ||
|
||
} // namespace cache_manager | ||
} // namespace fuse | ||
}; // namespace vineyard | ||
#endif // MODULES_FUSE_CACHE_MANAGER_MANAGER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#ifndef MODULES_FUSE_CACHE_MANAGER_MANAGER_HPP_ | ||
#define MODULES_FUSE_CACHE_MANAGER_MANAGER_HPP_ | ||
|
||
#include "fuse/cache_manager/manager.h" | ||
#include "arrow/buffer.h" | ||
|
||
namespace vineyard { | ||
namespace fuse { | ||
|
||
namespace cache_manager { | ||
template <typename KV> | ||
void CacheManager<KV>::popToNBytes(size_t n) { | ||
while (this->curBytes > n) { | ||
auto keyToBeDel = myList.back().key; | ||
auto dataToBeDel = myList.back().value; | ||
this->curBytes -= dataToBeDel->capacity(); | ||
myList.pop_back(); | ||
myMap.erase(keyToBeDel); | ||
DLOG(INFO) << "remove key: " << keyToBeDel << " value: " << dataToBeDel->ToString()<< " remaining bytes: "<<this->curBytes; | ||
} | ||
} | ||
template <class KV> | ||
bool CacheManager<KV>::WithInCapacity(size_t data) { | ||
return data <= capacityBytes; | ||
} | ||
template<class KV> | ||
CacheManager<KV>::CacheManager(size_t capacityBytes):capacityBytes(capacityBytes),curBytes(0){ | ||
} | ||
template<class KV> | ||
CacheManager<KV>::CacheManager():capacityBytes(0),curBytes(0){ | ||
} | ||
template<class KV> | ||
void CacheManager<KV>::resize(size_t targetCapacityBytes){ | ||
capacityBytes = targetCapacityBytes; | ||
} | ||
template<class KV> | ||
void CacheManager<KV>::destroy(){ | ||
this->~CacheManager(); | ||
} | ||
template<class KV> | ||
bool CacheManager<KV>::has(const typename KV::KeyType& key){ | ||
return myMap.find(key)!= myMap.end(); | ||
} | ||
template<class KV> | ||
typename KV::ValType CacheManager<KV>::operator[](const typename KV::KeyType& key) { | ||
return get(key); | ||
} | ||
|
||
template<class KV> | ||
size_t CacheManager<KV>::getCapacityBytes(){ | ||
return this->capacityBytes; | ||
} | ||
template<class KV> | ||
size_t CacheManager<KV>::getCurBytes(){ | ||
return this->curBytes; | ||
} | ||
template <class KV> | ||
Status CacheManager<KV>::put(const typename KV::KeyType& key, typename KV::ValType v) { | ||
|
||
if (WithInCapacity(v->capacity())) { | ||
|
||
auto found_map_iter = myMap.find(key); | ||
|
||
if (found_map_iter != myMap.end()) { | ||
DLOG(INFO) << "update key: " << key << " value: " << v->ToString()<<std::endl; | ||
|
||
auto found_key = found_map_iter->first; | ||
auto& found_kv = found_map_iter->second; | ||
|
||
curBytes -= found_kv->value->capacity(); | ||
popToNBytes(capacityBytes - v->capacity()); | ||
myList.splice(myList.begin(), this->myList, found_kv); | ||
found_kv->value = v; | ||
return Status::OK(); | ||
} else { | ||
DLOG(INFO) << "put key: " << key << " value: " << v->ToString()<<std::endl; | ||
popToNBytes(capacityBytes - v->capacity()); | ||
myList.emplace_front(key,v); | ||
// decltype(myMap[key])::nothing; | ||
myMap[key] = myList.begin(); | ||
this->curBytes += v->capacity(); | ||
return Status::OK(); | ||
} | ||
} else { | ||
DLOG(INFO)<<"this keyvalue is too large to put int"<<std::endl; | ||
return Status::NotEnoughMemory(""); | ||
} | ||
} | ||
template <class KV> | ||
|
||
std::list<KV> CacheManager<KV>::getLinkedList(){ | ||
return myList; | ||
|
||
} | ||
|
||
template <class KV> | ||
typename KV::ValType CacheManager<KV>::get(const typename KV::KeyType& key) { | ||
auto found_iter = myMap.find(key); | ||
if (found_iter == myMap.end()) // key doesn't exist | ||
{ | ||
DLOG(INFO)<< "not found key " << key; | ||
|
||
return nullptr;} | ||
DLOG(INFO)<< "found key " << key; | ||
|
||
myList.splice( | ||
myList.begin(), myList, | ||
found_iter->second); // move the node corresponding to key to front | ||
return found_iter->second->value; | ||
} | ||
} // namespace cache_manager | ||
|
||
} // namespace fuse | ||
} // namespace vineyard | ||
#endif // MODULES_FUSE_CACHE_MANAGER_MANAGER_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.