This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 792
/
HdfsFile.cpp
259 lines (227 loc) · 6.67 KB
/
HdfsFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2009- Facebook
// Distributed under the Scribe Software License
//
// See accompanying file LICENSE or visit the Scribe site at:
// http://developers.facebook.com/scribe/
//
#include <limits>
#include "common.h"
#include "file.h"
#include "HdfsFile.h"
using namespace std;
HdfsFile::HdfsFile(const std::string& name) : FileInterface(name, false), inputBuffer_(NULL), bufferSize_(0) {
LOG_OPER("[hdfs] Connecting to HDFS for %s", name.c_str());
// First attempt to parse the hdfs cluster from the path name specified.
// If it fails, then use the default hdfs cluster.
fileSys = connectToPath(name.c_str());
if (fileSys == 0) {
// ideally, we should throw an exception here, but the scribe store code
// does not handle this elegantly now.
LOG_OPER("[hdfs] ERROR: HDFS is not configured for file: %s", name.c_str());
}
hfile = 0;
}
HdfsFile::~HdfsFile() {
if (fileSys) {
LOG_OPER("[hdfs] disconnecting fileSys for %s", filename.c_str());
hdfsDisconnect(fileSys);
LOG_OPER("[hdfs] disconnected fileSys for %s", filename.c_str());
}
fileSys = 0;
hfile = 0;
}
bool HdfsFile::openRead() {
if (!fileSys) {
fileSys = connectToPath(filename.c_str());
}
if (fileSys) {
hfile = hdfsOpenFile(fileSys, filename.c_str(), O_RDONLY, 0, 0, 0);
}
if (hfile) {
LOG_OPER("[hdfs] opened for read %s", filename.c_str());
return true;
}
return false;
}
bool HdfsFile::openWrite() {
int flags;
if (!fileSys) {
fileSys = connectToPath(filename.c_str());
}
if (!fileSys) {
return false;
}
if (hfile) {
LOG_OPER("[hdfs] already opened for write %s", filename.c_str());
return false;
}
if (hdfsExists(fileSys, filename.c_str()) == 0) {
flags = O_WRONLY|O_APPEND; // file exists, append to it.
} else {
flags = O_WRONLY;
}
hfile = hdfsOpenFile(fileSys, filename.c_str(), flags, 0, 0, 0);
if (hfile) {
if (flags & O_APPEND) {
LOG_OPER("[hdfs] opened for append %s", filename.c_str());
} else {
LOG_OPER("[hdfs] opened for write %s", filename.c_str());
}
return true;
}
return false;
}
bool HdfsFile::openTruncate() {
LOG_OPER("[hdfs] truncate %s", filename.c_str());
deleteFile();
return openWrite();
}
bool HdfsFile::isOpen() {
bool retVal = (hfile) ? true : false;
return retVal;
}
void HdfsFile::close() {
if (fileSys) {
if (hfile) {
LOG_OPER("[hdfs] closing %s", filename.c_str());
hdfsCloseFile(fileSys, hfile );
}
hfile = 0;
// Close the file system
LOG_OPER("[hdfs] disconnecting fileSys for %s", filename.c_str());
hdfsDisconnect(fileSys);
LOG_OPER("[hdfs] disconnected fileSys for %s", filename.c_str());
fileSys = 0;
}
}
bool HdfsFile::write(const std::string& data) {
if (!isOpen()) {
bool success = openWrite();
if (!success) {
return false;
}
}
tSize bytesWritten = hdfsWrite(fileSys, hfile, data.data(),
(tSize) data.length());
bool retVal = (bytesWritten == (tSize) data.length()) ? true : false;
return retVal;
}
void HdfsFile::flush() {
if (hfile) {
hdfsFlush(fileSys, hfile);
}
}
unsigned long HdfsFile::fileSize() {
long size = 0L;
if (fileSys) {
hdfsFileInfo* pFileInfo = hdfsGetPathInfo(fileSys, filename.c_str());
if (pFileInfo != NULL) {
size = pFileInfo->mSize;
hdfsFreeFileInfo(pFileInfo, 1);
}
}
return size;
}
void HdfsFile::deleteFile() {
if (fileSys) {
hdfsDelete(fileSys, filename.c_str());
}
LOG_OPER("[hdfs] deleteFile %s", filename.c_str());
}
void HdfsFile::listImpl(const std::string& path,
std::vector<std::string>& _return) {
if (!fileSys) {
return;
}
int value = hdfsExists(fileSys, path.c_str());
if (value == 0) {
int numEntries = 0;
hdfsFileInfo* pHdfsFileInfo = 0;
pHdfsFileInfo = hdfsListDirectory(fileSys, path.c_str(), &numEntries);
if (pHdfsFileInfo) {
for(int i = 0; i < numEntries; i++) {
char* pathname = pHdfsFileInfo[i].mName;
char* filename = rindex(pathname, '/');
if (filename != NULL) {
_return.push_back(filename+1);
}
}
hdfsFreeFileInfo(pHdfsFileInfo, numEntries);
// A NULL indicates error
} else {
throw std::runtime_error("hdfsListDirectory call failed");
}
} else if (value == -1) {
throw std::runtime_error("hdfsExists call failed");
}
}
long HdfsFile::readNext(std::string& _return) {
/* choose a reasonable value for loss */
return (-1000 * 1000 * 1000);
}
string HdfsFile::getFrame(unsigned data_length) {
return std::string(); // not supported
}
bool HdfsFile::createDirectory(std::string path) {
// opening the file will create the directories.
return true;
}
/**
* HDFS currently does not support symlinks. So we create a
* normal file and write the symlink data into it
*/
bool HdfsFile::createSymlink(std::string oldpath, std::string newpath) {
LOG_OPER("[hdfs] Creating symlink oldpath %s newpath %s",
oldpath.c_str(), newpath.c_str());
HdfsFile* link = new HdfsFile(newpath);
if (link->openWrite() == false) {
LOG_OPER("[hdfs] Creating symlink failed because %s already exists.",
newpath.c_str());
return false;
}
if (link->write(oldpath) == false) {
LOG_OPER("[hdfs] Writing symlink %s failed", newpath.c_str());
return false;
}
link->close();
return true;
}
/**
* If the URI is specified of the form
* hdfs://server::port/path, then connect to the
* specified cluster
*/
hdfsFS HdfsFile::connectToPath(const char* uri) {
const char proto[] = "hdfs://";
if (strncmp(proto, uri, strlen(proto)) != 0) {
// uri doesn't start with hdfs:// -> use default:0, which is special
// to libhdfs.
return hdfsConnectNewInstance("default", 0);
}
// Skip the hdfs:// part.
uri += strlen(proto);
// Find the next colon.
const char* colon = strchr(uri, ':');
// No ':' or ':' is the last character.
if (!colon || !colon[1]) {
LOG_OPER("[hdfs] Missing port specification: \"%s\"", uri);
return NULL;
}
char* endptr = NULL;
const long port = strtol(colon + 1, &endptr, 10);
if (port < 0) {
LOG_OPER("[hdfs] Invalid port specification (negative): \"%s\"", uri);
return NULL;
} else if (port > std::numeric_limits<tPort>::max()) {
LOG_OPER("[hdfs] Invalid port specification (out of range): \"%s\"", uri);
return NULL;
}
char* const host = (char*) malloc(colon - uri + 1);
memcpy((char*) host, uri, colon - uri);
host[colon - uri] = '\0';
LOG_OPER("[hdfs] Before hdfsConnectNewInstance(%s, %li)", host, port);
hdfsFS fs = hdfsConnectNewInstance(host, port);
LOG_OPER("[hdfs] After hdfsConnectNewInstance");
free(host);
return fs;
}