block_hdfs.hpp 2.32 KiB
#ifndef TC_BLOCK_HDFS
#define TC_BLOCK_HDFS
#include <cstddef>
#include <fstream>
#include <cstring>
#include "hdfs.h"
#include "common/block.hpp"
#include "../stats/stats.hpp"
#include "container.hpp"
//block_sv general logger
static LoggerPtr bh_logger(Logger::getLogger( "block.hdfs"));
namespace aspide {
	class block_sv_hdfs{
		private:
			//HDFS Block size
			long long block_size;
			//Reference to the hdfs file
			hdfsFile & data;
			//Pointer to the HDFS filesystem
			hdfsFS * fs_ref;
			//Stats reference
			stats * _stat;
		public:
			//Constructor
			block_sv_hdfs(long long size, hdfsFile & container, hdfsFS * fs, stats * _stat) : block_size(size), data(container), fs_ref(fs), _stat{_stat} {LOG4CXX_INFO(bh_logger, "HDFS block_sv created on file=" << container);}
			//Destructor
			~block_sv_hdfs()=default;
			//Iterator
			class iterator : public std::iterator<std::input_iterator_tag, block>{
				private:
					//Buffer to store the current block
					char * raw;
					//outer reference
					block_sv_hdfs & outer;
					//Real bytes, i.e the actual size of the buffer (may read lees than b.size)
					size_t rbytes;
					//Flag to set the end of file
					bool _eof;
					//Flag to set the number of block (to compare iterators)
					unsigned long long num_blk;
				public:
					//The constructor allocates the buffer
					iterator(block_sv_hdfs & blk, bool is_end=false) : outer(blk), _eof(is_end), num_blk(0){raw = new char[outer.block_size]; LOG4CXX_INFO(bh_logger, "HDFS block_sv iterator created"); ++*this;}
					//Move constructor
					iterator(iterator && rhs) : outer(rhs.outer), _eof(rhs._eof), num_blk(rhs.num_blk), rbytes(rhs.rbytes), raw{rhs.raw}{
						//Allocate
						rhs.raw = nullptr;
					//Destructor
					~iterator(){delete [] raw; LOG4CXX_DEBUG(bh_logger, "HDFS block_sv iterator deleted, Pointer"<< this);}
					//iterator operands
	    			block operator*() const;
	    			//Operator to acquire the next line
	    			iterator & operator++();
7172737475767778798081828384858687
//Boolean comparison operators bool operator==(const iterator & rhs) const; bool operator!=(const iterator & rhs) { return !(*this == rhs);} }; //Get an iterator to the first element iterator begin(){ return iterator(*this);} //Get an iterator to the last element iterator end(){return iterator(*this,true);} }; } #endif