contrib/faiss/faiss/invlists/InvertedListsIOHook.cpp
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 | | #include <faiss/invlists/InvertedListsIOHook.h> |
9 | | |
10 | | #include <faiss/impl/FaissAssert.h> |
11 | | #include <faiss/impl/io.h> |
12 | | #include <faiss/impl/io_macros.h> |
13 | | |
14 | | #include <faiss/invlists/BlockInvertedLists.h> |
15 | | |
16 | | #ifndef _WIN32 |
17 | | #include <faiss/invlists/OnDiskInvertedLists.h> |
18 | | #endif // !_WIN32 |
19 | | |
20 | | namespace faiss { |
21 | | |
22 | | /********************************************************** |
23 | | * InvertedListIOHook's |
24 | | **********************************************************/ |
25 | | |
26 | | InvertedListsIOHook::InvertedListsIOHook( |
27 | | const std::string& key, |
28 | | const std::string& classname) |
29 | 18 | : key(key), classname(classname) {} |
30 | | |
31 | | namespace { |
32 | | |
33 | | /// std::vector that deletes its contents |
34 | | struct IOHookTable : std::vector<InvertedListsIOHook*> { |
35 | 9 | IOHookTable() { |
36 | 9 | #ifndef _WIN32 |
37 | 9 | push_back(new OnDiskInvertedListsIOHook()); |
38 | 9 | #endif |
39 | 9 | push_back(new BlockInvertedListsIOHook()); |
40 | 9 | } |
41 | | |
42 | 0 | ~IOHookTable() { |
43 | 0 | for (auto x : *this) { |
44 | 0 | delete x; |
45 | 0 | } |
46 | 0 | } |
47 | | }; |
48 | | |
49 | | static IOHookTable InvertedListsIOHook_table; |
50 | | |
51 | | } // namespace |
52 | | |
53 | 0 | InvertedListsIOHook* InvertedListsIOHook::lookup(int h) { |
54 | 0 | for (const auto& callback : InvertedListsIOHook_table) { |
55 | 0 | if (h == fourcc(callback->key)) { |
56 | 0 | return callback; |
57 | 0 | } |
58 | 0 | } |
59 | 0 | FAISS_THROW_FMT( |
60 | 0 | "read_InvertedLists: could not load ArrayInvertedLists as " |
61 | 0 | "%08x (\"%s\")", |
62 | 0 | h, |
63 | 0 | fourcc_inv_printable(h).c_str()); |
64 | 0 | } |
65 | | |
66 | | InvertedListsIOHook* InvertedListsIOHook::lookup_classname( |
67 | 0 | const std::string& classname) { |
68 | 0 | for (const auto& callback : InvertedListsIOHook_table) { |
69 | 0 | if (callback->classname == classname) { |
70 | 0 | return callback; |
71 | 0 | } |
72 | 0 | } |
73 | 0 | FAISS_THROW_FMT( |
74 | 0 | "read_InvertedLists: could not find classname %s", |
75 | 0 | classname.c_str()); |
76 | 0 | } |
77 | | |
78 | 0 | void InvertedListsIOHook::add_callback(InvertedListsIOHook* cb) { |
79 | 0 | InvertedListsIOHook_table.push_back(cb); |
80 | 0 | } |
81 | | |
82 | 0 | void InvertedListsIOHook::print_callbacks() { |
83 | 0 | printf("registered %zd InvertedListsIOHooks:\n", |
84 | 0 | InvertedListsIOHook_table.size()); |
85 | 0 | for (const auto& cb : InvertedListsIOHook_table) { |
86 | 0 | printf("%08x %s %s\n", |
87 | 0 | fourcc(cb->key.c_str()), |
88 | 0 | cb->key.c_str(), |
89 | 0 | cb->classname.c_str()); |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | InvertedLists* InvertedListsIOHook::read_ArrayInvertedLists( |
94 | | IOReader*, |
95 | | int, |
96 | | size_t, |
97 | | size_t, |
98 | 0 | const std::vector<size_t>&) const { |
99 | 0 | FAISS_THROW_FMT("read to array not implemented for %s", classname.c_str()); |
100 | 0 | } |
101 | | |
102 | | } // namespace faiss |