forked from xphoenix/afina
-
Notifications
You must be signed in to change notification settings - Fork 0
Mt blocking #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LevKats
wants to merge
7
commits into
master
Choose a base branch
from
mt_blocking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Mt blocking #2
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c5d24f
first
LevKats 1d3d738
some fixes...
LevKats d902f4b
std c++11 + assert
LevKats f99b0c5
mt_blocking impl
LevKats 11fb3b4
mt_blocking impl
LevKats 3086302
storage fixed
LevKats 57f9ea3
fix finishing executions
LevKats File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,22 +1,154 @@ | ||
| #include "SimpleLRU.h" | ||
| #include <cassert> | ||
|
|
||
| namespace Afina { | ||
| namespace Backend { | ||
|
|
||
| bool SimpleLRU::_FreeSpace(std::size_t size) { | ||
| if (size > _max_size) { | ||
| return false; | ||
| } | ||
| while (_current_size + size > _max_size) { | ||
| Delete(_lru_head->key); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| void SimpleLRU::_PutToTail(std::unique_ptr<lru_node> &&node) { | ||
| if (!node->next) { | ||
| // only head exitst or it's tail | ||
| if (!_lru_head) { | ||
| // head | ||
| _lru_head = std::move(node); | ||
| } else { | ||
| // tail | ||
| node->prev->next = std::move(node); | ||
| } | ||
| } else if (!_lru_head) { | ||
| // only if we've cut head | ||
| _lru_head = std::move(node->next); | ||
| auto tail = node->prev; | ||
| tail->next = std::move(node); | ||
| _lru_head->prev->next.reset(); | ||
| } else { | ||
| node->next->prev = node->prev; | ||
| node->prev->next = std::move(node->next); | ||
| auto tail = _lru_head->prev; | ||
| _lru_head->prev = node.get(); | ||
| node->prev = tail; | ||
| tail->next = std::move(node); | ||
| } | ||
| } | ||
|
|
||
| void SimpleLRU::_PutWithoutCheck(const std::string &key, const std::string &value) { | ||
| assert(_FreeSpace(key.size() + value.size())); | ||
|
|
||
| auto new_node = std::unique_ptr<lru_node>(new lru_node(key, value)); | ||
|
|
||
| _lru_index.insert(std::make_pair(std::ref(new_node->key), std::ref(*new_node))); | ||
| _current_size += key.size() + value.size(); | ||
|
|
||
| if (!_lru_head) { | ||
| new_node->prev = new_node.get(); | ||
| new_node->next.reset(); | ||
| _lru_head = std::move(new_node); | ||
| } else { | ||
| new_node->prev = _lru_head->prev; | ||
| new_node->next.reset(); | ||
| _lru_head->prev->next = std::move(new_node); | ||
| _lru_head->prev = _lru_head->prev->next.get(); | ||
| } | ||
| } | ||
|
|
||
| // See MapBasedGlobalLockImpl.h | ||
| bool SimpleLRU::Put(const std::string &key, const std::string &value) { return false; } | ||
| bool SimpleLRU::Put(const std::string &key, const std::string &value) { | ||
| if (key.size() + value.size() > _max_size) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!Set(key, value)) { | ||
| _PutWithoutCheck(key, value); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // See MapBasedGlobalLockImpl.h | ||
| bool SimpleLRU::PutIfAbsent(const std::string &key, const std::string &value) { return false; } | ||
| bool SimpleLRU::PutIfAbsent(const std::string &key, const std::string &value) { | ||
| if (key.size() + value.size() > _max_size) { | ||
| return false; | ||
| } | ||
| if (_lru_index.find(std::ref(key)) == _lru_index.end()) { | ||
| _PutWithoutCheck(key, value); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // See MapBasedGlobalLockImpl.h | ||
| bool SimpleLRU::Set(const std::string &key, const std::string &value) { return false; } | ||
| bool SimpleLRU::Set(const std::string &key, const std::string &value) { | ||
| if (key.size() + value.size() > _max_size) { | ||
| return false; | ||
| } | ||
|
|
||
| auto iter = _lru_index.find(std::ref(key)); | ||
| if (iter == _lru_index.end()) { | ||
| return false; | ||
| } | ||
|
|
||
| auto &node = iter->second.get(); | ||
| auto ptr = std::move((&node == _lru_head.get()) ? _lru_head : node.prev->next); | ||
| _PutToTail(std::move(ptr)); | ||
| std::size_t free = value.size() > node.value.size() ? value.size() - node.value.size() : 0; | ||
| _FreeSpace(free); | ||
| _current_size += free; | ||
| node.value = value; | ||
| return true; | ||
| } | ||
|
|
||
| // See MapBasedGlobalLockImpl.h | ||
| bool SimpleLRU::Delete(const std::string &key) { return false; } | ||
| bool SimpleLRU::Delete(const std::string &key) { | ||
| auto iter = _lru_index.find(std::ref(key)); | ||
| if (iter != _lru_index.end()) { | ||
| auto &node = iter->second.get(); | ||
| _lru_index.erase(iter); | ||
| _current_size -= node.key.size() + node.value.size(); | ||
|
|
||
| if (node.prev == &node) { | ||
| // only head exists | ||
| _lru_head.reset(); | ||
| } else { | ||
| if (&node == _lru_head.get()) { | ||
| // deleting head | ||
| node.next->prev = node.prev; | ||
| _lru_head = std::move(node.next); | ||
| } else if (!node.next) { | ||
| // deleting tail | ||
| _lru_head->prev = node.prev; | ||
| node.prev->next.reset(); | ||
| } else { | ||
| node.next->prev = node.prev; | ||
| node.prev->next = std::move(node.next); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // See MapBasedGlobalLockImpl.h | ||
| bool SimpleLRU::Get(const std::string &key, std::string &value) { return false; } | ||
| bool SimpleLRU::Get(const std::string &key, std::string &value) { | ||
| auto iter = _lru_index.find(std::ref(key)); | ||
| if (iter == _lru_index.end()) { | ||
| return false; | ||
| } else { | ||
| auto &node = iter->second.get(); | ||
| auto ptr = std::move((&node == _lru_head.get()) ? _lru_head : node.prev->next); | ||
| _PutToTail(std::move(ptr)); | ||
| value = node.value; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| } // namespace Backend | ||
| } // namespace Afina |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это должен делать поток accept