in cpp/src/hnswalg.h [654:690]
void resizeIndex(size_t new_max_elements) {
if (search_only_)
throw std::runtime_error(
"resizeIndex is not supported in search only mode");
std::unique_lock<std::shared_mutex> lock(resizeLock);
if (new_max_elements < cur_element_count)
throw IndexCannotBeShrunkError(
"Cannot resize to " + std::to_string(new_max_elements) +
" elements, as this index already contains " +
std::to_string(cur_element_count) + " elements.");
delete visited_list_pool_;
visited_list_pool_ = new VisitedListPool(1, new_max_elements);
element_levels_.resize(new_max_elements);
std::vector<std::mutex>(new_max_elements).swap(link_list_locks_);
// Reallocate base layer
char *data_level0_memory_new = (char *)realloc(
data_level0_memory_, new_max_elements * size_data_per_element_);
if (data_level0_memory_new == nullptr)
throw std::runtime_error(
"Not enough memory: resizeIndex failed to allocate base layer");
data_level0_memory_ = data_level0_memory_new;
// Reallocate all other layers
char **linkLists_new =
(char **)realloc(linkLists_, sizeof(void *) * new_max_elements);
if (linkLists_new == nullptr)
throw std::runtime_error(
"Not enough memory: resizeIndex failed to allocate other layers");
linkLists_ = linkLists_new;
max_elements_ = new_max_elements;
}