add contextManager to hanldle files

This commit is contained in:
Bechir Braham 2024-03-22 15:02:15 +01:00
parent e7bf408a21
commit d0151153fb
No known key found for this signature in database
GPG Key ID: 7F511B55FD8E9671
17 changed files with 105 additions and 81 deletions

4
.vscode/launch.json vendored
View File

@ -4,11 +4,11 @@
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/aare",
"program": "${workspaceFolder}/build/examples/json_example",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"environment": [{"name": "PROJECT_ROOT_DIR", "value": "/home/l_bechir/github/aare"}],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [

View File

@ -38,6 +38,15 @@ class Frame {
std::memcpy(m_data, other.m_data, m_rows * m_cols * m_bitdepth / 8);
return *this;
}
// add move constructor
Frame(Frame &&other) {
m_rows = other.rows();
m_cols = other.cols();
m_bitdepth = other.bitdepth();
m_data = other.m_data;
other.m_data = nullptr;
other.m_rows = other.m_cols = other.m_bitdepth = 0;
}
template <typename T>
NDView<T> view() {

View File

@ -10,4 +10,3 @@ target_link_libraries(numpy_example PUBLIC aare)

View File

@ -1,30 +1,29 @@
// Your First C++ Program
#include "aare/FileHandler.hpp"
#include "aare/ContextManager.hpp"
#include <iostream>
#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"
void test(FileHandler *f, int frame_number) {
void test(File *f, int frame_number) {
std::cout << "frame number: " << frame_number << std::endl;
Frame *frame = f->get_frame(frame_number);
std::cout << *((uint16_t *)frame->get(0, 0)) << std::endl;
std::cout << *((uint16_t *)frame->get(0, 1)) << std::endl;
std::cout << *((uint16_t *)frame->get(1, 0)) << std::endl;
std::cout << *((uint16_t *)frame->get(511, 1023)) << std::endl;
delete frame;
Frame frame = f->get_frame(frame_number);
std::cout << *((uint16_t *)frame.get(0, 0)) << std::endl;
std::cout << *((uint16_t *)frame.get(0, 1)) << std::endl;
std::cout << *((uint16_t *)frame.get(1, 0)) << std::endl;
std::cout << *((uint16_t *)frame.get(511, 1023)) << std::endl;
}
int main() {
ContextManager ctx_manager;
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "jungfrau_single_master_0.json");
// std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "test_numpy_file.npy");
std::cout << fpath << std::endl;
auto fileHandler = new FileHandler(fpath);
test(fileHandler, 0);
test(fileHandler, 2);
test(fileHandler, 9);
delete fileHandler;
}
File *file = ctx_manager.get_file(fpath);
test(file, 0);
test(file, 2);
test(file, 9);
}

View File

@ -1,30 +1,32 @@
// Your First C++ Program
#include "aare/FileHandler.hpp"
#include "aare/ContextManager.hpp"
#include <iostream>
#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"
void test(FileHandler *f, int frame_number) {
void test(File* f, int frame_number) {
std::cout << "frame number: " << frame_number << std::endl;
Frame *frame = f->get_frame(frame_number);
std::cout << *((uint16_t *)frame->get(0, 0)) << std::endl;
std::cout << *((uint16_t *)frame->get(0, 1)) << std::endl;
std::cout << *((uint16_t *)frame->get(1, 0)) << std::endl;
std::cout << *((uint16_t *)frame->get(49, 49)) << std::endl;
Frame frame = f->get_frame(frame_number);
std::cout << *((uint16_t *)frame.get(0, 0)) << std::endl;
std::cout << *((uint16_t *)frame.get(0, 1)) << std::endl;
std::cout << *((uint16_t *)frame.get(1, 0)) << std::endl;
std::cout << *((uint16_t *)frame.get(49, 49)) << std::endl;
delete frame;
}
int main() {
ContextManager ctx_manager;
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
// std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "jungfrau_single_master_0.json");
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "test_numpy_file.npy");
std::cout << fpath << std::endl;
auto fileHandler = new FileHandler(fpath);
test(fileHandler, 0);
test(fileHandler, 2);
test(fileHandler, 24);
File* file = ctx_manager.get_file(fpath);
test(file, 0);
test(file, 2);
test(file, 24);
delete fileHandler;
}

View File

@ -0,0 +1,34 @@
#include "aare/File.hpp"
#include "aare/FileFactory.hpp"
#include <filesystem>
#include <memory>
/**
* @brief A class to manage the context of the files and network connections
* closes the files and network connections when the context is destroyed
*/
class ContextManager {
public:
ContextManager() = default;
File *get_file(std::filesystem::path fname) {
auto tmp_file = FileFactory::load_file(fname);
this->files.push_back(tmp_file);
return tmp_file;
}
// prevent default copying, it can delete the file twice
ContextManager(const ContextManager &) = delete;
ContextManager &operator=(const ContextManager &) = delete;
~ContextManager() {
for (auto f : files) {
delete f;
}
}
private:
std::vector<File *> files;
// std::vector<NetworkConnection*> connections;
};

View File

@ -1,24 +1,22 @@
#pragma once
#include "aare/defs.hpp"
#include "aare/Frame.hpp"
#include "SubFile.hpp"
#include "aare/Frame.hpp"
#include "aare/defs.hpp"
#include <filesystem>
#include <fmt/core.h>
#include <iostream>
class File {
public:
virtual Frame* get_frame(size_t frame_number) = 0;
virtual Frame get_frame(size_t frame_number) =0;
private:
//comment
// comment
public:
virtual ~File() = default;
virtual ~File() = default;
std::filesystem::path fname;
std::filesystem::path base_path;
std::string base_name, ext;
@ -35,11 +33,9 @@ class File {
ssize_t cols{};
ssize_t bitdepth{};
// File();
inline size_t bytes_per_frame() const { return rows * cols * bitdepth / 8; }
inline size_t pixels() const { return rows * cols; }
// size_t total_frames();
};

View File

@ -9,6 +9,12 @@ class FileFactory{
public:
static FileFactory* get_factory(std::filesystem::path);
// virtual int deleteFile() = 0;
static File* load_file(std::filesystem::path p){
auto factory = get_factory(p);
File* tmp= factory->load_file();
delete factory;
return tmp;
};
virtual File* load_file()=0;//TODO: add option to load all file to memory or keep it on disk
virtual void parse_metadata(File*)=0;
virtual void parse_fname(File*)=0;

View File

@ -1,28 +0,0 @@
#include <filesystem>
#include "aare/FileFactory.hpp"
#include "aare/File.hpp"
class FileHandler{
private:
std::filesystem::path fpath;
FileFactory* fileFactory;
File* f;
public:
FileHandler(std::filesystem::path fname){
this->fpath = fname;
this->fileFactory= FileFactory::get_factory(fname);
this->f= fileFactory->load_file();
delete fileFactory;
}
Frame* get_frame(int index){
return f->get_frame(index);
}
~FileHandler(){
delete f;
}
};

View File

@ -9,7 +9,7 @@ class JsonFile : public File {
using config = RawFileConfig;
public:
Frame *get_frame(size_t frame_number);
Frame get_frame(size_t frame_number);
int n_subfiles;
std::vector<SubFile *> subfiles;
int subfile_rows, subfile_cols;

View File

@ -1,11 +1,12 @@
#include "aare/FileFactory.hpp"
#include "aare/JsonFile.hpp"
class JsonFileFactory: public FileFactory
{
private:
/* data */
public:
JsonFileFactory(std::filesystem::path fpath);
File* load_file() override;
virtual JsonFile* load_file() override;
void parse_metadata(File*) override;
void parse_fname(File*) override;
void open_subfiles(File*);

View File

@ -11,7 +11,7 @@ class NumpyFile : public File {
public:
NumpyFile(std::filesystem::path fname);
Frame *get_frame(size_t frame_number) override;
Frame get_frame(size_t frame_number) override;
header_t header{};
static constexpr std::array<char, 6> magic_str{'\x93', 'N', 'U', 'M', 'P', 'Y'};
uint8_t major_ver_{};
@ -24,4 +24,10 @@ class NumpyFile : public File {
return std::accumulate(header.shape.begin() + 1, header.shape.end(), 1, std::multiplies<uint64_t>());
};
inline ssize_t bytes_per_frame() { return header.dtype.itemsize * pixels_per_frame(); };
~NumpyFile(){
if (fp != nullptr) {
fclose(fp);
}
}
};

View File

@ -14,7 +14,7 @@ class NumpyFileFactory : public FileFactory {
public:
NumpyFileFactory(std::filesystem::path fpath);
void parse_metadata(File *_file) override;
File *load_file() override;
NumpyFile* load_file() override;
void parse_fname(File*){};
};

View File

@ -1,6 +1,6 @@
#include "aare/JsonFile.hpp"
Frame *JsonFile::get_frame(size_t frame_number) {
Frame JsonFile::get_frame(size_t frame_number) {
if (frame_number > this->total_frames) {
throw std::runtime_error("Frame number out of range");
}
@ -9,7 +9,7 @@ Frame *JsonFile::get_frame(size_t frame_number) {
size_t frame_size = this->subfiles[subfile_id]->bytes_per_frame();
buffer = new std::byte[frame_size];
this->subfiles[subfile_id]->get_frame(buffer, frame_number % this->max_frames_per_file);
auto f = new Frame(buffer, this->rows, this->cols, this->bitdepth );
auto f = Frame(buffer, this->rows, this->cols, this->bitdepth );
delete[] buffer;

View File

@ -49,8 +49,8 @@ void JsonFileFactory::open_subfiles(File*_file) {
}
}
File* JsonFileFactory::load_file() {
JsonFile *file = new JsonFile();
JsonFile* JsonFileFactory::load_file() {
JsonFile* file = new JsonFile();
file->fname = this->m_fpath;
this->parse_fname(file);
this->parse_metadata(file);

View File

@ -6,16 +6,16 @@ NumpyFile::NumpyFile(std::filesystem::path fname_){
fp = fopen(this->fname.c_str(), "rb");
}
Frame* NumpyFile::get_frame(size_t frame_number) {
Frame NumpyFile::get_frame(size_t frame_number) {
if (fp == nullptr) {
throw std::runtime_error("File not open");
}
if (frame_number > header.shape[0]) {
throw std::runtime_error("Frame number out of range");
}
Frame *frame = new Frame(header.shape[1], header.shape[2], header.dtype.itemsize*8);
Frame frame = Frame(header.shape[1], header.shape[2], header.dtype.itemsize*8);
fseek(fp, header_size + frame_number * bytes_per_frame(), SEEK_SET);
fread(frame->_get_data(), bytes_per_frame(), 1, fp);
fread(frame._get_data(), bytes_per_frame(), 1, fp);
return frame;
}

View File

@ -72,8 +72,8 @@ void NumpyFileFactory::parse_metadata(File *_file) {
file->header = {dtype, fortran_order, shape};
}
File* NumpyFileFactory::load_file() {
NumpyFile *file = new NumpyFile(this->m_fpath);
NumpyFile* NumpyFileFactory::load_file() {
NumpyFile* file = new NumpyFile(this->m_fpath);
parse_metadata(file);
std::cout << "parsed header: " << file->header.to_string() << std::endl;
return file;