casacore
Loading...
Searching...
No Matches
BufferedColumnarFile.h
Go to the documentation of this file.
1#ifndef CASACORE_BUFFERED_COLUMNAR_FILE_H_
2#define CASACORE_BUFFERED_COLUMNAR_FILE_H_
3
4#include <cassert>
5#include <complex>
6#include <cstdint>
7#include <string>
8#include <vector>
9
10#include "BitPacking.h"
11#include "RowBasedFile.h"
12
13namespace casacore {
14
32template <uint64_t BufferSize = 100 * 1024>
34 public:
43
44 VarBufferedColumnarFile() noexcept = default;
45
53 rhs.block_changed_ = false;
54 rhs.active_block_ = 0;
55 rhs.rows_per_block_ = 0;
56 }
57
59 if (IsOpen()) {
60 if (block_changed_) {
61 const uint64_t start_row = active_block_ * rows_per_block_;
62 const size_t n_rows_to_write =
63 std::min(rows_per_block_, std::max(NRows(), start_row) - start_row);
64 Seek(start_row * Stride() + DataLocation(), SEEK_SET);
65 WriteData(block_buffer_.data(), n_rows_to_write * Stride());
66 block_changed_ = false;
67 }
68 }
69 }
70
72 Close();
73 RowBasedFile::operator=(std::move(rhs));
74 std::swap(packed_buffer_, rhs.packed_buffer_);
75 std::swap(block_changed_, rhs.block_changed_);
76 std::swap(active_block_, rhs.active_block_);
77 std::swap(rows_per_block_, rhs.rows_per_block_);
78 std::swap(block_buffer_, rhs.block_buffer_);
79 return *this;
80 }
81
87 void Close() {
88 if (IsOpen()) {
89 if (block_changed_) {
90 try {
92 } catch (...) {
93 // Even if writing fails, the file should still be closed to
94 // prevent the file to remain open after destruction.
96 throw;
97 }
98 }
100 }
101 }
102
108 static VarBufferedColumnarFile CreateNew(const std::string& filename, uint64_t header_size,
109 uint64_t stride) {
110 return VarBufferedColumnarFile(filename, header_size, stride);
111 }
112
119 static VarBufferedColumnarFile OpenExisting(const std::string& filename, size_t header_size) {
120 return VarBufferedColumnarFile(filename, header_size);
121 }
122
132 void Read(uint64_t row, uint64_t column_offset, float* data, uint64_t n) {
133 ReadImplementation(row, column_offset, data, n);
134 }
135
139 void Read(uint64_t row, uint64_t column_offset, double* data, uint64_t n) {
140 ReadImplementation(row, column_offset, data, n);
141 }
142
146 void Read(uint64_t row, uint64_t column_offset, int32_t* data, uint64_t n) {
147 ReadImplementation(row, column_offset, data, n);
148 }
149
153 void Read(uint64_t row, uint64_t column_offset, std::complex<float>* data, uint64_t n) {
154 ReadImplementation(row, column_offset, data, n);
155 }
156
161 void Read(uint64_t row, uint64_t column_offset, bool* data, uint64_t n) {
162 const size_t byte_size = (n + 7) / 8;
163 assert(column_offset + byte_size <= Stride());
164 ActivateBlock(row);
165 if (row >= NRows()) {
166 std::fill_n(data, n, false);
167 } else {
168 Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
169 ReadData(packed_buffer_.data(), byte_size);
170 UnpackBoolArray(data, packed_buffer_.data(), n);
171 }
172 }
173
177 void Write(uint64_t row, uint64_t column_offset, const float* data, uint64_t n) {
178 WriteImplementation(row, column_offset, data, n);
179 }
180
184 void Write(uint64_t row, uint64_t column_offset, const double* data, uint64_t n) {
185 WriteImplementation(row, column_offset, data, n);
186 }
187
191 void Write(uint64_t row, uint64_t column_offset, const int32_t* data, uint64_t n) {
192 WriteImplementation(row, column_offset, data, n);
193 }
194
198 void Write(uint64_t row, uint64_t column_offset, const std::complex<float>* data, uint64_t n) {
199 WriteImplementation(row, column_offset, data, n);
200 }
201
205 void Write(uint64_t row, uint64_t column_offset, const std::complex<double>* data, uint64_t n) {
206 WriteImplementation(row, column_offset, data, n);
207 }
208
213 void Write(uint64_t row, uint64_t column_offset, const bool* data, uint64_t n) {
214 const size_t byte_size = (n + 7) / 8;
215 assert(column_offset + byte_size <= Stride());
216 ActivateBlock(row);
217 PackBoolArray(packed_buffer_.data(), data, n);
218 Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
219 WriteData(packed_buffer_.data(), byte_size);
220 SetNRows(std::max(row + 1, NRows()));
221 }
222
227 void SetStride(uint64_t new_stride) {
228 RowBasedFile::SetStride(new_stride);
229 packed_buffer_.resize(new_stride);
230 active_block_ = std::numeric_limits<uint64_t>::max();
231 rows_per_block_ = new_stride == 0 ? 0 : std::max<size_t>(1, BufferSize / new_stride);
232 block_buffer_.resize(rows_per_block_ * new_stride);
233 block_changed_ = false;
234 }
235
236 private:
237 // Create or overwrite a new columnar file on disk
238 VarBufferedColumnarFile(const std::string& filename, uint64_t header_size, uint64_t stride)
239 : RowBasedFile(filename, header_size, stride),
240 packed_buffer_(stride),
241 rows_per_block_(stride == 0 ? 0 : std::max<size_t>(1, BufferSize / stride)),
242 block_buffer_(rows_per_block_ * stride) {}
243
244 // Open an existing columnar file
245 VarBufferedColumnarFile(const std::string& filename, size_t header_size)
246 : RowBasedFile(filename, header_size) {
247 if (Stride() != 0) {
248 packed_buffer_.resize(Stride());
249 rows_per_block_ = std::max<size_t>(1, BufferSize / Stride());
251 }
252 active_block_ = std::numeric_limits<uint64_t>::max();
253 }
254
255 void ActivateBlock(uint64_t row) {
256 const uint64_t block = row / rows_per_block_;
257 if (active_block_ != block) {
258 if (block_changed_) {
260 }
261
262 const uint64_t start_row = block * rows_per_block_;
263 const size_t n_rows_to_read =
264 std::min(rows_per_block_, std::max(NRows(), start_row) - start_row);
265 Seek(start_row * Stride() + DataLocation(), SEEK_SET);
266 ReadData(block_buffer_.data(), n_rows_to_read * Stride());
267 // Fill the remainder of block_buffer_ with zeroes. Doing it here makes
268 // the code robust and avoids the need for inserting zeroes when adding
269 // rows out-of-order, e.g., when adding row 5 while NRows() is 2."
270 std::fill(block_buffer_.begin() + n_rows_to_read * Stride(), block_buffer_.end(), 0);
271
272 active_block_ = block;
273 }
274 }
275
276 template <typename ValueType>
277 void ReadImplementation(uint64_t row, uint64_t column_offset, ValueType* data, uint64_t n) {
278 assert(column_offset + n * sizeof(ValueType) <= Stride());
279 if (row >= NRows()) {
280 std::fill_n(data, n, ValueType());
281 } else {
282 ActivateBlock(row);
283 const uint64_t block_row = active_block_ * rows_per_block_;
284 const unsigned char* position =
285 block_buffer_.data() + (row - block_row) * Stride() + column_offset;
286 std::copy_n(position, n * sizeof(ValueType), reinterpret_cast<unsigned char*>(data));
287 }
288 }
289
290 template <typename ValueType>
291 void WriteImplementation(uint64_t row, uint64_t column_offset, const ValueType* data,
292 uint64_t n) {
293 assert(column_offset + n * sizeof(ValueType) <= Stride());
294 ActivateBlock(row);
295 const uint64_t block_row = active_block_ * rows_per_block_;
296 unsigned char* position = block_buffer_.data() + (row - block_row) * Stride() + column_offset;
297 std::copy_n(reinterpret_cast<const unsigned char*>(data), n * sizeof(ValueType), position);
298 SetNRows(std::max(row + 1, NRows()));
299 block_changed_ = true;
300 }
301
303 const uint64_t start_row = active_block_ * rows_per_block_;
304 const size_t n_rows_to_write =
305 std::min(rows_per_block_, std::max(NRows(), start_row) - start_row);
306 Seek(start_row * Stride() + DataLocation(), SEEK_SET);
307 WriteData(block_buffer_.data(), n_rows_to_write * Stride());
308 block_changed_ = false;
309 }
310
315 std::vector<unsigned char> packed_buffer_;
316
317 bool block_changed_ = false;
318 uint64_t active_block_ = 0;
319 uint64_t rows_per_block_ = 0;
320 std::vector<unsigned char> block_buffer_;
321};
322
324
325} // namespace casacore
326
327#endif
void UnpackBoolArray(bool *output, const unsigned char *packed_input, size_t n)
Definition BitPacking.h:28
void PackBoolArray(unsigned char *packed_buffer, const bool *input, size_t n)
Definition BitPacking.h:6
void WriteData(const unsigned char *data, uint64_t size)
void Seek(off_t pos, int seek_direction)
uint64_t DataLocation() const
Offset of the first row in the file.
void SetStride(uint64_t new_stride)
Set the number of bytes per row for this file.
void ReadHeader(unsigned char *data)
Read an optional extra header to the file.
const std::string & Filename() const
RowBasedFile & operator=(RowBasedFile &&rhs)
void WriteHeader(const unsigned char *data)
Write an optional extra header to the file.
void AddRows(uint64_t n_rows)
Adds a given number of rows to the back of the file.
uint64_t NRows() const
Total number of rows stored in this file.
void ReadData(unsigned char *data, uint64_t size)
void DeleteRow()
Deletes the last row.
uint64_t Stride() const
Total number of bytes in one row.
void SetNRows(uint64_t new_n_rows)
void Close()
Close the file.
Class that provides binary table I/O.
static VarBufferedColumnarFile CreateNew(const std::string &filename, uint64_t header_size, uint64_t stride)
Create a new file on disk.
static VarBufferedColumnarFile OpenExisting(const std::string &filename, size_t header_size)
Open an existing file from disk.
void Write(uint64_t row, uint64_t column_offset, const std::complex< double > *data, uint64_t n)
Write an array of complex doubles.
void Write(uint64_t row, uint64_t column_offset, const bool *data, uint64_t n)
Write an array of bools.
void ReadImplementation(uint64_t row, uint64_t column_offset, ValueType *data, uint64_t n)
void Write(uint64_t row, uint64_t column_offset, const double *data, uint64_t n)
Write an array of doubles.
void Read(uint64_t row, uint64_t column_offset, int32_t *data, uint64_t n)
Read array of int32_t.
void Read(uint64_t row, uint64_t column_offset, std::complex< float > *data, uint64_t n)
Read array of complex floats.
void SetStride(uint64_t new_stride)
Set the number of bytes per row for this file.
VarBufferedColumnarFile() noexcept=default
VarBufferedColumnarFile(const std::string &filename, size_t header_size)
Open an existing columnar file.
void Read(uint64_t row, uint64_t column_offset, double *data, uint64_t n)
Read array of doubles.
void WriteImplementation(uint64_t row, uint64_t column_offset, const ValueType *data, uint64_t n)
VarBufferedColumnarFile(const std::string &filename, uint64_t header_size, uint64_t stride)
Create or overwrite a new columnar file on disk.
void Write(uint64_t row, uint64_t column_offset, const int32_t *data, uint64_t n)
Write an array of int32_t.
VarBufferedColumnarFile & operator=(VarBufferedColumnarFile &&rhs)
void Write(uint64_t row, uint64_t column_offset, const std::complex< float > *data, uint64_t n)
Write an array of complex floats.
void Read(uint64_t row, uint64_t column_offset, bool *data, uint64_t n)
Read an array of bools.
uint64_t NRows() const
Total number of rows stored in this file.
void Read(uint64_t row, uint64_t column_offset, float *data, uint64_t n)
Read one cell containing an array of floats.
void Write(uint64_t row, uint64_t column_offset, const float *data, uint64_t n)
Write one cell containing an array of floats.
uint64_t Stride() const
Total number of bytes in one row.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
void move(TYPE *target, int npixels) const
VarBufferedColumnarFile< 100 *1024 > BufferedColumnarFile
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
Definition Complex.h:344