replace CHECK with UTIL_THROW_IFin OnDiskPt

This commit is contained in:
Hieu Hoang 2013-11-18 17:08:58 +00:00
parent 1accc75d14
commit c8f19220e9
10 changed files with 36 additions and 41 deletions

View File

@ -62,8 +62,7 @@ int main (int argc, char * const argv[])
Moses::InputFileStream inStream(filePath);
OnDiskWrapper onDiskWrapper;
bool retDb = onDiskWrapper.BeginSave(destPath, numSourceFactors, numTargetFactors, numScores);
assert(retDb);
onDiskWrapper.BeginSave(destPath, numSourceFactors, numTargetFactors, numScores);
PhraseNode &rootNode = onDiskWrapper.GetRootSourceNode();
size_t lineNum = 0;

View File

@ -43,18 +43,17 @@ OnDiskWrapper::~OnDiskWrapper()
delete m_rootSourceNode;
}
bool OnDiskWrapper::BeginLoad(const std::string &filePath)
void OnDiskWrapper::BeginLoad(const std::string &filePath)
{
if (!OpenForLoad(filePath))
return false;
if (!OpenForLoad(filePath)) {
UTIL_THROW(util::FileOpenException, "Couldn't open for loading: " << filePath);
}
if (!m_vocab.Load(*this))
return false;
UTIL_THROW(util::FileOpenException, "Couldn't load vocab");
UINT64 rootFilePos = GetMisc("RootNodeOffset");
m_rootSourceNode = new PhraseNode(rootFilePos, *this);
return true;
}
bool OnDiskWrapper::OpenForLoad(const std::string &filePath)
@ -110,7 +109,7 @@ bool OnDiskWrapper::LoadMisc()
return true;
}
bool OnDiskWrapper::BeginSave(const std::string &filePath
void OnDiskWrapper::BeginSave(const std::string &filePath
, int numSourceFactors, int numTargetFactors, int numScores)
{
m_numSourceFactors = numSourceFactors;
@ -175,8 +174,6 @@ bool OnDiskWrapper::BeginSave(const std::string &filePath
counts[0] = DEFAULT_COUNT;
m_rootSourceNode = new PhraseNode();
m_rootSourceNode->AddCounts(counts);
return true;
}
void OnDiskWrapper::EndSave()

View File

@ -55,9 +55,9 @@ public:
OnDiskWrapper();
~OnDiskWrapper();
bool BeginLoad(const std::string &filePath);
void BeginLoad(const std::string &filePath);
bool BeginSave(const std::string &filePath
void BeginSave(const std::string &filePath
, int numSourceFactors, int numTargetFactors, int numScores);
void EndSave();

View File

@ -56,7 +56,7 @@ PhraseNode::PhraseNode(UINT64 filePos, OnDiskWrapper &onDiskWrapper)
std::fstream &file = onDiskWrapper.GetFileSource();
file.seekg(filePos);
CHECK(filePos == (UINT64)file.tellg());
assert(filePos == (UINT64)file.tellg());
file.read((char*) &m_numChildrenLoad, sizeof(UINT64));
@ -65,11 +65,11 @@ PhraseNode::PhraseNode(UINT64 filePos, OnDiskWrapper &onDiskWrapper)
// go to start of node again
file.seekg(filePos);
CHECK(filePos == (UINT64)file.tellg());
assert(filePos == (UINT64)file.tellg());
// read everything into memory
file.read(m_memLoad, memAlloc);
CHECK(filePos + memAlloc == (UINT64)file.tellg());
assert(filePos + memAlloc == (UINT64)file.tellg());
// get value
m_value = ((UINT64*)m_memLoad)[1];
@ -77,7 +77,7 @@ PhraseNode::PhraseNode(UINT64 filePos, OnDiskWrapper &onDiskWrapper)
// get counts
float *memFloat = (float*) (m_memLoad + sizeof(UINT64) * 2);
CHECK(countSize == 1);
assert(countSize == 1);
m_counts[0] = memFloat[0];
m_memLoadLast = m_memLoad + memAlloc;
@ -86,7 +86,6 @@ PhraseNode::PhraseNode(UINT64 filePos, OnDiskWrapper &onDiskWrapper)
PhraseNode::~PhraseNode()
{
free(m_memLoad);
//CHECK(m_saved);
}
float PhraseNode::GetCount(size_t ind) const
@ -96,7 +95,7 @@ float PhraseNode::GetCount(size_t ind) const
void PhraseNode::Save(OnDiskWrapper &onDiskWrapper, size_t pos, size_t tableLimit)
{
CHECK(!m_saved);
UTIL_THROW_IF(m_saved, util::Exception, "Already saved");
// save this node
m_targetPhraseColl.Sort(tableLimit);
@ -117,7 +116,7 @@ void PhraseNode::Save(OnDiskWrapper &onDiskWrapper, size_t pos, size_t tableLimi
// count info
float *memFloat = (float*) (mem + memUsed);
CHECK(numCounts == 1);
UTIL_THROW_IF(numCounts != 1, util::Exception, "Can only store 1 phrase count");
memFloat[0] = (m_counts.size() == 0) ? DEFAULT_COUNT : m_counts[0]; // if count = 0, put in very large num to make sure its still used. HACK
memUsed += sizeof(float) * numCounts;
@ -143,7 +142,7 @@ void PhraseNode::Save(OnDiskWrapper &onDiskWrapper, size_t pos, size_t tableLimi
// save this node
//Moses::DebugMem(mem, memAlloc);
CHECK(memUsed == memAlloc);
assert(memUsed == memAlloc);
std::fstream &file = onDiskWrapper.GetFileSource();
m_filePos = file.tellp();
@ -151,7 +150,7 @@ void PhraseNode::Save(OnDiskWrapper &onDiskWrapper, size_t pos, size_t tableLimi
file.write(mem, memUsed);
UINT64 endPos = file.tellp();
CHECK(m_filePos + memUsed == endPos);
assert(m_filePos + memUsed == endPos);
free(mem);
@ -236,7 +235,7 @@ void PhraseNode::GetChild(Word &wordFound, UINT64 &childFilePos, size_t ind, OnD
+ childSize * ind;
size_t memRead = ReadChild(wordFound, childFilePos, currMem);
CHECK(memRead == childSize);
assert(memRead == childSize);
}
size_t PhraseNode::ReadChild(Word &wordFound, UINT64 &childFilePos, const char *mem) const

View File

@ -25,6 +25,7 @@
#include "moses/TranslationModel/PhraseDictionary.h"
#include "TargetPhrase.h"
#include "OnDiskWrapper.h"
#include "util/exception.hh"
#include <boost/algorithm/string.hpp>
@ -58,7 +59,7 @@ void TargetPhrase::Create1AlignFromString(const std::string &align1Str)
{
vector<size_t> alignPoints;
Moses::Tokenize<size_t>(alignPoints, align1Str, "-");
CHECK(alignPoints.size() == 2);
UTIL_THROW_IF(alignPoints.size() != 2, util::Exception, "Incorrectly formatted word alignment: " << align1Str);
m_align.push_back(pair<size_t, size_t>(alignPoints[0], alignPoints[1]) );
}
@ -76,7 +77,7 @@ void TargetPhrase::CreateAlignFromString(const std::string &alignStr)
void TargetPhrase::SetScore(float score, size_t ind)
{
CHECK(ind < m_scores.size());
assert(ind < m_scores.size());
m_scores[ind] = score;
}
@ -132,7 +133,7 @@ char *TargetPhrase::WriteToMemory(OnDiskWrapper &onDiskWrapper, size_t &memUsed)
memUsed += word.WriteToMemory((char*) currPtr);
}
CHECK(memUsed == memNeeded);
assert(memUsed == memNeeded);
return (char *) mem;
}
@ -150,7 +151,7 @@ void TargetPhrase::Save(OnDiskWrapper &onDiskWrapper)
file.write(mem, memUsed);
UINT64 endPos = file.tellp();
CHECK(startPos + memUsed == endPos);
assert(startPos + memUsed == endPos);
m_filePos = startPos;
free(mem);
@ -183,7 +184,7 @@ char *TargetPhrase::WriteOtherInfoToMemory(OnDiskWrapper &onDiskWrapper, size_t
memUsed += WriteScoresToMemory(mem + memUsed);
//DebugMem(mem, memNeeded);
CHECK(memNeeded == memUsed);
assert(memNeeded == memUsed);
return mem;
}
@ -234,7 +235,7 @@ Moses::TargetPhrase *TargetPhrase::ConvertToMoses(const std::vector<Moses::Facto
// words
size_t phraseSize = GetSize();
CHECK(phraseSize > 0); // last word is lhs
UTIL_THROW_IF(phraseSize == 0, util::Exception, "Target phrase cannot be empty"); // last word is lhs
if (isSyntax) {
--phraseSize;
}
@ -285,18 +286,18 @@ Moses::TargetPhrase *TargetPhrase::ConvertToMoses(const std::vector<Moses::Facto
UINT64 TargetPhrase::ReadOtherInfoFromFile(UINT64 filePos, std::fstream &fileTPColl)
{
CHECK(filePos == (UINT64)fileTPColl.tellg());
assert(filePos == (UINT64)fileTPColl.tellg());
UINT64 memUsed = 0;
fileTPColl.read((char*) &m_filePos, sizeof(UINT64));
memUsed += sizeof(UINT64);
CHECK(m_filePos != 0);
assert(m_filePos != 0);
memUsed += ReadAlignFromFile(fileTPColl);
CHECK((memUsed + filePos) == (UINT64)fileTPColl.tellg());
assert((memUsed + filePos) == (UINT64)fileTPColl.tellg());
memUsed += ReadScoresFromFile(fileTPColl);
CHECK((memUsed + filePos) == (UINT64)fileTPColl.tellg());
assert((memUsed + filePos) == (UINT64)fileTPColl.tellg());
return memUsed;
}
@ -355,7 +356,7 @@ UINT64 TargetPhrase::ReadAlignFromFile(std::fstream &fileTPColl)
UINT64 TargetPhrase::ReadScoresFromFile(std::fstream &fileTPColl)
{
CHECK(m_scores.size() > 0);
UTIL_THROW_IF(m_scores.size() == 0, util::Exception, "Translation rules must must have some scores");
UINT64 bytesRead = 0;

View File

@ -107,7 +107,7 @@ void TargetPhraseCollection::Save(OnDiskWrapper &onDiskWrapper)
free(mem);
UINT64 endPos = file.tellp();
CHECK(startPos + memUsed == endPos);
assert(startPos + memUsed == endPos);
m_filePos = startPos;

View File

@ -21,6 +21,7 @@
#include <fstream>
#include "OnDiskWrapper.h"
#include "Vocab.h"
#include "util/exception.hh"
using namespace std;
@ -35,7 +36,7 @@ bool Vocab::Load(OnDiskWrapper &onDiskWrapper)
while(getline(file, line)) {
vector<string> tokens;
Moses::Tokenize(tokens, line);
CHECK(tokens.size() == 2);
UTIL_THROW_IF(tokens.size() != 2, util::Exception, "Vocab file corrupted");
const string &key = tokens[0];
m_vocabColl[key] = Moses::Scan<UINT64>(tokens[1]);
}

View File

@ -91,7 +91,7 @@ size_t Word::ReadFromFile(std::fstream &file)
file.read(mem, memAlloc);
size_t memUsed = ReadFromMemory(mem);
CHECK(memAlloc == memUsed);
assert(memAlloc == memUsed);
return memAlloc;
}

View File

@ -41,8 +41,7 @@ int main(int argc, char **argv)
usage();
OnDiskWrapper onDiskWrapper;
bool retDb = onDiskWrapper.BeginLoad(ttable);
CHECK(retDb);
onDiskWrapper.BeginLoad(ttable);
OnDiskQuery onDiskQuery(onDiskWrapper);
cerr << "Ready..." << endl;

View File

@ -82,8 +82,7 @@ void PhraseDictionaryOnDisk::InitializeForInput(InputType const& source)
ReduceCache();
OnDiskPt::OnDiskWrapper *obj = new OnDiskPt::OnDiskWrapper();
if (!obj->BeginLoad(m_filePath))
return;
obj->BeginLoad(m_filePath);
CHECK(obj->GetMisc("Version") == OnDiskPt::OnDiskWrapper::VERSION_NUM);
CHECK(obj->GetMisc("NumSourceFactors") == m_input.size());