| 306 | */ |
| 307 | |
| 308 | bool OpenMdfFile(std::streambuf& buffer, const std::string &filename, |
| 309 | std::ios_base::openmode mode) { |
| 310 | const std::filebuf* file = nullptr; |
| 311 | // Dirty trick if the file was opened outside through the stream |
| 312 | // buffer interface. |
| 313 | if (filename.empty()) { |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | try { |
| 318 | auto& file_buffer = dynamic_cast<std::filebuf&>(buffer); |
| 319 | |
| 320 | for (size_t delay = 0; file == nullptr && delay < 6'000; delay += 100) { |
| 321 | if (file_buffer.is_open()) { |
| 322 | file_buffer.close(); |
| 323 | } |
| 324 | file = file_buffer.open(filename, mode); |
| 325 | if (file == nullptr) { |
| 326 | if (!exists(filename)) { |
| 327 | MDF_ERROR() << "The file doesn't exist. File: " << filename; |
| 328 | return false; |
| 329 | } |
| 330 | // Failed to open the file. Try again with a delay |
| 331 | std::this_thread::sleep_for( 100ms); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | } catch (const std::exception& ) { |
| 336 | // No error that the buffer isn't a file buffer. |
| 337 | // Simulate that the stream is open. |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | if (file == nullptr) { |
| 342 | MDF_ERROR() << "Failed to open the file due to lock timeout (5 s). File: " |
| 343 | << filename; |
| 344 | } |
| 345 | return file != nullptr; |
| 346 | } |
| 347 | |
| 348 | int64_t MdfBlock::Index() const { |
| 349 | return file_position_; |