mirror of
https://github.com/twisterarmy/twister-core.git
synced 2025-01-14 00:38:09 +00:00
leveldb repair for torrents
This commit is contained in:
parent
0071cb31db
commit
878061317e
@ -540,10 +540,17 @@ namespace libtorrent
|
||||
|
||||
std::string postStr(static_cast<char *>(bufs[0].iov_base), bufs[0].iov_len);
|
||||
|
||||
if( Write(std::make_pair('p', slot), postStr) ) {
|
||||
return postStr.size();
|
||||
} else {
|
||||
return -1;
|
||||
int tries = 2;
|
||||
while( tries-- ) {
|
||||
try {
|
||||
if( Write(std::make_pair('p', slot), postStr) ) {
|
||||
return postStr.size();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
} catch( leveldb_error &e ) {
|
||||
RepairDB();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -572,13 +579,20 @@ namespace libtorrent
|
||||
TORRENT_ASSERT(num_bufs == 1);
|
||||
TORRENT_ASSERT(offset == 0);
|
||||
|
||||
std::string postStr;
|
||||
if( Read(std::make_pair('p', slot), postStr) ) {
|
||||
TORRENT_ASSERT(bufs[0].iov_len >= postStr.size());
|
||||
memcpy(bufs[0].iov_base, postStr.data(), postStr.size());
|
||||
return postStr.size();
|
||||
} else {
|
||||
return 0;
|
||||
int tries = 2;
|
||||
while( tries-- ) {
|
||||
try {
|
||||
std::string postStr;
|
||||
if( Read(std::make_pair('p', slot), postStr) ) {
|
||||
TORRENT_ASSERT(bufs[0].iov_len >= postStr.size());
|
||||
memcpy(bufs[0].iov_base, postStr.data(), postStr.size());
|
||||
return postStr.size();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
} catch( leveldb_error &e ) {
|
||||
RepairDB();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,8 @@ static leveldb::Options GetOptions(size_t nCacheSize) {
|
||||
}
|
||||
|
||||
CLevelDB::CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory, bool fWipe) {
|
||||
m_path = path.string();
|
||||
m_nCacheSize = nCacheSize;
|
||||
penv = NULL;
|
||||
readoptions.verify_checksums = true;
|
||||
iteroptions.verify_checksums = true;
|
||||
@ -79,3 +81,21 @@ bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) throw(leveldb_error)
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void CLevelDB::RepairDB()
|
||||
{
|
||||
printf("CLevelDB::RepairDB trying to repair...\n");
|
||||
delete pdb;
|
||||
pdb = NULL;
|
||||
|
||||
leveldb::Status status;
|
||||
|
||||
status = leveldb::RepairDB(m_path, options);
|
||||
if (!status.ok())
|
||||
throw std::runtime_error(strprintf("CLevelDB(): error RepairDB %s", status.ToString().c_str()));
|
||||
|
||||
status = leveldb::DB::Open(options, m_path, &pdb);
|
||||
if (!status.ok())
|
||||
throw std::runtime_error(strprintf("CLevelDB(): error opening database environment %s", status.ToString().c_str()));
|
||||
printf("Repaired and reopened LevelDB successfully\n");
|
||||
}
|
||||
|
@ -76,6 +76,9 @@ private:
|
||||
// the database itself
|
||||
leveldb::DB *pdb;
|
||||
|
||||
std::string m_path;
|
||||
size_t m_nCacheSize;
|
||||
|
||||
public:
|
||||
CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
|
||||
~CLevelDB();
|
||||
@ -148,6 +151,8 @@ public:
|
||||
leveldb::Iterator *NewIterator() {
|
||||
return pdb->NewIterator(iteroptions);
|
||||
}
|
||||
|
||||
void RepairDB();
|
||||
};
|
||||
|
||||
#endif // BITCOIN_LEVELDB_H
|
||||
|
Loading…
Reference in New Issue
Block a user