| 345 | } |
| 346 | |
| 347 | bool PleReader::open(const std::string& path, uint64_t table_offset, uint64_t n_rows, uint32_t max_inflight, |
| 348 | uint64_t cache_rows, std::string& err, bool io_thread, uint32_t row_bytes) { |
| 349 | close(); |
| 350 | if (max_inflight == 0 || max_inflight > 1024) { err = "PleReader: max_inflight must be 1..1024"; return false; } |
| 351 | if (row_bytes == 0 || row_bytes > PAGE) { err = "PleReader: row_bytes must be 1..4096"; return false; } |
| 352 | if (!impl_->file.open(path, err)) return false; |
| 353 | impl_->row_bytes = row_bytes; |
| 354 | if (table_offset + n_rows * (uint64_t) row_bytes > impl_->file.size()) { |
| 355 | err = "PleReader: the table extends past the end of " + path; |
| 356 | close(); |
| 357 | return false; |
| 358 | } |
| 359 | impl_->table_offset = table_offset; |
| 360 | impl_->n_rows = n_rows; |
| 361 | impl_->max_inflight = max_inflight; |
| 362 | impl_->slab = (uint8_t*) DirectFile::alloc_aligned((size_t) max_inflight * 2 * PAGE); |
| 363 | if (impl_->slab == nullptr) { err = "PleReader: cannot allocate read buffers"; close(); return false; } |
| 364 | impl_->inflight.assign(max_inflight, Job{}); |
| 365 | impl_->free_slots.clear(); |
| 366 | for (uint32_t s = max_inflight; s-- > 0;) impl_->free_slots.push_back(s); |
| 367 | impl_->cache.init(cache_rows, row_bytes); |
| 368 | impl_->error.clear(); |
| 369 | impl_->keep_us = 0; |
| 370 | impl_->last_issue_us = impl_->last_read_us = 0; |
| 371 | impl_->rng = 0x9E3779B97F4A7C15ull ^ (uint64_t) now_us(); |
| 372 | if (impl_->rng == 0) impl_->rng = 1; |
| 373 | reset_stats(); |
| 374 | impl_->stop = false; |
| 375 | impl_->threaded = io_thread; |
| 376 | if (io_thread) { |
| 377 | try { |
| 378 | impl_->worker = std::thread([this] { strata::aux_cpus::pin_current_thread(); impl_->worker_loop(); }); |
| 379 | } catch (const std::exception& e) { |
| 380 | err = std::string("PleReader: cannot create I/O worker: ") + e.what(); |
| 381 | close(); |
| 382 | return false; |
| 383 | } catch (...) { |
| 384 | err = "PleReader: cannot create I/O worker"; |
| 385 | close(); |
| 386 | return false; |
| 387 | } |
| 388 | } |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | void PleReader::close() { |
| 393 | Impl& m = *impl_; |