mirror of
				https://github.com/ton-blockchain/ton
				synced 2025-03-09 15:40:10 +00:00 
			
		
		
		
	Add archive manager index (#685)
* Optimize get_file_desc_by_ seqno/lt/ut * Optimize get_next_file_desc --------- Co-authored-by: SpyCheese <mikle98@yandex.ru>
This commit is contained in:
		
							parent
							
								
									dd8658c4db
								
							
						
					
					
						commit
						8bc20ede2e
					
				
					 2 changed files with 260 additions and 176 deletions
				
			
		| 
						 | 
				
			
			@ -74,7 +74,7 @@ void ArchiveManager::add_handle(BlockHandle handle, td::Promise<td::Unit> promis
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
void ArchiveManager::update_handle(BlockHandle handle, td::Promise<td::Unit> promise) {
 | 
			
		||||
  FileDescription *f;
 | 
			
		||||
  const FileDescription *f;
 | 
			
		||||
  if (handle->handle_moved_to_archive()) {
 | 
			
		||||
    CHECK(handle->inited_unix_time());
 | 
			
		||||
    if (!handle->need_flush()) {
 | 
			
		||||
| 
						 | 
				
			
			@ -439,15 +439,15 @@ void ArchiveManager::check_persistent_state(BlockIdExt block_id, BlockIdExt mast
 | 
			
		|||
 | 
			
		||||
void ArchiveManager::get_block_by_unix_time(AccountIdPrefixFull account_id, UnixTime ts,
 | 
			
		||||
                                            td::Promise<ConstBlockHandle> promise) {
 | 
			
		||||
  auto f = get_file_desc_by_unix_time(account_id, ts, false);
 | 
			
		||||
  if (f) {
 | 
			
		||||
    auto n = f;
 | 
			
		||||
    do {
 | 
			
		||||
      n = get_next_file_desc(n);
 | 
			
		||||
    } while (n != nullptr && !n->has_account_prefix(account_id));
 | 
			
		||||
  auto f1 = get_file_desc_by_unix_time(account_id, ts, false);
 | 
			
		||||
  auto f2 = get_next_file_desc(f1, account_id, false);
 | 
			
		||||
  if (!f1) {
 | 
			
		||||
    std::swap(f1, f2);
 | 
			
		||||
  }
 | 
			
		||||
  if (f1) {
 | 
			
		||||
    td::actor::ActorId<ArchiveSlice> aid;
 | 
			
		||||
    if (n) {
 | 
			
		||||
      aid = n->file_actor_id();
 | 
			
		||||
    if (f2) {
 | 
			
		||||
      aid = f2->file_actor_id();
 | 
			
		||||
    }
 | 
			
		||||
    auto P = td::PromiseCreator::lambda(
 | 
			
		||||
        [aid, account_id, ts, promise = std::move(promise)](td::Result<ConstBlockHandle> R) mutable {
 | 
			
		||||
| 
						 | 
				
			
			@ -457,7 +457,7 @@ void ArchiveManager::get_block_by_unix_time(AccountIdPrefixFull account_id, Unix
 | 
			
		|||
            td::actor::send_closure(aid, &ArchiveSlice::get_block_by_unix_time, account_id, ts, std::move(promise));
 | 
			
		||||
          }
 | 
			
		||||
        });
 | 
			
		||||
    td::actor::send_closure(f->file_actor_id(), &ArchiveSlice::get_block_by_unix_time, account_id, ts, std::move(P));
 | 
			
		||||
    td::actor::send_closure(f1->file_actor_id(), &ArchiveSlice::get_block_by_unix_time, account_id, ts, std::move(P));
 | 
			
		||||
  } else {
 | 
			
		||||
    promise.set_error(td::Status::Error(ErrorCode::notready, "ts not in db"));
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			@ -465,15 +465,15 @@ void ArchiveManager::get_block_by_unix_time(AccountIdPrefixFull account_id, Unix
 | 
			
		|||
 | 
			
		||||
void ArchiveManager::get_block_by_lt(AccountIdPrefixFull account_id, LogicalTime lt,
 | 
			
		||||
                                     td::Promise<ConstBlockHandle> promise) {
 | 
			
		||||
  auto f = get_file_desc_by_lt(account_id, lt, false);
 | 
			
		||||
  if (f) {
 | 
			
		||||
    auto n = f;
 | 
			
		||||
    do {
 | 
			
		||||
      n = get_next_file_desc(n);
 | 
			
		||||
    } while (n != nullptr && !n->has_account_prefix(account_id));
 | 
			
		||||
  auto f1 = get_file_desc_by_lt(account_id, lt, false);
 | 
			
		||||
  auto f2 = get_next_file_desc(f1, account_id, false);
 | 
			
		||||
  if (!f1) {
 | 
			
		||||
    std::swap(f1, f2);
 | 
			
		||||
  }
 | 
			
		||||
  if (f1) {
 | 
			
		||||
    td::actor::ActorId<ArchiveSlice> aid;
 | 
			
		||||
    if (n) {
 | 
			
		||||
      aid = n->file_actor_id();
 | 
			
		||||
    if (f2) {
 | 
			
		||||
      aid = f2->file_actor_id();
 | 
			
		||||
    }
 | 
			
		||||
    auto P = td::PromiseCreator::lambda(
 | 
			
		||||
        [aid, account_id, lt, promise = std::move(promise)](td::Result<ConstBlockHandle> R) mutable {
 | 
			
		||||
| 
						 | 
				
			
			@ -483,7 +483,7 @@ void ArchiveManager::get_block_by_lt(AccountIdPrefixFull account_id, LogicalTime
 | 
			
		|||
            td::actor::send_closure(aid, &ArchiveSlice::get_block_by_lt, account_id, lt, std::move(promise));
 | 
			
		||||
          }
 | 
			
		||||
        });
 | 
			
		||||
    td::actor::send_closure(f->file_actor_id(), &ArchiveSlice::get_block_by_lt, account_id, lt, std::move(P));
 | 
			
		||||
    td::actor::send_closure(f1->file_actor_id(), &ArchiveSlice::get_block_by_lt, account_id, lt, std::move(P));
 | 
			
		||||
  } else {
 | 
			
		||||
    promise.set_error(td::Status::Error(ErrorCode::notready, "lt not in db"));
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			@ -558,7 +558,7 @@ void ArchiveManager::deleted_package(PackageId id, td::Promise<td::Unit> promise
 | 
			
		|||
  auto it = m.find(id);
 | 
			
		||||
  CHECK(it != m.end());
 | 
			
		||||
  CHECK(it->second.deleted);
 | 
			
		||||
  it->second.clear_actor_id();
 | 
			
		||||
  it->second.file.reset();
 | 
			
		||||
  promise.set_value(td::Unit());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -598,8 +598,8 @@ void ArchiveManager::load_package(PackageId id) {
 | 
			
		|||
  get_file_map(id).emplace(id, std::move(desc));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::get_file_desc(ShardIdFull shard, PackageId id, BlockSeqno seqno,
 | 
			
		||||
                                                               UnixTime ts, LogicalTime lt, bool force) {
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::get_file_desc(ShardIdFull shard, PackageId id, BlockSeqno seqno,
 | 
			
		||||
                                                                     UnixTime ts, LogicalTime lt, bool force) {
 | 
			
		||||
  auto &f = get_file_map(id);
 | 
			
		||||
  auto it = f.find(id);
 | 
			
		||||
  if (it != f.end()) {
 | 
			
		||||
| 
						 | 
				
			
			@ -607,7 +607,7 @@ ArchiveManager::FileDescription *ArchiveManager::get_file_desc(ShardIdFull shard
 | 
			
		|||
      return nullptr;
 | 
			
		||||
    }
 | 
			
		||||
    if (force && !id.temp) {
 | 
			
		||||
      update_desc(it->second, shard, seqno, ts, lt);
 | 
			
		||||
      update_desc(f, it->second, shard, seqno, ts, lt);
 | 
			
		||||
    }
 | 
			
		||||
    return &it->second;
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			@ -618,17 +618,18 @@ ArchiveManager::FileDescription *ArchiveManager::get_file_desc(ShardIdFull shard
 | 
			
		|||
  return add_file_desc(shard, id, seqno, ts, lt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::add_file_desc(ShardIdFull shard, PackageId id, BlockSeqno seqno,
 | 
			
		||||
                                                               UnixTime ts, LogicalTime lt) {
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::add_file_desc(ShardIdFull shard, PackageId id, BlockSeqno seqno,
 | 
			
		||||
                                                                     UnixTime ts, LogicalTime lt) {
 | 
			
		||||
  auto &f = get_file_map(id);
 | 
			
		||||
  CHECK(f.count(id) == 0);
 | 
			
		||||
 | 
			
		||||
  FileDescription desc{id, false};
 | 
			
		||||
  FileDescription new_desc{id, false};
 | 
			
		||||
  td::mkdir(db_root_ + id.path()).ensure();
 | 
			
		||||
  std::string prefix = PSTRING() << db_root_ << id.path() << id.name();
 | 
			
		||||
  desc.file = td::actor::create_actor<ArchiveSlice>("slice", id.id, id.key, id.temp, false, db_root_);
 | 
			
		||||
  new_desc.file = td::actor::create_actor<ArchiveSlice>("slice", id.id, id.key, id.temp, false, db_root_);
 | 
			
		||||
  const FileDescription &desc = f.emplace(id, std::move(new_desc));
 | 
			
		||||
  if (!id.temp) {
 | 
			
		||||
    update_desc(desc, shard, seqno, ts, lt);
 | 
			
		||||
    update_desc(f, desc, shard, seqno, ts, lt);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  std::vector<tl_object_ptr<ton_api::db_files_package_firstBlock>> vec;
 | 
			
		||||
| 
						 | 
				
			
			@ -668,17 +669,16 @@ ArchiveManager::FileDescription *ArchiveManager::add_file_desc(ShardIdFull shard
 | 
			
		|||
        .ensure();
 | 
			
		||||
  }
 | 
			
		||||
  index_->commit_transaction().ensure();
 | 
			
		||||
 | 
			
		||||
  return &f.emplace(id, std::move(desc)).first->second;
 | 
			
		||||
  return &desc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ArchiveManager::update_desc(FileDescription &desc, ShardIdFull shard, BlockSeqno seqno, UnixTime ts,
 | 
			
		||||
                                 LogicalTime lt) {
 | 
			
		||||
void ArchiveManager::update_desc(FileMap &f, const FileDescription &desc, ShardIdFull shard, BlockSeqno seqno,
 | 
			
		||||
                                 UnixTime ts, LogicalTime lt) {
 | 
			
		||||
  auto it = desc.first_blocks.find(shard);
 | 
			
		||||
  if (it != desc.first_blocks.end() && it->second.seqno <= seqno) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  desc.first_blocks[shard] = FileDescription::Desc{seqno, ts, lt};
 | 
			
		||||
  f.set_shard_first_block(desc, shard, FileDescription::Desc{seqno, ts, lt});
 | 
			
		||||
  std::vector<tl_object_ptr<ton_api::db_files_package_firstBlock>> vec;
 | 
			
		||||
  for (auto &e : desc.first_blocks) {
 | 
			
		||||
    vec.push_back(create_tl_object<ton_api::db_files_package_firstBlock>(e.first.workchain, e.first.shard,
 | 
			
		||||
| 
						 | 
				
			
			@ -694,150 +694,91 @@ void ArchiveManager::update_desc(FileDescription &desc, ShardIdFull shard, Block
 | 
			
		|||
  index_->commit_transaction().ensure();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_seqno(ShardIdFull shard, BlockSeqno seqno,
 | 
			
		||||
                                                                        bool key_block) {
 | 
			
		||||
  auto &f = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
  for (auto it = f.rbegin(); it != f.rend(); it++) {
 | 
			
		||||
    auto i = it->second.first_blocks.find(shard);
 | 
			
		||||
    if (i != it->second.first_blocks.end() && i->second.seqno <= seqno) {
 | 
			
		||||
      if (it->second.deleted) {
 | 
			
		||||
        return nullptr;
 | 
			
		||||
      } else {
 | 
			
		||||
        return &it->second;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_seqno(ShardIdFull shard, BlockSeqno seqno,
 | 
			
		||||
                                                                              bool key_block) {
 | 
			
		||||
  return get_file_map(PackageId{0, key_block, false}).get_file_desc_by_seqno(shard, seqno);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_unix_time(ShardIdFull shard, UnixTime ts,
 | 
			
		||||
                                                                            bool key_block) {
 | 
			
		||||
  auto &f = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
  for (auto it = f.rbegin(); it != f.rend(); it++) {
 | 
			
		||||
    auto i = it->second.first_blocks.find(shard);
 | 
			
		||||
    if (i != it->second.first_blocks.end() && i->second.ts <= ts) {
 | 
			
		||||
      if (it->second.deleted) {
 | 
			
		||||
        return nullptr;
 | 
			
		||||
      } else {
 | 
			
		||||
        return &it->second;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_unix_time(ShardIdFull shard, UnixTime ts,
 | 
			
		||||
                                                                                  bool key_block) {
 | 
			
		||||
  return get_file_map(PackageId{0, key_block, false}).get_file_desc_by_unix_time(shard, ts);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_lt(ShardIdFull shard, LogicalTime lt,
 | 
			
		||||
                                                                     bool key_block) {
 | 
			
		||||
  auto &f = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
  for (auto it = f.rbegin(); it != f.rend(); it++) {
 | 
			
		||||
    auto i = it->second.first_blocks.find(shard);
 | 
			
		||||
    if (i != it->second.first_blocks.end() && i->second.lt <= lt) {
 | 
			
		||||
      if (it->second.deleted) {
 | 
			
		||||
        return nullptr;
 | 
			
		||||
      } else {
 | 
			
		||||
        return &it->second;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_lt(ShardIdFull shard, LogicalTime lt,
 | 
			
		||||
                                                                           bool key_block) {
 | 
			
		||||
  return get_file_map(PackageId{0, key_block, false}).get_file_desc_by_lt(shard, lt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_seqno(AccountIdPrefixFull account, BlockSeqno seqno,
 | 
			
		||||
                                                                        bool key_block) {
 | 
			
		||||
  auto &f = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_seqno(AccountIdPrefixFull account,
 | 
			
		||||
                                                                              BlockSeqno seqno, bool key_block) {
 | 
			
		||||
  if (account.is_masterchain()) {
 | 
			
		||||
    return get_file_desc_by_seqno(ShardIdFull{masterchainId}, seqno, key_block);
 | 
			
		||||
  }
 | 
			
		||||
  for (auto it = f.rbegin(); it != f.rend(); it++) {
 | 
			
		||||
    if (it->second.deleted) {
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
    bool found = false;
 | 
			
		||||
    for (int i = 0; i < 60; i++) {
 | 
			
		||||
      auto shard = shard_prefix(account, i);
 | 
			
		||||
      auto it2 = it->second.first_blocks.find(shard);
 | 
			
		||||
      if (it2 != it->second.first_blocks.end()) {
 | 
			
		||||
        if (it2->second.seqno <= seqno) {
 | 
			
		||||
          return &it->second;
 | 
			
		||||
        }
 | 
			
		||||
        found = true;
 | 
			
		||||
      } else if (found) {
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
  auto &f = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
  const FileDescription *result = nullptr;
 | 
			
		||||
  for (int i = 0; i <= 60; i++) {
 | 
			
		||||
    const FileDescription *desc = f.get_file_desc_by_seqno(shard_prefix(account, i), seqno);
 | 
			
		||||
    if (desc && (!result || result->id < desc->id)) {
 | 
			
		||||
      result = desc;
 | 
			
		||||
    } else if (result && (!desc || desc->id < result->id)) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_unix_time(AccountIdPrefixFull account, UnixTime ts,
 | 
			
		||||
                                                                            bool key_block) {
 | 
			
		||||
  auto &f = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_unix_time(AccountIdPrefixFull account,
 | 
			
		||||
                                                                                  UnixTime ts, bool key_block) {
 | 
			
		||||
  if (account.is_masterchain()) {
 | 
			
		||||
    return get_file_desc_by_unix_time(ShardIdFull{masterchainId}, ts, key_block);
 | 
			
		||||
  }
 | 
			
		||||
  for (auto it = f.rbegin(); it != f.rend(); it++) {
 | 
			
		||||
    if (it->second.deleted) {
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
    bool found = false;
 | 
			
		||||
    for (int i = 0; i < 60; i++) {
 | 
			
		||||
      auto shard = shard_prefix(account, i);
 | 
			
		||||
      auto it2 = it->second.first_blocks.find(shard);
 | 
			
		||||
      if (it2 != it->second.first_blocks.end()) {
 | 
			
		||||
        if (it2->second.ts <= ts) {
 | 
			
		||||
          return &it->second;
 | 
			
		||||
        }
 | 
			
		||||
        found = true;
 | 
			
		||||
      } else if (found) {
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
  auto &f = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
  const FileDescription *result = nullptr;
 | 
			
		||||
  for (int i = 0; i <= 60; i++) {
 | 
			
		||||
    const FileDescription *desc = f.get_file_desc_by_unix_time(shard_prefix(account, i), ts);
 | 
			
		||||
    if (desc && (!result || result->id < desc->id)) {
 | 
			
		||||
      result = desc;
 | 
			
		||||
    } else if (result && (!desc || desc->id < result->id)) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_lt(AccountIdPrefixFull account, LogicalTime lt,
 | 
			
		||||
                                                                     bool key_block) {
 | 
			
		||||
  auto &f = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::get_file_desc_by_lt(AccountIdPrefixFull account, LogicalTime lt,
 | 
			
		||||
                                                                           bool key_block) {
 | 
			
		||||
  if (account.is_masterchain()) {
 | 
			
		||||
    return get_file_desc_by_lt(ShardIdFull{masterchainId}, lt, key_block);
 | 
			
		||||
  }
 | 
			
		||||
  for (auto it = f.rbegin(); it != f.rend(); it++) {
 | 
			
		||||
    if (it->second.deleted) {
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
    bool found = false;
 | 
			
		||||
    for (int i = 0; i < 60; i++) {
 | 
			
		||||
      auto shard = shard_prefix(account, i);
 | 
			
		||||
      auto it2 = it->second.first_blocks.find(shard);
 | 
			
		||||
      if (it2 != it->second.first_blocks.end()) {
 | 
			
		||||
        if (it2->second.lt <= lt) {
 | 
			
		||||
          return &it->second;
 | 
			
		||||
        }
 | 
			
		||||
        found = true;
 | 
			
		||||
      } else if (found) {
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
  auto &f = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
  const FileDescription *result = nullptr;
 | 
			
		||||
  for (int i = 0; i <= 60; i++) {
 | 
			
		||||
    const FileDescription *desc = f.get_file_desc_by_lt(shard_prefix(account, i), lt);
 | 
			
		||||
    if (desc && (!result || result->id < desc->id)) {
 | 
			
		||||
      result = desc;
 | 
			
		||||
    } else if (result && (!desc || desc->id < result->id)) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::get_next_file_desc(FileDescription *f) {
 | 
			
		||||
  auto &m = get_file_map(f->id);
 | 
			
		||||
  auto it = m.find(f->id);
 | 
			
		||||
  CHECK(it != m.end());
 | 
			
		||||
  while (true) {
 | 
			
		||||
    it++;
 | 
			
		||||
    if (it == m.end()) {
 | 
			
		||||
      return nullptr;
 | 
			
		||||
    } else if (!it->second.deleted) {
 | 
			
		||||
      return &it->second;
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::get_next_file_desc(const FileDescription *f,
 | 
			
		||||
                                                                          AccountIdPrefixFull shard, bool key_block) {
 | 
			
		||||
  auto &m = get_file_map(PackageId{0, key_block, false});
 | 
			
		||||
  const FileDescription *result = nullptr;
 | 
			
		||||
  for (int i = 0; i <= 60; i++) {
 | 
			
		||||
    const FileDescription *desc = m.get_next_file_desc(shard_prefix(shard, i), f);
 | 
			
		||||
    if (desc && (!result || desc->id < result->id)) {
 | 
			
		||||
      result = desc;
 | 
			
		||||
    } else if (result && (!desc || result->id < desc->id)) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ArchiveManager::FileDescription *ArchiveManager::get_temp_file_desc_by_idx(PackageId idx) {
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::get_temp_file_desc_by_idx(PackageId idx) {
 | 
			
		||||
  auto it = temp_files_.find(idx);
 | 
			
		||||
  if (it != temp_files_.end()) {
 | 
			
		||||
    if (it->second.deleted) {
 | 
			
		||||
| 
						 | 
				
			
			@ -1257,14 +1198,100 @@ void ArchiveManager::truncate(BlockSeqno masterchain_seqno, ConstBlockHandle han
 | 
			
		|||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ArchiveManager::FileDescription::has_account_prefix(AccountIdPrefixFull account_id) const {
 | 
			
		||||
  for (int i = 0; i < 60; i++) {
 | 
			
		||||
    auto shard = shard_prefix(account_id, i);
 | 
			
		||||
    if (first_blocks.count(shard)) {
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
void ArchiveManager::FileMap::shard_index_add(const FileDescription &desc) {
 | 
			
		||||
  for (const auto &p : desc.first_blocks) {
 | 
			
		||||
    ShardIndex &s = shards_[p.first];
 | 
			
		||||
    s.seqno_index_[p.second.seqno] = &desc;
 | 
			
		||||
    s.lt_index_[p.second.lt] = &desc;
 | 
			
		||||
    s.unix_time_index_[p.second.ts] = &desc;
 | 
			
		||||
    s.packages_index_[desc.id] = &desc;
 | 
			
		||||
  }
 | 
			
		||||
  return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ArchiveManager::FileMap::shard_index_del(const FileDescription &desc) {
 | 
			
		||||
  for (const auto &p : desc.first_blocks) {
 | 
			
		||||
    ShardIndex &s = shards_[p.first];
 | 
			
		||||
    s.seqno_index_.erase(p.second.seqno);
 | 
			
		||||
    s.lt_index_.erase(p.second.lt);
 | 
			
		||||
    s.unix_time_index_.erase(p.second.ts);
 | 
			
		||||
    s.packages_index_.erase(desc.id);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ArchiveManager::FileMap::set_shard_first_block(const FileDescription &desc, ShardIdFull shard,
 | 
			
		||||
                                                    FileDescription::Desc v) {
 | 
			
		||||
  ShardIndex &s = shards_[shard];
 | 
			
		||||
  auto &d = const_cast<FileDescription &>(desc);
 | 
			
		||||
  auto it = d.first_blocks.find(shard);
 | 
			
		||||
  if (it != d.first_blocks.end()) {
 | 
			
		||||
    s.seqno_index_.erase(it->second.seqno);
 | 
			
		||||
    s.lt_index_.erase(it->second.lt);
 | 
			
		||||
    s.unix_time_index_.erase(it->second.ts);
 | 
			
		||||
  }
 | 
			
		||||
  d.first_blocks[shard] = v;
 | 
			
		||||
  s.seqno_index_[v.seqno] = &d;
 | 
			
		||||
  s.lt_index_[v.lt] = &d;
 | 
			
		||||
  s.unix_time_index_[v.ts] = &d;
 | 
			
		||||
  s.packages_index_[d.id] = &d;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::FileMap::get_file_desc_by_seqno(ShardIdFull shard,
 | 
			
		||||
                                                                                       BlockSeqno seqno) const {
 | 
			
		||||
  auto it = shards_.find(shard);
 | 
			
		||||
  if (it == shards_.end()) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
  const auto &map = it->second.seqno_index_;
 | 
			
		||||
  auto it2 = map.upper_bound(seqno);
 | 
			
		||||
  if (it2 == map.begin()) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
  --it2;
 | 
			
		||||
  return it2->second->deleted ? nullptr : it2->second;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::FileMap::get_file_desc_by_lt(ShardIdFull shard,
 | 
			
		||||
                                                                                    LogicalTime lt) const {
 | 
			
		||||
  auto it = shards_.find(shard);
 | 
			
		||||
  if (it == shards_.end()) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
  const auto &map = it->second.lt_index_;
 | 
			
		||||
  auto it2 = map.upper_bound(lt);
 | 
			
		||||
  if (it2 == map.begin()) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
  --it2;
 | 
			
		||||
  return it2->second->deleted ? nullptr : it2->second;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::FileMap::get_file_desc_by_unix_time(ShardIdFull shard,
 | 
			
		||||
                                                                                           UnixTime ts) const {
 | 
			
		||||
  auto it = shards_.find(shard);
 | 
			
		||||
  if (it == shards_.end()) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
  const auto &map = it->second.unix_time_index_;
 | 
			
		||||
  auto it2 = map.upper_bound(ts);
 | 
			
		||||
  if (it2 == map.begin()) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
  --it2;
 | 
			
		||||
  return it2->second->deleted ? nullptr : it2->second;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const ArchiveManager::FileDescription *ArchiveManager::FileMap::get_next_file_desc(ShardIdFull shard,
 | 
			
		||||
                                                                                   const FileDescription *desc) const {
 | 
			
		||||
  auto it = shards_.find(shard);
 | 
			
		||||
  if (it == shards_.end()) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
  const auto &map = it->second.packages_index_;
 | 
			
		||||
  auto it2 = desc ? map.upper_bound(desc->id) : map.begin();
 | 
			
		||||
  if (it2 == map.end()) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
  return it2->second->deleted ? nullptr : it2->second;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace validator
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -71,7 +71,6 @@ class ArchiveManager : public td::actor::Actor {
 | 
			
		|||
 | 
			
		||||
  void start_up() override;
 | 
			
		||||
 | 
			
		||||
  void begin_transaction();
 | 
			
		||||
  void commit_transaction();
 | 
			
		||||
  void set_async_mode(bool mode, td::Promise<td::Unit> promise);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -94,26 +93,83 @@ class ArchiveManager : public td::actor::Actor {
 | 
			
		|||
    auto file_actor_id() const {
 | 
			
		||||
      return file.get();
 | 
			
		||||
    }
 | 
			
		||||
    void clear_actor_id() {
 | 
			
		||||
      file.reset();
 | 
			
		||||
    }
 | 
			
		||||
    bool has_account_prefix(AccountIdPrefixFull account_id) const;
 | 
			
		||||
    PackageId id;
 | 
			
		||||
    bool deleted;
 | 
			
		||||
    mutable bool deleted;
 | 
			
		||||
 | 
			
		||||
    std::map<ShardIdFull, Desc> first_blocks;
 | 
			
		||||
    td::actor::ActorOwn<ArchiveSlice> file;
 | 
			
		||||
    mutable td::actor::ActorOwn<ArchiveSlice> file;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  std::map<PackageId, FileDescription> files_;
 | 
			
		||||
  std::map<PackageId, FileDescription> key_files_;
 | 
			
		||||
  std::map<PackageId, FileDescription> temp_files_;
 | 
			
		||||
  class FileMap {
 | 
			
		||||
   public:
 | 
			
		||||
    std::map<PackageId, FileDescription>::const_iterator begin() const {
 | 
			
		||||
      return files_.cbegin();
 | 
			
		||||
    }
 | 
			
		||||
    std::map<PackageId, FileDescription>::const_iterator end() const {
 | 
			
		||||
      return files_.cend();
 | 
			
		||||
    }
 | 
			
		||||
    std::map<PackageId, FileDescription>::const_reverse_iterator rbegin() const {
 | 
			
		||||
      return files_.crbegin();
 | 
			
		||||
    }
 | 
			
		||||
    std::map<PackageId, FileDescription>::const_reverse_iterator rend() const {
 | 
			
		||||
      return files_.crend();
 | 
			
		||||
    }
 | 
			
		||||
    std::map<PackageId, FileDescription>::const_iterator find(PackageId x) const {
 | 
			
		||||
      return files_.find(x);
 | 
			
		||||
    }
 | 
			
		||||
    size_t count(const PackageId &x) const {
 | 
			
		||||
      return files_.count(x);
 | 
			
		||||
    }
 | 
			
		||||
    size_t size() const {
 | 
			
		||||
      return files_.size();
 | 
			
		||||
    }
 | 
			
		||||
    std::map<PackageId, FileDescription>::const_iterator lower_bound(const PackageId &x) const {
 | 
			
		||||
      return files_.lower_bound(x);
 | 
			
		||||
    }
 | 
			
		||||
    std::map<PackageId, FileDescription>::const_iterator upper_bound(const PackageId &x) const {
 | 
			
		||||
      return files_.upper_bound(x);
 | 
			
		||||
    }
 | 
			
		||||
    void clear() {
 | 
			
		||||
      files_.clear();
 | 
			
		||||
      shards_.clear();
 | 
			
		||||
    }
 | 
			
		||||
    const FileDescription &emplace(const PackageId &id, FileDescription desc) {
 | 
			
		||||
      auto it = files_.emplace(id, std::move(desc));
 | 
			
		||||
      if (it.second) {
 | 
			
		||||
        shard_index_add(it.first->second);
 | 
			
		||||
      }
 | 
			
		||||
      return it.first->second;
 | 
			
		||||
    }
 | 
			
		||||
    void erase(std::map<PackageId, FileDescription>::const_iterator it) {
 | 
			
		||||
      shard_index_del(it->second);
 | 
			
		||||
      files_.erase(it);
 | 
			
		||||
    }
 | 
			
		||||
    void set_shard_first_block(const FileDescription &desc, ShardIdFull shard, FileDescription::Desc v);
 | 
			
		||||
    const FileDescription *get_file_desc_by_seqno(ShardIdFull shard, BlockSeqno seqno) const;
 | 
			
		||||
    const FileDescription *get_file_desc_by_lt(ShardIdFull shard, LogicalTime lt) const;
 | 
			
		||||
    const FileDescription *get_file_desc_by_unix_time(ShardIdFull shard, UnixTime ts) const;
 | 
			
		||||
    const FileDescription *get_next_file_desc(ShardIdFull shard, const FileDescription *desc) const;
 | 
			
		||||
 | 
			
		||||
   private:
 | 
			
		||||
    std::map<PackageId, FileDescription> files_;
 | 
			
		||||
    struct ShardIndex {
 | 
			
		||||
      std::map<BlockSeqno, const FileDescription *> seqno_index_;
 | 
			
		||||
      std::map<LogicalTime, const FileDescription *> lt_index_;
 | 
			
		||||
      std::map<UnixTime, const FileDescription *> unix_time_index_;
 | 
			
		||||
      std::map<PackageId, const FileDescription *> packages_index_;
 | 
			
		||||
    };
 | 
			
		||||
    std::map<ShardIdFull, ShardIndex> shards_;
 | 
			
		||||
 | 
			
		||||
    void shard_index_add(const FileDescription &desc);
 | 
			
		||||
    void shard_index_del(const FileDescription &desc);
 | 
			
		||||
  };
 | 
			
		||||
  FileMap files_, key_files_, temp_files_;
 | 
			
		||||
  BlockSeqno finalized_up_to_{0};
 | 
			
		||||
  bool async_mode_ = false;
 | 
			
		||||
  bool huge_transaction_started_ = false;
 | 
			
		||||
  td::uint32 huge_transaction_size_ = 0;
 | 
			
		||||
 | 
			
		||||
  auto &get_file_map(const PackageId &p) {
 | 
			
		||||
  FileMap &get_file_map(const PackageId &p) {
 | 
			
		||||
    return p.key ? key_files_ : p.temp ? temp_files_ : files_;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -126,18 +182,19 @@ class ArchiveManager : public td::actor::Actor {
 | 
			
		|||
  void get_handle_finish(BlockHandle handle, td::Promise<BlockHandle> promise);
 | 
			
		||||
  void get_file_short_cont(FileReference ref_id, PackageId idx, td::Promise<td::BufferSlice> promise);
 | 
			
		||||
 | 
			
		||||
  FileDescription *get_file_desc(ShardIdFull shard, PackageId id, BlockSeqno seqno, UnixTime ts, LogicalTime lt,
 | 
			
		||||
                                 bool force);
 | 
			
		||||
  FileDescription *add_file_desc(ShardIdFull shard, PackageId id, BlockSeqno seqno, UnixTime ts, LogicalTime lt);
 | 
			
		||||
  void update_desc(FileDescription &desc, ShardIdFull shard, BlockSeqno seqno, UnixTime ts, LogicalTime lt);
 | 
			
		||||
  FileDescription *get_file_desc_by_seqno(ShardIdFull shard, BlockSeqno seqno, bool key_block);
 | 
			
		||||
  FileDescription *get_file_desc_by_lt(ShardIdFull shard, LogicalTime lt, bool key_block);
 | 
			
		||||
  FileDescription *get_file_desc_by_unix_time(ShardIdFull shard, UnixTime ts, bool key_block);
 | 
			
		||||
  FileDescription *get_file_desc_by_seqno(AccountIdPrefixFull shard, BlockSeqno seqno, bool key_block);
 | 
			
		||||
  FileDescription *get_file_desc_by_lt(AccountIdPrefixFull shard, LogicalTime lt, bool key_block);
 | 
			
		||||
  FileDescription *get_file_desc_by_unix_time(AccountIdPrefixFull shard, UnixTime ts, bool key_block);
 | 
			
		||||
  FileDescription *get_next_file_desc(FileDescription *f);
 | 
			
		||||
  FileDescription *get_temp_file_desc_by_idx(PackageId idx);
 | 
			
		||||
  const FileDescription *get_file_desc(ShardIdFull shard, PackageId id, BlockSeqno seqno, UnixTime ts, LogicalTime lt,
 | 
			
		||||
                                       bool force);
 | 
			
		||||
  const FileDescription *add_file_desc(ShardIdFull shard, PackageId id, BlockSeqno seqno, UnixTime ts, LogicalTime lt);
 | 
			
		||||
  void update_desc(FileMap &f, const FileDescription &desc, ShardIdFull shard, BlockSeqno seqno, UnixTime ts,
 | 
			
		||||
                   LogicalTime lt);
 | 
			
		||||
  const FileDescription *get_file_desc_by_seqno(ShardIdFull shard, BlockSeqno seqno, bool key_block);
 | 
			
		||||
  const FileDescription *get_file_desc_by_lt(ShardIdFull shard, LogicalTime lt, bool key_block);
 | 
			
		||||
  const FileDescription *get_file_desc_by_unix_time(ShardIdFull shard, UnixTime ts, bool key_block);
 | 
			
		||||
  const FileDescription *get_file_desc_by_seqno(AccountIdPrefixFull shard, BlockSeqno seqno, bool key_block);
 | 
			
		||||
  const FileDescription *get_file_desc_by_lt(AccountIdPrefixFull shard, LogicalTime lt, bool key_block);
 | 
			
		||||
  const FileDescription *get_file_desc_by_unix_time(AccountIdPrefixFull shard, UnixTime ts, bool key_block);
 | 
			
		||||
  const FileDescription *get_next_file_desc(const FileDescription *f, AccountIdPrefixFull shard, bool key_block);
 | 
			
		||||
  const FileDescription *get_temp_file_desc_by_idx(PackageId idx);
 | 
			
		||||
  PackageId get_max_temp_file_desc_idx();
 | 
			
		||||
  PackageId get_prev_temp_file_desc_idx(PackageId id);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue