17 #include <boost/thread/locks.hpp>
18 #include <gutil/strings/substitute.h>
28 using namespace strings;
32 scoped_ptr<HdfsFsCache> HdfsFsCache::instance_;
34 void HdfsFsCache::Init() {
35 DCHECK(HdfsFsCache::instance_.
get() == NULL);
39 Status HdfsFsCache::GetConnection(
const string&
path, hdfsFS* fs,
42 const string& namenode = GetNameNodeFromPath(path, &err);
43 if (!err.empty())
return Status(err);
44 DCHECK(!namenode.empty());
47 if (local_cache != NULL) {
48 HdfsFsMap::iterator local_iter = local_cache->find(namenode);
49 if (local_iter != local_cache->end()) {
50 *fs = local_iter->second;
56 lock_guard<mutex> l(
lock_);
57 HdfsFsMap::iterator i = fs_map_.find(namenode);
58 if (i == fs_map_.end()) {
59 hdfsBuilder* hdfs_builder = hdfsNewBuilder();
60 hdfsBuilderSetNameNode(hdfs_builder, namenode.c_str());
61 *fs = hdfsBuilderConnect(hdfs_builder);
65 fs_map_.insert(make_pair(namenode, *fs));
72 if (local_cache != NULL) {
73 local_cache->insert(make_pair(namenode, *fs));
78 Status HdfsFsCache::GetLocalConnection(hdfsFS* fs) {
79 return GetConnection(
"file:///", fs);
82 string HdfsFsCache::GetNameNodeFromPath(
const string&
path,
string* err) {
84 const string local_fs(
"file:/");
85 size_t n = path.find(
"://");
88 if (n == string::npos) {
89 if (path.compare(0, local_fs.length(), local_fs) == 0) {
91 namenode =
"file:///";
97 *err = Substitute(
"Path missing scheme: $0", path);
101 n = path.find(
'/', n + 3);
102 if (n == string::npos) {
103 *err = Substitute(
"Path missing '/' after authority: $0", path);
106 namenode = path.substr(0, n + 1);
string path("/usr/lib/sasl2:/usr/lib64/sasl2:/usr/local/lib/sasl2:/usr/lib/x86_64-linux-gnu/sasl2")
boost::unordered_map< std::string, hdfsFS > HdfsFsMap
string GetHdfsErrorMsg(const string &prefix, const string &file)