6 #include <unordered_map> 9 #include "hdfsEngine.h" 11 faster::hdfsEngine::hdfsEngine(){
13 _fs = hdfsConnect(
"default", 0);
16 faster::hdfsEngine::~hdfsEngine(){
17 hdfsDisconnect((hdfsFS) _fs);
20 bool faster::hdfsEngine::isReady(){
24 bool faster::hdfsEngine::isConnected(){
29 return hdfsFile((hdfsFS) _fs, path, mode);
36 void faster::hdfsEngine::del(std::string path){
37 hdfsDelete((::hdfsFS)_fs, path.data(), 1);
40 bool faster::hdfsEngine::exists(std::string path){
41 int r = hdfsExists((::hdfsFS) _fs, path.data());
45 faster::hdfsFile::hdfsFile(
void * fs, std::string & path, fileMode mode){
48 _f = hdfsOpenFile((::hdfsFS) fs, path.data(), mode,0,0,0);
50 _buffer.resize(128*1024);
53 faster::hdfsFile::~hdfsFile(){
56 void faster::hdfsFile::close(){
58 hdfsCloseFile((::hdfsFS) _fs, (::hdfsFile) _f);
63 size_t faster::hdfsFile::read(
char * v,
size_t n){
64 return hdfsRead((::hdfsFS)_fs, (::hdfsFile)_f, (
void*) v, n);
67 size_t faster::hdfsFile::write(
char * v,
size_t n){
68 return hdfsWrite((::hdfsFS)_fs, (::hdfsFile)_f, (
void*) v, n);
71 size_t faster::hdfsFile::seek(
size_t offset){
72 return hdfsSeek((::hdfsFS)_fs, (::hdfsFile)_f, (tOffset) offset);
75 size_t faster::hdfsFile::readLine(
char * v,
size_t n,
char sep){
82 m = read(buffer, std::min(1024, (
int) n - count));
85 while ((c != sep) && (k < m)){
90 std::copy_n(buffer, k, &v[count]);
101 std:: vector<std::deque<int>> faster::hdfsFile::getBlocksLocations(){
102 std::vector<std::deque<int>> loc;
103 std::unordered_map<std::string,int> hostMap;
107 hdfsFileInfo * info = hdfsGetPathInfo((::hdfsFS)_fs, _path.data());
109 std::cerr <<
"Getting Blocks Locations for: " << _path <<
" (" << info->mSize <<
" bytes)";
110 char *** blocksLoc = hdfsGetHosts((::hdfsFS)_fs, _path.data(), 0, info->mSize);
111 int numBlocks = std::ceil((
float)info->mSize/info->mBlockSize);
112 loc.resize(numBlocks);
115 for (
int i = 0; i < numBlocks ; i++){
118 char ** hostList = blocksLoc[i];
119 std::cerr <<
" (" << i <<
") ";
120 while ( hostList[j] != NULL ){
121 std::cerr << hostList[j] <<
" ";
122 auto search = hostMap.find(hostList[j]);
123 if ( search == hostMap.end()){
124 hostMap[hostList[j]] = hostsMapped ++;
126 auto & locList = loc[i];
127 locList.insert(locList.end(), hostMap[hostList[j]]);
133 for (
int i = 0; i < numBlocks ; i++){
134 std::cerr <<
" (" << i <<
") ";
135 for (
auto it = loc[i].begin() ; it != loc[i].end(); it++){
136 std::cerr << *it <<
" ";
142 hdfsFreeFileInfo(info,1);
143 hdfsFreeHosts(blocksLoc);
148 void faster::hdfsFile::del(){
150 hdfsDelete((::hdfsFS)_fs, _path.data(), 1);