libQtCassandra 0.3.2
|
00001 /* 00002 * Header: 00003 * QCassandraValue.h 00004 * 00005 * Description: 00006 * Handling of a cell value to access data within the Cassandra database. 00007 * 00008 * Documentation: 00009 * See the corresponding .cpp file. 00010 * 00011 * License: 00012 * Copyright (c) 2011 Made to Order Software Corp. 00013 * 00014 * http://snapwebsites.org/ 00015 * contact@m2osw.com 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining a 00018 * copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be included 00026 * in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00029 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00031 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 00032 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 00033 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 00034 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 */ 00036 #ifndef QCASSANDRA_VALUE_H 00037 #define QCASSANDRA_VALUE_H 00038 00039 #include "QtCassandra/QCassandraConsistencyLevel.h" 00040 #include <controlled_vars/controlled_vars_limited_auto_init.h> 00041 #include <QtCore/QString> 00042 #include <QtCore/QByteArray> 00043 #include <QtCore/QDebug> 00044 #include <stdint.h> 00045 00046 00047 namespace QtCassandra 00048 { 00049 00050 class QCassandraPrivate; 00051 00052 00053 // Null 00054 inline void setNullValue(QByteArray& array) 00055 { 00056 array.clear(); 00057 } 00058 00059 // Bool 00060 inline void appendBoolValue(QByteArray& array, bool value) 00061 { 00062 char buf[1]; 00063 buf[0] = value ? 1 : 0; 00064 array.append(buf, 1); 00065 } 00066 00067 inline void setBoolValue(QByteArray& array, bool value) 00068 { 00069 array.clear(); 00070 appendBoolValue(array, value); 00071 } 00072 00073 // Char 00074 inline void appendCharValue(QByteArray& array, const char value) 00075 { 00076 array.append(&value, 1); 00077 } 00078 00079 inline void setCharValue(QByteArray& array, const char value) 00080 { 00081 array.clear(); 00082 appendCharValue(array, value); 00083 } 00084 00085 inline void appendSignedCharValue(QByteArray& array, signed char value) 00086 { 00087 appendCharValue(array, value); 00088 } 00089 00090 inline void setSignedCharValue(QByteArray& array, signed char value) 00091 { 00092 setCharValue(array, value); 00093 } 00094 00095 inline void appendUnsignedCharValue(QByteArray& array, unsigned char value) 00096 { 00097 appendCharValue(array, value); 00098 } 00099 00100 inline void setUnsignedCharValue(QByteArray& array, unsigned char value) 00101 { 00102 setCharValue(array, value); 00103 } 00104 00105 // Int16 00106 inline void appendInt16Value(QByteArray& array, int16_t value) 00107 { 00108 char buf[2]; 00109 buf[0] = static_cast<char>(value >> 8); 00110 buf[1] = static_cast<char>(value); 00111 array.append(buf, 2); 00112 } 00113 00114 inline void setInt16Value(QByteArray& array, int16_t value) 00115 { 00116 array.clear(); 00117 appendInt16Value(array, value); 00118 } 00119 00120 inline void appendUInt16Value(QByteArray& array, uint16_t value) 00121 { 00122 appendInt16Value(array, value); 00123 } 00124 00125 inline void setUInt16Value(QByteArray& array, uint16_t value) 00126 { 00127 setInt16Value(array, value); 00128 } 00129 00130 // Int32 00131 inline void appendInt32Value(QByteArray& array, int32_t value) 00132 { 00133 char buf[4]; 00134 buf[0] = static_cast<char>(value >> 24); 00135 buf[1] = static_cast<char>(value >> 16); 00136 buf[2] = static_cast<char>(value >> 8); 00137 buf[3] = static_cast<char>(value); 00138 array.append(buf, 4); 00139 } 00140 00141 inline void setInt32Value(QByteArray& array, int32_t value) 00142 { 00143 array.clear(); 00144 appendInt32Value(array, value); 00145 } 00146 00147 inline void appendUInt32Value(QByteArray& array, uint32_t value) 00148 { 00149 appendInt32Value(array, value); 00150 } 00151 00152 inline void setUInt32Value(QByteArray& array, uint32_t value) 00153 { 00154 setInt32Value(array, value); 00155 } 00156 00157 // Int64 00158 inline void appendInt64Value(QByteArray& array, int64_t value) 00159 { 00160 char buf[4]; 00161 buf[0] = static_cast<char>(value >> 56); 00162 buf[1] = static_cast<char>(value >> 48); 00163 buf[2] = static_cast<char>(value >> 40); 00164 buf[3] = static_cast<char>(value >> 32); 00165 buf[4] = static_cast<char>(value >> 24); 00166 buf[5] = static_cast<char>(value >> 16); 00167 buf[6] = static_cast<char>(value >> 8); 00168 buf[7] = static_cast<char>(value); 00169 array.append(buf, 8); 00170 } 00171 00172 inline void setInt64Value(QByteArray& array, int64_t value) 00173 { 00174 array.clear(); 00175 appendInt64Value(array, value); 00176 } 00177 00178 inline void appendUInt64Value(QByteArray& array, uint64_t value) 00179 { 00180 appendInt64Value(array, value); 00181 } 00182 00183 inline void setUInt64Value(QByteArray& array, uint64_t value) 00184 { 00185 setInt64Value(array, value); 00186 } 00187 00188 // Float 00189 inline void setFloatValue(QByteArray& array, float value) 00190 { 00191 union switch_t { 00192 float f; 00193 uint64_t v; 00194 }; 00195 switch_t s; 00196 s.f = value; 00197 setInt32Value(array, s.v); 00198 } 00199 00200 inline void appendFloatValue(QByteArray& array, float value) 00201 { 00202 union switch_t { 00203 float f; 00204 uint64_t v; 00205 }; 00206 switch_t s; 00207 s.f = value; 00208 appendInt32Value(array, s.v); 00209 } 00210 00211 // Double 00212 inline void setDoubleValue(QByteArray& array, double value) 00213 { 00214 union switch_t { 00215 double d; 00216 uint64_t v; 00217 }; 00218 switch_t s; 00219 s.d = value; 00220 setInt64Value(array, s.v); 00221 } 00222 00223 inline void appendDoubleValue(QByteArray& array, double value) 00224 { 00225 union switch_t { 00226 double d; 00227 uint64_t v; 00228 }; 00229 switch_t s; 00230 s.d = value; 00231 appendInt64Value(array, s.v); 00232 } 00233 00234 // String 00235 inline void setStringValue(QByteArray& array, const QString& value) 00236 { 00237 array = value.toUtf8(); 00238 } 00239 00240 inline void appendStringValue(QByteArray& array, const QString& value) 00241 { 00242 array.append(value.toUtf8()); 00243 } 00244 00245 // Binary 00246 inline void setBinaryValue(QByteArray& array, const QByteArray& value) 00247 { 00248 array = value; 00249 } 00250 00251 inline void appendBinaryValue(QByteArray& array, const QByteArray& value) 00252 { 00253 array.append(value); 00254 } 00255 00256 00257 00258 00259 class QCassandraValue //: public QObject -- values are copyable and not named 00260 { 00261 public: 00262 static const int32_t TTL_PERMANENT = 0; 00263 00264 // TTL must be positive, although Cassandra allows 0 as "permanent" 00265 typedef controlled_vars::limited_auto_init<int32_t, 0, INT_MAX, TTL_PERMANENT> cassandra_ttl_t; 00266 00267 enum def_timestamp_mode_t { 00268 TIMESTAMP_MODE_CASSANDRA, 00269 TIMESTAMP_MODE_AUTO, 00270 TIMESTAMP_MODE_DEFINED 00271 }; 00272 typedef controlled_vars::limited_auto_init<def_timestamp_mode_t, TIMESTAMP_MODE_CASSANDRA, TIMESTAMP_MODE_DEFINED, TIMESTAMP_MODE_AUTO> timestamp_mode_t; 00273 00274 // CASSANDRA_VALUE_TYPE_BINARY (empty buffer) 00275 QCassandraValue(); 00276 00277 // CASSANDRA_VALUE_TYPE_INTEGER 00278 QCassandraValue(bool value); 00279 QCassandraValue(char value); 00280 QCassandraValue(signed char value); 00281 QCassandraValue(unsigned char value); 00282 QCassandraValue(int16_t value); 00283 QCassandraValue(uint16_t value); 00284 QCassandraValue(int32_t value); 00285 QCassandraValue(uint32_t value); 00286 QCassandraValue(int64_t value); 00287 QCassandraValue(uint64_t value); 00288 00289 // CASSANDRA_VALUE_TYPE_FLOAT 00290 QCassandraValue(float value); 00291 QCassandraValue(double value); 00292 00293 // CASSANDRA_VALUE_TYPE_STRING 00294 QCassandraValue(const QString& value); 00295 00296 // CASSANDRA_VALUE_TYPE_BINARY 00297 QCassandraValue(const QByteArray& value); 00298 00299 void setNullValue(); 00300 void setBoolValue(bool value); 00301 void setCharValue(char value); 00302 void setSignedCharValue(signed char value); 00303 void setUnsignedCharValue(unsigned char value); 00304 void setInt16Value(int16_t value); 00305 void setUInt16Value(uint16_t value); 00306 void setInt32Value(int32_t value); 00307 void setUInt32Value(uint32_t value); 00308 void setInt64Value(int64_t value); 00309 void setUInt64Value(uint64_t value); 00310 void setFloatValue(float value); 00311 void setDoubleValue(double value); 00312 void setStringValue(const QString& value); 00313 void setBinaryValue(const QByteArray& value); 00314 00315 int size() const; 00316 00317 bool nullValue() const; 00318 char charValue() const; 00319 signed char signedCharValue() const; 00320 unsigned char unsignedCharValue() const; 00321 int16_t int16Value() const; 00322 uint16_t uint16Value() const; 00323 int32_t int32Value() const; 00324 uint32_t uint32Value() const; 00325 int64_t int64Value() const; 00326 uint64_t uint64Value() const; 00327 float floatValue() const; 00328 double doubleValue() const; 00329 QString stringValue() const; 00330 const QByteArray& binaryValue() const; 00331 00332 QCassandraValue& operator = (const char *null_value); // i.e. NULL 00333 QCassandraValue& operator = (bool value); 00334 QCassandraValue& operator = (char value); 00335 QCassandraValue& operator = (signed char value); 00336 QCassandraValue& operator = (unsigned char value); 00337 QCassandraValue& operator = (int16_t value); 00338 QCassandraValue& operator = (uint16_t value); 00339 QCassandraValue& operator = (int32_t value); 00340 QCassandraValue& operator = (uint32_t value); 00341 QCassandraValue& operator = (int64_t value); 00342 QCassandraValue& operator = (uint64_t value); 00343 QCassandraValue& operator = (float value); 00344 QCassandraValue& operator = (double value); 00345 QCassandraValue& operator = (const QString& value); 00346 QCassandraValue& operator = (const QByteArray& value); 00347 00348 bool operator == (const QCassandraValue& rhs); 00349 bool operator != (const QCassandraValue& rhs); 00350 bool operator < (const QCassandraValue& rhs); 00351 bool operator <= (const QCassandraValue& rhs); 00352 bool operator > (const QCassandraValue& rhs); 00353 bool operator >= (const QCassandraValue& rhs); 00354 00355 int32_t ttl() const; 00356 void setTtl(int32_t ttl = TTL_PERMANENT); 00357 00358 consistency_level_t consistencyLevel() const; 00359 void setConsistencyLevel(consistency_level_t level); 00360 00361 timestamp_mode_t timestampMode() const; 00362 void setTimestampMode(timestamp_mode_t mode); 00363 int64_t timestamp() const; 00364 void setTimestamp(int64_t timestamp); 00365 00366 private: 00367 void assignTimestamp(int64_t timestamp); 00368 00369 friend class QCassandraPrivate; 00370 00371 QByteArray f_value; 00372 cassandra_ttl_t f_ttl; 00373 consistency_level_t f_consistency_level; 00374 timestamp_mode_t f_timestamp_mode; 00375 controlled_vars::zint64_t f_timestamp; 00376 }; 00377 00378 00379 00380 } // namespace QtCassandra 00381 #endif 00382 //#ifndef QCASSANDRA_VALUE_H 00383 // vim: ts=4 sw=4 et
This document is part of the libQtCassandra Project.
Copyright by Made to Order Software Corp.