forked from facebookarchive/scribe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHdfsFile.cpp
More file actions
301 lines (258 loc) · 7.2 KB
/
Copy pathHdfsFile.cpp
File metadata and controls
301 lines (258 loc) · 7.2 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// 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 <boost/scoped_ptr.hpp>
#include "Common.h"
#include "FileInterface.h"
#include "HdfsFile.h"
using namespace boost;
using scoped_ptr;
namespace scribe {
HdfsFile::HdfsFile(const string& name)
: FileInterface(name, false), inputBuffer_(NULL), bufferSize_(0) {
LOG_OPER("[hdfs] Connecting to HDFS for %s", name.c_str());
// Attempt to parse the hdfs cluster from the path name specified.
fileSys_ = connectToPath(name);
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;
}
// true for existence
// false for not absence
// throws exception for error
bool HdfsFile::exists() {
if (!fileSys_) {
throw std::runtime_error("No Filesys in HdfsFile::exists");
}
int value = hdfsExists(fileSys_, filename_.c_str());
if (value == 0) {
return true;
} else if (value == 1) {
return false;
} else {
throw std::runtime_error(kErrorHdfsExists);
}
}
bool HdfsFile::openRead() {
if (!fileSys_) {
fileSys_ = connectToPath(filename_);
}
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_);
}
if (!fileSys_) {
LOG_OPER("[hdfs] cannot open hdfs for write %s", filename_.c_str());
return false;
}
if (hfile_) {
LOG_OPER("[hdfs] already opened for write %s", filename_.c_str());
return false;
}
int ret = hdfsExists(fileSys_, filename.c_str());
if (0 == ret) {
flags = O_WRONLY|O_APPEND; // file exists, append to it.
} else if (1 == ret) {
flags = O_WRONLY;
}
else {
throw std::runtime_error(kErrorHdfsExists);
}
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_);
LOG_OPER("[hdfs] closed %s", filename_.c_str());
} else {
LOG_OPER("[hdfs] No hfile_! So no write/flush!");
}
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 string& data) {
if (!isOpen()) {
bool success = openWrite();
if (!success) {
return false;
}
}
tSize bytesWritten = hdfsWrite(fileSys_, hfile_, data.data(),
(tSize) data.length());
return (bytesWritten == (tSize) data.length()) ? true : false;
}
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 string& path, vector<string>* files) {
if (!fileSys_) {
return;
}
int value = hdfsExists(fileSys_, path.c_str());
switch (value) {
case 0: {
int numEntries = 0;
hdfsFileInfo* pHdfsFileInfo = 0;
pHdfsFileInfo = hdfsListDirectory(fileSys_, path.c_str(), &numEntries);
if (numEntries >= 0) {
for(int i = 0; i < numEntries; i++) {
char* pathname = pHdfsFileInfo[i].mName;
char* filename = rindex(pathname, '/');
if (filename != NULL) {
files->push_back(filename+1);
}
}
if (pHdfsFileInfo != NULL) {
hdfsFreeFileInfo(pHdfsFileInfo, numEntries);
}
} else {
// numEntries < 0 indicates error
throw std::runtime_error(
string("hdfsListDirectory call failed with error ") +
boost::lexical_cast<string>(errno));
}
break;
}
case 1:
// directory does not exist, do nothing
break;
// anything else should be an error
default:
throw std::runtime_error(kErrorHdfsExists);
}
}
long HdfsFile::readNext(string* item) {
/* choose a reasonable value for loss */
return (-1000 * 1000 * 1000);
}
string HdfsFile::getFrame(unsigned dataLength) {
return string(); // not supported
}
bool HdfsFile::createDirectory(const 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(const string& oldPath, const string& newPath) {
LOG_OPER("[hdfs] Creating symlink oldpath %s newpath %s",
oldPath.c_str(), newPath.c_str());
scoped_ptr<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
* else connect to default
*/
hdfsFS HdfsFile::connectToPath(const string& uri) {
if (uri.empty()) {
return NULL;
}
if (uri.find(kProto) != 0) {
// uri doesn't start with hdfs:// -> use default:0, which is special
// to libhdfs.
return hdfsConnectNewInstance("default", 0);
}
vector <string> parts;
split(parts, uri, is_any_of(":/"), token_compress_on);
if (parts.size() < 3) {
return NULL;
}
// parts[1] = hosts, parts[2] = port
string host(parts[1]);
tPort port;
try {
port = lexical_cast<tPort>(parts[2]);
} catch(const bad_lexical_cast&) {
LOG_OPER("[hdfs] lexical cast failed");
return NULL;
}
LOG_OPER("[hdfs] Before hdfsConnectNewInstance(%s, %li)", host, port);
hdfsFS fs = hdfsConnectNewInstance(host.c_str(), port);
LOG_OPER("[hdfs] After hdfsConnectNewInstance");
return fs;
}
} //! namespace scribe