void TargetPhraseCollection::ReadFromFile(size_t tableLimit, UINT64 filePos, OnDiskWrapper &onDiskWrapper) { fstream &fileTPColl = onDiskWrapper.GetFileTargetColl(); fstream &fileTP = onDiskWrapper.GetFileTargetInd(); size_t numScores = onDiskWrapper.GetNumScores(); UINT64 numPhrases; UINT64 currFilePos = filePos; fileTPColl.seekg(filePos); fileTPColl.read((char*) &numPhrases, sizeof(UINT64)); // table limit numPhrases = std::min(numPhrases, (UINT64) tableLimit); currFilePos += sizeof(UINT64); for (size_t ind = 0; ind < numPhrases; ++ind) { TargetPhrase *tp = new TargetPhrase(numScores); UINT64 sizeOtherInfo = tp->ReadOtherInfoFromFile(currFilePos, fileTPColl); tp->ReadFromFile(fileTP); currFilePos += sizeOtherInfo; m_coll.push_back(tp); } }
void TargetPhraseCollection::Save(OnDiskWrapper &onDiskWrapper) { std::fstream &file = onDiskWrapper.GetFileTargetColl(); size_t memUsed = sizeof(uint64_t); char *mem = (char*) malloc(memUsed); // size of coll uint64_t numPhrases = GetSize(); ((uint64_t*)mem)[0] = numPhrases; // MAIN LOOP CollType::iterator iter; for (iter = m_coll.begin(); iter != m_coll.end(); ++iter) { // save phrase TargetPhrase &targetPhrase = **iter; targetPhrase.Save(onDiskWrapper); // save coll size_t memUsedTPOtherInfo; char *memTPOtherInfo = targetPhrase.WriteOtherInfoToMemory(onDiskWrapper, memUsedTPOtherInfo); // expand existing mem mem = (char*) realloc(mem, memUsed + memUsedTPOtherInfo); memcpy(mem + memUsed, memTPOtherInfo, memUsedTPOtherInfo); memUsed += memUsedTPOtherInfo; free(memTPOtherInfo); } // total number of bytes //((uint64_t*)mem)[0] = (uint64_t) memUsed; uint64_t startPos = file.tellp(); file.seekp(0, ios::end); file.write((char*) mem, memUsed); free(mem); #ifndef NDEBUG uint64_t endPos = file.tellp(); assert(startPos + memUsed == endPos); #endif m_filePos = startPos; }