31 #include <boost/algorithm/string.hpp>
32 #include <boost/tokenizer.hpp>
41 : m_hyperspace(hyperspace), m_toplevel_dir(toplevel_dir) {
48 for (
const char *ptr=toplevel_dir.c_str(); *ptr; ptr++)
53 m_ids_dir = toplevel_dir +
"/namemap/ids";
59 std::vector<Attribute> init_attr;
60 init_attr.push_back(
Attribute(
"nid",
"0", 1));
65 lock_guard<mutex> lock(
m_mutex);
66 return do_mapping(name,
false,
id, is_namespacep);
70 lock_guard<mutex> lock(
m_mutex);
71 return do_mapping(
id,
true, name, is_namespacep);
75 std::vector<uint64_t> &ids,
bool is_namespace) {
77 string names_file =
m_names_dir + names_parent +
"/" + names_entry;
80 for (
size_t i=0; i<ids.size(); i++)
81 parent_ids_file +=
String(
"/") + ids[i];
88 m_hyperspace->attr_get(names_file,
"id", attr_exists, dbuf);
90 id = strtoll((
const char *)dbuf.
base, 0, 0);
101 string ids_file = parent_ids_file +
String(
"/") + id;
102 std::vector<Attribute> attrs;
103 attrs.push_back(
Attribute(
"name", names_entry.c_str(), names_entry.length()));
104 attrs.push_back(
Attribute(
"nid",
"0", 1));
125 "name", names_entry.c_str(), names_entry.length());
134 std::vector<Attribute> init_attr;
147 lock_guard<mutex> lock(
m_mutex);
148 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
149 boost::char_separator<char> sep(
"/");
150 std::vector<String> name_components;
151 std::vector<uint64_t> id_components;
152 std::vector<DirEntryAttr> listing;
155 tokenizer tokens(name, sep);
156 for (tokenizer::iterator tok_iter = tokens.begin();
157 tok_iter != tokens.end(); ++tok_iter)
158 name_components.push_back(*tok_iter);
162 string names_parent =
"";
163 string names_child =
"";
165 listing.reserve(name_components.size());
166 for (
size_t i=0; i<name_components.size()-1; i++) {
168 names_child +=
String(
"/") + name_components[i];
175 if (!listing.back().is_dir)
178 id_components.push_back( strtoll((
const char *)value_buf.
base, 0, 0) );
189 add_entry(names_parent, name_components[i], id_components,
true);
192 names_parent = names_child;
196 add_entry(names_parent, name_components.back(),
205 id = (name[0] ==
'/') ?
"/" :
"";
206 id =
id + id_components[0];
208 for (
size_t i=1; i<id_components.size(); i++)
209 id +=
String(
"/") + id_components[i];
213 lock_guard<mutex> lock(
m_mutex);
216 string old_name = curr_name;
217 string new_name = next_name;
218 string new_name_last_comp;
219 size_t new_name_last_slash_pos;
220 string id_last_component;
221 size_t id_last_component_pos;
223 boost::trim_if(old_name, boost::is_any_of(
"/ "));
224 boost::trim_if(new_name, boost::is_any_of(
"/ "));
226 new_name_last_slash_pos = new_name.find_last_of(
'/');
227 if (new_name_last_slash_pos != String::npos)
228 new_name_last_comp = new_name.substr(new_name_last_slash_pos+1);
230 new_name_last_comp = new_name;
235 m_hyperspace->attr_set(id_file, oflags,
"name", new_name_last_comp.c_str(),
236 new_name_last_comp.length());
239 id_last_component_pos =
id.find_last_of(
'/');
240 if (id_last_component_pos != String::npos)
241 id_last_component =
id.substr(id_last_component_pos+1);
243 id_last_component = id;
246 id_last_component.length());
255 lock_guard<mutex> lock(
m_mutex);
257 string table_name = name;
259 boost::trim_if(table_name, boost::is_any_of(
"/ "));
284 string namespace_name = name;
285 boost::trim_if(namespace_name, boost::is_any_of(
"/ "));
286 return do_mapping(namespace_name,
false,
id, is_namespace);
290 bool *is_namespacep) {
291 vector <DirEntryAttr> listing;
292 int num_path_components = 0;
293 string hyperspace_file;
307 if (input.find(
'/') != 0)
308 hyperspace_file +=
"/";
310 hyperspace_file += input;
313 num_path_components = 1;
314 for(
size_t ii=0; ii< hyperspace_file.size()-1; ++ii) {
315 if (hyperspace_file[ii] ==
'/')
316 ++num_path_components;
320 m_hyperspace->readpath_attr(hyperspace_file, attr, listing);
331 if (listing.size() != (size_t) num_path_components || listing.size() == 0)
335 sort(listing.begin(), listing.end(), ascending);
338 string attr_val((
const char*)listing[ii].attr.base);
339 output += attr_val +
"/";
344 output.resize(output.size()-1);
346 bool is_dir = listing[listing.size()-1].is_dir;
349 *is_namespacep = is_dir;
356 vector <DirEntryAttr> dir_listing;
357 string hyperspace_dir;
363 hyperspace_dir += (
String)
"/" +
id;
365 m_hyperspace->readdir_attr(hyperspace_dir, attr, include_sub_entries, dir_listing);
373 listing.reserve(dir_listing.size());
374 for (
const auto &dir_entry : dir_listing) {
375 if (dir_entry.has_attr) {
376 entry.
name = (
String)((
const char*)dir_entry.attr.base);
378 entry.
id = dir_entry.name;
379 listing.push_back(entry);
380 if (!dir_entry.sub_entries.empty())
386 sort(listing.begin(), listing.end(), ascending);
bool id_to_name(const std::string &id, std::string &name, bool *is_namespacep=0)
std::string String
A String is simply a typedef to std::string.
Error if create and file exists.
void add_entry(const std::string &names_parent, const std::string &names_entry, std::vector< uint64_t > &ids, bool is_namespace)
void id_to_sublisting(const std::string &id, bool include_sub_entries, std::vector< NamespaceListing > &listing)
bool do_mapping(const std::string &input, bool id_in, std::string &output, bool *is_namespacep)
void drop_mapping(const std::string &name)
Drops a mapping.
A dynamic, resizable and reference counted memory buffer.
std::shared_ptr< Session > SessionPtr
bool exists_mapping(const std::string &name, bool *is_namespace)
void add_mapping(const std::string &name, std::string &id, int flags=0, bool ignore_exists=false)
Adds a new mapping.
Logging routines and macros.
Compatibility Macros for C/C++.
Hyperspace::SessionPtr m_hyperspace
void rename(const std::string &old_name, const std::string &new_name)
Rename one entity, it doesn't recursively rename all entities under the path specified by old_name...
static void get_namespace_listing(const std::vector< Hyperspace::DirEntryAttr > &dir_listing, std::vector< NamespaceListing > &listing)
uint8_t * base
Pointer to the allocated memory buffer.
This is a generic exception class for Hypertable.
Holds extended attribute and value.
bool name_to_id(const std::string &name, std::string &id, bool *is_namespacep=0)
Create file if it does not exist.
size_t m_prefix_components
String extensions and helpers: sets, maps, append operators etc.
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
NameIdMapper(Hyperspace::SessionPtr &hyperspace, const std::string &toplevel_dir)
int code() const
Returns the error code.
Executes user-defined functions when leaving the current scope.