@@ -30,8 +30,10 @@ int open_file(const fs::path& path, int flags) {
3030}
3131
3232void close_file (int fd) {
33- if (close (fd)) {
34- print_errno (" could not close index file" );
33+ if (fd>=0 ) {
34+ if (close (fd)) {
35+ print_errno (" could not close index file" );
36+ }
3537 }
3638}
3739
@@ -88,6 +90,43 @@ MMapHandle initialize_mmap(const fs::path& path)
8890 }
8991}
9092
93+ MMapHandle initialize_stream (std::ifstream& is, int64_t index_file_size)
94+ {
95+ off_t size = index_file_size;
96+ LOG1 << " Reading complete index from stream" ;
97+ void * ptr = nullptr ;
98+ if (posix_memalign (&ptr, 2 * 1024 * 1024 , size)) {
99+ print_errno (" posix_memalign()" );
100+ }
101+ char * data_ptr = reinterpret_cast <char *>(ptr);
102+ #if defined(MADV_HUGEPAGE)
103+ if (madvise (data_ptr, size, MADV_HUGEPAGE)) {
104+ print_errno (" madvise failed for MADV_HUGEPAGE" );
105+ }
106+ LOG1 << " Advising to use huge pages" ;
107+ #endif
108+ uint64_t remain = size;
109+ const uint64_t one_gb = 1024 *1024 *1024 ;
110+ uint64_t pos = 0 ;
111+ while (remain != 0 ) {
112+ is.read (data_ptr + pos, std::min (one_gb, remain));
113+ int64_t rb = is.gcount ();
114+ if (rb < 0 ) {
115+ print_errno (" read failed" );
116+ break ;
117+ }
118+ remain -= rb;
119+ pos += rb;
120+ LOG1 << " Read " << tlx::format_iec_units (pos)
121+ << " B / " << tlx::format_iec_units (size) << " B - "
122+ << pos * 100 / size << " %" ;
123+ }
124+ LOG1 << " Index loaded into RAM." ;
125+ return MMapHandle {
126+ -1 /* not a valid fd, won't be closed */ , reinterpret_cast <uint8_t *>(data_ptr), uint64_t (size)
127+ };
128+ }
129+
91130void destroy_mmap (MMapHandle& handle)
92131{
93132 if (!gopt_load_complete_index) {
0 commit comments