Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
zip_writer.h
1/*
2** Copyright 2007-2024, RTE (https://www.rte-france.com)
3** See AUTHORS.txt
4** SPDX-License-Identifier: MPL-2.0
5** This file is part of Antares-Simulator,
6** Adequacy and Performance assessment for interconnected energy networks.
7**
8** Antares_Simulator is free software: you can redistribute it and/or modify
9** it under the terms of the Mozilla Public Licence 2.0 as published by
10** the Mozilla Foundation, either version 2 of the License, or
11** (at your option) any later version.
12**
13** Antares_Simulator is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16** Mozilla Public Licence 2.0 for more details.
17**
18** You should have received a copy of the Mozilla Public Licence 2.0
19** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20*/
21#pragma once
22
23#include <mutex>
24#include <string>
25
26#include <yuni/core/string.h>
27#include <yuni/job/queue/service.h>
28
29#include <antares/benchmarking/DurationCollector.h>
30#include "antares/concurrency/concurrency.h"
31#include "antares/writer/i_writer.h"
32
33namespace Antares::Solver
34{
35enum class ZipState
36{
37 can_receive_data,
38 blocking
39};
40
41class ZipWriter;
42
47template<class ContentT>
49{
50public:
51 ZipWriteJob(ZipWriter& writer,
52 const std::string& entryPath,
53 ContentT& content,
54 Benchmarking::DurationCollector& duration_collector);
55 void writeEntry();
56
57 void operator()()
58 {
59 writeEntry();
60 }
61
62private:
63 // Pointer to Zip handle
64 void* pZipHandle;
65 // Protect pZipHandle against concurrent writes, since minizip-ng isn't thread-safe
66 std::mutex& pZipMutex;
67 // State
68 ZipState& pState;
69 // Entry path for the new file within the zip archive
70 const std::string pEntryPath;
71 // Content of the new file
72 ContentT pContent;
73 // Benchmarking. How long do we wait ? How long does the zip write take ?
74 Benchmarking::DurationCollector& pDurationCollector;
75};
76
78{
79public:
80 ZipWriter(std::shared_ptr<Yuni::Job::QueueService> qs,
81 const std::filesystem::path& archivePath,
82 Benchmarking::DurationCollector& duration_collector);
83 virtual ~ZipWriter();
84 void addEntryFromBuffer(const std::string& entryPath, Yuni::Clob& entryContent) override;
85 void addEntryFromBuffer(const std::filesystem::path& entryPath,
86 std::string& entryContent) override;
87 void addEntryFromFile(const std::filesystem::path& entryPath,
88 const std::filesystem::path& filePath) override;
89 void flush() override;
90 bool needsTheJobQueue() const override;
91 void finalize(bool verbose) override;
92 friend class ZipWriteJob<Yuni::Clob>;
93 friend class ZipWriteJob<std::string>;
94
95private:
96 // Queue where jobs will be appended
97 std::shared_ptr<Yuni::Job::QueueService> pQueueService;
98 // Prevent concurrent writes to the zip file
99 std::mutex pZipMutex;
100 // minizip-ng requires a void* as a zip handle.
101 void* pZipHandle;
102 // State, to allow/prevent new jobs being added to the queue
103 ZipState pState;
104 // Absolute path to the archive
105 const std::filesystem::path pArchivePath;
106 // Benchmarking. Passed to jobs
107 Benchmarking::DurationCollector& pDurationCollector;
108
109 Concurrency::FutureSet pendingTasks_;
110
111private:
112 template<class ContentType>
113 void addEntryFromBufferHelper(const std::filesystem::path& entryPath,
114 ContentType& entryContent);
115};
116} // namespace Antares::Solver
117
118#include "zip_writer.hxx"
Utility class to gather futures to wait for.
Definition concurrency.h:61
Definition i_writer.h:34
Definition zip_writer.h:49
Definition zip_writer.h:78
void flush() override
Definition zip_writer.cpp:228
Definition DurationCollector.h:36