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 @@ -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 @@ -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();
}
}
}

20
src/leveldb.cpp

@ -35,6 +35,8 @@ static leveldb::Options GetOptions(size_t nCacheSize) { @@ -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) @@ -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");
}

5
src/leveldb.h

@ -76,6 +76,9 @@ private: @@ -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: @@ -148,6 +151,8 @@ public:
leveldb::Iterator *NewIterator() {
return pdb->NewIterator(iteroptions);
}
void RepairDB();
};
#endif // BITCOIN_LEVELDB_H

Loading…
Cancel
Save