Correct container of files.

This commit is contained in:
vng 2010-12-31 16:35:49 +02:00 committed by Alex Zolotarev
parent 0eb019aaa9
commit 2cf972fcbe
4 changed files with 55 additions and 9 deletions

View file

@ -25,7 +25,8 @@ SOURCES += ../../testing/testingmain.cpp \
writer_test.cpp \
var_record_reader_test.cpp \
file_sort_test.cpp \
reader_cache_test.cpp
reader_cache_test.cpp \
file_container_test.cpp \
HEADERS += \
reader_test.hpp

View file

@ -0,0 +1,43 @@
#include "../../testing/testing.hpp"
#include "../file_container.hpp"
#include "../varint.hpp"
#include "../../base/string_utils.hpp"
UNIT_TEST(FileContainer_Smoke)
{
string const fName = "file_container.tmp";
size_t const count = 10;
{
FilesContainerW writer(fName);
for (size_t i = 0; i < count; ++i)
{
FileWriter w = writer.GetWriter(utils::to_string(i));
for (uint32_t j = 0; j < i; ++j)
WriteVarUint(w, j);
}
writer.Finish();
}
{
FilesContainerR reader(fName);
for (size_t i = 0; i < count; ++i)
{
FileReader r = reader.GetReader(utils::to_string(i));
ReaderSource<FileReader> src(r);
for (uint32_t j = 0; j < i; ++j)
{
uint32_t const test = ReadVarUint<uint32_t>(src);
CHECK_EQUAL(j, test, ());
}
}
}
}

View file

@ -2,14 +2,15 @@
#include "file_container.hpp"
#include "varint.hpp"
#include "write_to_sink.hpp"
FilesContainerR::FilesContainerR(string const & fName)
: m_source(fName)
{
ReaderSource<FileReader> src(m_source);
uint64_t offset = ReadPrimitiveFromPos<uint64_t>(m_source, 0);
uint64_t const offset = ReadVarUint<uint64_t>(src);
ReaderSource<FileReader> src(m_source);
src.Skip(offset);
uint32_t const count = ReadVarUint<uint32_t>(src);
@ -64,18 +65,17 @@ FileWriter FilesContainerW::GetWriter(Tag const & tag)
void FilesContainerW::Finish()
{
uint64_t const curr = SaveCurrentSize();
{
uint64_t const curr = SaveCurrentSize();
FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING);
writer.Write(&curr, sizeof(curr));
writer.Seek(0);
WriteToSink(writer, curr);
}
FileWriter writer(m_name, FileWriter::OP_APPEND);
writer.Write(&curr, sizeof(curr));
sort(m_info.begin(), m_info.end(), less_info());
FileWriter writer(m_name, FileWriter::OP_APPEND);
uint32_t const count = m_info.size();
WriteVarUint(writer, count);

View file

@ -23,6 +23,8 @@ public:
OP_APPEND = 3
};
/// Works like "move semantics".
/// Added for use in FilesContainerW interface.
FileWriter(FileWriter const & rhs);
explicit FileWriter(string const & fileName, Op operation = OP_WRITE_TRUNCATE);