Skip to content

Commit 1b68b14

Browse files
authored
Merge pull request #137 from akyrtzi/keep-original-database-size
[Database] When opening an existing database make sure to preserve its original size
2 parents d5a7402 + 28c9c7a commit 1b68b14

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/Database/Database.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,16 @@ Database::Implementation::create(StringRef path, bool readonly, Optional<size_t>
173173
db->DBEnv = lmdb::env::create();
174174
db->DBEnv.set_max_dbs(14);
175175

176-
// Start with 64MB. We'll update with the actual size after we open the database.
177-
db->MapSize = initialDBSize.getValueOr(64ULL*1024ULL*1024ULL);
176+
uint64_t dbFileSize = 0;
177+
if (existingDB) {
178+
if (std::error_code ec = llvm::sys::fs::file_size(dbPath + "/data.mdb", dbFileSize)) {
179+
LOG_WARN_FUNC("failed reading database file size " << dbPath << "/data.mdb: " << ec.message());
180+
}
181+
}
182+
// Start with 64MB.
183+
uint64_t initialSize = initialDBSize.getValueOr(64ULL*1024ULL*1024ULL);
184+
185+
db->MapSize = std::max(dbFileSize, initialSize);
178186
db->DBEnv.set_mapsize(db->MapSize);
179187

180188
unsigned openflags = MDB_NOMEMINIT|MDB_WRITEMAP|MDB_NOSYNC;
@@ -183,11 +191,6 @@ Database::Implementation::create(StringRef path, bool readonly, Optional<size_t>
183191
db->DBEnv.open(dbPath, openflags);
184192
db->MaxKeySize = lmdb::env_get_max_keysize(db->DBEnv);
185193

186-
// Get actual map size of the database.
187-
MDB_envinfo envInfo;
188-
lmdb::env_info(db->DBEnv, &envInfo);
189-
db->MapSize = envInfo.me_mapsize;
190-
191194
unsigned txnflags = lmdb::txn::default_flags;
192195
if (readonly)
193196
txnflags |= MDB_RDONLY;

0 commit comments

Comments
 (0)