Loading...
Searching...
No Matches
SimpleColumnarFile.h
Go to the documentation of this file.
#ifndef CASACORE_COLUMNAR_FILE_H_
#define CASACORE_COLUMNAR_FILE_H_
#include <cassert>
#include <complex>
#include <cstdint>
#include <string>
#include <vector>
#include "BitPacking.h"
#include "RowBasedFile.h"
namespace casacore {
class SimpleColumnarFile : private RowBasedFile {
public:
using RowBasedFile::AddRows;
using RowBasedFile::Close;
using RowBasedFile::DeleteRow;
using RowBasedFile::Filename;
using RowBasedFile::IsOpen;
using RowBasedFile::NRows;
using RowBasedFile::ReadHeader;
using RowBasedFile::Stride;
using RowBasedFile::WriteHeader;
SimpleColumnarFile() noexcept = default;
SimpleColumnarFile(const SimpleColumnarFile& rhs) = delete;
SimpleColumnarFile(SimpleColumnarFile&& rhs) noexcept
: packed_buffer_(std::move(rhs.packed_buffer_)) {}
~SimpleColumnarFile() noexcept = default;
SimpleColumnarFile& operator=(SimpleColumnarFile&& rhs) {
RowBasedFile::operator=(std::move(rhs));
std::swap(packed_buffer_, rhs.packed_buffer_);
return *this;
}
static SimpleColumnarFile CreateNew(const std::string& filename,
uint64_t header_size, uint64_t stride) {
return SimpleColumnarFile(filename, header_size, stride);
}
static SimpleColumnarFile OpenExisting(const std::string& filename,
size_t header_size) {
return SimpleColumnarFile(filename, header_size);
}
void Read(uint64_t row, uint64_t column_offset, std::complex<float>* data,
uint64_t n) {
ReadImplementation(row, column_offset, data, n);
}
void Read(uint64_t row, uint64_t column_offset, float* data, uint64_t n) {
ReadImplementation(row, column_offset, data, n);
}
void Read(uint64_t row, uint64_t column_offset, double* data, uint64_t n) {
ReadImplementation(row, column_offset, data, n);
}
void Read(uint64_t row, uint64_t column_offset, int32_t* data, uint64_t n) {
ReadImplementation(row, column_offset, data, n);
}
void Read(uint64_t row, uint64_t column_offset, bool* data, uint64_t n) {
const size_t byte_size = (n + 7) / 8;
assert(column_offset + byte_size <= Stride());
if (row >= NRows()) {
std::fill_n(data, n, false);
} else {
Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
ReadData(packed_buffer_.data(), byte_size);
UnpackBoolArray(data, packed_buffer_.data(), n);
}
}
void Write(uint64_t row, uint64_t column_offset,
const std::complex<float>* data, uint64_t n) {
WriteImplementation(row, column_offset, data, n);
}
void Write(uint64_t row, uint64_t column_offset,
const std::complex<double>* data, uint64_t n) {
WriteImplementation(row, column_offset, data, n);
}
void Write(uint64_t row, uint64_t column_offset, const float* data,
uint64_t n) {
WriteImplementation(row, column_offset, data, n);
}
void Write(uint64_t row, uint64_t column_offset, const double* data,
uint64_t n) {
WriteImplementation(row, column_offset, data, n);
}
void Write(uint64_t row, uint64_t column_offset, const int32_t* data,
uint64_t n) {
WriteImplementation(row, column_offset, data, n);
}
void Write(uint64_t row, uint64_t column_offset, const bool* data,
uint64_t n) {
const size_t byte_size = (n + 7) / 8;
assert(column_offset + byte_size <= Stride());
PackBoolArray(packed_buffer_.data(), data, n);
Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
WriteData(packed_buffer_.data(), byte_size);
SetNRows(std::max(row + 1, NRows()));
}
void SetStride(uint64_t new_stride) {
RowBasedFile::SetStride(new_stride);
packed_buffer_.resize((new_stride + 7) / 8);
}
private:
// Create or overwrite a new columnar file on disk
SimpleColumnarFile(const std::string& filename, uint64_t header_size,
uint64_t stride)
: RowBasedFile(filename, header_size, stride),
packed_buffer_((stride + 7) / 8) {}
// Open an existing columnar file
SimpleColumnarFile(const std::string& filename, size_t header_size)
: RowBasedFile(filename, header_size) {
packed_buffer_.resize((Stride() + 7) / 8);
}
template <typename ValueType>
void ReadImplementation(uint64_t row, uint64_t column_offset, ValueType* data,
uint64_t n) {
assert(column_offset + n * sizeof(ValueType) <= Stride());
if (row >= NRows()) {
std::fill_n(data, n, ValueType());
} else {
Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
ReadData(reinterpret_cast<unsigned char*>(data), n * sizeof(ValueType));
}
}
template <typename ValueType>
void WriteImplementation(uint64_t row, uint64_t column_offset,
const ValueType* data, uint64_t n) {
assert(column_offset + n * sizeof(ValueType) <= Stride());
Seek(row * Stride() + column_offset + DataLocation(), SEEK_SET);
WriteData(reinterpret_cast<const unsigned char*>(data),
n * sizeof(ValueType));
SetNRows(std::max(row + 1, NRows()));
}
std::vector<unsigned char> packed_buffer_;
};
} // namespace casacore
#endif
BitPacking.h
UnpackBoolArray
void UnpackBoolArray(bool *output, const unsigned char *packed_input, size_t n)
Definition BitPacking.h:29
PackBoolArray
void PackBoolArray(unsigned char *packed_buffer, const bool *input, size_t n)
Definition BitPacking.h:6
RowBasedFile.h
casacore::RowBasedFile::WriteData
void WriteData(const unsigned char *data, uint64_t size)
Definition RowBasedFile.h:166
casacore::RowBasedFile::Seek
void Seek(off_t pos, int seek_direction)
Definition RowBasedFile.h:152
casacore::RowBasedFile::DataLocation
uint64_t DataLocation() const
Offset of the first row in the file.
Definition RowBasedFile.h:177
casacore::RowBasedFile::SetStride
void SetStride(uint64_t new_stride)
Set the number of bytes per row for this file.
Definition RowBasedFile.h:219
casacore::RowBasedFile::ReadHeader
void ReadHeader(unsigned char *data)
Read an optional extra header to the file.
Definition RowBasedFile.h:196
casacore::RowBasedFile::Filename
const std::string & Filename() const
Definition RowBasedFile.h:178
casacore::RowBasedFile::RowBasedFile
RowBasedFile()=default
casacore::RowBasedFile::operator=
RowBasedFile & operator=(RowBasedFile &&rhs)
Definition RowBasedFile.h:109
casacore::RowBasedFile::WriteHeader
void WriteHeader(const unsigned char *data)
Write an optional extra header to the file.
Definition RowBasedFile.h:189
casacore::RowBasedFile::AddRows
void AddRows(uint64_t n_rows)
Adds a given number of rows to the back of the file.
Definition RowBasedFile.h:232
casacore::RowBasedFile::NRows
uint64_t NRows() const
Total number of rows stored in this file.
Definition RowBasedFile.h:203
casacore::RowBasedFile::ReadData
void ReadData(unsigned char *data, uint64_t size)
Definition RowBasedFile.h:159
casacore::RowBasedFile::DeleteRow
void DeleteRow()
Deletes the last row.
Definition RowBasedFile.h:237
casacore::RowBasedFile::Stride
uint64_t Stride() const
Total number of bytes in one row.
Definition RowBasedFile.h:214
casacore::RowBasedFile::SetNRows
void SetNRows(uint64_t new_n_rows)
Definition RowBasedFile.h:205
casacore::RowBasedFile::Close
void Close()
Close the file.
Definition RowBasedFile.h:126
casacore::RowBasedFile::IsOpen
bool IsOpen() const
Definition RowBasedFile.h:172
casacore::SimpleColumnarFile::Write
void Write(uint64_t row, uint64_t column_offset, const bool *data, uint64_t n)
Definition SimpleColumnarFile.h:104
casacore::SimpleColumnarFile::ReadImplementation
void ReadImplementation(uint64_t row, uint64_t column_offset, ValueType *data, uint64_t n)
Definition SimpleColumnarFile.h:137
casacore::SimpleColumnarFile::Read
void Read(uint64_t row, uint64_t column_offset, std::complex< float > *data, uint64_t n)
Definition SimpleColumnarFile.h:60
casacore::SimpleColumnarFile::Read
void Read(uint64_t row, uint64_t column_offset, bool *data, uint64_t n)
Definition SimpleColumnarFile.h:73
casacore::SimpleColumnarFile::Read
void Read(uint64_t row, uint64_t column_offset, int32_t *data, uint64_t n)
Definition SimpleColumnarFile.h:70
casacore::SimpleColumnarFile::OpenExisting
static SimpleColumnarFile OpenExisting(const std::string &filename, size_t header_size)
Definition SimpleColumnarFile.h:55
casacore::SimpleColumnarFile::Read
void Read(uint64_t row, uint64_t column_offset, double *data, uint64_t n)
Definition SimpleColumnarFile.h:67
casacore::SimpleColumnarFile::WriteImplementation
void WriteImplementation(uint64_t row, uint64_t column_offset, const ValueType *data, uint64_t n)
Definition SimpleColumnarFile.h:149
casacore::SimpleColumnarFile::SimpleColumnarFile
SimpleColumnarFile(const std::string &filename, size_t header_size)
Open an existing columnar file.
Definition SimpleColumnarFile.h:131
casacore::SimpleColumnarFile::Write
void Write(uint64_t row, uint64_t column_offset, const std::complex< float > *data, uint64_t n)
Definition SimpleColumnarFile.h:84
casacore::SimpleColumnarFile::packed_buffer_
std::vector< unsigned char > packed_buffer_
This buffer is used temporarily for (un)packing booleans.
Definition SimpleColumnarFile.h:162
casacore::SimpleColumnarFile::Write
void Write(uint64_t row, uint64_t column_offset, const std::complex< double > *data, uint64_t n)
Definition SimpleColumnarFile.h:88
casacore::SimpleColumnarFile::Write
void Write(uint64_t row, uint64_t column_offset, const float *data, uint64_t n)
Definition SimpleColumnarFile.h:92
casacore::SimpleColumnarFile::SimpleColumnarFile
SimpleColumnarFile() noexcept=default
casacore::SimpleColumnarFile::Read
void Read(uint64_t row, uint64_t column_offset, float *data, uint64_t n)
Definition SimpleColumnarFile.h:64
casacore::SimpleColumnarFile::CreateNew
static SimpleColumnarFile CreateNew(const std::string &filename, uint64_t header_size, uint64_t stride)
Definition SimpleColumnarFile.h:50
casacore::SimpleColumnarFile::NRows
uint64_t NRows() const
Total number of rows stored in this file.
Definition RowBasedFile.h:203
casacore::SimpleColumnarFile::SetStride
void SetStride(uint64_t new_stride)
Set the number of bytes per row for this file.
Definition SimpleColumnarFile.h:118
casacore::SimpleColumnarFile::Write
void Write(uint64_t row, uint64_t column_offset, const int32_t *data, uint64_t n)
Definition SimpleColumnarFile.h:100
casacore::SimpleColumnarFile::Write
void Write(uint64_t row, uint64_t column_offset, const double *data, uint64_t n)
Definition SimpleColumnarFile.h:96
casacore::SimpleColumnarFile::SimpleColumnarFile
SimpleColumnarFile(const std::string &filename, uint64_t header_size, uint64_t stride)
Create or overwrite a new columnar file on disk.
Definition SimpleColumnarFile.h:125
casacore::SimpleColumnarFile::~SimpleColumnarFile
~SimpleColumnarFile() noexcept=default
casacore::SimpleColumnarFile::Stride
uint64_t Stride() const
Total number of bytes in one row.
Definition RowBasedFile.h:214
casacore
this file contains all the compiler specific defines
Definition mainpage.dox:28
casacore::move
void move(TYPE *target, int npixels) const
std
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
Definition Complex.h:350