]> git.sesse.net Git - plocate/blob - database-builder.cpp
Optimize trigram reading somewhat.
[plocate] / database-builder.cpp
1 #include "database-builder.h"
2
3 #include "dprintf.h"
4 #include "turbopfor-encode.h"
5
6 #include <algorithm>
7 #include <assert.h>
8 #ifdef HAS_ENDIAN_H
9 #include <endian.h>
10 #endif
11 #include <fcntl.h>
12 #include <string.h>
13 #include <string_view>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <zdict.h>
19 #include <zstd.h>
20
21 #define P4NENC_BOUND(n) ((n + 127) / 128 + (n + 32) * sizeof(uint32_t))
22
23 #define NUM_TRIGRAMS 16777216
24
25 using namespace std;
26 using namespace std::chrono;
27
28 constexpr unsigned num_overflow_slots = 16;
29
30 string zstd_compress(const string &src, ZSTD_CDict *cdict, string *tempbuf);
31
32 // NOTE: Will read one byte past the end of the trigram, but it's OK,
33 // since we always call it from contexts where there's a terminating zero byte.
34 static inline uint32_t read_trigram(const string_view s, size_t start)
35 {
36         uint32_t trgm;
37         memcpy(&trgm, s.data() + start, sizeof(trgm));
38         trgm = le32toh(trgm);
39         return trgm & 0xffffff;
40 }
41
42 class PostingListBuilder {
43 public:
44         inline void add_docid(uint32_t docid);
45         inline void add_first_docid(uint32_t docid);
46         void finish();
47
48         vector<unsigned char> encoded;
49         size_t num_docids = 0;
50
51 private:
52         void write_header(uint32_t docid);
53         void append_block();
54
55         vector<uint32_t> pending_deltas;
56
57         uint32_t last_docid = -1;
58 };
59
60 void PostingListBuilder::add_docid(uint32_t docid)
61 {
62         // Deduplicate against the last inserted value, if any.
63         if (docid == last_docid) {
64                 return;
65         }
66
67         assert(num_docids != 0);
68
69         pending_deltas.push_back(docid - last_docid - 1);
70         last_docid = docid;
71         if (pending_deltas.size() == 128) {
72                 append_block();
73                 pending_deltas.clear();
74         }
75         ++num_docids;
76 }
77
78 void PostingListBuilder::add_first_docid(uint32_t docid)
79 {
80         write_header(docid);
81         ++num_docids;
82         last_docid = docid;
83 }
84
85 void PostingListBuilder::finish()
86 {
87         if (pending_deltas.empty()) {
88                 return;
89         }
90
91         assert(!encoded.empty());  // write_header() should already have run.
92
93         // No interleaving for partial blocks.
94         unsigned char buf[P4NENC_BOUND(128)];
95         unsigned char *end = encode_pfor_single_block<128>(pending_deltas.data(), pending_deltas.size(), /*interleaved=*/false, buf);
96         encoded.insert(encoded.end(), buf, end);
97 }
98
99 void PostingListBuilder::append_block()
100 {
101         unsigned char buf[P4NENC_BOUND(128)];
102         assert(pending_deltas.size() == 128);
103         unsigned char *end = encode_pfor_single_block<128>(pending_deltas.data(), 128, /*interleaved=*/true, buf);
104         encoded.insert(encoded.end(), buf, end);
105 }
106
107 void PostingListBuilder::write_header(uint32_t docid)
108 {
109         unsigned char buf[P4NENC_BOUND(1)];
110         unsigned char *end = write_baseval(docid, buf);
111         encoded.insert(encoded.end(), buf, end);
112 }
113
114 void DictionaryBuilder::add_file(string filename, dir_time)
115 {
116         if (keep_current_block) {  // Only bother saving the filenames if we're actually keeping the block.
117                 if (!current_block.empty()) {
118                         current_block.push_back('\0');
119                 }
120                 current_block += filename;
121         }
122         if (++num_files_in_block == block_size) {
123                 flush_block();
124         }
125 }
126
127 void DictionaryBuilder::flush_block()
128 {
129         if (keep_current_block) {
130                 if (slot_for_current_block == -1) {
131                         lengths.push_back(current_block.size());
132                         sampled_blocks.push_back(move(current_block));
133                 } else {
134                         lengths[slot_for_current_block] = current_block.size();
135                         sampled_blocks[slot_for_current_block] = move(current_block);
136                 }
137         }
138         current_block.clear();
139         num_files_in_block = 0;
140         ++block_num;
141
142         if (block_num < blocks_to_keep) {
143                 keep_current_block = true;
144                 slot_for_current_block = -1;
145         } else {
146                 // Keep every block with equal probability (reservoir sampling).
147                 uint64_t idx = uniform_int_distribution<uint64_t>(0, block_num)(reservoir_rand);
148                 keep_current_block = (idx < blocks_to_keep);
149                 slot_for_current_block = idx;
150         }
151 }
152
153 string DictionaryBuilder::train(size_t buf_size)
154 {
155         string dictionary_buf;
156         sort(sampled_blocks.begin(), sampled_blocks.end());  // Seemingly important for decompression speed.
157         for (const string &block : sampled_blocks) {
158                 dictionary_buf += block;
159         }
160
161         string buf;
162         buf.resize(buf_size);
163         size_t ret = ZDICT_trainFromBuffer(&buf[0], buf_size, dictionary_buf.data(), lengths.data(), lengths.size());
164         if (ZDICT_isError(ret)) {
165                 return "";
166         }
167         dprintf("Sampled %zu bytes in %zu blocks, built a dictionary of size %zu\n", dictionary_buf.size(), lengths.size(), ret);
168         buf.resize(ret);
169
170         sampled_blocks.clear();
171         lengths.clear();
172
173         return buf;
174 }
175
176 class EncodingCorpus : public DatabaseReceiver {
177 public:
178         EncodingCorpus(FILE *outfp, size_t block_size, ZSTD_CDict *cdict, bool store_dir_times);
179         ~EncodingCorpus();
180
181         void add_file(std::string filename, dir_time dt) override;
182         void flush_block() override;
183         void finish() override;
184
185         std::vector<uint64_t> filename_blocks;
186         size_t num_files = 0, num_files_in_block = 0, num_blocks = 0;
187         bool seen_trigram(uint32_t trgm)
188         {
189                 return invindex[trgm] != nullptr;
190         }
191         size_t num_files_seen() const override { return num_files; }
192         PostingListBuilder &get_pl_builder(uint32_t trgm)
193         {
194                 return *invindex[trgm];
195         }
196
197         void add_docid(uint32_t trgm, uint32_t docid)
198         {
199                 if (invindex[trgm] == nullptr) {
200                         invindex[trgm] = new PostingListBuilder;
201                         invindex[trgm]->add_first_docid(docid);
202                 } else {
203                         invindex[trgm]->add_docid(docid);
204                 }
205         }
206
207         size_t num_trigrams() const;
208         std::string get_compressed_dir_times();
209
210 private:
211         void compress_dir_times(size_t allowed_slop);
212
213         std::unique_ptr<PostingListBuilder *[]> invindex;
214         FILE *outfp;
215         std::string current_block;
216         std::string tempbuf;
217         const size_t block_size;
218         const bool store_dir_times;
219         ZSTD_CDict *cdict;
220
221         ZSTD_CStream *dir_time_ctx = nullptr;
222         std::string dir_times;  // Buffer of still-uncompressed data.
223         std::string dir_times_compressed;
224 };
225
226
227 EncodingCorpus::EncodingCorpus(FILE *outfp, size_t block_size, ZSTD_CDict *cdict, bool store_dir_times)
228         : invindex(new PostingListBuilder *[NUM_TRIGRAMS]), outfp(outfp), block_size(block_size), store_dir_times(store_dir_times), cdict(cdict)
229 {
230         fill(invindex.get(), invindex.get() + NUM_TRIGRAMS, nullptr);
231         if (store_dir_times) {
232                 dir_time_ctx = ZSTD_createCStream();
233                 ZSTD_initCStream(dir_time_ctx, /*level=*/6);
234         }
235 }
236
237 EncodingCorpus::~EncodingCorpus()
238 {
239         for (unsigned i = 0; i < NUM_TRIGRAMS; ++i) {
240                 delete invindex[i];
241         }
242 }
243
244 void EncodingCorpus::add_file(string filename, dir_time dt)
245 {
246         ++num_files;
247         if (!current_block.empty()) {
248                 current_block.push_back('\0');
249         }
250         current_block += filename;
251         if (++num_files_in_block == block_size) {
252                 flush_block();
253         }
254
255         if (store_dir_times) {
256                 if (dt.sec == -1) {
257                         // Not a directory.
258                         dir_times.push_back('\0');
259                 } else {
260                         dir_times.push_back('\1');
261                         dir_times.append(reinterpret_cast<char *>(&dt.sec), sizeof(dt.sec));
262                         dir_times.append(reinterpret_cast<char *>(&dt.nsec), sizeof(dt.nsec));
263                 }
264                 compress_dir_times(/*allowed_slop=*/4096);
265         }
266 }
267
268 void EncodingCorpus::compress_dir_times(size_t allowed_slop)
269 {
270         while (dir_times.size() >= allowed_slop) {
271                 size_t old_size = dir_times_compressed.size();
272                 dir_times_compressed.resize(old_size + 4096);
273
274                 ZSTD_outBuffer outbuf;
275                 outbuf.dst = dir_times_compressed.data() + old_size;
276                 outbuf.size = 4096;
277                 outbuf.pos = 0;
278
279                 ZSTD_inBuffer inbuf;
280                 inbuf.src = dir_times.data();
281                 inbuf.size = dir_times.size();
282                 inbuf.pos = 0;
283
284                 int ret = ZSTD_compressStream(dir_time_ctx, &outbuf, &inbuf);
285                 if (ret < 0) {
286                         fprintf(stderr, "ZSTD_compressStream() failed\n");
287                         exit(1);
288                 }
289
290                 dir_times_compressed.resize(old_size + outbuf.pos);
291                 dir_times.erase(dir_times.begin(), dir_times.begin() + inbuf.pos);
292
293                 if (outbuf.pos == 0 && inbuf.pos == 0) {
294                         // Nothing happened (not enough data?), try again later.
295                         return;
296                 }
297         }
298 }
299
300 void EncodingCorpus::flush_block()
301 {
302         if (current_block.empty()) {
303                 return;
304         }
305
306         uint32_t docid = num_blocks;
307
308         // Create trigrams.
309         const char *ptr = current_block.c_str();
310         while (ptr < current_block.c_str() + current_block.size()) {
311                 string_view s(ptr);
312                 if (s.size() >= 3) {
313                         for (size_t j = 0; j < s.size() - 2; ++j) {
314                                 uint32_t trgm = read_trigram(s, j);
315                                 add_docid(trgm, docid);
316                         }
317                 }
318                 ptr += s.size() + 1;
319         }
320
321         // Compress and add the filename block.
322         filename_blocks.push_back(ftell(outfp));
323         string compressed = zstd_compress(current_block, cdict, &tempbuf);
324         if (fwrite(compressed.data(), compressed.size(), 1, outfp) != 1) {
325                 perror("fwrite()");
326                 exit(1);
327         }
328
329         current_block.clear();
330         num_files_in_block = 0;
331         ++num_blocks;
332 }
333
334 void EncodingCorpus::finish()
335 {
336         flush_block();
337 }
338
339 size_t EncodingCorpus::num_trigrams() const
340 {
341         size_t num = 0;
342         for (unsigned trgm = 0; trgm < NUM_TRIGRAMS; ++trgm) {
343                 if (invindex[trgm] != nullptr) {
344                         ++num;
345                 }
346         }
347         return num;
348 }
349
350 string EncodingCorpus::get_compressed_dir_times()
351 {
352         if (!store_dir_times) {
353                 return "";
354         }
355         compress_dir_times(/*allowed_slop=*/0);
356         assert(dir_times.empty());
357
358         for (;;) {
359                 size_t old_size = dir_times_compressed.size();
360                 dir_times_compressed.resize(old_size + 4096);
361
362                 ZSTD_outBuffer outbuf;
363                 outbuf.dst = dir_times_compressed.data() + old_size;
364                 outbuf.size = 4096;
365                 outbuf.pos = 0;
366
367                 int ret = ZSTD_endStream(dir_time_ctx, &outbuf);
368                 if (ret < 0) {
369                         fprintf(stderr, "ZSTD_compressStream() failed\n");
370                         exit(1);
371                 }
372
373                 dir_times_compressed.resize(old_size + outbuf.pos);
374
375                 if (ret == 0) {
376                         // All done.
377                         break;
378                 }
379         }
380
381         return dir_times_compressed;
382 }
383
384 string zstd_compress(const string &src, ZSTD_CDict *cdict, string *tempbuf)
385 {
386         static ZSTD_CCtx *ctx = nullptr;
387         if (ctx == nullptr) {
388                 ctx = ZSTD_createCCtx();
389         }
390
391         size_t max_size = ZSTD_compressBound(src.size());
392         if (tempbuf->size() < max_size) {
393                 tempbuf->resize(max_size);
394         }
395         size_t size;
396         if (cdict == nullptr) {
397                 size = ZSTD_compressCCtx(ctx, &(*tempbuf)[0], max_size, src.data(), src.size(), /*level=*/6);
398         } else {
399                 size = ZSTD_compress_usingCDict(ctx, &(*tempbuf)[0], max_size, src.data(), src.size(), cdict);
400         }
401         return string(tempbuf->data(), size);
402 }
403
404 bool is_prime(uint32_t x)
405 {
406         if ((x % 2) == 0 || (x % 3) == 0) {
407                 return false;
408         }
409         uint32_t limit = ceil(sqrt(x));
410         for (uint32_t factor = 5; factor <= limit; ++factor) {
411                 if ((x % factor) == 0) {
412                         return false;
413                 }
414         }
415         return true;
416 }
417
418 uint32_t next_prime(uint32_t x)
419 {
420         if ((x % 2) == 0) {
421                 ++x;
422         }
423         while (!is_prime(x)) {
424                 x += 2;
425         }
426         return x;
427 }
428
429 unique_ptr<Trigram[]> create_hashtable(EncodingCorpus &corpus, const vector<uint32_t> &all_trigrams, uint32_t ht_size, uint32_t num_overflow_slots)
430 {
431         unique_ptr<Trigram[]> ht(new Trigram[ht_size + num_overflow_slots + 1]);  // 1 for the sentinel element at the end.
432         for (unsigned i = 0; i < ht_size + num_overflow_slots + 1; ++i) {
433                 ht[i].trgm = uint32_t(-1);
434                 ht[i].num_docids = 0;
435                 ht[i].offset = 0;
436         }
437         for (uint32_t trgm : all_trigrams) {
438                 // We don't know offset yet, so set it to zero.
439                 Trigram to_insert{ trgm, uint32_t(corpus.get_pl_builder(trgm).num_docids), 0 };
440
441                 uint32_t bucket = hash_trigram(trgm, ht_size);
442                 unsigned distance = 0;
443                 while (ht[bucket].num_docids != 0) {
444                         // Robin Hood hashing; reduces the longest distance by a lot.
445                         unsigned other_distance = bucket - hash_trigram(ht[bucket].trgm, ht_size);
446                         if (distance > other_distance) {
447                                 swap(to_insert, ht[bucket]);
448                                 distance = other_distance;
449                         }
450
451                         ++bucket, ++distance;
452                         if (distance > num_overflow_slots) {
453                                 return nullptr;
454                         }
455                 }
456                 ht[bucket] = to_insert;
457         }
458         return ht;
459 }
460
461 DatabaseBuilder::DatabaseBuilder(const char *outfile, gid_t owner, int block_size, string dictionary, bool check_visibility)
462         : outfile(outfile), block_size(block_size)
463 {
464         umask(0027);
465
466         string path = outfile;
467         path.resize(path.find_last_of('/') + 1);
468         if (path.empty()) {
469                 path = ".";
470         }
471 #ifdef O_TMPFILE
472         int fd = open(path.c_str(), O_WRONLY | O_TMPFILE, 0640);
473         if (fd == -1) {
474                 perror(path.c_str());
475                 exit(1);
476         }
477 #else
478         temp_filename = string(outfile) + ".XXXXXX";
479         int fd = mkstemp(&temp_filename[0]);
480         if (fd == -1) {
481                 perror(temp_filename.c_str());
482                 exit(1);
483         }
484         if (fchmod(fd, 0640) == -1) {
485                 perror("fchmod");
486                 exit(1);
487         }
488 #endif
489
490         if (owner != (gid_t)-1) {
491                 if (fchown(fd, (uid_t)-1, owner) == -1) {
492                         perror("fchown");
493                         exit(1);
494                 }
495         }
496
497         outfp = fdopen(fd, "wb");
498         if (outfp == nullptr) {
499                 perror(outfile);
500                 exit(1);
501         }
502
503         // Write the header.
504         memcpy(hdr.magic, "\0plocate", 8);
505         hdr.version = -1;  // Mark as broken.
506         hdr.hashtable_size = 0;  // Not known yet.
507         hdr.extra_ht_slots = num_overflow_slots;
508         hdr.num_docids = 0;
509         hdr.hash_table_offset_bytes = -1;  // We don't know these offsets yet.
510         hdr.max_version = 2;
511         hdr.filename_index_offset_bytes = -1;
512         hdr.zstd_dictionary_length_bytes = -1;
513         hdr.check_visibility = check_visibility;
514         fwrite(&hdr, sizeof(hdr), 1, outfp);
515
516         if (dictionary.empty()) {
517                 hdr.zstd_dictionary_offset_bytes = 0;
518                 hdr.zstd_dictionary_length_bytes = 0;
519         } else {
520                 hdr.zstd_dictionary_offset_bytes = ftell(outfp);
521                 fwrite(dictionary.data(), dictionary.size(), 1, outfp);
522                 hdr.zstd_dictionary_length_bytes = dictionary.size();
523                 cdict = ZSTD_createCDict(dictionary.data(), dictionary.size(), /*level=*/6);
524         }
525
526         hdr.directory_data_length_bytes = 0;
527         hdr.directory_data_offset_bytes = 0;
528         hdr.next_zstd_dictionary_length_bytes = 0;
529         hdr.next_zstd_dictionary_offset_bytes = 0;
530         hdr.conf_block_length_bytes = 0;
531         hdr.conf_block_offset_bytes = 0;
532 }
533
534 DatabaseReceiver *DatabaseBuilder::start_corpus(bool store_dir_times)
535 {
536         corpus_start = steady_clock::now();
537         corpus = new EncodingCorpus(outfp, block_size, cdict, store_dir_times);
538         return corpus;
539 }
540
541 void DatabaseBuilder::set_next_dictionary(std::string next_dictionary)
542 {
543         this->next_dictionary = move(next_dictionary);
544 }
545
546 void DatabaseBuilder::set_conf_block(std::string conf_block)
547 {
548         this->conf_block = move(conf_block);
549 }
550
551 void DatabaseBuilder::finish_corpus()
552 {
553         corpus->finish();
554         hdr.num_docids = corpus->filename_blocks.size();
555
556         // Stick an empty block at the end as sentinel.
557         corpus->filename_blocks.push_back(ftell(outfp));
558         const size_t bytes_for_filenames = corpus->filename_blocks.back() - corpus->filename_blocks.front();
559
560         // Write the offsets to the filenames.
561         hdr.filename_index_offset_bytes = ftell(outfp);
562         const size_t bytes_for_filename_index = corpus->filename_blocks.size() * sizeof(uint64_t);
563         fwrite(corpus->filename_blocks.data(), corpus->filename_blocks.size(), sizeof(uint64_t), outfp);
564         corpus->filename_blocks.clear();
565         corpus->filename_blocks.shrink_to_fit();
566
567         // Finish up encoding the posting lists.
568         size_t trigrams = 0, longest_posting_list = 0;
569         size_t bytes_for_posting_lists = 0;
570         for (unsigned trgm = 0; trgm < NUM_TRIGRAMS; ++trgm) {
571                 if (!corpus->seen_trigram(trgm))
572                         continue;
573                 PostingListBuilder &pl_builder = corpus->get_pl_builder(trgm);
574                 pl_builder.finish();
575                 longest_posting_list = max(longest_posting_list, pl_builder.num_docids);
576                 trigrams += pl_builder.num_docids;
577                 bytes_for_posting_lists += pl_builder.encoded.size();
578         }
579         size_t num_trigrams = corpus->num_trigrams();
580         dprintf("%zu files, %zu different trigrams, %zu entries, avg len %.2f, longest %zu\n",
581                 corpus->num_files, num_trigrams, trigrams, double(trigrams) / num_trigrams, longest_posting_list);
582         dprintf("%zu bytes used for posting lists (%.2f bits/entry)\n", bytes_for_posting_lists, 8 * bytes_for_posting_lists / double(trigrams));
583
584         dprintf("Building posting lists took %.1f ms.\n\n", 1e3 * duration<float>(steady_clock::now() - corpus_start).count());
585
586         // Find the used trigrams.
587         vector<uint32_t> all_trigrams;
588         for (unsigned trgm = 0; trgm < NUM_TRIGRAMS; ++trgm) {
589                 if (corpus->seen_trigram(trgm)) {
590                         all_trigrams.push_back(trgm);
591                 }
592         }
593
594         // Create the hash table.
595         unique_ptr<Trigram[]> hashtable;
596         uint32_t ht_size = next_prime(all_trigrams.size());
597         for (;;) {
598                 hashtable = create_hashtable(*corpus, all_trigrams, ht_size, num_overflow_slots);
599                 if (hashtable == nullptr) {
600                         dprintf("Failed creating hash table of size %u, increasing by 5%% and trying again.\n", ht_size);
601                         ht_size = next_prime(ht_size * 1.05);
602                 } else {
603                         dprintf("Created hash table of size %u.\n\n", ht_size);
604                         break;
605                 }
606         }
607
608         // Find the offsets for each posting list.
609         size_t bytes_for_hashtable = (ht_size + num_overflow_slots + 1) * sizeof(Trigram);
610         uint64_t offset = ftell(outfp) + bytes_for_hashtable;
611         for (unsigned i = 0; i < ht_size + num_overflow_slots + 1; ++i) {
612                 hashtable[i].offset = offset;  // Needs to be there even for empty slots.
613                 if (hashtable[i].num_docids == 0) {
614                         continue;
615                 }
616
617                 const vector<unsigned char> &encoded = corpus->get_pl_builder(hashtable[i].trgm).encoded;
618                 offset += encoded.size();
619         }
620
621         // Write the hash table.
622         hdr.hash_table_offset_bytes = ftell(outfp);
623         hdr.hashtable_size = ht_size;
624         fwrite(hashtable.get(), ht_size + num_overflow_slots + 1, sizeof(Trigram), outfp);
625
626         // Write the actual posting lists.
627         for (unsigned i = 0; i < ht_size + num_overflow_slots + 1; ++i) {
628                 if (hashtable[i].num_docids == 0) {
629                         continue;
630                 }
631                 const vector<unsigned char> &encoded = corpus->get_pl_builder(hashtable[i].trgm).encoded;
632                 fwrite(encoded.data(), encoded.size(), 1, outfp);
633         }
634
635         // Finally, write the directory times (for updatedb).
636         string compressed_dir_times = corpus->get_compressed_dir_times();
637         size_t bytes_for_compressed_dir_times = 0;
638         if (!compressed_dir_times.empty()) {
639                 hdr.directory_data_offset_bytes = ftell(outfp);
640                 hdr.directory_data_length_bytes = compressed_dir_times.size();
641                 fwrite(compressed_dir_times.data(), compressed_dir_times.size(), 1, outfp);
642                 bytes_for_compressed_dir_times = compressed_dir_times.size();
643                 compressed_dir_times.clear();
644         }
645
646         // Write the recommended dictionary for next update.
647         if (!next_dictionary.empty()) {
648                 hdr.next_zstd_dictionary_offset_bytes = ftell(outfp);
649                 hdr.next_zstd_dictionary_length_bytes = next_dictionary.size();
650                 fwrite(next_dictionary.data(), next_dictionary.size(), 1, outfp);
651         }
652
653         // And the configuration block.
654         if (!conf_block.empty()) {
655                 hdr.conf_block_offset_bytes = ftell(outfp);
656                 hdr.conf_block_length_bytes = conf_block.size();
657                 fwrite(conf_block.data(), conf_block.size(), 1, outfp);
658         }
659
660         // Rewind, and write the updated header.
661         hdr.version = 1;
662         fseek(outfp, 0, SEEK_SET);
663         fwrite(&hdr, sizeof(hdr), 1, outfp);
664
665 #ifdef O_TMPFILE
666         // Give the file a proper name, making it visible in the file system.
667         // TODO: It would be nice to be able to do this atomically, like with rename.
668         unlink(outfile.c_str());
669         char procpath[256];
670         snprintf(procpath, sizeof(procpath), "/proc/self/fd/%d", fileno(outfp));
671         if (linkat(AT_FDCWD, procpath, AT_FDCWD, outfile.c_str(), AT_SYMLINK_FOLLOW) == -1) {
672                 perror("linkat");
673                 exit(1);
674         }
675 #else
676         if (rename(temp_filename.c_str(), outfile.c_str()) == -1) {
677                 perror("rename");
678                 exit(1);
679         }
680 #endif
681
682         fclose(outfp);
683
684         size_t total_bytes = (bytes_for_hashtable + bytes_for_posting_lists + bytes_for_filename_index + bytes_for_filenames + bytes_for_compressed_dir_times);
685
686         dprintf("Block size:     %7d files\n", block_size);
687         dprintf("Dictionary:     %'7.1f MB\n", hdr.zstd_dictionary_length_bytes / 1048576.0);
688         dprintf("Hash table:     %'7.1f MB\n", bytes_for_hashtable / 1048576.0);
689         dprintf("Posting lists:  %'7.1f MB\n", bytes_for_posting_lists / 1048576.0);
690         dprintf("Filename index: %'7.1f MB\n", bytes_for_filename_index / 1048576.0);
691         dprintf("Filenames:      %'7.1f MB\n", bytes_for_filenames / 1048576.0);
692         if (bytes_for_compressed_dir_times != 0) {
693                 dprintf("Modify times:   %'7.1f MB\n", bytes_for_compressed_dir_times / 1048576.0);
694         }
695         dprintf("Total:          %'7.1f MB\n", total_bytes / 1048576.0);
696         dprintf("\n");
697 }