4 #include <unordered_map>
11 #include <arpa/inet.h>
17 #define P4NENC_BOUND(n) ((n+127)/128+(n+32)*sizeof(uint32_t))
20 using namespace std::chrono;
23 //#define dprintf(...) fprintf(stderr, __VA_ARGS__);
25 static inline uint32_t read_unigram(const string &s, size_t idx)
28 return (unsigned char)s[idx];
34 static inline uint32_t read_trigram(const string &s, size_t start)
36 return read_unigram(s, start) |
37 (read_unigram(s, start + 1) << 8) |
38 (read_unigram(s, start + 2) << 16);
41 bool has_access(const char *filename, unordered_map<string, bool> *access_rx_cache)
43 const char *end = strchr(filename + 1, '/');
44 while (end != nullptr) {
45 string parent_path(filename, end);
46 auto it = access_rx_cache->find(parent_path);
48 if (it == access_rx_cache->end()) {
49 ok = access(parent_path.c_str(), R_OK | X_OK) == 0;
50 access_rx_cache->emplace(move(parent_path), ok);
57 end = strchr(end + 1, '/');
61 // Check for rx first in the cache; if that isn't true, check R_OK uncached.
62 // This is roughly the same thing as mlocate does.
63 auto it = access_rx_cache->find(filename);
64 if (it != access_rx_cache->end() && it->second) {
68 return access(filename, R_OK) == 0;
79 size_t scan_docid(const string &needle, uint32_t docid, const char *data, const uint64_t *filename_offsets, unordered_map<string, bool> *access_rx_cache)
81 const char *compressed = (const char *)(data + filename_offsets[docid]);
82 size_t compressed_size = filename_offsets[docid + 1] - filename_offsets[docid]; // Allowed we have a sentinel block at the end.
86 block.resize(ZSTD_getFrameContentSize(compressed, compressed_size) + 1);
88 ZSTD_decompress(&block[0], block.size(), compressed, compressed_size);
89 block[block.size() - 1] = '\0';
91 for (const char *filename = block.data();
92 filename != block.data() + block.size();
93 filename += strlen(filename) + 1) {
94 if (strstr(filename, needle.c_str()) == nullptr) {
97 if (has_access(filename, access_rx_cache)) {
99 printf("%s\n", filename);
105 void do_search_file(const string &needle, const char *filename)
107 int fd = open(filename, O_RDONLY);
114 if (setgid(getgid()) != 0) {
119 //steady_clock::time_point start = steady_clock::now();
120 if (access("/", R_OK | X_OK)) {
121 // We can't find anything, no need to bother...
125 off_t len = lseek(fd, 0, SEEK_END);
130 const char *data = (char *)mmap(nullptr, len, PROT_READ, MAP_SHARED, fd, /*offset=*/0);
131 if (data == MAP_FAILED) {
135 uint64_t num_trigrams = *(const uint64_t *)data;
136 uint64_t filename_index_offset = *(const uint64_t *)(data + sizeof(uint64_t));
138 const Trigram *trgm_begin = (Trigram *)(data + sizeof(uint64_t) * 2);
139 const Trigram *trgm_end = trgm_begin + num_trigrams;
141 vector<const Trigram *> trigrams;
142 for (size_t i = 0; i < needle.size() - 2; ++i) {
143 uint32_t trgm = read_trigram(needle, i);
144 const Trigram *trgmptr = lower_bound(trgm_begin, trgm_end, trgm, [](const Trigram &trgm, uint32_t t) {
145 return trgm.trgm < t;
147 if (trgmptr == trgm_end || trgmptr->trgm != trgm) {
148 dprintf("trigram %06x isn't found, we abort the search\n", trgm);
149 munmap((void *)data, len);
153 trigrams.push_back(trgmptr);
155 sort(trigrams.begin(), trigrams.end());
157 auto last = unique(trigrams.begin(), trigrams.end());
158 trigrams.erase(last, trigrams.end());
160 sort(trigrams.begin(), trigrams.end(), [&](const Trigram *a, const Trigram *b) {
161 return a->num_docids < b->num_docids;
164 vector<uint32_t> in1, in2, out;
165 for (const Trigram *trgmptr : trigrams) {
166 //uint32_t trgm = trgmptr->trgm;
167 size_t num = trgmptr->num_docids;
168 unsigned char *pldata = (unsigned char *)(data + trgmptr->offset);
170 in1.resize(num + 128);
171 p4nd1dec128v32((unsigned char *)pldata, num, &in1[0]);
173 dprintf("trigram '%c%c%c' decoded to %zu entries\n", trgm & 0xff, (trgm >> 8) & 0xff, (trgm >> 16) & 0xff, num);
175 if (num > in1.size() * 100) {
176 dprintf("trigram '%c%c%c' has %zu entries, ignoring the rest (will weed out false positives later)\n",
177 trgm & 0xff, (trgm >> 8) & 0xff, (trgm >> 16) & 0xff, num);
181 if (in2.size() < num + 128) {
182 in2.resize(num + 128);
184 p4nd1dec128v32((unsigned char *)pldata, num, &in2[0]);
187 set_intersection(in1.begin(), in1.end(), in2.begin(), in2.begin() + num, back_inserter(out));
189 dprintf("trigram '%c%c%c' decoded to %zu entries, %zu left\n", trgm & 0xff, (trgm >> 8) & 0xff, (trgm >> 16) & 0xff, num, in1.size());
191 dprintf("no matches (intersection list is empty)\n");
196 steady_clock::time_point end = steady_clock::now();
198 dprintf("Intersection took %.1f ms. Doing final verification and printing:\n",
199 1e3 * duration<float>(end - start).count());
201 unordered_map<string, bool> access_rx_cache;
203 const uint64_t *filename_offsets = (const uint64_t *)(data + filename_index_offset);
205 for (uint32_t docid : in1) {
206 matched += scan_docid(needle, docid, data, filename_offsets, &access_rx_cache);
208 end = steady_clock::now();
209 dprintf("Done in %.1f ms, found %d matches.\n",
210 1e3 * duration<float>(end - start).count(), matched);
212 munmap((void *)data, len);
216 int main(int argc, char **argv)
218 //do_search_file(argv[1], "all.trgm");
219 do_search_file(argv[1], "/var/lib/mlocate/plocate.db");