|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "VecSim/vec_sim_tiered_index.h" |
| 4 | +#include "hnsw.h" |
| 5 | +#include "hnsw_factory.h" |
| 6 | + |
| 7 | +#include <unordered_map> |
| 8 | + |
| 9 | +template <typename DataType, typename DistType> |
| 10 | +class TieredHNSWIndex : public VecSimTieredIndex<DataType, DistType> { |
| 11 | +private: |
| 12 | + /// Mappings from id/label to associated jobs, for invalidating and update ids if necessary. |
| 13 | + // In MULTI, we can have more than one insert job pending per label |
| 14 | + std::unordered_map<labelType, std::vector<HNSWInsertJob *>> labelToInsertJobs; |
| 15 | + std::unordered_map<idType, std::vector<HNSWRepairJob *>> idToRepairJobs; |
| 16 | + std::unordered_map<idType, HNSWSwapJob *> idToSwapJob; |
| 17 | + |
| 18 | + // Todo: implement these methods later on |
| 19 | + void executeInsertJob(HNSWInsertJob *job) {} |
| 20 | + void executeRepairJob(HNSWRepairJob *job) {} |
| 21 | + |
| 22 | + // To be executed synchronously upon deleting a vector, doesn't require a wrapper. |
| 23 | + void executeSwapJob(HNSWSwapJob *job) {} |
| 24 | + |
| 25 | + // Wrappers static functions to be sent as callbacks upon creating the jobs (since members |
| 26 | + // functions cannot serve as callback, this serve as the "gateway" to the appropriate index). |
| 27 | + static void executeInsertJobWrapper(HNSWInsertJob *job) { |
| 28 | + reinterpret_cast<TieredHNSWIndex<DataType, DistType> *>(job->index)->executeInsertJob(job); |
| 29 | + } |
| 30 | + static void executeRepairJobWrapper(HNSWRepairJob *job) { |
| 31 | + reinterpret_cast<TieredHNSWIndex<DataType, DistType> *>(job->index)->executeRepairJob(job); |
| 32 | + } |
| 33 | + |
| 34 | +#ifdef BUILD_TESTS |
| 35 | +#include "VecSim/algorithms/hnsw/hnsw_tiered_tests_friends.h" |
| 36 | +#endif |
| 37 | + |
| 38 | +public: |
| 39 | + TieredHNSWIndex(HNSWIndex<DataType, DistType> *hnsw_index, TieredIndexParams tieredParams) |
| 40 | + : VecSimTieredIndex<DataType, DistType>(hnsw_index, tieredParams) {} |
| 41 | + virtual ~TieredHNSWIndex() = default; |
| 42 | + |
| 43 | + // TODO: Implement the actual methods instead of these temporary ones. |
| 44 | + int addVector(const void *blob, labelType id) override { |
| 45 | + return this->index->addVector(blob, id); |
| 46 | + } |
| 47 | + int deleteVector(labelType id) override { return this->index->deleteVector(id); } |
| 48 | + double getDistanceFrom(labelType id, const void *blob) const override { |
| 49 | + return this->index->getDistanceFrom(id, blob); |
| 50 | + } |
| 51 | + size_t indexSize() const override { return this->index->indexSize(); } |
| 52 | + size_t indexLabelCount() const override { return this->index->indexLabelCount(); } |
| 53 | + VecSimQueryResult_List topKQuery(const void *queryBlob, size_t k, |
| 54 | + VecSimQueryParams *queryParams) override { |
| 55 | + return this->index->topKQuery(queryBlob, k, queryParams); |
| 56 | + } |
| 57 | + VecSimQueryResult_List rangeQuery(const void *queryBlob, double radius, |
| 58 | + VecSimQueryParams *queryParams) override { |
| 59 | + return this->index->rangeQuery(queryBlob, radius, queryParams); |
| 60 | + } |
| 61 | + VecSimIndexInfo info() const override { return this->index->info(); } |
| 62 | + VecSimInfoIterator *infoIterator() const override { return this->index->infoIterator(); } |
| 63 | + VecSimBatchIterator *newBatchIterator(const void *queryBlob, |
| 64 | + VecSimQueryParams *queryParams) const override { |
| 65 | + return this->index->newBatchIterator(queryBlob, queryParams); |
| 66 | + } |
| 67 | + bool preferAdHocSearch(size_t subsetSize, size_t k, bool initial_check) override { |
| 68 | + return this->index->preferAdHocSearch(subsetSize, k, initial_check); |
| 69 | + } |
| 70 | + inline void setLastSearchMode(VecSearchMode mode) override { |
| 71 | + return this->index->setLastSearchMode(mode); |
| 72 | + } |
| 73 | +}; |
0 commit comments