diff --git a/internal/core/src/index/CMakeLists.txt b/internal/core/src/index/CMakeLists.txt index a6ff3f7866..68a8303107 100644 --- a/internal/core/src/index/CMakeLists.txt +++ b/internal/core/src/index/CMakeLists.txt @@ -17,6 +17,7 @@ set(INDEX_FILES VectorDiskIndex.cpp ScalarIndex.cpp ScalarIndexSort.cpp + SkipIndex.cpp ) milvus_add_pkg_config("milvus_index") diff --git a/internal/core/src/segcore/SkipIndex.cpp b/internal/core/src/index/SkipIndex.cpp similarity index 68% rename from internal/core/src/segcore/SkipIndex.cpp rename to internal/core/src/index/SkipIndex.cpp index 3de9aa84e9..dcf850bae2 100644 --- a/internal/core/src/segcore/SkipIndex.cpp +++ b/internal/core/src/index/SkipIndex.cpp @@ -17,11 +17,12 @@ static const FieldChunkMetrics defaultFieldChunkMetrics; const FieldChunkMetrics& SkipIndex::GetFieldChunkMetrics(milvus::FieldId field_id, int chunk_id) const { + std::shared_lock lck(mutex_); auto field_metrics = fieldChunkMetrics_.find(field_id); if (field_metrics != fieldChunkMetrics_.end()) { auto field_chunk_metrics = field_metrics->second.find(chunk_id); if (field_chunk_metrics != field_metrics->second.end()) { - return field_chunk_metrics->second; + return *(field_chunk_metrics->second.get()); } } return defaultFieldChunkMetrics; @@ -33,17 +34,18 @@ SkipIndex::LoadPrimitive(milvus::FieldId field_id, milvus::DataType data_type, const void* chunk_data, int64_t count) { - FieldChunkMetrics chunkMetrics; + auto chunkMetrics = std::make_unique(); + if (count > 0) { - chunkMetrics.hasValue_ = true; + chunkMetrics->hasValue_ = true; switch (data_type) { case DataType::INT8: { const int8_t* typedData = static_cast(chunk_data); std::pair minMax = ProcessFieldMetrics(typedData, count); - chunkMetrics.min_ = Metrics(minMax.first); - chunkMetrics.max_ = Metrics(minMax.second); + chunkMetrics->min_ = Metrics(minMax.first); + chunkMetrics->max_ = Metrics(minMax.second); break; } case DataType::INT16: { @@ -51,8 +53,8 @@ SkipIndex::LoadPrimitive(milvus::FieldId field_id, static_cast(chunk_data); std::pair minMax = ProcessFieldMetrics(typedData, count); - chunkMetrics.min_ = Metrics(minMax.first); - chunkMetrics.max_ = Metrics(minMax.second); + chunkMetrics->min_ = Metrics(minMax.first); + chunkMetrics->max_ = Metrics(minMax.second); break; } case DataType::INT32: { @@ -60,8 +62,8 @@ SkipIndex::LoadPrimitive(milvus::FieldId field_id, static_cast(chunk_data); std::pair minMax = ProcessFieldMetrics(typedData, count); - chunkMetrics.min_ = Metrics(minMax.first); - chunkMetrics.max_ = Metrics(minMax.second); + chunkMetrics->min_ = Metrics(minMax.first); + chunkMetrics->max_ = Metrics(minMax.second); break; } case DataType::INT64: { @@ -69,16 +71,16 @@ SkipIndex::LoadPrimitive(milvus::FieldId field_id, static_cast(chunk_data); std::pair minMax = ProcessFieldMetrics(typedData, count); - chunkMetrics.min_ = Metrics(minMax.first); - chunkMetrics.max_ = Metrics(minMax.second); + chunkMetrics->min_ = Metrics(minMax.first); + chunkMetrics->max_ = Metrics(minMax.second); break; } case DataType::FLOAT: { const float* typedData = static_cast(chunk_data); std::pair minMax = ProcessFieldMetrics(typedData, count); - chunkMetrics.min_ = Metrics(minMax.first); - chunkMetrics.max_ = Metrics(minMax.second); + chunkMetrics->min_ = Metrics(minMax.first); + chunkMetrics->max_ = Metrics(minMax.second); break; } case DataType::DOUBLE: { @@ -86,13 +88,20 @@ SkipIndex::LoadPrimitive(milvus::FieldId field_id, static_cast(chunk_data); std::pair minMax = ProcessFieldMetrics(typedData, count); - chunkMetrics.min_ = Metrics(minMax.first); - chunkMetrics.max_ = Metrics(minMax.second); + chunkMetrics->min_ = Metrics(minMax.first); + chunkMetrics->max_ = Metrics(minMax.second); break; } } } - fieldChunkMetrics_[field_id][chunk_id] = chunkMetrics; + std::unique_lock lck(mutex_); + if (fieldChunkMetrics_.count(field_id) == 0) { + fieldChunkMetrics_.insert(std::make_pair( + field_id, + std::unordered_map>())); + } + + fieldChunkMetrics_[field_id].emplace(chunk_id, std::move(chunkMetrics)); } void @@ -100,9 +109,9 @@ SkipIndex::LoadString(milvus::FieldId field_id, int64_t chunk_id, const milvus::VariableColumn& var_column) { int num_rows = var_column.NumRows(); - FieldChunkMetrics chunkMetrics; + auto chunkMetrics = std::make_unique(); if (num_rows > 0) { - chunkMetrics.hasValue_ = true; + chunkMetrics->hasValue_ = true; std::string_view min_string = var_column.RawAt(0); std::string_view max_string = var_column.RawAt(0); for (size_t i = 1; i < num_rows; i++) { @@ -114,10 +123,16 @@ SkipIndex::LoadString(milvus::FieldId field_id, max_string = val; } } - chunkMetrics.min_ = Metrics(min_string); - chunkMetrics.max_ = Metrics(max_string); + chunkMetrics->min_ = Metrics(min_string); + chunkMetrics->max_ = Metrics(max_string); } - fieldChunkMetrics_[field_id][chunk_id] = chunkMetrics; + std::unique_lock lck(mutex_); + if (fieldChunkMetrics_.count(field_id) == 0) { + fieldChunkMetrics_.insert(std::make_pair( + field_id, + std::unordered_map>())); + } + fieldChunkMetrics_[field_id].emplace(chunk_id, std::move(chunkMetrics)); } } // namespace milvus diff --git a/internal/core/src/segcore/SkipIndex.h b/internal/core/src/index/SkipIndex.h similarity index 97% rename from internal/core/src/segcore/SkipIndex.h rename to internal/core/src/index/SkipIndex.h index 40a9712f62..dba2cb1ebe 100644 --- a/internal/core/src/segcore/SkipIndex.h +++ b/internal/core/src/index/SkipIndex.h @@ -35,12 +35,6 @@ struct FieldChunkMetrics { class SkipIndex { public: - SkipIndex() { - fieldChunkMetrics_ = std::unordered_map< - FieldId, - std::unordered_map>(); - } - template bool CanSkipUnaryRange(FieldId field_id, @@ -245,7 +239,10 @@ class SkipIndex { } private: - std::unordered_map> + std::unordered_map< + FieldId, + std::unordered_map>> fieldChunkMetrics_; + mutable std::shared_mutex mutex_; }; } // namespace milvus diff --git a/internal/core/src/query/visitors/ExecExprVisitor.cpp b/internal/core/src/query/visitors/ExecExprVisitor.cpp index 8f8803aaee..ad219cc56f 100644 --- a/internal/core/src/query/visitors/ExecExprVisitor.cpp +++ b/internal/core/src/query/visitors/ExecExprVisitor.cpp @@ -36,7 +36,7 @@ #include "segcore/SegmentGrowingImpl.h" #include "simdjson/error.h" #include "query/PlanProto.h" -#include "segcore/SkipIndex.h" +#include "index/SkipIndex.h" #include "simd/hook.h" #include "index/Meta.h" diff --git a/internal/core/src/segcore/CMakeLists.txt b/internal/core/src/segcore/CMakeLists.txt index d51e47718e..4719a860d1 100644 --- a/internal/core/src/segcore/CMakeLists.txt +++ b/internal/core/src/segcore/CMakeLists.txt @@ -38,8 +38,7 @@ set(SEGCORE_FILES ScalarIndex.cpp TimestampIndex.cpp Utils.cpp - ConcurrentVector.cpp - SkipIndex.cpp) + ConcurrentVector.cpp) add_library(milvus_segcore SHARED ${SEGCORE_FILES}) target_link_libraries(milvus_segcore milvus_query milvus_exec ${OpenMP_CXX_FLAGS} milvus-storage) diff --git a/internal/core/src/segcore/SegmentInterface.h b/internal/core/src/segcore/SegmentInterface.h index f5a5c22a7c..27ec6a3fca 100644 --- a/internal/core/src/segcore/SegmentInterface.h +++ b/internal/core/src/segcore/SegmentInterface.h @@ -33,7 +33,7 @@ #include "pb/schema.pb.h" #include "pb/segcore.pb.h" #include "index/IndexInfo.h" -#include "SkipIndex.h" +#include "index/SkipIndex.h" #include "mmap/Column.h" namespace milvus::segcore {