@@ -31,74 +31,44 @@ namespace milvus {
3131namespace codec {
3232
3333void
34- DefaultVectorsFormat::read_vectors_internal (const std::string& file_path, off_t offset, size_t num,
35- std::vector<uint8_t >& raw_vectors) {
36- int rv_fd = open (file_path.c_str (), O_RDONLY, 00664 );
37- if (rv_fd == -1 ) {
34+ DefaultVectorsFormat::read_vectors_internal (const storage::FSHandlerPtr& fs_ptr, const std::string& file_path,
35+ off_t offset, size_t num, std::vector<uint8_t >& raw_vectors) {
36+ if (!fs_ptr->reader_ptr_ ->open (file_path.c_str ())) {
3837 std::string err_msg = " Failed to open file: " + file_path + " , error: " + std::strerror (errno);
3938 ENGINE_LOG_ERROR << err_msg;
40- throw Exception (SERVER_CANNOT_CREATE_FILE , err_msg);
39+ throw Exception (SERVER_CANNOT_OPEN_FILE , err_msg);
4140 }
4241
4342 size_t num_bytes;
44- if (::read (rv_fd, &num_bytes, sizeof (size_t )) == -1 ) {
45- std::string err_msg = " Failed to read from file: " + file_path + " , error: " + std::strerror (errno);
46- ENGINE_LOG_ERROR << err_msg;
47- throw Exception (SERVER_WRITE_ERROR, err_msg);
48- }
43+ fs_ptr->reader_ptr_ ->read (&num_bytes, sizeof (size_t ));
4944
5045 num = std::min (num, num_bytes - offset);
5146
5247 offset += sizeof (size_t ); // Beginning of file is num_bytes
53- int off = lseek (rv_fd, offset, SEEK_SET);
54- if (off == -1 ) {
55- std::string err_msg = " Failed to seek file: " + file_path + " , error: " + std::strerror (errno);
56- ENGINE_LOG_ERROR << err_msg;
57- throw Exception (SERVER_WRITE_ERROR, err_msg);
58- }
48+ fs_ptr->reader_ptr_ ->seekg (offset);
5949
6050 raw_vectors.resize (num / sizeof (uint8_t ));
61- if (::read (rv_fd, raw_vectors.data (), num) == -1 ) {
62- std::string err_msg = " Failed to read from file: " + file_path + " , error: " + std::strerror (errno);
63- ENGINE_LOG_ERROR << err_msg;
64- throw Exception (SERVER_WRITE_ERROR, err_msg);
65- }
51+ fs_ptr->reader_ptr_ ->read (raw_vectors.data (), num);
6652
67- if (::close (rv_fd) == -1 ) {
68- std::string err_msg = " Failed to close file: " + file_path + " , error: " + std::strerror (errno);
69- ENGINE_LOG_ERROR << err_msg;
70- throw Exception (SERVER_WRITE_ERROR, err_msg);
71- }
53+ fs_ptr->reader_ptr_ ->close ();
7254}
7355
7456void
75- DefaultVectorsFormat::read_uids_internal (const std::string& file_path, std::vector<segment:: doc_id_t >& uids) {
76- int uid_fd = open (file_path. c_str (), O_RDONLY, 00664 );
77- if (uid_fd == - 1 ) {
57+ DefaultVectorsFormat::read_uids_internal (const storage::FSHandlerPtr& fs_ptr, const std::string& file_path,
58+ std::vector<segment:: doc_id_t >& uids) {
59+ if (!fs_ptr-> reader_ptr_ -> open (file_path. c_str ()) ) {
7860 std::string err_msg = " Failed to open file: " + file_path + " , error: " + std::strerror (errno);
7961 ENGINE_LOG_ERROR << err_msg;
80- throw Exception (SERVER_CANNOT_CREATE_FILE , err_msg);
62+ throw Exception (SERVER_CANNOT_OPEN_FILE , err_msg);
8163 }
8264
8365 size_t num_bytes;
84- if (::read (uid_fd, &num_bytes, sizeof (size_t )) == -1 ) {
85- std::string err_msg = " Failed to read from file: " + file_path + " , error: " + std::strerror (errno);
86- ENGINE_LOG_ERROR << err_msg;
87- throw Exception (SERVER_WRITE_ERROR, err_msg);
88- }
66+ fs_ptr->reader_ptr_ ->read (&num_bytes, sizeof (size_t ));
8967
9068 uids.resize (num_bytes / sizeof (segment::doc_id_t ));
91- if (::read (uid_fd, uids.data (), num_bytes) == -1 ) {
92- std::string err_msg = " Failed to read from file: " + file_path + " , error: " + std::strerror (errno);
93- ENGINE_LOG_ERROR << err_msg;
94- throw Exception (SERVER_WRITE_ERROR, err_msg);
95- }
69+ fs_ptr->reader_ptr_ ->read (uids.data (), num_bytes);
9670
97- if (::close (uid_fd) == -1 ) {
98- std::string err_msg = " Failed to close file: " + file_path + " , error: " + std::strerror (errno);
99- ENGINE_LOG_ERROR << err_msg;
100- throw Exception (SERVER_WRITE_ERROR, err_msg);
101- }
71+ fs_ptr->reader_ptr_ ->close ();
10272}
10373
10474void
@@ -121,13 +91,13 @@ DefaultVectorsFormat::read(const storage::FSHandlerPtr& fs_ptr, segment::Vectors
12191 const auto & path = it->path ();
12292 if (path.extension ().string () == raw_vector_extension_) {
12393 std::vector<uint8_t > vector_list;
124- read_vectors_internal (path.string (), 0 , INT64_MAX, vector_list);
94+ read_vectors_internal (fs_ptr, path.string (), 0 , INT64_MAX, vector_list);
12595 vectors_read->AddData (vector_list);
12696 vectors_read->SetName (path.stem ().string ());
12797 }
12898 if (path.extension ().string () == user_id_extension_) {
12999 std::vector<segment::doc_id_t > uids;
130- read_uids_internal (path.string (), uids);
100+ read_uids_internal (fs_ptr, path.string (), uids);
131101 vectors_read->AddUids (uids);
132102 }
133103 }
@@ -144,54 +114,28 @@ DefaultVectorsFormat::write(const storage::FSHandlerPtr& fs_ptr, const segment::
144114
145115 TimeRecorder rc (" write vectors" );
146116
147- int rv_fd = open (rv_file_path.c_str (), O_WRONLY | O_TRUNC | O_CREAT, 00664 );
148- if (rv_fd == -1 ) {
117+ if (!fs_ptr->writer_ptr_ ->open (rv_file_path.c_str ())) {
149118 std::string err_msg = " Failed to open file: " + rv_file_path + " , error: " + std::strerror (errno);
150119 ENGINE_LOG_ERROR << err_msg;
151120 throw Exception (SERVER_CANNOT_CREATE_FILE, err_msg);
152121 }
153122
154123 size_t rv_num_bytes = vectors->GetData ().size () * sizeof (uint8_t );
155- if (::write (rv_fd, &rv_num_bytes, sizeof (size_t )) == -1 ) {
156- std::string err_msg = " Failed to write to file: " + rv_file_path + " , error: " + std::strerror (errno);
157- ENGINE_LOG_ERROR << err_msg;
158- throw Exception (SERVER_WRITE_ERROR, err_msg);
159- }
160- if (::write (rv_fd, vectors->GetData ().data (), rv_num_bytes) == -1 ) {
161- std::string err_msg = " Failed to write to file: " + rv_file_path + " , error: " + std::strerror (errno);
162- ENGINE_LOG_ERROR << err_msg;
163- throw Exception (SERVER_WRITE_ERROR, err_msg);
164- }
165- if (::close (rv_fd) == -1 ) {
166- std::string err_msg = " Failed to close file: " + rv_file_path + " , error: " + std::strerror (errno);
167- ENGINE_LOG_ERROR << err_msg;
168- throw Exception (SERVER_WRITE_ERROR, err_msg);
169- }
124+ fs_ptr->writer_ptr_ ->write (&rv_num_bytes, sizeof (size_t ));
125+ fs_ptr->writer_ptr_ ->write ((void *)vectors->GetData ().data (), rv_num_bytes);
126+ fs_ptr->writer_ptr_ ->close ();
170127
171128 rc.RecordSection (" write rv done" );
172129
173- int uid_fd = open (uid_file_path.c_str (), O_WRONLY | O_TRUNC | O_CREAT, 00664 );
174- if (uid_fd == -1 ) {
130+ if (!fs_ptr->writer_ptr_ ->open (uid_file_path.c_str ())) {
175131 std::string err_msg = " Failed to open file: " + uid_file_path + " , error: " + std::strerror (errno);
176132 ENGINE_LOG_ERROR << err_msg;
177133 throw Exception (SERVER_CANNOT_CREATE_FILE, err_msg);
178134 }
179135 size_t uid_num_bytes = vectors->GetUids ().size () * sizeof (segment::doc_id_t );
180- if (::write (uid_fd, &uid_num_bytes, sizeof (size_t )) == -1 ) {
181- std::string err_msg = " Failed to write to file" + rv_file_path + " , error: " + std::strerror (errno);
182- ENGINE_LOG_ERROR << err_msg;
183- throw Exception (SERVER_WRITE_ERROR, err_msg);
184- }
185- if (::write (uid_fd, vectors->GetUids ().data (), uid_num_bytes) == -1 ) {
186- std::string err_msg = " Failed to write to file" + uid_file_path + " , error: " + std::strerror (errno);
187- ENGINE_LOG_ERROR << err_msg;
188- throw Exception (SERVER_WRITE_ERROR, err_msg);
189- }
190- if (::close (uid_fd) == -1 ) {
191- std::string err_msg = " Failed to close file: " + uid_file_path + " , error: " + std::strerror (errno);
192- ENGINE_LOG_ERROR << err_msg;
193- throw Exception (SERVER_WRITE_ERROR, err_msg);
194- }
136+ fs_ptr->writer_ptr_ ->write (&uid_num_bytes, sizeof (size_t ));
137+ fs_ptr->writer_ptr_ ->write ((void *)vectors->GetUids ().data (), uid_num_bytes);
138+ fs_ptr->writer_ptr_ ->close ();
195139
196140 rc.RecordSection (" write uids done" );
197141}
@@ -215,7 +159,7 @@ DefaultVectorsFormat::read_uids(const storage::FSHandlerPtr& fs_ptr, std::vector
215159 for (; it != it_end; ++it) {
216160 const auto & path = it->path ();
217161 if (path.extension ().string () == user_id_extension_) {
218- read_uids_internal (path.string (), uids);
162+ read_uids_internal (fs_ptr, path.string (), uids);
219163 }
220164 }
221165}
@@ -240,7 +184,7 @@ DefaultVectorsFormat::read_vectors(const storage::FSHandlerPtr& fs_ptr, off_t of
240184 for (; it != it_end; ++it) {
241185 const auto & path = it->path ();
242186 if (path.extension ().string () == raw_vector_extension_) {
243- read_vectors_internal (path.string (), offset, num_bytes, raw_vectors);
187+ read_vectors_internal (fs_ptr, path.string (), offset, num_bytes, raw_vectors);
244188 }
245189 }
246190}
0 commit comments