casacore
Loading...
Searching...
No Matches
SimpleColumnarFile.h
Go to the documentation of this file.
1#ifndef CASACORE_COLUMNAR_FILE_H_
2#define CASACORE_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
25 public:
35
36 SimpleColumnarFile() noexcept = default;
37
41
42 ~SimpleColumnarFile() noexcept = default;
43
45 RowBasedFile::operator=(std::move(rhs));
46 std::swap(packed_buffer_, rhs.packed_buffer_);
47 return *this;
48 }
49
50 static SimpleColumnarFile CreateNew(const std::string& filename, uint64_t header_size,
51 uint64_t stride) {
52 return SimpleColumnarFile(filename, header_size, stride);
53 }
54
55 static SimpleColumnarFile OpenExisting(const std::string& filename, size_t header_size) {
56 return SimpleColumnarFile(filename, header_size);
57 }
58
59 void Read(uint64_t row, uint64_t column_offset, std::complex<float>* data, uint64_t n) {
60 ReadImplementation(row, column_offset, data, n);
61 }
62 void Read(uint64_t row, uint64_t column_offset, float* data, uint64_t n) {
63 ReadImplementation(row, column_offset, data, n);
64 }
65 void Read(uint64_t row, uint64_t column_offset, double* data, uint64_t n) {
66 ReadImplementation(row, column_offset, data, n);
67 }
68 void Read(uint64_t row, uint64_t column_offset, int32_t* data, uint64_t n) {
69 ReadImplementation(row, column_offset, data, n);
70 }
71 void Read(uint64_t row, uint64_t column_offset, bool* data, uint64_t n) {
72 const size_t byte_size = (n + 7) / 8;
73 assert(column_offset + byte_size <= Stride());
74 if (row >= NRows()) {
75 std::fill_n(data, n, false);
76 } else {
77 Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
78 ReadData(packed_buffer_.data(), byte_size);
79 UnpackBoolArray(data, packed_buffer_.data(), n);
80 }
81 }
82 void Write(uint64_t row, uint64_t column_offset, const std::complex<float>* data, uint64_t n) {
83 WriteImplementation(row, column_offset, data, n);
84 }
85 void Write(uint64_t row, uint64_t column_offset, const std::complex<double>* data, uint64_t n) {
86 WriteImplementation(row, column_offset, data, n);
87 }
88 void Write(uint64_t row, uint64_t column_offset, const float* data, uint64_t n) {
89 WriteImplementation(row, column_offset, data, n);
90 }
91 void Write(uint64_t row, uint64_t column_offset, const double* data, uint64_t n) {
92 WriteImplementation(row, column_offset, data, n);
93 }
94 void Write(uint64_t row, uint64_t column_offset, const int32_t* data, uint64_t n) {
95 WriteImplementation(row, column_offset, data, n);
96 }
97 void Write(uint64_t row, uint64_t column_offset, const bool* data, uint64_t n) {
98 const size_t byte_size = (n + 7) / 8;
99 assert(column_offset + byte_size <= Stride());
100 PackBoolArray(packed_buffer_.data(), data, n);
101 Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
102 WriteData(packed_buffer_.data(), byte_size);
103 SetNRows(std::max(row + 1, NRows()));
104 }
105
110 void SetStride(uint64_t new_stride) {
111 RowBasedFile::SetStride(new_stride);
112 packed_buffer_.resize((new_stride + 7) / 8);
113 }
114
115 private:
116 // Create or overwrite a new columnar file on disk
117 SimpleColumnarFile(const std::string& filename, uint64_t header_size, uint64_t stride)
118 : RowBasedFile(filename, header_size, stride), packed_buffer_((stride + 7) / 8) {}
119
120 // Open an existing columnar file
121 SimpleColumnarFile(const std::string& filename, size_t header_size)
122 : RowBasedFile(filename, header_size) {
123 packed_buffer_.resize((Stride() + 7) / 8);
124 }
125
126 template <typename ValueType>
127 void ReadImplementation(uint64_t row, uint64_t column_offset, ValueType* data, uint64_t n) {
128 assert(column_offset + n * sizeof(ValueType) <= Stride());
129 if (row >= NRows()) {
130 std::fill_n(data, n, ValueType());
131 } else {
132 Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
133 ReadData(reinterpret_cast<unsigned char*>(data), n * sizeof(ValueType));
134 }
135 }
136
137 template <typename ValueType>
138 void WriteImplementation(uint64_t row, uint64_t column_offset, const ValueType* data,
139 uint64_t n) {
140 assert(column_offset + n * sizeof(ValueType) <= Stride());
141 Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
142 WriteData(reinterpret_cast<const unsigned char*>(data), n * sizeof(ValueType));
143 SetNRows(std::max(row + 1, NRows()));
144 }
145
150 std::vector<unsigned char> packed_buffer_;
151};
152
153} // namespace casacore
154
155#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.
void Write(uint64_t row, uint64_t column_offset, const bool *data, uint64_t n)
void ReadImplementation(uint64_t row, uint64_t column_offset, ValueType *data, uint64_t n)
void Read(uint64_t row, uint64_t column_offset, std::complex< float > *data, uint64_t n)
void Read(uint64_t row, uint64_t column_offset, bool *data, uint64_t n)
void Read(uint64_t row, uint64_t column_offset, int32_t *data, uint64_t n)
static SimpleColumnarFile OpenExisting(const std::string &filename, size_t header_size)
void Read(uint64_t row, uint64_t column_offset, double *data, uint64_t n)
void WriteImplementation(uint64_t row, uint64_t column_offset, const ValueType *data, uint64_t n)
SimpleColumnarFile(const std::string &filename, size_t header_size)
Open an existing columnar file.
void Write(uint64_t row, uint64_t column_offset, const std::complex< float > *data, uint64_t n)
std::vector< unsigned char > packed_buffer_
This buffer is used temporarily for (un)packing booleans.
void Write(uint64_t row, uint64_t column_offset, const std::complex< double > *data, uint64_t n)
void Write(uint64_t row, uint64_t column_offset, const float *data, uint64_t n)
SimpleColumnarFile() noexcept=default
void Read(uint64_t row, uint64_t column_offset, float *data, uint64_t n)
static SimpleColumnarFile CreateNew(const std::string &filename, uint64_t header_size, uint64_t stride)
uint64_t NRows() const
Total number of rows stored in this file.
void SetStride(uint64_t new_stride)
Set the number of bytes per row for this file.
void Write(uint64_t row, uint64_t column_offset, const int32_t *data, uint64_t n)
void Write(uint64_t row, uint64_t column_offset, const double *data, uint64_t n)
SimpleColumnarFile(const std::string &filename, uint64_t header_size, uint64_t stride)
Create or overwrite a new columnar file on disk.
~SimpleColumnarFile() noexcept=default
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
void move(TYPE *target, int npixels) const
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
Definition Complex.h:344