Fix using of FilesContainerR, FilesContainerW.

Note! FilesContainerW::GetWriter can reallocate file on disk.
This commit is contained in:
vng 2011-09-08 15:12:36 +03:00 committed by Alex Zolotarev
parent 0c3352b864
commit bd75e193c0
5 changed files with 27 additions and 32 deletions

View file

@ -177,9 +177,8 @@ FileWriter FilesContainerW::GetWriter(Tag const & tag)
uint64_t const curr = m_info.back().m_offset + m_info.back().m_size;
m_info.push_back(Info(tag, curr));
FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING);
FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING, true);
writer.Seek(curr);
writer.Truncate(curr);
return writer;
}
else

View file

@ -4,20 +4,26 @@
#include "../../base/start_mem_debug.hpp"
FileWriter::FileWriter(FileWriter const & rhs) : Writer(*this)
FileWriter::FileWriter(FileWriter const & rhs)
: Writer(*this), m_bTruncOnClose(rhs.m_bTruncOnClose)
{
m_pFileData.swap(const_cast<FileWriter &>(rhs).m_pFileData);
}
FileWriter::FileWriter(string const & fileName, FileWriter::Op op)
: m_pFileData(new fdata_t(fileName, static_cast<fdata_t::Op>(op)))
FileWriter::FileWriter(string const & fileName, FileWriter::Op op, bool bTruncOnClose)
: m_pFileData(new fdata_t(fileName, static_cast<fdata_t::Op>(op))), m_bTruncOnClose(bTruncOnClose)
{
}
FileWriter::~FileWriter()
{
if (m_pFileData)
{
Flush();
if (m_bTruncOnClose)
m_pFileData->Truncate(Pos());
}
}
int64_t FileWriter::Pos() const
@ -51,11 +57,6 @@ void FileWriter::Flush()
m_pFileData->Flush();
}
void FileWriter::Truncate(uint64_t sz)
{
m_pFileData->Truncate(sz);
}
void FileWriter::DeleteFileX(string const & fName)
{
my::DeleteFileX(fName);

View file

@ -27,7 +27,8 @@ public:
/// Added for use in FilesContainerW interface.
FileWriter(FileWriter const & rhs);
explicit FileWriter(string const & fileName, Op operation = OP_WRITE_TRUNCATE);
explicit FileWriter(string const & fileName,
Op operation = OP_WRITE_TRUNCATE, bool bTruncOnClose = false);
~FileWriter();
void Seek(int64_t pos);
@ -36,7 +37,6 @@ public:
uint64_t Size() const;
void Flush();
void Truncate(uint64_t sz);
static void DeleteFileX(string const & fName);
@ -45,4 +45,5 @@ public:
private:
typedef my::FileData fdata_t;
scoped_ptr<fdata_t> m_pFileData;
bool m_bTruncOnClose;
};

View file

@ -12,6 +12,11 @@ namespace indexer
{
try
{
// First - open container for writing (it can be reallocated).
FilesContainerW writeCont(datFile, FileWriter::OP_WRITE_EXISTING);
FileWriter writer = writeCont.GetWriter(INDEX_FILE_TAG);
// Second - open container for reading.
FilesContainerR readCont(datFile);
feature::DataHeader header;
@ -19,9 +24,6 @@ namespace indexer
FeaturesVector featuresVector(readCont, header);
FilesContainerW writeCont(datFile, FileWriter::OP_WRITE_EXISTING);
FileWriter writer = writeCont.GetWriter(INDEX_FILE_TAG);
BuildIndex(header.GetLastScale() + 1, featuresVector, writer, tmpFile);
}
catch (Reader::Exception const & e)
@ -34,13 +36,6 @@ namespace indexer
LOG(LERROR, ("Error writing index file: ", e.what()));
}
#ifdef DEBUG
FilesContainerR readCont(datFile);
FilesContainerR::ReaderT r = readCont.GetReader(HEADER_FILE_TAG);
int64_t const base = ReadPrimitiveFromPos<int64_t>(r, 0);
LOG(LINFO, ("OFFSET = ", base));
#endif
return true;
}
} // namespace indexer

View file

@ -106,23 +106,22 @@ bool indexer::BuildSearchIndexFromDatFile(string const & datFile)
{
try
{
FilesContainerR readCont(datFile);
feature::DataHeader header;
header.Load(readCont.GetReader(HEADER_FILE_TAG));
FeaturesVector featuresVector(readCont, header);
FilesContainerW writeCont(datFile, FileWriter::OP_WRITE_EXISTING);
vector<char> serialTrie;
{
FilesContainerR readCont(datFile);
feature::DataHeader header;
header.Load(readCont.GetReader(HEADER_FILE_TAG));
FeaturesVector featuresVector(readCont, header);
MemWriter<vector<char> > writer(serialTrie);
BuildSearchIndex(featuresVector, writer);
reverse(serialTrie.begin(), serialTrie.end());
}
writeCont.Write(serialTrie, SEARCH_INDEX_FILE_TAG);
FilesContainerW(datFile).Write(serialTrie, SEARCH_INDEX_FILE_TAG);
}
catch (Reader::Exception const & e)
{