0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationRenameTable.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Compat.h>
23 
24 #include "OperationRenameTable.h"
25 #include "Utility.h"
26 
27 #include <Common/Error.h>
28 #include <Common/FailureInducer.h>
29 #include <Common/Serialization.h>
30 
31 #include <boost/algorithm/string.hpp>
32 
33 using namespace Hypertable;
34 using namespace std;
35 
37  const String &from,
38  const String &to)
39  : Operation(context, MetaLog::EntityType::OPERATION_RENAME_TABLE),
40  m_params(from, to) {
41  m_exclusivities.insert(m_params.from());
42  m_exclusivities.insert(m_params.to());
44 }
45 
47  const MetaLog::EntityHeader &header_)
48  : Operation(context, header_) {
49 }
50 
52  : Operation(context, event, MetaLog::EntityType::OPERATION_RENAME_TABLE) {
53  const uint8_t *ptr = event->payload;
54  size_t remaining = event->payload_len;
55  m_params.decode(&ptr, &remaining);
56  m_exclusivities.insert(m_params.from());
57  m_exclusivities.insert(m_params.to());
59 }
60 
62  int32_t state = get_state();
63  String id;
64 
65  HT_INFOF("Entering RenameTable-%lld(%s, %s) state=%s",
66  (Lld)header.id, m_params.from().c_str(), m_params.to().c_str(), OperationState::get_text(state));
67 
68  switch (state) {
69 
71 
72  // Verify old name refers to an existing table
75  break;
76  }
77 
78  // Verify new name does not refer to an existing table
81  break;
82  }
83 
84  // Verify new name does not refer to an existing namespace
85  bool is_namespace;
86  if (m_context->namemap->exists_mapping(m_params.to(), &is_namespace) && is_namespace) {
88  break;
89  }
90 
91  try {
92  DynamicBuffer value_buf;
93  string filename = m_context->toplevel_dir + "/tables/" + m_id;
94  m_context->hyperspace->attr_get(filename, "schema", value_buf);
95  m_parts = Utility::get_index_parts((const char *)value_buf.base);
96  }
97  catch (Exception &e) {
98  complete_error(e);
99  break;
100  }
101 
103  record_state();
104 
106 
107  if (m_parts.value_index()) {
108  string old_index = Filesystem::dirname(m_params.from())
109  + "/^" + Filesystem::basename(m_params.from());
110  string new_index = Filesystem::dirname(m_params.to())
111  + "/^" + Filesystem::basename(m_params.to());
112 
113  boost::trim_if(old_index, boost::is_any_of("/ "));
114  boost::trim_if(new_index, boost::is_any_of("/ "));
115  stage_subop(make_shared<OperationRenameTable>(m_context, old_index, new_index));
117  record_state();
118  break;
119  }
120 
122 
124 
125  if (!validate_subops())
126  break;
127 
128  if (m_parts.qualifier_index()) {
129  string old_index = Filesystem::dirname(m_params.from())
130  + "/^^" + Filesystem::basename(m_params.from());
131  string new_index = Filesystem::dirname(m_params.to())
132  + "/^^" + Filesystem::basename(m_params.to());
133 
134  boost::trim_if(old_index, boost::is_any_of("/ "));
135  boost::trim_if(new_index, boost::is_any_of("/ "));
136  stage_subop(make_shared<OperationRenameTable>(m_context, old_index, new_index));
138  record_state();
139  break;
140  }
141 
143 
145  if (!validate_subops())
146  break;
147  m_context->namemap->rename(m_params.from(), m_params.to());
148  m_context->monitoring->change_id_mapping(m_id, m_params.to());
149 
150  HT_MAYBE_FAIL("rename-table-STARTED");
151  complete_ok();
152  break;
153 
154  default:
155  HT_FATALF("Unrecognized state %d", state);
156  }
157 
158  HT_INFOF("Leaving RenameTable-%lld(%s, %s) state=%s",
159  (Lld)header.id, m_params.from().c_str(), m_params.to().c_str(),
161 }
162 
163 
164 void OperationRenameTable::display_state(std::ostream &os) {
165  os << " from=" << m_params.from() << " to=" << m_params.to()
166  << " id=" << m_id << " ";
167 }
168 
170  return 1;
171 }
172 
174  return m_params.encoded_length() +
177 }
178 
179 void OperationRenameTable::encode_state(uint8_t **bufp) const {
180  m_params.encode(bufp);
182  m_parts.encode(bufp);
183 }
184 
185 void OperationRenameTable::decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) {
186  m_params.decode(bufp, remainp);
187  m_id = Serialization::decode_vstr(bufp, remainp);
188  m_parts.decode(bufp, remainp);
189 }
190 
191 void OperationRenameTable::decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) {
192  {
193  string from = Serialization::decode_vstr(bufp, remainp);
194  string to = Serialization::decode_vstr(bufp, remainp);
196  }
197  m_id = Serialization::decode_vstr(bufp, remainp);
198  if (version >= 2) {
199  int8_t parts = (int8_t)Serialization::decode_byte(bufp, remainp);
200  m_parts = TableParts(parts);
201  }
202 }
203 
205  return "OperationRenameTable";
206 }
207 
209  return String("RenameTable ") + m_params.from() + " -> " + m_params.to();
210 }
211 
int64_t id()
Operation identifier.
Definition: Operation.h:326
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
The FailureInducer simulates errors.
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
static String filename
Definition: Config.cc:48
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
bool validate_subops()
Handles the results of sub operations.
Definition: Operation.cc:512
void execute() override
Executes (carries out) the operation.
virtual size_t encoded_length() const
Returns serialized object length.
Definition: Serializable.cc:37
int64_t id
Unique ID of entity.
const String name() override
Name of operation used for exclusivity.
Lib::Master::Request::Parameters::RenameTable m_params
Request parmaeters.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
STL namespace.
EntityHeader header
Entity header
size_t encoded_length_vstr(size_t len)
Computes the encoded length of vstr (vint64, data, null)
const char * get_text(int32_t state)
Definition: Operation.cc:609
const string & from() const
Gets original table name.
Definition: RenameTable.h:62
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
Represents a set of table parts (sub-tables).
Definition: TableParts.h:47
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
const String label() override
Human readable label for operation.
TableParts m_parts
Index tables requiring renaming.
virtual void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: Serializable.cc:64
void decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) override
void set_state(int32_t state)
Definition: Operation.h:473
const char * INIT
Definition: Operation.cc:45
void display_state(std::ostream &os) override
Write human readable operation state to output stream.
Compatibility Macros for C/C++.
void stage_subop(std::shared_ptr< Operation > operation)
Stages a sub operation for execution.
Definition: Operation.cc:536
Functions to serialize/deserialize primitives to/from a memory buffer.
void record_state()
Records operation state to the MML.
Definition: Operation.h:401
Request parameters for rename table operation.
Definition: RenameTable.h:46
static String basename(String name, char separator= '/')
A posix-compliant basename() which strips directory names from a filename.
Definition: Filesystem.cc:154
OperationRenameTable(ContextPtr &context, const String &from, const String &to)
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
void encode_vstr(uint8_t **bufp, const void *buf, size_t len)
Encode a buffer as variable length string (vint64, data, null)
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
bool table_exists(ContextPtr &context, const String &name, String &id)
Checks if table exists and returns table ID.
Definition: Utility.cc:100
Declarations for general-purpose utility functions.
String m_id
Table ID path for new name.
DependencySet m_dependencies
Set of dependencies.
Definition: Operation.h:595
#define HT_INFOF(msg,...)
Definition: Logger.h:272
uint8_t * base
Pointer to the allocated memory buffer.
bool value_index() const
Test if value index is included in set.
Definition: TableParts.h:75
Abstract base class for master operations.
Definition: Operation.h:124
void decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Decode operation state.
This is a generic exception class for Hypertable.
Definition: Error.h:314
void encode_state(uint8_t **bufp) const override
Encode operation state.
uint8_t encoding_version_state() const override
Returns version of encoding format of state.
static String dirname(String name, char separator= '/')
A posix-compliant dirname() which strips the last component from a file name.
Definition: Filesystem.cc:127
DependencySet m_exclusivities
Set of exclusivities.
Definition: Operation.h:592
TableParts get_index_parts(const std::string &schema)
Gets index parts specified in schema.
Definition: Utility.cc:124
#define HT_MAYBE_FAIL(_label_)
void complete_ok(std::vector< MetaLog::EntityPtr > &additional)
Definition: Operation.cc:436
uint8_t decode_byte(const uint8_t **bufp, size_t *remainp)
Decodes a single byte from the given buffer.
Definition: Serialization.h:73
const string & to() const
Gets new table name.
Definition: RenameTable.h:66
Error codes, Exception handling, error logging.
void complete_error(int error, const String &msg, std::vector< MetaLog::EntityPtr > &additional)
Completes operation with error.
Definition: Operation.cc:400
bool qualifier_index() const
Test if qualifier index is included in set.
Definition: TableParts.h:80
size_t encoded_length_state() const override
Encoded length of operation state.