0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MetadataRoot.cc
Go to the documentation of this file.
1 
22 #include "Common/Compat.h"
23 #include <cassert>
24 #include <cstdlib>
25 #include <cstring>
26 
27 #include "Common/Error.h"
28 
29 #include "Global.h"
30 #include "MetadataRoot.h"
31 
32 using namespace Hypertable;
33 using namespace Hyperspace;
34 
35 
36 MetadataRoot::MetadataRoot(SchemaPtr &schema) : m_next(0) {
37 
38  for (auto ag_spec : schema->get_access_groups())
39  m_agnames.push_back(ag_spec->get_name());
40 
41  try {
44  }
45  catch (Exception &e) {
46  HT_ERRORF("Problem opening Hyperspace root file '%s/root' - %s - "
47  "%s", Global::toplevel_dir.c_str(), Error::get_text(e.code()), e.what());
48  HT_ABORT;
49  }
50 
51 }
52 
53 
55  try {
56  Global::hyperspace->close_nowait(m_handle);
57  }
58  catch (Exception &e) {
59  HT_ERROR_OUT << e << HT_END;
60  }
61 }
62 
63 
65  m_next = 0;
66 }
67 
68 
69 bool MetadataRoot::get_next_files(String &ag_name, String &files, uint32_t *nextcsidp) {
70 
71  while (m_next < m_agnames.size()) {
72  std::vector<String> attrs;
73  attrs.push_back((String)"files." + m_agnames[m_next]);
74  attrs.push_back((String)"nextcsid." + m_agnames[m_next]);
75  ag_name = m_agnames[m_next];
76  m_next++;
77 
78  *nextcsidp = 0;
79 
80  // Read files and nextcsid
81  try {
82  std::vector<DynamicBufferPtr> values;
83  Global::hyperspace->attrs_get(m_handle, attrs, values);
84  if (values.front()) {
85  if (values.back())
86  *nextcsidp = atoi((const char *)values.back()->base);
87  files = (const char *)values.front()->base;
88  return true;
89  }
90  }
91  catch (Exception &e) {
92  HT_ERRORF("Problem getting attributes %s/%s on Hyperspace file "
93  "'%s/root' - %s", attrs.front().c_str(), attrs.back().c_str(),
95  return false;
96  }
97  }
98 
99  return false;
100 }
101 
102 
103 void MetadataRoot::write_files(const String &ag_name, const String &files, int64_t total_blocks) {
104  std::vector<Attribute> attrs;
105  String files_attrname = (String)"files." + ag_name;
106  String blockcount_attrname = (String)"blockcount." + ag_name;
107  char blockcount_buf[32];
108 
109  sprintf(blockcount_buf, "%llu", (Llu)total_blocks);
110 
111  attrs.push_back(Attribute(files_attrname.c_str(), files.c_str(), files.length()));
112  attrs.push_back(Attribute(blockcount_attrname.c_str(), blockcount_buf, strlen(blockcount_buf)));
113 
114  // Write "Files" and "BlockCount"
115  try {
116  Global::hyperspace->attr_set(m_handle, attrs);
117  }
118  catch (Exception &e) {
119  HT_THROW2(e.code(), e, (String)"Problem creating attributes '" + files_attrname
120  + "/" + blockcount_attrname + "' on Hyperspace file '/hypertable/root'");
121  }
122 
123 }
124 
125 void MetadataRoot::write_files(const String &ag_name, const String &files, int64_t total_blocks, uint32_t nextcsid) {
126  String files_attrname = (String)"files." + ag_name;
127  String nextcsid_attrname = (String)"nextcsid." + ag_name;
128  String blockcount_attrname = (String)"blockcount." + ag_name;
129  char nextcsid_buf[32], blockcount_buf[32];
130 
131  sprintf(nextcsid_buf, "%u", (unsigned)nextcsid);
132  sprintf(blockcount_buf, "%llu", (Llu)total_blocks);
133 
134  std::vector<Attribute> attrs;
135  attrs.push_back(Attribute(files_attrname.c_str(), files.c_str(), files.length()));
136  attrs.push_back(Attribute(nextcsid_attrname.c_str(), nextcsid_buf, strlen(nextcsid_buf)));
137  attrs.push_back(Attribute(blockcount_attrname.c_str(), blockcount_buf, strlen(blockcount_buf)));
138 
139  // Write "Files", "NextCSID", and "BlockCount"
140  try {
141  Global::hyperspace->attr_set(m_handle, attrs);
142  }
143  catch (Exception &e) {
144  HT_THROW2(e.code(), e, (String)"Problem creating attributes '" + files_attrname
145  + "/" + nextcsid_attrname + "/" + blockcount_attrname +
146  "' on Hyperspace file '/hypertable/root'");
147  }
148 
149 }
virtual bool get_next_files(String &ag_name, String &files, uint32_t *nextcsidp)
Definition: MetadataRoot.cc:69
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
long long unsigned int Llu
Shortcut for printf formats.
Definition: String.h:50
#define HT_ABORT
Definition: Logger.h:175
MetadataRoot(SchemaPtr &schema_ptr)
Definition: MetadataRoot.cc:36
Hyperspace definitions
static Hyperspace::SessionPtr hyperspace
Definition: Global.h:63
static std::string toplevel_dir
Definition: Global.h:108
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
virtual void write_files(const String &ag_name, const String &files, int64_t total_blocks)
Compatibility Macros for C/C++.
virtual void reset_files_scan()
Definition: MetadataRoot.cc:64
#define HT_END
Definition: Logger.h:220
#define HT_ERROR_OUT
Definition: Logger.h:301
std::vector< String > m_agnames
Definition: MetadataRoot.h:44
Hypertable definitions
This is a generic exception class for Hypertable.
Definition: Error.h:314
Holds extended attribute and value.
Definition: Protocol.h:48
#define HT_ERRORF(msg,...)
Definition: Logger.h:300
std::shared_ptr< Schema > SchemaPtr
Smart pointer to Schema.
Definition: Schema.h:465
Open file for reading.
Definition: Session.h:71
Error codes, Exception handling, error logging.
int code() const
Returns the error code.
Definition: Error.h:391
#define HT_THROW2(_code_, _ex_, _msg_)
Definition: Error.h:484