@@ -64,6 +64,8 @@ class HNSWIndex_Single : public HNSWIndex<DataType, DistType> {
6464 int addVector (const void *vector_data, labelType label, bool overwrite_allowed = true ) override ;
6565 double getDistanceFrom (labelType label, const void *vector_data) const override ;
6666 inline std::vector<idType> markDelete (labelType label) override ;
67+ inline bool safeCheckIfLabelExistsInIndex (labelType label,
68+ bool also_done_processing = false ) const override ;
6769};
6870
6971/* *
@@ -121,6 +123,7 @@ int HNSWIndex_Single<DataType, DistType>::addVector(const void *vector_data, con
121123 bool overwrite_allowed) {
122124
123125 // Checking if an element with the given label already exists.
126+ std::unique_lock<std::mutex> index_data_lock (this ->index_data_guard_ );
124127 bool label_exists = false ;
125128 if (label_lookup_.find (label) != label_lookup_.end ()) {
126129 label_exists = true ;
@@ -132,7 +135,7 @@ int HNSWIndex_Single<DataType, DistType>::addVector(const void *vector_data, con
132135 return -1 ;
133136 }
134137 }
135-
138+ index_data_lock. unlock ();
136139 this ->appendVector (vector_data, label);
137140 // Return the delta in the index size due to the insertion.
138141 return label_exists ? 0 : 1 ;
@@ -159,6 +162,7 @@ HNSWIndex_Single<DataType, DistType>::newBatchIterator(const void *queryBlob,
159162template <typename DataType, typename DistType>
160163std::vector<idType> HNSWIndex_Single<DataType, DistType>::markDelete(labelType label) {
161164 std::vector<idType> idsToDelete;
165+ std::unique_lock<std::mutex> index_data_lock (this ->index_data_guard_ );
162166 auto search = label_lookup_.find (label);
163167 if (search == label_lookup_.end ()) {
164168 return idsToDelete;
@@ -168,3 +172,17 @@ std::vector<idType> HNSWIndex_Single<DataType, DistType>::markDelete(labelType l
168172 label_lookup_.erase (search);
169173 return idsToDelete;
170174}
175+
176+ template <typename DataType, typename DistType>
177+ inline bool HNSWIndex_Single<DataType, DistType>::safeCheckIfLabelExistsInIndex(
178+ labelType label, bool also_done_processing) const {
179+ std::unique_lock<std::mutex> index_data_lock (this ->index_data_guard_ );
180+ auto it = label_lookup_.find (label);
181+ bool exists = it != label_lookup_.end ();
182+ // If we want to make sure that the vector stored under the label was already indexed,
183+ // we go on and check that its associated internal id is no longer in process.
184+ if (exists && also_done_processing) {
185+ return !this ->isInProcess (it->second );
186+ }
187+ return exists;
188+ }
0 commit comments