Browse Source

leveldb repair for torrents

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

36
libtorrent/src/storage.cpp

@ -540,10 +540,17 @@ 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);
if( Write(std::make_pair('p', slot), postStr) ) { int tries = 2;
return postStr.size(); while( tries-- ) {
} else { try {
return -1; 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(num_bufs == 1);
TORRENT_ASSERT(offset == 0); TORRENT_ASSERT(offset == 0);
std::string postStr; int tries = 2;
if( Read(std::make_pair('p', slot), postStr) ) { while( tries-- ) {
TORRENT_ASSERT(bufs[0].iov_len >= postStr.size()); try {
memcpy(bufs[0].iov_base, postStr.data(), postStr.size()); std::string postStr;
return postStr.size(); if( Read(std::make_pair('p', slot), postStr) ) {
} else { TORRENT_ASSERT(bufs[0].iov_len >= postStr.size());
return 0; memcpy(bufs[0].iov_base, postStr.data(), postStr.size());
return postStr.size();
} else {
return 0;
}
} catch( leveldb_error &e ) {
RepairDB();
}
} }
} }

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