1414
1515void save_from_memory_to_file (const path &fn, sqlite3 *db);
1616
17+ #include < primitives/context.h>
1718#include < primitives/date_time.h>
1819#include < primitives/debug.h>
1920#include < primitives/lock.h>
@@ -164,32 +165,27 @@ void FileDb::save(FileStorage &fs, ConcurrentHashMap<path, FileRecord> &files) c
164165 r = sqlite3_open (" :memory:" , &db);
165166 CHECK_RC (r, SQLITE_OK);
166167
167- // r = sqlite3_db_config(db, SQLITE_CONFIG_SINGLETHREAD, 1);
168- // CHECK_RC(r, SQLITE_OK);
169-
170168 r = sqlite3_exec (db, R"xxx(
171169CREATE TABLE "file" (
172- "file_id " INTEGER NOT NULL ,
170+ "hash " INTEGER,
173171 "path" TEXT,
174172 "last_write_time" INTEGER,
175173 "size" INTEGER,
176- "hash" INTEGER,
177- "flags" INTEGER,
178- PRIMARY KEY ("file_id")
174+ "flags" INTEGER
179175);
180176
181177CREATE TABLE "file_dependency" (
182- "file_id" INTEGER NOT NULL,
183- "dependency_file_id" INTEGER NOT NULL,
184- PRIMARY KEY ("file_id", "dependency_file_id"),
185- FOREIGN KEY ("file_id") REFERENCES "file" ("file_id") ON DELETE CASCADE ON UPDATE CASCADE
186- --, FOREIGN KEY ("dependency_file_id") REFERENCES "file" ("file_id") ON DELETE CASCADE ON UPDATE CASCADE
178+ "hash" INTEGER,
179+ "dependency_hash" INTEGER
187180);
188181)xxx" , 0 , 0 , 0 );
189182 CHECK_RC (r, SQLITE_OK);
190183
184+ r = sqlite3_exec (db, " BEGIN" , 0 , 0 , 0 );
185+ CHECK_RC (r, SQLITE_OK);
186+
191187 sqlite3_stmt *sf;
192- r = sqlite3_prepare_v2 (db, " INSERT INTO file (path, last_write_time, size, hash) VALUES (?, ?,?,?)" , -1 , &sf, 0 );
188+ r = sqlite3_prepare_v2 (db, " INSERT INTO file (path, last_write_time, hash) VALUES (?,?,?)" , -1 , &sf, 0 );
193189 CHECK_RC (r, SQLITE_OK);
194190
195191 sqlite3_stmt *sd;
@@ -202,22 +198,22 @@ CREATE TABLE "file_dependency" (
202198 if (!f.data )
203199 continue ;
204200
205- sqlite3_bind_text (sf, 1 , normalize_path (f.file ).c_str (), -1 , 0 );
201+ auto h1 = std::hash<path>()(f.file );
202+ auto s = normalize_path (f.file );
203+ sqlite3_bind_text (sf, 1 , s.c_str (), s.size () + 1 , 0 );
206204 sqlite3_bind_int64 (sf, 2 , f.data ->last_write_time .time_since_epoch ().count ());
207- sqlite3_bind_int64 (sf, 3 , f.data ->size );
208- sqlite3_bind_int64 (sf, 4 , std::hash<path>()(f. file ) );
205+ // sqlite3_bind_int64(sf, 3, f.data->size);
206+ sqlite3_bind_int64 (sf, 3 , h1 );
209207
210208 r = sqlite3_step (sf);
211209 CHECK_RC (r, SQLITE_DONE);
212210
213211 r = sqlite3_reset (sf);
214212 CHECK_RC (r, SQLITE_OK);
215213
216- auto rowid = sqlite3_last_insert_rowid (db);
217-
218214 for (auto &[f, d] : f.implicit_dependencies )
219215 {
220- sqlite3_bind_int64 (sd, 1 , rowid );
216+ sqlite3_bind_int64 (sd, 1 , h1 );
221217 sqlite3_bind_int64 (sd, 2 , std::hash<path>()(d->file ));
222218
223219 r = sqlite3_step (sd);
@@ -234,12 +230,38 @@ CREATE TABLE "file_dependency" (
234230 r = sqlite3_finalize (sd);
235231 CHECK_RC (r, SQLITE_OK);
236232
233+ r = sqlite3_exec (db, " COMMIT" , 0 , 0 , 0 );
234+ CHECK_RC (r, SQLITE_OK);
235+
237236 save_from_memory_to_file (f += " .sqlite" , db);
238237
239238 r = sqlite3_close_v2 (db);
240239 CHECK_RC (r, SQLITE_OK);
241240 }
242241 LOG_INFO (logger, " save to sqlite db time: " << t2.getTimeFloat () << " s." );
242+
243+ ScopedTime t3;
244+ {
245+ BinaryContext b (10'000'000 );
246+ for (auto i = files.getIterator (); i.isValid (); i.next ())
247+ {
248+ auto &f = *i.getValue ();
249+ if (!f.data )
250+ continue ;
251+
252+ auto h1 = std::hash<path>()(f.file );
253+ b.write (h1);
254+ b.write (normalize_path (f.file ));
255+ b.write (f.data ->last_write_time .time_since_epoch ().count ());
256+ // b.write(f.data->size);
257+ b.write (f.implicit_dependencies .size ());
258+
259+ for (auto &[f, d] : f.implicit_dependencies )
260+ b.write (std::hash<path>()(d->file ));
261+ }
262+ b.save (f += " .2" );
263+ }
264+ LOG_INFO (logger, " save to file2 time: " << t3.getTimeFloat () << " s." );
243265}
244266
245267template <class T >
@@ -266,8 +288,8 @@ void FileDb::write(std::vector<uint8_t> &v, const FileRecord &f) const
266288 write_int (v, std::hash<path>()(f.file ));
267289 write_str (v, normalize_path (f.file ));
268290 write_int (v, f.data ->last_write_time );
269- write_int (v, f.data ->size );
270- write_int (v, f.data ->flags .to_ullong ());
291+ // write_int(v, f.data->size);
292+ // write_int(v, f.data->flags.to_ullong());
271293
272294 auto n = f.implicit_dependencies .size ();
273295 write_int (v, n);
0 commit comments