contrib/faiss/faiss/impl/random_access_reader.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/impl/random_access_reader.h> |
9 | | |
10 | | #include <cerrno> |
11 | | #include <cstring> |
12 | | |
13 | | #include <faiss/impl/FaissAssert.h> |
14 | | |
15 | | #ifndef _WIN32 |
16 | | #include <fcntl.h> |
17 | | #include <unistd.h> |
18 | | #endif |
19 | | |
20 | | namespace faiss { |
21 | | |
22 | | /******************************************************* |
23 | | * ReadRef — default borrow() implementation |
24 | | *******************************************************/ |
25 | | |
26 | | namespace { |
27 | | |
28 | | /// ReadRef that owns a heap-allocated buffer. |
29 | | struct OwnedReadRef : ReadRef { |
30 | | std::unique_ptr<uint8_t[]> buf_; |
31 | | OwnedReadRef(std::unique_ptr<uint8_t[]> buf, size_t len) |
32 | 0 | : buf_(std::move(buf)) { |
33 | 0 | data_ = buf_.get(); |
34 | 0 | size_ = len; |
35 | 0 | } |
36 | | }; |
37 | | |
38 | | } // namespace |
39 | | |
40 | | std::unique_ptr<ReadRef> RandomAccessReader::borrow( |
41 | | size_t offset, |
42 | 0 | size_t nbytes) const { |
43 | 0 | auto buf = std::make_unique<uint8_t[]>(nbytes); |
44 | 0 | read_at(offset, buf.get(), nbytes); |
45 | 0 | return std::make_unique<OwnedReadRef>(std::move(buf), nbytes); |
46 | 0 | } |
47 | | |
48 | | /******************************************************* |
49 | | * FileRandomAccessReader — default POSIX pread backend |
50 | | *******************************************************/ |
51 | | |
52 | 0 | FileRandomAccessReader::FileRandomAccessReader(const std::string& filename) { |
53 | 0 | #ifndef _WIN32 |
54 | 0 | fd_ = ::open(filename.c_str(), O_RDONLY); |
55 | 0 | FAISS_THROW_IF_NOT_FMT( |
56 | 0 | fd_ >= 0, |
57 | 0 | "FileRandomAccessReader: cannot open %s: %s", |
58 | 0 | filename.c_str(), |
59 | 0 | strerror(errno)); |
60 | | #else |
61 | | FAISS_THROW_MSG("FileRandomAccessReader is not supported on Windows"); |
62 | | #endif |
63 | 0 | } |
64 | | |
65 | 0 | FileRandomAccessReader::~FileRandomAccessReader() { |
66 | 0 | #ifndef _WIN32 |
67 | 0 | if (fd_ >= 0) { |
68 | 0 | ::close(fd_); |
69 | 0 | } |
70 | 0 | #endif |
71 | 0 | } |
72 | | |
73 | | void FileRandomAccessReader::read_at( |
74 | | size_t offset, |
75 | | void* ptr, |
76 | 0 | size_t nbytes) const { |
77 | 0 | #ifndef _WIN32 |
78 | 0 | size_t done = 0; |
79 | 0 | auto* out = static_cast<uint8_t*>(ptr); |
80 | 0 | while (done < nbytes) { |
81 | 0 | ssize_t nr = ::pread(fd_, out + done, nbytes - done, offset + done); |
82 | 0 | FAISS_THROW_IF_NOT_MSG( |
83 | 0 | nr >= 0, "pread failed in FileRandomAccessReader"); |
84 | 0 | FAISS_THROW_IF_NOT_MSG( |
85 | 0 | nr > 0, "unexpected EOF in FileRandomAccessReader"); |
86 | 0 | done += static_cast<size_t>(nr); |
87 | 0 | } |
88 | | #else |
89 | | (void)offset; |
90 | | (void)ptr; |
91 | | (void)nbytes; |
92 | | FAISS_THROW_MSG("FileRandomAccessReader is not supported on Windows"); |
93 | | #endif |
94 | 0 | } |
95 | | |
96 | | } // namespace faiss |