/******************************************************************************* * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved * Unauthorized copying of this file, via any medium is strictly prohibited. * Proprietary and confidential. ******************************************************************************/ #pragma once #include "MemManager.h" #include "Meta.h" #include "MetaConsts.h" #include "metrics/Metrics.h" #include #include #include #include namespace zilliz { namespace vecwise { namespace engine { template MemVectors::MemVectors(const std::shared_ptr& meta_ptr, const meta::TableFileSchema& schema, const Options& options) : pMeta_(meta_ptr), options_(options), schema_(schema), pIdGenerator_(new SimpleIDGenerator()), pEE_(new EngineT(schema_.dimension, schema_.location)) { } template void MemVectors::Add(size_t n_, const float* vectors_, IDNumbers& vector_ids_) { auto start_time = METRICS_NOW_TIME; pIdGenerator_->GetNextIDNumbers(n_, vector_ids_); pEE_->AddWithIds(n_, vectors_, vector_ids_.data()); auto end_time = METRICS_NOW_TIME; auto total_time = METRICS_MICROSECONDS(start_time, end_time); server::Metrics::GetInstance().AddVectorsPerSecondGaugeSet(static_cast(n_), static_cast(schema_.dimension), total_time); } template size_t MemVectors::Total() const { return pEE_->Count(); } template size_t MemVectors::ApproximateSize() const { return pEE_->Size(); } template Status MemVectors::Serialize(std::string& table_id) { table_id = schema_.table_id; auto size = ApproximateSize(); auto start_time = METRICS_NOW_TIME; pEE_->Serialize(); auto end_time = METRICS_NOW_TIME; auto total_time = METRICS_MICROSECONDS(start_time, end_time); schema_.size = size; server::Metrics::GetInstance().DiskStoreIOSpeedGaugeSet(size/total_time); schema_.file_type = (size >= options_.index_trigger_size) ? meta::TableFileSchema::TO_INDEX : meta::TableFileSchema::RAW; auto status = pMeta_->UpdateTableFile(schema_); LOG(DEBUG) << "New " << ((schema_.file_type == meta::TableFileSchema::RAW) ? "raw" : "to_index") << " file " << schema_.file_id << " of size " << pEE_->Size() / meta::M << " M"; pEE_->Cache(); return status; } template MemVectors::~MemVectors() { if (pIdGenerator_ != nullptr) { delete pIdGenerator_; pIdGenerator_ = nullptr; } } /* * MemManager */ template typename MemManager::MemVectorsPtr MemManager::GetMemByTable( const std::string& table_id) { auto memIt = memMap_.find(table_id); if (memIt != memMap_.end()) { return memIt->second; } meta::TableFileSchema table_file; table_file.table_id = table_id; auto status = pMeta_->CreateTableFile(table_file); if (!status.ok()) { return nullptr; } memMap_[table_id] = MemVectorsPtr(new MemVectors(pMeta_, table_file, options_)); return memMap_[table_id]; } template Status MemManager::InsertVectors(const std::string& table_id_, size_t n_, const float* vectors_, IDNumbers& vector_ids_) { std::unique_lock lock(mutex_); return InsertVectorsNoLock(table_id_, n_, vectors_, vector_ids_); } template Status MemManager::InsertVectorsNoLock(const std::string& table_id, size_t n, const float* vectors, IDNumbers& vector_ids) { MemVectorsPtr mem = GetMemByTable(table_id); if (mem == nullptr) { return Status::NotFound("Group " + table_id + " not found!"); } mem->Add(n, vectors, vector_ids); return Status::OK(); } template Status MemManager::ToImmutable() { std::unique_lock lock(mutex_); for (auto& kv: memMap_) { immMems_.push_back(kv.second); } memMap_.clear(); return Status::OK(); } template Status MemManager::Serialize(std::vector& table_ids) { ToImmutable(); std::unique_lock lock(serialization_mtx_); std::string table_id; table_ids.clear(); for (auto& mem : immMems_) { mem->Serialize(table_id); table_ids.push_back(table_id); } immMems_.clear(); return Status::OK(); } } // namespace engine } // namespace vecwise } // namespace zilliz