Browse Source

leveldb repair for torrents

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
878061317e
  1. 14
      libtorrent/src/storage.cpp
  2. 20
      src/leveldb.cpp
  3. 5
      src/leveldb.h

14
libtorrent/src/storage.cpp

@ -540,11 +540,18 @@ namespace libtorrent
std::string postStr(static_cast<char *>(bufs[0].iov_base), bufs[0].iov_len); std::string postStr(static_cast<char *>(bufs[0].iov_base), bufs[0].iov_len);
int tries = 2;
while( tries-- ) {
try {
if( Write(std::make_pair('p', slot), postStr) ) { if( Write(std::make_pair('p', slot), postStr) ) {
return postStr.size(); return postStr.size();
} else { } else {
return -1; return -1;
} }
} catch( leveldb_error &e ) {
RepairDB();
}
}
} }
@ -572,6 +579,9 @@ namespace libtorrent
TORRENT_ASSERT(num_bufs == 1); TORRENT_ASSERT(num_bufs == 1);
TORRENT_ASSERT(offset == 0); TORRENT_ASSERT(offset == 0);
int tries = 2;
while( tries-- ) {
try {
std::string postStr; std::string postStr;
if( Read(std::make_pair('p', slot), postStr) ) { if( Read(std::make_pair('p', slot), postStr) ) {
TORRENT_ASSERT(bufs[0].iov_len >= postStr.size()); TORRENT_ASSERT(bufs[0].iov_len >= postStr.size());
@ -580,6 +590,10 @@ namespace libtorrent
} else { } else {
return 0; return 0;
} }
} catch( leveldb_error &e ) {
RepairDB();
}
}
} }
int default_storage::write( int default_storage::write(

20
src/leveldb.cpp

@ -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) { CLevelDB::CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory, bool fWipe) {
m_path = path.string();
m_nCacheSize = nCacheSize;
penv = NULL; penv = NULL;
readoptions.verify_checksums = true; readoptions.verify_checksums = true;
iteroptions.verify_checksums = true; iteroptions.verify_checksums = true;
@ -79,3 +81,21 @@ bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) throw(leveldb_error)
} }
return true; 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");
}

5
src/leveldb.h

@ -76,6 +76,9 @@ private:
// the database itself // the database itself
leveldb::DB *pdb; leveldb::DB *pdb;
std::string m_path;
size_t m_nCacheSize;
public: public:
CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false, bool fWipe = false); CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
~CLevelDB(); ~CLevelDB();
@ -148,6 +151,8 @@ public:
leveldb::Iterator *NewIterator() { leveldb::Iterator *NewIterator() {
return pdb->NewIterator(iteroptions); return pdb->NewIterator(iteroptions);
} }
void RepairDB();
}; };
#endif // BITCOIN_LEVELDB_H #endif // BITCOIN_LEVELDB_H

Loading…
Cancel
Save