0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
KeySpec.h
Go to the documentation of this file.
1 /* -*- c++ -*-
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 #ifndef HYPERTABLE_KEYSPEC_H
23 #define HYPERTABLE_KEYSPEC_H
24 
25 #include <boost/noncopyable.hpp>
26 
27 #include <vector>
28 
29 #include "Common/String.h"
30 #include "Common/Error.h"
31 
32 namespace Hypertable {
33 
34  static const int64_t TIMESTAMP_MIN = INT64_MIN;
35  static const int64_t TIMESTAMP_MAX = INT64_MAX;
36  static const int64_t TIMESTAMP_NULL = INT64_MIN + 1;
37  static const int64_t TIMESTAMP_AUTO = INT64_MIN + 2;
38  static const int64_t AUTO_ASSIGN = INT64_MIN + 2;
39 
40  static const uint32_t FLAG_DELETE_ROW = 0x00;
41  static const uint32_t FLAG_DELETE_COLUMN_FAMILY = 0x01;
42  static const uint32_t FLAG_DELETE_CELL = 0x02;
43  static const uint32_t FLAG_DELETE_CELL_VERSION = 0x03;
44  enum {
46  };
47  static const uint32_t FLAG_INSERT = 0xFF;
48 
49  class KeySpec {
50  public:
51 
53  column_qualifier_len(0), timestamp(AUTO_ASSIGN),
54  revision(AUTO_ASSIGN), flag(FLAG_INSERT) {}
55 
56  explicit KeySpec(const char *r, const char *cf, const char *cq,
57  int64_t ts = AUTO_ASSIGN, uint8_t flag_=FLAG_INSERT)
58  : row(r), row_len(r ? strlen(r) : 0), column_family(cf),
59  column_qualifier(cq), column_qualifier_len(cq ? strlen(cq) : 0),
60  timestamp(ts), revision(AUTO_ASSIGN), flag(flag_) {}
61 
62  explicit KeySpec(const char *r, const char *cf,
63  int64_t ts = AUTO_ASSIGN, uint8_t flag_=FLAG_INSERT)
64  : row(r), row_len(r ? strlen(r) : 0), column_family(cf),
66  timestamp(ts), revision(AUTO_ASSIGN), flag(flag_) {}
67 
68  void clear() {
69  row = 0;
70  row_len = 0;
71  column_family = 0;
72  column_qualifier = 0;
76  flag = FLAG_INSERT;
77  }
78 
79  void sanity_check() const {
80  const char *r = (const char *)row;
81  const char *cq = (const char *)column_qualifier;
82 
83  // Sanity check the row key
84  if (row_len == 0)
85  HT_THROW(Error::BAD_KEY, "Invalid row key - cannot be zero length");
86 
87  if (r[row_len] != 0)
89  "Invalid row key - must be followed by a '\\0' character");
90 
91  if (strlen(r) != row_len)
92  HT_THROWF(Error::BAD_KEY, "Invalid row key - '\\0' character not "
93  "allowed (offset=%d)", (int)strlen(r));
94 
95  if (r[0] == (char)0xff && r[1] == (char)0xff)
96  HT_THROW(Error::BAD_KEY, "Invalid row key - cannot start with "
97  "character sequence 0xff 0xff");
98 
99  // Sanity check the column qualifier
100  if (column_qualifier_len > 0) {
101  if (cq[column_qualifier_len] != 0)
102  HT_THROW(Error::BAD_KEY, "Invalid column qualifier - must be followed"
103  " by a '\\0' character");
104  if (strlen(cq) != column_qualifier_len)
105  HT_THROWF(Error::BAD_KEY, "Invalid column qualifier - '\\0' character"
106  " not allowed (offset=%d)", (int)strlen(cq));
107  }
108 
109  if (flag > FLAG_DELETE_ROW && flag < FLAG_INSERT) {
110  if (column_family == 0)
111  HT_THROWF(Error::BAD_KEY, "Flag is set to %d but column family is null", flag);
112  if (flag > FLAG_DELETE_COLUMN_FAMILY && flag < FLAG_INSERT) {
113  if (flag == FLAG_DELETE_CELL_VERSION && timestamp == AUTO_ASSIGN)
114  HT_THROWF(Error::BAD_KEY, "Flag is set to %d but timestamp is AUTO_ASSIGN", flag);
115  }
116  }
117 
118  if (timestamp == TIMESTAMP_NULL)
120  "Invalid timestamp %lld (reserved for Hypertable use)",
121  (Lld)TIMESTAMP_NULL);
122 
123  }
124 
125  const void *row;
126  size_t row_len;
127  const char *column_family;
128  const char *column_qualifier;
130  int64_t timestamp;
131  int64_t revision; // internal use only
132  uint8_t flag;
133  };
134 
135  std::ostream &operator<<(std::ostream &, const KeySpec &);
136 
137 
142  class KeySpecBuilder : boost::noncopyable {
143  public:
144  void set_row(const std::string &row) {
145  m_strings.push_back(row);
146  m_key_spec.row = m_strings.back().c_str();
147  m_key_spec.row_len = m_strings.back().length();
148  }
149 
150  void set_column_family(const std::string &cf) {
151  m_strings.push_back(cf);
152  m_key_spec.column_family = m_strings.back().c_str();
153  }
154 
155  void set_column_qualifier(const std::string &cq) {
156  m_strings.push_back(cq);
157  m_key_spec.column_qualifier = m_strings.back().c_str();
158  m_key_spec.column_qualifier_len = m_strings.back().length();
159  }
160 
161  void set_timestamp(int64_t timestamp) {
162  m_key_spec.timestamp = timestamp;
163  }
164 
165  void set_flag(uint8_t flag_) {
166  m_key_spec.flag = flag_;
167  }
168 
169 
173  void clear() {
174  m_key_spec.clear();
175  m_strings.clear();
176  }
177 
183  KeySpec &get() { return m_key_spec; }
184 
185  private:
186  std::vector<String> m_strings;
188  };
189 
190 
191 } // namespace Hypertable
192 
193 #endif // HYPERTABLE_KEYSPEC_H
194 
int64_t timestamp
Definition: KeySpec.h:130
KeySpec(const char *r, const char *cf, const char *cq, int64_t ts=AUTO_ASSIGN, uint8_t flag_=FLAG_INSERT)
Definition: KeySpec.h:56
Helper class for building a KeySpec.
Definition: KeySpec.h:142
void set_column_family(const std::string &cf)
Definition: KeySpec.h:150
static const uint32_t FLAG_DELETE_ROW
Definition: KeySpec.h:40
void set_flag(uint8_t flag_)
Definition: KeySpec.h:165
static const uint32_t FLAG_INSERT
Definition: KeySpec.h:47
const char * column_qualifier
Definition: KeySpec.h:128
void set_row(const std::string &row)
Definition: KeySpec.h:144
static const uint32_t FLAG_DELETE_CELL
Definition: KeySpec.h:42
int64_t revision
Definition: KeySpec.h:131
size_t column_qualifier_len
Definition: KeySpec.h:129
const void * row
Definition: KeySpec.h:125
static const uint32_t FLAG_DELETE_COLUMN_FAMILY
Definition: KeySpec.h:41
static const int64_t TIMESTAMP_MIN
Definition: KeySpec.h:34
void set_timestamp(int64_t timestamp)
Definition: KeySpec.h:161
std::ostream & operator<<(std::ostream &os, const crontab_entry &entry)
Helper function to write crontab_entry to an ostream.
Definition: Crontab.cc:301
static const int64_t TIMESTAMP_NULL
Definition: KeySpec.h:36
KeySpec(const char *r, const char *cf, int64_t ts=AUTO_ASSIGN, uint8_t flag_=FLAG_INSERT)
Definition: KeySpec.h:62
std::vector< String > m_strings
Definition: KeySpec.h:186
Hypertable definitions
void set_column_qualifier(const std::string &cq)
Definition: KeySpec.h:155
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
static const int64_t TIMESTAMP_MAX
Definition: KeySpec.h:35
static const int64_t TIMESTAMP_AUTO
Definition: KeySpec.h:37
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
void sanity_check() const
Definition: KeySpec.h:79
A String class based on std::string.
void clear()
Clears the state.
Definition: KeySpec.h:173
static const int64_t AUTO_ASSIGN
Definition: KeySpec.h:38
const char * column_family
Definition: KeySpec.h:127
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
static const uint32_t FLAG_DELETE_CELL_VERSION
Definition: KeySpec.h:43