ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RecordFile.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File RecordFile.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
45/*--------------------------------------------------------------------------*/
46/*----------------------------- DEFINITIONS --------------------------------*/
47/*--------------------------------------------------------------------------*/
48
49#ifndef __ADS_RecordFile
50 #define __ADS_RecordFile
51/* self-identification: #endif constant */
52
53/*--------------------------------------------------------------------------*/
54/*------------------------------ INCLUDES ----------------------------------*/
55/*--------------------------------------------------------------------------*/
56
57#include "ADS/Contract.h"
58
59#include <cstddef>
60#include <cstdint>
61#include <filesystem>
62#include <string>
63#include <type_traits>
64#include <vector>
65
66/*--------------------------------------------------------------------------*/
67/*--------------------------- NAMESPACE ------------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70namespace ADS
71{
72
73/*--------------------------------------------------------------------------*/
74/*---------------------------- CLASS RecordFile ----------------------------*/
75/*--------------------------------------------------------------------------*/
76
78
79template< class T >
80 requires std::is_trivially_copyable_v< T >
82{
83
84/*--------------------------------------------------------------------------*/
85
86 public:
87 using value_type = T;
88 using size_type = std::size_t;
89
92 static constexpr std::size_t defaultBuffer =
93 sizeof( T ) >= ( std::size_t( 1 ) << 20 )
94 ? 1
95 : ( std::size_t( 1 ) << 20 ) / sizeof( T );
96
98 explicit RecordFile( std::filesystem::path path );
99
101 [[nodiscard]] const std::filesystem::path & path( void ) const;
102
104 [[nodiscard]] std::size_t size( void ) const;
105
107 [[nodiscard]] bool empty( void ) const;
108
115 void append( const T * src , std::size_t n );
116
120 template< class F >
121 void forEach( F && f , std::size_t buffer = defaultBuffer ) const;
122
124 [[nodiscard]] std::vector< T > readAll( void ) const;
125
128 void clear( void );
129
132 void remove( void );
133
134/*--------------------------------------------------------------------------*/
135
136 private:
137 std::filesystem::path f_path;
138
139 }; // end( class RecordFile )
140
141/*--------------------------------------------------------------------------*/
142/*--------------------------- CLASS RecordWriter ---------------------------*/
143/*--------------------------------------------------------------------------*/
144
147
148template< class T >
149 requires std::is_trivially_copyable_v< T >
151{
152
153/*--------------------------------------------------------------------------*/
154
155 public:
160 std::size_t buffer = RecordFile< T >::defaultBuffer );
161
163 RecordWriter( RecordWriter && ) noexcept = default;
164
166 RecordWriter( const RecordWriter & ) = delete;
167
169 RecordWriter & operator=( const RecordWriter & ) = delete;
170
174
187 void push( const T & x );
188
191 void flush( void );
192
194 [[nodiscard]] std::size_t pending( void ) const;
195
197 [[nodiscard]] RecordFile< T > & file( void ) const;
198
199/*--------------------------------------------------------------------------*/
200
201 private:
202 RecordFile< T > * f_file;
203 std::size_t f_cap;
204 std::vector< T > f_buf;
205
206 }; // end( class RecordWriter )
207
208/*--------------------------------------------------------------------------*/
209/*-------------------------- CLASS TempDirectory ---------------------------*/
210/*--------------------------------------------------------------------------*/
211
213
215{
216
217/*--------------------------------------------------------------------------*/
218
219 public:
222 explicit TempDirectory( const std::filesystem::path & parent = {} ,
223 const std::string & prefix = "ads-" );
224
226 TempDirectory( TempDirectory && other ) noexcept;
227
229 TempDirectory( const TempDirectory & ) = delete;
230
232 TempDirectory & operator=( const TempDirectory & ) = delete;
233
236
238 [[nodiscard]] const std::filesystem::path & path( void ) const;
239
241 [[nodiscard]] std::filesystem::path
242 operator/( const std::string & name ) const;
243
244/*--------------------------------------------------------------------------*/
245
246 private:
247 std::filesystem::path f_path;
248
249 }; // end( class TempDirectory )
250
251/*--------------------------------------------------------------------------*/
252
253} // end( namespace ADS )
254
255/*--------------------------------------------------------------------------*/
256/*-------------------------- IMPLEMENTATION --------------------------------*/
257/*--------------------------------------------------------------------------*/
258
259#include "ADS/external/impl/RecordFile.tpp"
260
261/*--------------------------------------------------------------------------*/
262
263#endif /* RecordFile.h included */
264
265/*--------------------------------------------------------------------------*/
266/*------------------------- End File RecordFile.h --------------------------*/
267/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
a file of records of a trivially copyable type on the disk
Definition RecordFile.h:82
T value_type
the type of the records
Definition RecordFile.h:87
std::size_t size_type
the type of the sizes
Definition RecordFile.h:88
static constexpr std::size_t defaultBuffer
the records of a buffer of 1 MB, the default of the scans and of the writers
Definition RecordFile.h:92
void forEach(F &&f, std::size_t buffer=defaultBuffer) const
for each record of the file, in order, read through a buffer of buffer records
bool empty(void) const
whether the file has no record
void append(const T *src, std::size_t n)
the records of src appended at the end of the file, which is made if it does not exist,...
void remove(void)
the file deleted from the disk, if it is there
RecordFile(std::filesystem::path path)
the file at path, whose records are those already there, if any
const std::filesystem::path & path(void) const
the path of the file
std::size_t size(void) const
the number of whole records of the file, 0 if it does not exist
void clear(void)
the file emptied, and made if it does not exist
std::vector< T > readAll(void) const
all the records of the file, in order
RecordWriter(RecordWriter &&) noexcept=default
the writer moved, its buffer with it
std::size_t pending(void) const
the records in the buffer, not yet in the file
void push(const T &x)
appends , writing the buffer when it is full
RecordFile< T > & file(void) const
the file written
void flush(void)
the records of the buffer written to the file
RecordWriter(RecordFile< T > &file, std::size_t buffer=RecordFile< T >::defaultBuffer)
the writer at the end of file (which must outlive it), with a buffer of buffer records
TempDirectory(TempDirectory &&other) noexcept
the directory moved: the moved one removes nothing
TempDirectory(const TempDirectory &)=delete
no copy, since the directory would be removed twice
TempDirectory & operator=(const TempDirectory &)=delete
no copy, since the directory would be removed twice
~TempDirectory()
the directory removed with all that it holds, errors dropped
const std::filesystem::path & path(void) const
the path of the directory
TempDirectory(const std::filesystem::path &parent={}, const std::string &prefix="ads-")
a new directory, of a name not taken, in parent (the temporary directory of the system if empty),...
std::filesystem::path operator/(const std::string &name) const
the path of the file name in the directory
Definition ADT.h:96