contrib/faiss/faiss/impl/random_access_reader.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 <cstddef> |
11 | | #include <cstdint> |
12 | | #include <memory> |
13 | | #include <string> |
14 | | |
15 | | namespace faiss { |
16 | | |
17 | | /** |
18 | | * Opaque handle returned by RandomAccessReader::borrow(). |
19 | | * |
20 | | * Keeps the underlying data alive (e.g. a pinned cache entry, an mmap |
21 | | * region, or a plain heap buffer). The data() pointer is valid for the |
22 | | * lifetime of this object. |
23 | | */ |
24 | | struct ReadRef { |
25 | 0 | virtual ~ReadRef() = default; |
26 | | |
27 | 0 | const uint8_t* data() const { |
28 | 0 | return data_; |
29 | 0 | } |
30 | 0 | size_t size() const { |
31 | 0 | return size_; |
32 | 0 | } |
33 | | |
34 | | protected: |
35 | | const uint8_t* data_ = nullptr; |
36 | | size_t size_ = 0; |
37 | | }; |
38 | | |
39 | | /** |
40 | | * Abstract interface for random-access (pread-like) reads. |
41 | | * |
42 | | * Unlike IOReader (which is a sequential stream), RandomAccessReader |
43 | | * supports positional reads without internal state, making it safe |
44 | | * for concurrent use from multiple threads. |
45 | | * |
46 | | * This is the runtime data-access interface used during search to |
47 | | * fetch inverted-list data on demand, as opposed to IOReader which |
48 | | * is used for index serialization / deserialization. |
49 | | */ |
50 | | struct RandomAccessReader { |
51 | 0 | virtual ~RandomAccessReader() = default; |
52 | | |
53 | | /** |
54 | | * Read exactly @p nbytes starting at byte @p offset into @p ptr. |
55 | | * Must throw on short read or I/O error. |
56 | | */ |
57 | | virtual void read_at(size_t offset, void* ptr, size_t nbytes) const = 0; |
58 | | |
59 | | /** |
60 | | * Borrow a region of data and return a ReadRef that keeps it alive. |
61 | | * |
62 | | * The default implementation allocates a buffer and calls read_at(). |
63 | | * Subclasses (e.g. a cache-backed reader) can override this to return |
64 | | * a direct pointer into cached / mapped memory without any copy. |
65 | | */ |
66 | | virtual std::unique_ptr<ReadRef> borrow( |
67 | | size_t offset, |
68 | | size_t nbytes) const; |
69 | | }; |
70 | | |
71 | | /** |
72 | | * Default RandomAccessReader backed by pread(fd) on a local file. |
73 | | * Only available on POSIX systems. |
74 | | */ |
75 | | struct FileRandomAccessReader : RandomAccessReader { |
76 | | explicit FileRandomAccessReader(const std::string& filename); |
77 | | ~FileRandomAccessReader() override; |
78 | | |
79 | | void read_at(size_t offset, void* ptr, size_t nbytes) const override; |
80 | | |
81 | | private: |
82 | | int fd_ = -1; |
83 | | }; |
84 | | |
85 | | } // namespace faiss |