catching bad alloc

This commit is contained in:
Rafał Jaworski 2019-02-28 15:38:43 +01:00
parent 829c70d320
commit 7f2a212c6a

View File

@ -119,48 +119,52 @@ void Concordia::loadRAMIndexFromDisk() {
if (boost::filesystem::exists(_getWordMapFilePath()) if (boost::filesystem::exists(_getWordMapFilePath())
&& boost::filesystem::exists(_getHashedIndexFilePath()) && boost::filesystem::exists(_getHashedIndexFilePath())
&& boost::filesystem::exists(_getMarkersFilePath())) { && boost::filesystem::exists(_getMarkersFilePath())) {
// reading index from file try {
_T->clear(); // reading index from file
std::ifstream hashedIndexFile; _T->clear();
hashedIndexFile.open( std::ifstream hashedIndexFile;
_getHashedIndexFilePath().c_str(), std::ios::in hashedIndexFile.open(
| std::ios::ate | std::ios::binary); _getHashedIndexFilePath().c_str(), std::ios::in
saidx_t hiFileSize = hashedIndexFile.tellg(); | std::ios::ate | std::ios::binary);
if (hiFileSize > 0) { long hiFileSize = hashedIndexFile.tellg();
hashedIndexFile.seekg(0, std::ios::beg); if (hiFileSize > 0) {
hashedIndexFile.seekg(0, std::ios::beg);
while (!hashedIndexFile.eof()) { while (!hashedIndexFile.eof()) {
INDEX_CHARACTER_TYPE character = INDEX_CHARACTER_TYPE character =
Utils::readIndexCharacter(hashedIndexFile); Utils::readIndexCharacter(hashedIndexFile);
Utils::appendCharToSaucharVector(_T, character); Utils::appendCharToSaucharVector(_T, character);
}
hashedIndexFile.close();
} else {
hashedIndexFile.close();
throw ConcordiaException("Index corrupt: empty hash index file");
} }
hashedIndexFile.close();
} else {
hashedIndexFile.close();
throw ConcordiaException("Index corrupt: empty hash index file");
}
// reading markers from file // reading markers from file
_markers->clear(); _markers->clear();
std::ifstream markersFile; std::ifstream markersFile;
markersFile.open(_getMarkersFilePath().c_str(), std::ios::in markersFile.open(_getMarkersFilePath().c_str(), std::ios::in
| std::ios::ate | std::ios::binary); | std::ios::ate | std::ios::binary);
long maFileSize = markersFile.tellg(); long maFileSize = markersFile.tellg();
if (maFileSize > 0) { if (maFileSize > 0) {
markersFile.seekg(0, std::ios::beg); markersFile.seekg(0, std::ios::beg);
while (!markersFile.eof()) { while (!markersFile.eof()) {
SUFFIX_MARKER_TYPE marker = SUFFIX_MARKER_TYPE marker =
Utils::readMarker(markersFile); Utils::readMarker(markersFile);
_markers->push_back(marker); _markers->push_back(marker);
}
markersFile.close();
} else {
markersFile.close();
throw ConcordiaException("Index corrupt: empty markers file");
} }
markersFile.close(); // generating suffix array
} else { _SA = _index->generateSuffixArray(_T);
markersFile.close(); } catch (const std::bad_alloc&) {
throw ConcordiaException("Index corrupt: empty markers file"); throw ConcordiaException("Error allocating memory, probably out of memory.");
} }
// generating suffix array
_SA = _index->generateSuffixArray(_T);
} else { } else {
throw ConcordiaException("Index corrupt: missing files"); throw ConcordiaException("Index corrupt: missing files");
} }