MultiDiskAdaptor: Use std::unique_ptr for DiskWriterEntry and DiskWriter

MultiFileAllocationIterator is also rewritten so that it does not
requre copying DiskWriterEntry objects.
This commit is contained in:
Tatsuhiro Tsujikawa
2013-07-05 21:08:24 +09:00
parent e823fe8bb5
commit f000fd0cab
12 changed files with 236 additions and 275 deletions

View File

@@ -38,18 +38,18 @@ void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries()
std::string storeDir = A2_TEST_OUT_DIR"/aria2_MultiFileAllocationIteratorTest"
"_testMakeDiskWriterEntries";
std::shared_ptr<FileEntry> fs[] = {
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/file1", 1536, 0)),
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/file2", 2048, 1536)),// req no
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/file3", 1024, 3584)),
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/file4", 1024, 4608)),// req no
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/file5", 1024, 5632)),// req no
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/file6", 1024, 6656)),// req no
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/file7", 256, 7680)),// req no
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/file8", 255, 7936)),
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/file9", 1025, 8191)),// req no
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/fileA", 1024, 9216)),// req no
std::shared_ptr<FileEntry>(new FileEntry(storeDir+"/fileB", 1024, 10240)),
auto fs = std::vector<std::shared_ptr<FileEntry>>{
std::make_shared<FileEntry>(storeDir+"/file1", 1536, 0),
std::make_shared<FileEntry>(storeDir+"/file2", 2048, 1536),// req no
std::make_shared<FileEntry>(storeDir+"/file3", 1024, 3584),
std::make_shared<FileEntry>(storeDir+"/file4", 1024, 4608),// req no
std::make_shared<FileEntry>(storeDir+"/file5", 1024, 5632),// req no
std::make_shared<FileEntry>(storeDir+"/file6", 1024, 6656),// req no
std::make_shared<FileEntry>(storeDir+"/file7", 256, 7680),// req no
std::make_shared<FileEntry>(storeDir+"/file8", 255, 7936),
std::make_shared<FileEntry>(storeDir+"/file9", 1025, 8191),// req no
std::make_shared<FileEntry>(storeDir+"/fileA", 1024, 9216),// req no
std::make_shared<FileEntry>(storeDir+"/fileB", 1024, 10240)
};
fs[1]->setRequested(false); // file2
fs[3]->setRequested(false); // file4
@@ -59,21 +59,22 @@ void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries()
fs[8]->setRequested(false); // file9
fs[9]->setRequested(false); // fileA
for(auto& fe : fs) {
File{fe->getPath()}.remove();
}
// create empty file4
createFile(storeDir+std::string("/file4"), 0);
std::shared_ptr<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
diskAdaptor->setFileEntries(std::begin(fs), std::end(fs));
diskAdaptor->setPieceLength(1024);
diskAdaptor->openFile();
MultiDiskAdaptor diskAdaptor;
diskAdaptor.setFileEntries(std::begin(fs), std::end(fs));
diskAdaptor.setPieceLength(1024);
diskAdaptor.openFile();
auto itr = std::dynamic_pointer_cast<MultiFileAllocationIterator>
(diskAdaptor->fileAllocationIterator());
const std::deque<std::shared_ptr<DiskWriterEntry> >& entries =
itr->getDiskWriterEntries();
auto allocitr = diskAdaptor.fileAllocationIterator();
auto itr = dynamic_cast<MultiFileAllocationIterator*>(allocitr.get());
auto& entries = itr->getDiskWriterEntries();
CPPUNIT_ASSERT_EQUAL((size_t)11, entries.size());
// file1
@@ -152,58 +153,49 @@ void MultiFileAllocationIteratorTest::testAllocate()
int64_t length6 = 30;
try {
std::shared_ptr<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
diskAdaptor->setPieceLength(1);
MultiDiskAdaptor diskAdaptor;
diskAdaptor.setPieceLength(1);
int64_t offset = 0;
std::shared_ptr<FileEntry> fileEntry1(new FileEntry(storeDir+"/"+fname1,
length1,
offset));
auto fileEntry1 = std::make_shared<FileEntry>(storeDir+"/"+fname1,
length1, offset);
offset += length1;
std::shared_ptr<FileEntry> fileEntry2(new FileEntry(storeDir+"/"+fname2,
length2,
offset));
auto fileEntry2 = std::make_shared<FileEntry>(storeDir+"/"+fname2,
length2, offset);
offset += length2;
std::shared_ptr<FileEntry> fileEntry3(new FileEntry(storeDir+"/"+fname3,
length3,
offset));
auto fileEntry3 = std::make_shared<FileEntry>(storeDir+"/"+fname3,
length3, offset);
offset += length3;
std::shared_ptr<FileEntry> fileEntry4(new FileEntry(storeDir+"/"+fname4,
length4,
offset));
auto fileEntry4 = std::make_shared<FileEntry>(storeDir+"/"+fname4,
length4, offset);
fileEntry4->setRequested(false);
offset += length4;
std::shared_ptr<FileEntry> fileEntry5(new FileEntry(storeDir+"/"+fname5,
length5,
offset));
auto fileEntry5 = std::make_shared<FileEntry>(storeDir+"/"+fname5,
length5, offset);
offset += length5;
std::shared_ptr<FileEntry> fileEntry6(new FileEntry(storeDir+"/"+fname6,
length6,
offset));
auto fileEntry6 = std::make_shared<FileEntry>(storeDir+"/"+fname6,
length6, offset);
fileEntry6->setRequested(false);
std::vector<std::shared_ptr<FileEntry> > fs;
fs.push_back(fileEntry1);
fs.push_back(fileEntry2);
fs.push_back(fileEntry3);
fs.push_back(fileEntry4);
fs.push_back(fileEntry5);
fs.push_back(fileEntry6);
diskAdaptor->setFileEntries(fs.begin(), fs.end());
auto fs = std::vector<std::shared_ptr<FileEntry>>{
fileEntry1,
fileEntry2,
fileEntry3,
fileEntry4,
fileEntry5,
fileEntry6
};
diskAdaptor.setFileEntries(std::begin(fs), std::end(fs));
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i = fs.begin();
i != fs.end(); ++i) {
File((*i)->getPath()).remove();
for(auto& fe : fs) {
File{fe->getPath()}.remove();
}
// we have to open file first.
diskAdaptor->initAndOpenFile();
auto itr = std::dynamic_pointer_cast<MultiFileAllocationIterator>
(diskAdaptor->fileAllocationIterator());
diskAdaptor.initAndOpenFile();
auto allocitr = diskAdaptor.fileAllocationIterator();
auto itr = dynamic_cast<MultiFileAllocationIterator*>(allocitr.get());
while(!itr->finished()) {
itr->allocateChunk();
}