+ filename != block.data() + block.size();
+ filename += strlen(filename) + 1) {
+ const char *haystack = filename;
+ if (match_basename) {
+ haystack = strrchr(filename, '/');
+ if (haystack == nullptr) {
+ haystack = filename;
+ } else {
+ ++haystack;
+ }
+ }
+
+ bool found = true;
+ for (const Needle &needle : needles) {
+ if (!matches(needle, haystack)) {
+ found = false;
+ break;
+ }
+ }
+ if (found) {
+ if (pending_candidate != nullptr) {
+ test_candidate(pending_candidate, local_seq, local_seq + 1);
+ ++local_seq;
+ }
+ pending_candidate = filename;
+ }
+ }
+ if (pending_candidate == nullptr) {
+ serializer->print(seq << 32, 1ULL << 32, "");
+ } else {
+ test_candidate(pending_candidate, local_seq, (seq + 1) << 32);
+ }
+}
+
+size_t scan_docids(const vector<Needle> &needles, const vector<uint32_t> &docids, const Corpus &corpus, IOUringEngine *engine)
+{
+ Serializer docids_in_order;
+ AccessRXCache access_rx_cache(engine, corpus.get_hdr().require_visibility);
+ atomic<uint64_t> matched{ 0 };
+ for (size_t i = 0; i < docids.size(); ++i) {
+ uint32_t docid = docids[i];
+ corpus.get_compressed_filename_block(docid, [i, &matched, &needles, &access_rx_cache, &docids_in_order](string_view compressed) {
+ scan_file_block(needles, compressed, &access_rx_cache, i, &docids_in_order, &matched);
+ });
+ }
+ engine->finish();
+ return matched;
+}
+
+struct WorkerThread {
+ thread t;
+
+ // We use a result queue instead of synchronizing Serializer,
+ // since a lock on it becomes a huge choke point if there are
+ // lots of threads.
+ mutex result_mu;
+ struct Result {
+ uint64_t seq;
+ uint64_t skip;
+ string msg;
+ };
+ vector<Result> results;
+};
+
+class WorkerThreadReceiver : public ResultReceiver {
+public:
+ WorkerThreadReceiver(WorkerThread *wt)
+ : wt(wt) {}
+
+ void print(uint64_t seq, uint64_t skip, const string msg) override
+ {
+ lock_guard<mutex> lock(wt->result_mu);
+ if (msg.empty() && !wt->results.empty() && wt->results.back().seq + wt->results.back().skip == seq) {
+ wt->results.back().skip += skip;
+ } else {
+ wt->results.emplace_back(WorkerThread::Result{ seq, skip, move(msg) });
+ }
+ }
+
+private:
+ WorkerThread *wt;
+};
+
+void deliver_results(WorkerThread *wt, Serializer *serializer)
+{
+ vector<WorkerThread::Result> results;
+ {
+ lock_guard<mutex> lock(wt->result_mu);
+ results = move(wt->results);
+ }
+ for (const WorkerThread::Result &result : results) {
+ serializer->print(result.seq, result.skip, move(result.msg));
+ }
+}
+
+// We do this sequentially, as it's faster than scattering
+// a lot of I/O through io_uring and hoping the kernel will
+// coalesce it plus readahead for us. Since we assume that
+// we will primarily be CPU-bound, we'll be firing up one
+// worker thread for each spare core (the last one will
+// only be doing I/O). access() is still synchronous.
+uint64_t scan_all_docids(const vector<Needle> &needles, int fd, const Corpus &corpus)
+{
+ {
+ const Header &hdr = corpus.get_hdr();
+ if (hdr.zstd_dictionary_length_bytes > 0) {
+ string dictionary;
+ dictionary.resize(hdr.zstd_dictionary_length_bytes);
+ complete_pread(fd, &dictionary[0], hdr.zstd_dictionary_length_bytes, hdr.zstd_dictionary_offset_bytes);
+ ddict = ZSTD_createDDict(dictionary.data(), dictionary.size());
+ }
+ }
+
+ AccessRXCache access_rx_cache(nullptr, corpus.get_hdr().require_visibility);
+ Serializer serializer;
+ uint32_t num_blocks = corpus.get_num_filename_blocks();
+ unique_ptr<uint64_t[]> offsets(new uint64_t[num_blocks + 1]);
+ complete_pread(fd, offsets.get(), (num_blocks + 1) * sizeof(uint64_t), corpus.offset_for_block(0));
+ atomic<uint64_t> matched{ 0 };
+
+ mutex mu;
+ condition_variable queue_added, queue_removed;
+ deque<tuple<int, int, string>> work_queue; // Under mu.
+ bool done = false; // Under mu.
+
+ unsigned num_threads = max<int>(sysconf(_SC_NPROCESSORS_ONLN) - 1, 1);
+ dprintf("Using %u worker threads for linear scan.\n", num_threads);
+ unique_ptr<WorkerThread[]> threads(new WorkerThread[num_threads]);
+ for (unsigned i = 0; i < num_threads; ++i) {
+ threads[i].t = thread([&threads, &mu, &queue_added, &queue_removed, &work_queue, &done, &offsets, &needles, &access_rx_cache, &matched, i] {
+ // regcomp() takes a lock on the regex, so each thread will need its own.
+ const vector<Needle> *use_needles = &needles;
+ vector<Needle> recompiled_needles;
+ if (i != 0 && patterns_are_regex) {
+ recompiled_needles = needles;
+ for (Needle &needle : recompiled_needles) {
+ needle.re = compile_regex(needle.str);
+ }
+ use_needles = &recompiled_needles;
+ }
+
+ WorkerThreadReceiver receiver(&threads[i]);
+ for (;;) {
+ uint32_t io_docid, last_docid;
+ string compressed;
+
+ {
+ unique_lock<mutex> lock(mu);
+ queue_added.wait(lock, [&work_queue, &done] { return !work_queue.empty() || done; });
+ if (done && work_queue.empty()) {
+ return;
+ }
+ tie(io_docid, last_docid, compressed) = move(work_queue.front());
+ work_queue.pop_front();
+ queue_removed.notify_all();
+ }
+
+ for (uint32_t docid = io_docid; docid < last_docid; ++docid) {
+ size_t relative_offset = offsets[docid] - offsets[io_docid];
+ size_t len = offsets[docid + 1] - offsets[docid];
+ scan_file_block(*use_needles, { &compressed[relative_offset], len }, &access_rx_cache, docid, &receiver, &matched);
+ }
+ }
+ });
+ }
+
+ string compressed;
+ for (uint32_t io_docid = 0; io_docid < num_blocks; io_docid += 32) {
+ uint32_t last_docid = std::min(io_docid + 32, num_blocks);
+ size_t io_len = offsets[last_docid] - offsets[io_docid];
+ if (compressed.size() < io_len) {
+ compressed.resize(io_len);