|
|
|
@ -3733,167 +3733,169 @@ void FileManager::destroy_query(int32 file_id) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FileManager::memory_cleanup() {
|
|
|
|
|
if (file_id_info_.size() > 1000 || empty_file_ids_.size() < 200) {
|
|
|
|
|
is_closed_ = true;
|
|
|
|
|
auto time = std::time(nullptr);
|
|
|
|
|
is_closed_ = true;
|
|
|
|
|
auto time = std::time(nullptr);
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < file_id_info_.size(); i++) {
|
|
|
|
|
auto main_node_id = file_id_info_[i].node_id_;
|
|
|
|
|
if (main_node_id != 0) {
|
|
|
|
|
auto &node = file_nodes_[main_node_id];
|
|
|
|
|
if (node != nullptr && ((int32) i) == node->main_file_id_.fast_get()) {
|
|
|
|
|
if (time - node->main_file_id_.get_time() > 20 /* MAIN FILE TTL */) {
|
|
|
|
|
node->main_file_id_.reset_time();
|
|
|
|
|
for (unsigned int i = 0; i < file_id_info_.size(); i++) {
|
|
|
|
|
auto main_node_id = file_id_info_[i].node_id_;
|
|
|
|
|
if (main_node_id != 0) {
|
|
|
|
|
auto &node = file_nodes_[main_node_id];
|
|
|
|
|
|
|
|
|
|
for (auto &file_id : node->file_ids_) {
|
|
|
|
|
/* DESTROY ASSOCIATED QUERIES */
|
|
|
|
|
destroy_query(file_id.fast_get());
|
|
|
|
|
if (node != nullptr &&
|
|
|
|
|
node->download_priority_ == 0 &&
|
|
|
|
|
((int32) i) == node->main_file_id_.fast_get()) {
|
|
|
|
|
if (time - node->main_file_id_.get_time() > 20 /* MAIN FILE TTL */) {
|
|
|
|
|
node->main_file_id_.reset_time();
|
|
|
|
|
|
|
|
|
|
/* DESTROY ASSOCIATED NODE */
|
|
|
|
|
auto &ref_node_id = file_id_info_[file_id.fast_get()].node_id_;
|
|
|
|
|
if (ref_node_id != 0 && ref_node_id != main_node_id) {
|
|
|
|
|
auto &ref_node = file_nodes_[ref_node_id];
|
|
|
|
|
if (ref_node != nullptr) {
|
|
|
|
|
context_->destroy_file_source(file_id);
|
|
|
|
|
ref_node.reset();
|
|
|
|
|
}
|
|
|
|
|
empty_node_ids_.push_back(ref_node_id);
|
|
|
|
|
file_nodes_[ref_node_id] = nullptr;
|
|
|
|
|
for (auto &file_id : node->file_ids_) {
|
|
|
|
|
/* DESTROY ASSOCIATED QUERIES */
|
|
|
|
|
destroy_query(file_id.fast_get());
|
|
|
|
|
|
|
|
|
|
/* DESTROY ASSOCIATED NODE */
|
|
|
|
|
auto &ref_node_id = file_id_info_[file_id.fast_get()].node_id_;
|
|
|
|
|
if (ref_node_id != 0 && ref_node_id != main_node_id) {
|
|
|
|
|
auto &ref_node = file_nodes_[ref_node_id];
|
|
|
|
|
if (ref_node != nullptr) {
|
|
|
|
|
context_->destroy_file_source(file_id);
|
|
|
|
|
ref_node.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY ASSOCIATED FILE */
|
|
|
|
|
empty_file_ids_.push_back(file_id.fast_get());
|
|
|
|
|
file_id_info_[file_id.fast_get()] = FileIdInfo();
|
|
|
|
|
empty_node_ids_.push_back(ref_node_id);
|
|
|
|
|
file_nodes_[ref_node_id] = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY MAIN QUERY */
|
|
|
|
|
destroy_query(i);
|
|
|
|
|
|
|
|
|
|
/* DESTROY FILE REFERENCE */
|
|
|
|
|
context_->destroy_file_source(node->main_file_id_);
|
|
|
|
|
|
|
|
|
|
/* DESTROY MAIN FILE */
|
|
|
|
|
empty_file_ids_.push_back(i);
|
|
|
|
|
file_id_info_[i] = FileIdInfo();
|
|
|
|
|
|
|
|
|
|
/* DESTROY MAIN NODE */
|
|
|
|
|
node.reset();
|
|
|
|
|
file_nodes_[main_node_id] = nullptr;
|
|
|
|
|
empty_node_ids_.push_back(main_node_id);
|
|
|
|
|
/* DESTROY ASSOCIATED FILE */
|
|
|
|
|
empty_file_ids_.push_back(file_id.fast_get());
|
|
|
|
|
file_id_info_[file_id.fast_get()] = FileIdInfo();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID FILES */
|
|
|
|
|
for (unsigned int i = 0; i < file_id_info_.size(); i++) {
|
|
|
|
|
auto &file = file_id_info_[i];
|
|
|
|
|
|
|
|
|
|
if (file.node_id_ != 0) {
|
|
|
|
|
auto &node = file_nodes_[file.node_id_];
|
|
|
|
|
|
|
|
|
|
if (node == nullptr) {
|
|
|
|
|
/* DESTROY MAIN QUERY */
|
|
|
|
|
destroy_query(i);
|
|
|
|
|
context_->destroy_file_source({(int32) i, 0});
|
|
|
|
|
|
|
|
|
|
/* DESTROY FILE REFERENCE */
|
|
|
|
|
context_->destroy_file_source(node->main_file_id_);
|
|
|
|
|
|
|
|
|
|
/* DESTROY MAIN FILE */
|
|
|
|
|
empty_file_ids_.push_back(i);
|
|
|
|
|
file_id_info_[i] = FileIdInfo();
|
|
|
|
|
|
|
|
|
|
/* DESTROY MAIN NODE */
|
|
|
|
|
node.reset();
|
|
|
|
|
file_nodes_[main_node_id] = nullptr;
|
|
|
|
|
empty_node_ids_.push_back(main_node_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID NODES */
|
|
|
|
|
for (unsigned int i = 0; i < file_nodes_.size(); i++) {
|
|
|
|
|
auto &node = file_nodes_[i];
|
|
|
|
|
|
|
|
|
|
if (node != nullptr) {
|
|
|
|
|
auto invalid = file_id_info_[node->main_file_id_.fast_get()].node_id_ != ((int32) i);
|
|
|
|
|
|
|
|
|
|
if (!invalid) {
|
|
|
|
|
for (auto &file : node->file_ids_) {
|
|
|
|
|
if (file_id_info_[file.fast_get()].node_id_ != ((int32) i)) {
|
|
|
|
|
invalid = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (invalid) {
|
|
|
|
|
for (auto &file : node->file_ids_) {
|
|
|
|
|
destroy_query(file.fast_get());
|
|
|
|
|
context_->destroy_file_source(file);
|
|
|
|
|
empty_file_ids_.push_back(file.fast_get());
|
|
|
|
|
file_id_info_[file.fast_get()] = FileIdInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
empty_node_ids_.push_back(i);
|
|
|
|
|
file_nodes_[i] = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID file_hash_to_file_id_ */
|
|
|
|
|
{
|
|
|
|
|
auto it = file_hash_to_file_id_.begin();
|
|
|
|
|
while (it != file_hash_to_file_id_.end()) {
|
|
|
|
|
auto &file = file_id_info_[it->second.fast_get()];
|
|
|
|
|
if (file_nodes_[file.node_id_] == nullptr) {
|
|
|
|
|
file_hash_to_file_id_.erase(it++);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID local_location_to_file_id_ */
|
|
|
|
|
{
|
|
|
|
|
auto it = local_location_to_file_id_.begin();
|
|
|
|
|
while (it != local_location_to_file_id_.end()) {
|
|
|
|
|
auto &file = file_id_info_[it->second.fast_get()];
|
|
|
|
|
if (file_nodes_[file.node_id_] == nullptr) {
|
|
|
|
|
it = local_location_to_file_id_.erase(it++);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID generate_location_to_file_id_ */
|
|
|
|
|
{
|
|
|
|
|
auto it = generate_location_to_file_id_.begin();
|
|
|
|
|
while (it != generate_location_to_file_id_.end()) {
|
|
|
|
|
auto &file = file_id_info_[it->second.fast_get()];
|
|
|
|
|
if (file_nodes_[file.node_id_] == nullptr) {
|
|
|
|
|
it = generate_location_to_file_id_.erase(it++);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID remote_location_info_ */
|
|
|
|
|
{
|
|
|
|
|
auto map = remote_location_info_.get_map();
|
|
|
|
|
auto it = map.begin();
|
|
|
|
|
while (it != map.end()) {
|
|
|
|
|
auto &file = file_id_info_[it->first.file_id_.fast_get()];
|
|
|
|
|
if (file_nodes_[file.node_id_] == nullptr) {
|
|
|
|
|
remote_location_info_.erase(it->second);
|
|
|
|
|
map.erase(it++);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file_hash_to_file_id_.rehash(0);
|
|
|
|
|
file_id_info_.shrink_to_fit();
|
|
|
|
|
empty_file_ids_.shrink_to_fit();
|
|
|
|
|
empty_node_ids_.shrink_to_fit();
|
|
|
|
|
|
|
|
|
|
LOG(ERROR) << empty_file_ids_.size() << " empty ids and " << queries_container_.size() << " running queries";
|
|
|
|
|
is_closed_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID FILES */
|
|
|
|
|
for (unsigned int i = 0; i < file_id_info_.size(); i++) {
|
|
|
|
|
auto &file = file_id_info_[i];
|
|
|
|
|
|
|
|
|
|
if (file.node_id_ != 0) {
|
|
|
|
|
auto &node = file_nodes_[file.node_id_];
|
|
|
|
|
|
|
|
|
|
if (node == nullptr) {
|
|
|
|
|
destroy_query(i);
|
|
|
|
|
context_->destroy_file_source({(int32) i, 0});
|
|
|
|
|
empty_file_ids_.push_back(i);
|
|
|
|
|
file_id_info_[i] = FileIdInfo();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID NODES */
|
|
|
|
|
for (unsigned int i = 0; i < file_nodes_.size(); i++) {
|
|
|
|
|
auto &node = file_nodes_[i];
|
|
|
|
|
|
|
|
|
|
if (node != nullptr) {
|
|
|
|
|
auto invalid = file_id_info_[node->main_file_id_.fast_get()].node_id_ != ((int32) i);
|
|
|
|
|
|
|
|
|
|
if (!invalid) {
|
|
|
|
|
for (auto &file : node->file_ids_) {
|
|
|
|
|
if (file_id_info_[file.fast_get()].node_id_ != ((int32) i)) {
|
|
|
|
|
invalid = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (invalid) {
|
|
|
|
|
for (auto &file : node->file_ids_) {
|
|
|
|
|
destroy_query(file.fast_get());
|
|
|
|
|
context_->destroy_file_source(file);
|
|
|
|
|
empty_file_ids_.push_back(file.fast_get());
|
|
|
|
|
file_id_info_[file.fast_get()] = FileIdInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
empty_node_ids_.push_back(i);
|
|
|
|
|
file_nodes_[i] = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID file_hash_to_file_id_ */
|
|
|
|
|
{
|
|
|
|
|
auto it = file_hash_to_file_id_.begin();
|
|
|
|
|
while (it != file_hash_to_file_id_.end()) {
|
|
|
|
|
auto &file = file_id_info_[it->second.fast_get()];
|
|
|
|
|
if (file_nodes_[file.node_id_] == nullptr) {
|
|
|
|
|
file_hash_to_file_id_.erase(it++);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID local_location_to_file_id_ */
|
|
|
|
|
{
|
|
|
|
|
auto it = local_location_to_file_id_.begin();
|
|
|
|
|
while (it != local_location_to_file_id_.end()) {
|
|
|
|
|
auto &file = file_id_info_[it->second.fast_get()];
|
|
|
|
|
if (file_nodes_[file.node_id_] == nullptr) {
|
|
|
|
|
it = local_location_to_file_id_.erase(it++);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID generate_location_to_file_id_ */
|
|
|
|
|
{
|
|
|
|
|
auto it = generate_location_to_file_id_.begin();
|
|
|
|
|
while (it != generate_location_to_file_id_.end()) {
|
|
|
|
|
auto &file = file_id_info_[it->second.fast_get()];
|
|
|
|
|
if (file_nodes_[file.node_id_] == nullptr) {
|
|
|
|
|
it = generate_location_to_file_id_.erase(it++);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DESTROY INVALID remote_location_info_ */
|
|
|
|
|
{
|
|
|
|
|
auto map = remote_location_info_.get_map();
|
|
|
|
|
auto it = map.begin();
|
|
|
|
|
while (it != map.end()) {
|
|
|
|
|
auto &file = file_id_info_[it->first.file_id_.fast_get()];
|
|
|
|
|
if (file_nodes_[file.node_id_] == nullptr) {
|
|
|
|
|
remote_location_info_.erase(it->second);
|
|
|
|
|
map.erase(it++);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file_hash_to_file_id_.rehash(0);
|
|
|
|
|
file_id_info_.shrink_to_fit();
|
|
|
|
|
empty_file_ids_.shrink_to_fit();
|
|
|
|
|
empty_node_ids_.shrink_to_fit();
|
|
|
|
|
|
|
|
|
|
LOG(ERROR) << empty_file_ids_.size() << " empty ids and " << queries_container_.size() << " running queries";
|
|
|
|
|
is_closed_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileManager::tear_down() {
|
|
|
|
|