-
Notifications
You must be signed in to change notification settings - Fork 15
/
ImportHelper.cpp
105 lines (90 loc) · 2.95 KB
/
ImportHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "ImportHelper.h"
#include "Common/IFileStreamProvider.h"
#include "Common/IForwardIterator.h"
#include "Common/Logger.h"
#include "Common/SignalHandler.h"
#include "Store/DebugTorrentStateIterator.h"
#include "Store/ITorrentStateStore.h"
#include "Torrent/Box.h"
#include <exception>
#include <filesystem>
#include <iostream>
#include <thread>
#include <vector>
namespace fs = std::filesystem;
ImportHelper::Result::Result() :
SuccessCount(0),
FailCount(0),
SkipCount(0)
{
//
}
ImportHelper::ImportHelper(ITorrentStateStorePtr sourceStore, fs::path const& sourceDataDir,
ITorrentStateStorePtr targetStore, fs::path const& targetDataDir, IFileStreamProvider& fileStreamProvider,
SignalHandler const& signalHandler) :
m_sourceStore(std::move(sourceStore)),
m_sourceDataDir(sourceDataDir),
m_targetStore(std::move(targetStore)),
m_targetDataDir(targetDataDir),
m_fileStreamProvider(fileStreamProvider),
m_signalHandler(signalHandler)
{
//
}
ImportHelper::~ImportHelper() = default;
ImportHelper::Result ImportHelper::Import(unsigned int threadCount)
{
Result result;
try
{
ITorrentStateIteratorPtr boxes = m_sourceStore->Export(m_sourceDataDir, m_fileStreamProvider);
boxes = std::make_unique<DebugTorrentStateIterator>(std::move(boxes));
std::vector<std::thread> threads;
for (unsigned int i = 0; i < threadCount; ++i)
{
threads.emplace_back(&ImportHelper::ImportImpl, this, std::cref(m_targetDataDir), std::ref(*boxes),
std::ref(result));
}
for (auto& thread : threads)
{
thread.join();
}
}
catch (std::exception const& e)
{
Logger(Logger::Error) << "Error: " << e.what();
throw;
}
if (m_signalHandler.IsInterrupted())
{
throw Exception("Execution has been interrupted");
}
Logger(Logger::Info) << "Finished: " << result.SuccessCount << " succeeded, " << result.FailCount << " failed, " <<
result.SkipCount << " skipped";
return result;
}
void ImportHelper::ImportImpl(fs::path const& targetDataDir, ITorrentStateIterator& boxes, Result& result)
{
Box box;
while (!m_signalHandler.IsInterrupted() && boxes.GetNext(box))
{
std::string const prefix = "[" + box.SavePath.filename().string() + "] ";
try
{
Logger(Logger::Info) << prefix << "Import started";
m_targetStore->Import(targetDataDir, box, m_fileStreamProvider);
++result.SuccessCount;
Logger(Logger::Info) << prefix << "Import succeeded";
}
catch (ImportCancelledException const& e)
{
++result.SkipCount;
Logger(Logger::Warning) << prefix << "Import skipped: " << e.what();
}
catch (std::exception const& e)
{
++result.FailCount;
Logger(Logger::Error) << prefix << "Import failed: " << e.what();
}
}
}