0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CellPredicate.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 
26 
27 #ifndef HYPERTABLE_CELLPREDICATE_H
28 #define HYPERTABLE_CELLPREDICATE_H
29 
31 
32 #include <re2/re2.h>
33 
34 #include <boost/shared_ptr.hpp>
35 
36 #include <bitset>
37 #include <memory>
38 #include <vector>
39 
40 namespace Hypertable {
41 
42  using namespace Lib;
43 
46 
48  class CellPredicate {
49 
50  struct CellPattern {
51  CellPattern(const ColumnPredicate &cp, size_t id) :
52  qualifier_len(cp.column_qualifier_len), value_len(cp.value_len),
53  operation(cp.operation), id(id) {
54  buffer = std::shared_ptr<char>(new char[cp.column_qualifier_len+cp.value_len+2],
55  []( char *p ) { delete[] p; });
56  char *ptr = buffer.get();
57  if (cp.column_qualifier) {
58  qualifier = ptr;
59  memcpy(ptr, cp.column_qualifier, cp.column_qualifier_len);
60  ptr += cp.column_qualifier_len;
61  *ptr++ = 0;
62  }
63  if (cp.value) {
64  value = ptr;
65  memcpy(ptr, cp.value, cp.value_len);
66  ptr += cp.value_len;
67  *ptr++ = 0;
68  }
69  }
70  bool regex_qualifier_match(const char *str) {
71  if (!qualifier_regex) {
72  std::string pattern(qualifier, (size_t)qualifier_len);
73  qualifier_regex.reset(new RE2(pattern));
74  }
75  return RE2::PartialMatch(str, *qualifier_regex);
76  }
77  bool regex_value_match(const char *str) {
78  if (!value_regex) {
79  std::string pattern(value, (size_t)value_len);
80  value_regex.reset(new RE2(pattern));
81  }
82  return RE2::PartialMatch(str, *value_regex);
83  }
84  const char *qualifier {};
85  const char *value {};
86  uint32_t qualifier_len;
87  uint32_t value_len;
88  uint32_t operation;
89  boost::shared_ptr<RE2> value_regex;
90  boost::shared_ptr<RE2> qualifier_regex;
91  std::shared_ptr<char> buffer;
92  size_t id;
93  };
94 
96  typedef std::shared_ptr<CellPattern> CellPatternPtr;
97 
98  public:
99 
102  cutoff_time(0), max_versions(0), counter(false), indexed(false) { }
103 
104  void all_matches(const char *qualifier, size_t qualifier_len,
105  const char* value, size_t value_len,
106  std::bitset<32> &matching) {
107  for (auto & cp : patterns) {
108  if (pattern_match(cp, qualifier, qualifier_len, value, value_len))
109  matching.set(cp->id);
110  }
111  }
112 
119  bool matches(const char *qualifier, size_t qualifier_len,
120  const char* value, size_t value_len) {
121  if (patterns.empty())
122  return true;
123  for (auto & cp : patterns) {
124  if (pattern_match(cp, qualifier, qualifier_len, value, value_len))
125  return true;
126  }
127  return false;
128  }
129 
130  bool pattern_match(CellPatternPtr &cp, const char *qualifier,
131  size_t qualifier_len, const char* value,
132  size_t value_len) {
133 
134  // Qualifier match
135  if (cp->operation & ColumnPredicate::QUALIFIER_MATCH) {
136  if (cp->operation & ColumnPredicate::QUALIFIER_EXACT_MATCH) {
137  if (qualifier_len != cp->qualifier_len ||
138  memcmp(qualifier, cp->qualifier, qualifier_len))
139  return false;
140  }
141  else if (cp->operation & ColumnPredicate::QUALIFIER_PREFIX_MATCH) {
142  if (qualifier_len < cp->qualifier_len)
143  return false;
144  const char *p1 = qualifier;
145  const char *p2 = cp->qualifier;
146  const char *prefix_end = cp->qualifier + cp->qualifier_len;
147  for (; p2 < prefix_end; ++p1,++p2) {
148  if (*p1 != *p2)
149  break;
150  }
151  if (p2 != prefix_end)
152  return false;
153  }
154  else if (cp->operation & ColumnPredicate::QUALIFIER_REGEX_MATCH) {
155  if (!cp->regex_qualifier_match(qualifier))
156  return false;
157  }
158  }
159 
160  // Value match
161  if (cp->operation & ColumnPredicate::VALUE_MATCH) {
162  if (cp->operation & ColumnPredicate::EXACT_MATCH) {
163  if (cp->value_len != value_len ||
164  memcmp(cp->value, value, cp->value_len))
165  return false;
166  }
167  else if (cp->operation & ColumnPredicate::PREFIX_MATCH) {
168  if (cp->value_len > value_len ||
169  memcmp(cp->value, value, cp->value_len))
170  return false;
171  }
172  else if (cp->operation & ColumnPredicate::REGEX_MATCH) {
173  if (!cp->regex_value_match(value))
174  return false;
175  }
176  }
177  return true;
178  }
179 
180  void add_column_predicate(const ColumnPredicate &column_predicate, size_t id) {
181  patterns.push_back(std::make_shared<CellPattern>(column_predicate, id));
182  }
183 
185  int64_t cutoff_time;
186 
188  uint32_t max_versions;
189 
191  bool counter;
192 
194  bool indexed;
195 
196  private:
197 
199  std::vector<CellPatternPtr> patterns;
200  };
201 
203 }
204 
205 #endif // HYPERTABLE_CELLPREDICATE_H
std::vector< CellPatternPtr > patterns
Vector of patterns used in predicate match.
uint32_t max_versions
Max versions (0 for all versions)
void add_column_predicate(const ColumnPredicate &column_predicate, size_t id)
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
CellPattern(const ColumnPredicate &cp, size_t id)
Definition: CellPredicate.h:51
bool matches(const char *qualifier, size_t qualifier_len, const char *value, size_t value_len)
Evaluates predicate for the given cell.
std::shared_ptr< CellPattern > CellPatternPtr
Smart pointer to CellPattern.
Definition: CellPredicate.h:96
int64_t cutoff_time
TTL cutoff time.
bool indexed
Column family is indexed.
bool regex_qualifier_match(const char *str)
Definition: CellPredicate.h:70
Represents a column predicate (e.g.
bool counter
Column family has counter option.
Hypertable definitions
boost::shared_ptr< RE2 > value_regex
Definition: CellPredicate.h:89
boost::shared_ptr< RE2 > qualifier_regex
Definition: CellPredicate.h:90
bool pattern_match(CellPatternPtr &cp, const char *qualifier, size_t qualifier_len, const char *value, size_t value_len)
CellPredicate()
Default constructor.
bool regex_value_match(const char *str)
Definition: CellPredicate.h:77
void all_matches(const char *qualifier, size_t qualifier_len, const char *value, size_t value_len, std::bitset< 32 > &matching)