contrib/faiss/faiss/invlists/InvertedListsIOHook.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <faiss/impl/io.h> |
11 | | #include <faiss/invlists/InvertedLists.h> |
12 | | #include <string> |
13 | | |
14 | | namespace faiss { |
15 | | |
16 | | /** Callbacks to handle other types of InvertedList objects. |
17 | | * |
18 | | * The callbacks should be registered with add_callback before calling |
19 | | * read_index or read_InvertedLists. The callbacks for |
20 | | * OnDiskInvertedLists are registrered by default. The invlist type is |
21 | | * identified by: |
22 | | * |
23 | | * - the key (a fourcc) at read time |
24 | | * - the class name (as given by typeid.name) at write time |
25 | | */ |
26 | | struct InvertedListsIOHook { |
27 | | const std::string key; ///< string version of the fourcc |
28 | | const std::string classname; ///< typeid.name |
29 | | |
30 | | InvertedListsIOHook(const std::string& key, const std::string& classname); |
31 | | |
32 | | /// write the index to the IOWriter (including the fourcc) |
33 | | virtual void write(const InvertedLists* ils, IOWriter* f) const = 0; |
34 | | |
35 | | /// called when the fourcc matches this class's fourcc |
36 | | virtual InvertedLists* read(IOReader* f, int io_flags) const = 0; |
37 | | |
38 | | /** read from a ArrayInvertedLists into this invertedlist type. |
39 | | * For this to work, the callback has to be enabled and the io_flag has to |
40 | | * be set to IO_FLAG_SKIP_IVF_DATA | (16 upper bits of the fourcc) |
41 | | * |
42 | | * (default implementation fails) |
43 | | */ |
44 | | virtual InvertedLists* read_ArrayInvertedLists( |
45 | | IOReader* f, |
46 | | int io_flags, |
47 | | size_t nlist, |
48 | | size_t code_size, |
49 | | const std::vector<size_t>& sizes) const; |
50 | | |
51 | 0 | virtual ~InvertedListsIOHook() {} |
52 | | |
53 | | /**************************** Manage the set of callbacks ******/ |
54 | | |
55 | | // transfers ownership |
56 | | static void add_callback(InvertedListsIOHook*); |
57 | | static void print_callbacks(); |
58 | | static InvertedListsIOHook* lookup(int h); |
59 | | static InvertedListsIOHook* lookup_classname(const std::string& classname); |
60 | | }; |
61 | | |
62 | | } // namespace faiss |