0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RangeSpec.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 
26 
27 #include <Common/Compat.h>
28 
29 #include "RangeSpec.h"
30 
31 #include <Common/Logger.h>
32 #include <Common/Properties.h>
33 #include <Common/Serialization.h>
34 #include <Common/StringExt.h>
35 #include <Common/md5.h>
36 
37 using namespace std;
38 using namespace Hypertable;
39 using namespace Serialization;
40 
41 String RangeSpec::type_str(int type) {
42  switch(type) {
43  case (ROOT):
44  return (String) "root";
45  case (METADATA):
46  return (String) "metadata";
47  case (SYSTEM):
48  return (String) "system";
49  case (USER):
50  return "user";
51  case (UNKNOWN):
52  return "unknown";
53  }
54  return "unknown";
55 }
56 
57 bool RangeSpec::operator==(const RangeSpec &other) const {
58 
59  bool start_null = (start_row == 0) || (strlen(start_row)==0);
60  bool other_start_null = (other.start_row == 0) || (strlen(other.start_row)==0);
61 
62  if (start_null || other_start_null) {
63  if (start_null != other_start_null)
64  return false;
65  }
66  else {
67  if (strcmp(start_row, other.start_row))
68  return false;
69  }
70  if (end_row == 0 || other.end_row == 0) {
71  if (end_row != other.end_row)
72  return false;
73  }
74  else {
75  if (strcmp(end_row, other.end_row))
76  return false;
77  }
78  return true;
79 }
80 
81 bool RangeSpec::operator!=(const RangeSpec &other) const {
82  return !(*this == other);
83 }
84 
85 bool RangeSpec::operator<(const RangeSpec &other) const {
86  bool start_null = (start_row == 0) || (strlen(start_row)==0);
87  bool other_start_null = (other.start_row == 0) || (strlen(other.start_row)==0);
88 
89  if (start_null || other_start_null) {
90  if (!other_start_null)
91  return true;
92  else if (!start_null)
93  return false;
94  }
95  else {
96  int cmpval = strcmp(start_row, other.start_row);
97  if (cmpval < 0)
98  return true;
99  else if (cmpval > 0)
100  return false;
101  }
102 
103  bool end_null = (end_row==0) || (strlen(end_row)==0);
104  bool other_end_null = (other.end_row == 0) || (strlen(other.end_row)==0);
105 
106  if (end_null || other_end_null) {
107  if (!other_end_null)
108  return true;
109  else if (!end_null)
110  return false;
111  }
112  else {
113  int cmpval = strcmp(end_row, other.end_row);
114  if (cmpval < 0)
115  return true;
116  else if (cmpval > 0)
117  return false;
118  }
119 
120  return false;
121 }
122 
123 uint8_t RangeSpec::encoding_version() const {
124  return 1;
125 }
126 
127 size_t RangeSpec::encoded_length_internal() const {
128  return encoded_length_vstr(start_row) + encoded_length_vstr(end_row);
129 }
130 
147 void RangeSpec::encode_internal(uint8_t **bufp) const {
148  Serialization::encode_vstr(bufp, start_row);
149  Serialization::encode_vstr(bufp, end_row);
150 }
151 
152 void RangeSpec::decode_internal(uint8_t version, const uint8_t **bufp,
153  size_t *remainp) {
154  HT_TRY("decoding range spec",
155  start_row = Serialization::decode_vstr(bufp, remainp);
156  end_row = Serialization::decode_vstr(bufp, remainp));
157 }
158 
159 void RangeSpecManaged::decode_internal(uint8_t version, const uint8_t **bufp,
160  size_t *remainp) {
161  RangeSpec::decode_internal(version, bufp, remainp);
162  *this = *this;
163 }
164 
166 ostream &Hypertable::operator<<(ostream &os, const RangeSpec &range) {
167  os <<"{RangeSpec:";
168  HT_DUMP_CSTR(os, start, range.start_row);
169  HT_DUMP_CSTR(os, end, range.end_row);
170  os <<'}';
171  return os;
172 }
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
Range specification.
Definition: RangeSpec.h:40
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Program options handling.
STL namespace.
size_t encoded_length_vstr(size_t len)
Computes the encoded length of vstr (vint64, data, null)
const char * end_row
Definition: RangeSpec.h:60
const char * USER
Definition: Operation.cc:50
Logging routines and macros.
Compatibility Macros for C/C++.
#define HT_DUMP_CSTR(_os_, _label_, _str_)
Definition: Logger.h:227
std::ostream & operator<<(std::ostream &os, const crontab_entry &entry)
Helper function to write crontab_entry to an ostream.
Definition: Crontab.cc:301
Functions to serialize/deserialize primitives to/from a memory buffer.
const char * METADATA
Definition: Operation.cc:48
bool operator==(const directory_entry< _Key, _Tp > &lhs, const directory_entry< _Key, _Tp > &rhs)
Definition: directory.h:112
Declarations for RangeSpec and RangeSpecManaged.
bool operator!=(const directory_entry< _Key, _Tp > &lhs, const directory_entry< _Key, _Tp > &rhs)
Definition: directory.h:122
void encode_vstr(uint8_t **bufp, const void *buf, size_t len)
Encode a buffer as variable length string (vint64, data, null)
bool operator<(const directory_entry< _Key, _Tp > &lhs, const directory_entry< _Key, _Tp > &rhs)
Definition: directory.h:128
Hypertable definitions
const char * start_row
Definition: RangeSpec.h:59
const char * SYSTEM
Definition: Operation.cc:49
const char * ROOT
Definition: Operation.cc:47
#define HT_TRY(_s_, _code_)
Definition: Error.h:517
String extensions and helpers: sets, maps, append operators etc.
md5 digest routines.