libQtCassandra 0.3.2
|
00001 /* 00002 * Text: 00003 * QCassandraRow.cpp 00004 * 00005 * Description: 00006 * Handling of rows. There is no class representing a row in Cassandra. 00007 * A row is just a key. We have this object to allow for our C++ array 00008 * syntax to access the Cassandra data. 00009 * 00010 * Documentation: 00011 * See each function below. 00012 * 00013 * License: 00014 * Copyright (c) 2011 Made to Order Software Corp. 00015 * 00016 * http://snapwebsites.org/ 00017 * contact@m2osw.com 00018 * 00019 * Permission is hereby granted, free of charge, to any person obtaining a 00020 * copy of this software and associated documentation files (the 00021 * "Software"), to deal in the Software without restriction, including 00022 * without limitation the rights to use, copy, modify, merge, publish, 00023 * distribute, sublicense, and/or sell copies of the Software, and to 00024 * permit persons to whom the Software is furnished to do so, subject to 00025 * the following conditions: 00026 * 00027 * The above copyright notice and this permission notice shall be included 00028 * in all copies or substantial portions of the Software. 00029 * 00030 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00031 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00032 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00033 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 00034 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 00035 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 00036 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00037 */ 00038 00039 #include "QtCassandra/QCassandraRow.h" 00040 #include "QtCassandra/QCassandraTable.h" 00041 #include "QCassandraPrivate.h" 00042 #include "thrift-gencpp-cassandra/cassandra_types.h" 00043 #include <stdexcept> 00044 00045 namespace QtCassandra 00046 { 00047 00107 QCassandraRow::QCassandraRow(QCassandraTable *table, const QByteArray& row_key) 00108 : f_table(table), 00109 f_key(row_key) 00110 //f_cells() -- auto-init 00111 { 00112 if(f_key.size() > 64535) { 00113 throw std::runtime_error("row key is more than 64Kb"); 00114 } 00115 } 00116 00122 QCassandraRow::~QCassandraRow() 00123 { 00124 } 00125 00141 QString QCassandraRow::rowName() const 00142 { 00143 return QString::fromUtf8(f_key.data()); 00144 } 00145 00159 const QByteArray& QCassandraRow::rowKey() const 00160 { 00161 return f_key; 00162 } 00163 00179 int QCassandraRow::cellCount(const QCassandraColumnPredicate& column_predicate) 00180 { 00181 if(f_table == NULL) { 00182 throw std::runtime_error("row was dropped and is not attached to a table anymore"); 00183 } 00184 f_table->getCellCount(f_key, column_predicate); 00185 } 00186 00202 void QCassandraRow::readCells(const QCassandraColumnPredicate& column_predicate) 00203 { 00204 if(f_table == NULL) { 00205 throw std::runtime_error("row was dropped and is not attached to a table anymore"); 00206 } 00207 f_table->getColumnSlice(f_key, column_predicate); 00208 } 00209 00226 QSharedPointer<QCassandraCell> QCassandraRow::cell(const QString& column_name) 00227 { 00228 return cell(column_name.toUtf8()); 00229 } 00230 00246 QSharedPointer<QCassandraCell> QCassandraRow::cell(const QByteArray& column_key) 00247 { 00248 // column already exists? 00249 QCassandraCells::iterator ci(f_cells.find(column_key)); 00250 if(ci != f_cells.end()) { 00251 return ci.value(); 00252 } 00253 00254 // this is a new column, allocate it 00255 QSharedPointer<QCassandraCell> c(new QCassandraCell(this, column_key)); 00256 f_cells.insert(column_key, c); 00257 return c; 00258 } 00259 00270 const QCassandraCells& QCassandraRow::cells() const 00271 { 00272 return f_cells; 00273 } 00274 00288 QSharedPointer<QCassandraCell> QCassandraRow::findCell(const QString& column_name) const 00289 { 00290 return findCell(column_name.toUtf8()); 00291 } 00292 00305 QSharedPointer<QCassandraCell> QCassandraRow::findCell(const QByteArray& column_key) const 00306 { 00307 QCassandraCells::const_iterator ci(f_cells.find(column_key)); 00308 if(ci == f_cells.end()) { 00309 QSharedPointer<QCassandraCell> null; 00310 return null; 00311 } 00312 return *ci; 00313 } 00314 00324 bool QCassandraRow::exists(const QString& column_name) const 00325 { 00326 return exists(column_name.toUtf8()); 00327 } 00328 00338 bool QCassandraRow::exists(const QByteArray& column_key) const 00339 { 00340 if(f_table == NULL) { 00341 throw std::runtime_error("row was dropped and is not attached to a table anymore"); 00342 } 00343 QCassandraCells::const_iterator ci(f_cells.find(column_key)); 00344 if(ci != f_cells.end()) { 00345 // exists in the cache already 00346 return true; 00347 } 00348 00349 // try reading this cell 00350 QCassandraValue value; 00351 try { 00352 f_table->getValue(f_key, column_key, value); 00353 } 00354 catch(const org::apache::cassandra::NotFoundException&) { 00355 // it doesn't exist in Cassandra either 00356 return false; 00357 } 00358 00359 QSharedPointer<QCassandraCell> c(const_cast<QCassandraRow *>(this)->cell(column_key)); 00360 c->setValue(value); 00361 00362 return true; 00363 } 00364 00380 QCassandraCell& QCassandraRow::operator [] (const QString& column_name) 00381 { 00382 return *cell(column_name).data(); 00383 } 00384 00399 QCassandraCell& QCassandraRow::operator [] (const QByteArray& column_key) 00400 { 00401 return *cell(column_key).data(); 00402 } 00403 00423 const QCassandraCell& QCassandraRow::operator [] (const QString& column_name) const 00424 { 00425 return operator [] (column_name.toUtf8()); 00426 } 00427 00441 const QCassandraCell& QCassandraRow::operator [] (const QByteArray& column_key) const 00442 { 00443 const QCassandraCell *cell = findCell(column_key).data(); 00444 if(cell == NULL) { 00445 throw std::runtime_error("named column while retrieving a cell was not found, cannot return a reference"); 00446 } 00447 00448 return *cell; 00449 } 00450 00458 void QCassandraRow::clearCache() 00459 { 00460 for(QCassandraCells::iterator ci(f_cells.begin()); ci != f_cells.end(); ++ci) { 00461 (*ci)->unparent(); 00462 } 00463 f_cells.clear(); 00464 } 00465 00491 void QCassandraRow::dropCell(const QByteArray& column_key, QCassandraValue::timestamp_mode_t mode, int64_t timestamp) 00492 { 00493 if(f_table == NULL) { 00494 throw std::runtime_error("row was dropped and is not attached to a table anymore"); 00495 } 00496 if(QCassandraValue::TIMESTAMP_MODE_AUTO != mode && QCassandraValue::TIMESTAMP_MODE_DEFINED != mode) { 00497 throw std::runtime_error("invalid timestamp mode in dropCell()"); 00498 } 00499 00500 QSharedPointer<QCassandraCell> c(cell(column_key)); 00501 00502 // default to the timestamp of the value (which is most certainly 00503 // what people want in 99.9% of the cases.) 00504 if(QCassandraValue::TIMESTAMP_MODE_AUTO == mode) { 00505 // the current timestamp mode of f_value is currently ignored 00506 // because we cannot really know whether the f_value.timestamp() 00507 // value was assigned or not... (not from the mode that is) 00508 timestamp = c->value().timestamp(); 00509 } 00510 f_table->remove(f_key, column_key, timestamp, c->value().consistencyLevel()); 00511 f_cells.remove(column_key); 00512 c->unparent(); 00513 } 00514 00525 void QCassandraRow::dropCell(const QString& column_name, QCassandraValue::timestamp_mode_t mode, int64_t timestamp) 00526 { 00527 dropCell(column_name.toUtf8(), mode, timestamp); 00528 } 00529 00538 void QCassandraRow::insertValue(const QByteArray& column_key, const QCassandraValue& value) 00539 { 00540 if(f_table == NULL) { 00541 throw std::runtime_error("row was dropped and is not attached to a table anymore"); 00542 } 00543 f_table->insertValue(f_key, column_key, value); 00544 } 00545 00554 void QCassandraRow::getValue(const QByteArray& column_key, QCassandraValue& value) 00555 { 00556 if(f_table == NULL) { 00557 throw std::runtime_error("row was dropped and is not attached to a table anymore"); 00558 } 00559 f_table->getValue(f_key, column_key, value); 00560 } 00561 00572 void QCassandraRow::unparent() 00573 { 00574 f_table = NULL; 00575 clearCache(); 00576 } 00577 00578 } // namespace QtCassandra 00579 // vim: ts=4 sw=4 et
This document is part of the libQtCassandra Project.
Copyright by Made to Order Software Corp.