|
| 1 | +// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. |
| 2 | + |
| 3 | +#include "RocksDBInvertedLists.h" |
| 4 | + |
| 5 | +#include <faiss/impl/FaissAssert.h> |
| 6 | + |
| 7 | +using namespace faiss; |
| 8 | + |
| 9 | +namespace faiss_rocksdb { |
| 10 | + |
| 11 | +RocksDBInvertedListsIterator::RocksDBInvertedListsIterator( |
| 12 | + rocksdb::DB* db, |
| 13 | + size_t list_no, |
| 14 | + size_t code_size) |
| 15 | + : InvertedListsIterator(), |
| 16 | + it(db->NewIterator(rocksdb::ReadOptions())), |
| 17 | + list_no(list_no), |
| 18 | + code_size(code_size), |
| 19 | + codes(code_size) { |
| 20 | + it->Seek(rocksdb::Slice( |
| 21 | + reinterpret_cast<const char*>(&list_no), sizeof(size_t))); |
| 22 | +} |
| 23 | + |
| 24 | +bool RocksDBInvertedListsIterator::is_available() const { |
| 25 | + return it->Valid() && |
| 26 | + it->key().starts_with(rocksdb::Slice( |
| 27 | + reinterpret_cast<const char*>(&list_no), sizeof(size_t))); |
| 28 | +} |
| 29 | + |
| 30 | +void RocksDBInvertedListsIterator::next() { |
| 31 | + it->Next(); |
| 32 | +} |
| 33 | + |
| 34 | +std::pair<idx_t, const uint8_t*> RocksDBInvertedListsIterator:: |
| 35 | + get_id_and_codes() { |
| 36 | + idx_t id = |
| 37 | + *reinterpret_cast<const idx_t*>(&it->key().data()[sizeof(size_t)]); |
| 38 | + assert(code_size == it->value().size()); |
| 39 | + return {id, reinterpret_cast<const uint8_t*>(it->value().data())}; |
| 40 | +} |
| 41 | + |
| 42 | +RocksDBInvertedLists::RocksDBInvertedLists( |
| 43 | + const char* db_directory, |
| 44 | + size_t nlist, |
| 45 | + size_t code_size) |
| 46 | + : InvertedLists(nlist, code_size) { |
| 47 | + use_iterator = true; |
| 48 | + |
| 49 | + rocksdb::Options options; |
| 50 | + options.create_if_missing = true; |
| 51 | + rocksdb::DB* db; |
| 52 | + rocksdb::Status status = rocksdb::DB::Open(options, db_directory, &db); |
| 53 | + db_ = std::unique_ptr<rocksdb::DB>(db); |
| 54 | + assert(status.ok()); |
| 55 | +} |
| 56 | + |
| 57 | +size_t RocksDBInvertedLists::list_size(size_t /*list_no*/) const { |
| 58 | + FAISS_THROW_MSG("list_size is not supported"); |
| 59 | +} |
| 60 | + |
| 61 | +const uint8_t* RocksDBInvertedLists::get_codes(size_t /*list_no*/) const { |
| 62 | + FAISS_THROW_MSG("get_codes is not supported"); |
| 63 | +} |
| 64 | + |
| 65 | +const idx_t* RocksDBInvertedLists::get_ids(size_t /*list_no*/) const { |
| 66 | + FAISS_THROW_MSG("get_ids is not supported"); |
| 67 | +} |
| 68 | + |
| 69 | +size_t RocksDBInvertedLists::add_entries( |
| 70 | + size_t list_no, |
| 71 | + size_t n_entry, |
| 72 | + const idx_t* ids, |
| 73 | + const uint8_t* code) { |
| 74 | + rocksdb::WriteOptions wo; |
| 75 | + std::vector<char> key(sizeof(size_t) + sizeof(idx_t)); |
| 76 | + memcpy(key.data(), &list_no, sizeof(size_t)); |
| 77 | + for (size_t i = 0; i < n_entry; i++) { |
| 78 | + memcpy(key.data() + sizeof(size_t), ids + i, sizeof(idx_t)); |
| 79 | + rocksdb::Status status = db_->Put( |
| 80 | + wo, |
| 81 | + rocksdb::Slice(key.data(), key.size()), |
| 82 | + rocksdb::Slice( |
| 83 | + reinterpret_cast<const char*>(code + i * code_size), |
| 84 | + code_size)); |
| 85 | + assert(status.ok()); |
| 86 | + } |
| 87 | + return 0; // ignored |
| 88 | +} |
| 89 | + |
| 90 | +void RocksDBInvertedLists::update_entries( |
| 91 | + size_t /*list_no*/, |
| 92 | + size_t /*offset*/, |
| 93 | + size_t /*n_entry*/, |
| 94 | + const idx_t* /*ids*/, |
| 95 | + const uint8_t* /*code*/) { |
| 96 | + FAISS_THROW_MSG("update_entries is not supported"); |
| 97 | +} |
| 98 | + |
| 99 | +void RocksDBInvertedLists::resize(size_t /*list_no*/, size_t /*new_size*/) { |
| 100 | + FAISS_THROW_MSG("resize is not supported"); |
| 101 | +} |
| 102 | + |
| 103 | +InvertedListsIterator* RocksDBInvertedLists::get_iterator( |
| 104 | + size_t list_no) const { |
| 105 | + return new RocksDBInvertedListsIterator(db_.get(), list_no, code_size); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace faiss_rocksdb |
0 commit comments