Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
yhoogstrate committed Jan 22, 2023
1 parent 5d37a28 commit 97b6682
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 486 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ project(fastafs)
# Do this once in a while - find different compiler warnings
#set(CMAKE_CXX_COMPILER "clang++")

set(PROJECT_VERSION "1.9.0")
set(PROJECT_VERSION "1.10.0")
set(PACKAGE_URL "https://github.com/yhoogstrate/fastafs")
set(PACKAGE_BUGREPORT "${PACKAGE_URL}/issues")

Expand Down
7 changes: 7 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2023-01-22
* v.10.0
* Better ninja/meson support
* Code clean-ups
* Restructured the chunked_reader class and subclasses according to
the desgin patterns philisopy

2020-04-29 Youri Hoogstrate

* v1.9.0
Expand Down
49 changes: 0 additions & 49 deletions include/chunked_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,6 @@ enum compression_type : signed char { // dit is State
//url: https://refactoring.guru/design-patterns/state



class chunked_reader_old // dit is Context
{
private:
std::ifstream *fh_flat;
ZSTD_seekable_decompress_init_data* fh_zstd;

std::string filename; // try doing this with inode

compression_type filetype;

char buffer[READ_BUFFER_SIZE + 1];
size_t buffer_i;
size_t buffer_n;

off_t file_i;


public:
chunked_reader_old(char *); // filename
chunked_reader_old(const char *); // filename
~chunked_reader_old();

void init(); // generic tasks needed for init

void update_flat_buffer();
void update_zstd_buffer();


void find_filetype();

void set_filetype(compression_type);
compression_type get_filetype()
{
return this->filetype ;
};

size_t read(char *, size_t);// @deprecate
size_t read(unsigned char *, size_t);
unsigned char read();

void seek(off_t);
size_t tell();
//size_t size();
};




class chunked_reader;

class State
Expand Down
249 changes: 0 additions & 249 deletions src/chunked_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,255 +3,6 @@



chunked_reader_old::chunked_reader_old(char * afilename) :
fh_flat(nullptr), fh_zstd(nullptr), buffer_i(0), buffer_n(0), file_i(0)
{
this->filename = realpath_cpp(afilename);
this->init();
}

chunked_reader_old::chunked_reader_old(const char * afilename) :
fh_flat(nullptr), fh_zstd(nullptr), buffer_i(0), buffer_n(0), file_i(0)
{
this->filename = realpath_cpp(afilename);
this->init();
}

chunked_reader_old::~chunked_reader_old()
{
if(this->fh_flat != nullptr) {
if(this->fh_flat->is_open()) {
this->fh_flat->close();
}

delete this->fh_flat;
}

if(this->fh_zstd != nullptr) {
//ZSTD_seekable_free(this->fh_zstd->seekable);
fclose_orDie(this->fh_zstd->fin);

//delete this->fh_zstd->seekable;
//delete this->fh_zstd->fin;

delete this->fh_zstd;
}
}

void chunked_reader_old::init()
{
this->find_filetype();

switch(this->filetype) {

case uncompressed:
this->fh_flat = new std::ifstream;
this->fh_flat->open(this->filename.c_str(), std::ios::in | std::ios::binary | std::ios::ate);

if(this->fh_flat->is_open()) {
this->fh_flat->seekg(0, std::ios::beg);
this->update_flat_buffer();
} else {
throw std::runtime_error("[chunked_reader_old::init] Cannot open file for reading.\n");
}
break;

case zstd:
//printf("[chunked_reader_old::init()] - init ZSTD_seekable_decompress_init_data* fh_zstd; \n");
this->fh_zstd = ZSTD_seekable_decompressFile_init(this->filename.c_str());
// make zstd handle - to be implemented later on
//ZSTD_seekable_decompress_data
break;

default:
throw std::runtime_error("[chunked_reader_old::init] Should never happen - but avoids compiler warning.\n");
break;
}
}

void chunked_reader_old::find_filetype()
{
if(is_zstd_file((const char*) this->filename.c_str())) {
this->set_filetype(zstd);
} else {
this->set_filetype(uncompressed);
}
}

void chunked_reader_old::set_filetype(compression_type arg_filetype)
{
this->filetype = arg_filetype;
}


void set_filtetype(compression_type &filetype_arg);


size_t chunked_reader_old::read(char *arg_buffer, size_t buffer_size)
{
buffer_size = std::min(buffer_size, (size_t) READ_BUFFER_SIZE);
size_t written = 0;

while(this->buffer_i < this->buffer_n and written < buffer_size) {
arg_buffer[written++] = this->buffer[this->buffer_i++];
}



/*
size_t n = std::min(this->buffer_n - this->buffer_i, buffer_size - written);
memcpy(&arg_buffer[written], &this->buffer[this->buffer_i] , n);
written += n;
this->buffer_i += n;
*/

if(written < buffer_size) {
// overwrite buffer
switch(this->filetype) {
case uncompressed:
this->update_flat_buffer();
break;
case zstd:
this->update_zstd_buffer();
break;
default:
throw std::runtime_error("[chunked_reader_old::read] reading from uninitialized object\n");
break;
}

// same loop again
while(this->buffer_i < this->buffer_n and written < buffer_size) {
arg_buffer[written++] = this->buffer[this->buffer_i++];
}
/* - somehow memcpy is slightly slower - test again @ mom laptop
size_t n = std::min(this->buffer_n - this->buffer_i, buffer_size - written);
memcpy(&arg_buffer[written], &this->buffer[this->buffer_i] , n);
written += n;
this->buffer_i += n;
*/
}

return written;
}





size_t chunked_reader_old::read(unsigned char *arg_buffer, size_t buffer_size)
{


buffer_size = std::min(buffer_size, (size_t) READ_BUFFER_SIZE);
size_t written = 0;

while(this->buffer_i < this->buffer_n and written < buffer_size) {
arg_buffer[written++] = this->buffer[this->buffer_i++];
}


if(written < buffer_size) {
// overwrite buffer
switch(this->filetype) {
case uncompressed:
this->update_flat_buffer();
break;
case zstd:
this->update_zstd_buffer();
break;
default:
throw std::runtime_error("[chunked_reader_old::read] reading from uninitialized object\n");
break;
}

// same loop again
while(this->buffer_i < this->buffer_n and written < buffer_size) {
arg_buffer[written++] = this->buffer[this->buffer_i++];
}
}

return written;
}



// reads single byte from the buffer
unsigned char chunked_reader_old::read()
{
if(this->buffer_i >= this->buffer_n) {
switch(this->filetype) {
case uncompressed:
this->update_flat_buffer();
break;
case zstd:
this->update_zstd_buffer();
break;
default:
throw std::runtime_error("[chunked_reader_old::read] reading from uninitialized object\n");
break;
}
}

return this->buffer[this->buffer_i++];
}




void chunked_reader_old::update_flat_buffer()
{
this->fh_flat->read(this->buffer, READ_BUFFER_SIZE);

this->buffer_i = 0;
this->buffer_n = (size_t) this->fh_flat->gcount();
this->file_i += this->buffer_n;
}


void chunked_reader_old::update_zstd_buffer()
{
//size_t written = ZSTD_seekable_decompressFile_orDie(this->filename.c_str(), this->file_i, this->buffer, this->file_i + READ_BUFFER_SIZE);
size_t written = ZSTD_seekable_decompressFile_orDie(this->fh_zstd, this->file_i, this->buffer, this->file_i + READ_BUFFER_SIZE);

this->buffer_i = 0;
this->buffer_n = written;
this->file_i += written;
}



void chunked_reader_old::seek(off_t offset)
{
this->file_i = offset;

switch(this->filetype) {
case uncompressed:
this->fh_flat->clear(); // reset error state

if(!this->fh_flat->is_open()) {
this->fh_flat->open(this->filename.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
}

this->fh_flat->seekg(offset, std::ios::beg);
this->update_flat_buffer();
break;
default:
this->update_zstd_buffer();
break;
}
}


size_t chunked_reader_old::tell()
{
//@todo decide what to return when out of bound
//e.g. when exceeding file size

return this->file_i - this->buffer_n + this->buffer_i;
}



void State::set_context(chunked_reader *arg_context)
{
this->context = arg_context;
Expand Down
4 changes: 2 additions & 2 deletions src/fuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static int do_getattr(const char *path, struct stat *st)



static int do_readdir(const char *path, void *buffer, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
static int do_readdir(const char *path, void *buffer, fuse_fill_dir_t filler, __attribute__((__unused__)) off_t offset, __attribute__((__unused__)) struct fuse_file_info *fi)
{
fuse_instance *ffi = static_cast<fuse_instance *>(fuse_get_context()->private_data);

Expand Down Expand Up @@ -272,7 +272,7 @@ static int do_open(const char *path, struct fuse_file_info *fi)
return 0;
}

static int do_flush(const char *path, struct fuse_file_info *fi)
static int do_flush(const char *path, __attribute__((__unused__)) struct fuse_file_info *fi)
{
return 0;
}
Expand Down
Loading

0 comments on commit 97b6682

Please sign in to comment.