Antares Simulator
Power System Simulator
zip_writer.h
1 /*
2 ** Copyright 2007-2025, 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 
33 namespace Antares::Solver
34 {
35 enum class ZipState
36 {
37  can_receive_data,
38  blocking
39 };
40 
41 class ZipWriter;
42 
47 template<class ContentT>
48 class ZipWriteJob final
49 {
50 public:
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 
62 private:
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 
77 class ZipWriter final: public IResultWriter
78 {
79 public:
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::filesystem::path& entryPath,
85  std::string& entryContent) override;
86  void addEntryFromFile(const std::filesystem::path& entryPath,
87  const std::filesystem::path& filePath) override;
88  void flush() override;
89  bool needsTheJobQueue() const override;
90  void finalize(bool verbose) override;
91  friend class ZipWriteJob<Yuni::Clob>;
92  friend class ZipWriteJob<std::string>;
93 
94 private:
95  // Queue where jobs will be appended
96  std::shared_ptr<Yuni::Job::QueueService> pQueueService;
97  // Prevent concurrent writes to the zip file
98  std::mutex pZipMutex;
99  // minizip-ng requires a void* as a zip handle.
100  void* pZipHandle;
101  // State, to allow/prevent new jobs being added to the queue
102  ZipState pState;
103  // Absolute path to the archive
104  const std::filesystem::path pArchivePath;
105  // Benchmarking. Passed to jobs
106  Benchmarking::DurationCollector& pDurationCollector;
107 
108  Concurrency::FutureSet pendingTasks_;
109 
110 private:
111  template<class ContentType>
112  void addEntryFromBufferHelper(const std::filesystem::path& entryPath,
113  ContentType& entryContent);
114 };
115 } // namespace Antares::Solver
116 
117 #include "zip_writer.hxx"
Utility class to gather futures to wait for.
Definition: concurrency.h:61
Definition: i_writer.h:32
Definition: zip_writer.h:49
Definition: zip_writer.h:78
void flush() override
Definition: zip_writer.cpp:220
Definition: DurationCollector.h:36