libQtCassandra 0.3.2

QCassandra.cpp

Go to the documentation of this file.
00001 /*
00002  * Text:
00003  *      QCassandra.cpp
00004  *
00005  * Description:
00006  *      Handling of the cassandra::CassandraClient and corresponding transports,
00007  *      protocols, sockets, etc.
00008  *
00009  * Documentation:
00010  *      See each function below.
00011  *
00012  * License:
00013  *      Copyright (c) 2011 Made to Order Software Corp.
00014  *
00015  *      http://snapwebsites.org/
00016  *      contact@m2osw.com
00017  *
00018  *      Permission is hereby granted, free of charge, to any person obtaining a
00019  *      copy of this software and associated documentation files (the
00020  *      "Software"), to deal in the Software without restriction, including
00021  *      without limitation the rights to use, copy, modify, merge, publish,
00022  *      distribute, sublicense, and/or sell copies of the Software, and to
00023  *      permit persons to whom the Software is furnished to do so, subject to
00024  *      the following conditions:
00025  *
00026  *      The above copyright notice and this permission notice shall be included
00027  *      in all copies or substantial portions of the Software.
00028  *
00029  *      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00030  *      OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00031  *      MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00032  *      IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00033  *      CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00034  *      TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00035  *      SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00036  */
00037 
00038 #include "QCassandraPrivate.h"
00039 #include <protocol/TBinaryProtocol.h>
00040 #include <transport/TSocket.h>
00041 #include <transport/TTransportUtils.h>
00042 
00043 #include <QtCore/QDebug>
00044 
00045 namespace QtCassandra
00046 {
00047 
00608 QCassandra::QCassandra()
00609     : f_private(new QCassandraPrivate(this)),
00610       //f_current_context(NULL) -- auto-init
00611       //f_contexts_read(false) -- auto-init
00612       //f_contexts() -- auto-init
00613       //f_cluster_name() -- auto-init
00614       //f_protocol_version() -- auto-init
00615       f_default_consistency_level(CONSISTENCY_LEVEL_ONE) // default is CONSISTENCY_LEVEL_DEFAULT
00616 {
00617     // we are passing this to the private object that we control
00618     // so we make make sure it is used wisely; at this time it is
00619     // just saved in a variable member
00620 }
00621 
00629 QCassandra::~QCassandra()
00630 {
00631 }
00632 
00643 QCassandraPrivate *QCassandra::getPrivate()
00644 {
00645     return f_private.get();
00646 }
00647 
00672 bool QCassandra::connect(const QString& host, int port)
00673 {
00674     // first make sure we're disconnected from the other cluster
00675     disconnect();
00676 
00677     // then we reconnect
00678     return f_private->connect(host, port);
00679 }
00680 
00688 void QCassandra::disconnect()
00689 {
00690     f_private->disconnect();
00691     f_current_context.clear();
00692     f_contexts.clear();
00693     f_cluster_name = "";
00694     f_protocol_version = "";
00695 }
00696 
00707 bool QCassandra::isConnected() const
00708 {
00709     return f_private->isConnected();
00710 }
00711 
00730 const QString& QCassandra::clusterName() const
00731 {
00732     if(f_cluster_name.isEmpty()) {
00733         // retrieve the name of the cluster once
00734         const_cast<QString&>(f_cluster_name) = f_private->clusterName();
00735     }
00736     return f_cluster_name;
00737 }
00738 
00757 const QString& QCassandra::protocolVersion() const
00758 {
00759     if(f_protocol_version.isEmpty()) {
00760         // retrieve the version of the protocol once
00761         const_cast<QString&>(f_protocol_version) = f_private->protocolVersion();
00762     }
00763     return f_protocol_version;
00764 }
00765 
00794 QSharedPointer<QCassandraContext> QCassandra::context(const QString& context_name)
00795 {
00796     // get the list of existing contexts
00797     const QCassandraContexts& cs = contexts();
00798 
00799     // already exists?
00800     QCassandraContexts::const_iterator ci = cs.find(context_name);
00801     if(ci != cs.end()) {
00802         return ci.value();
00803     }
00804 
00805     // otherwise create a new one
00806     QSharedPointer<QCassandraContext> c(new QCassandraContext(this, context_name));
00807     f_contexts.insert(context_name, c);
00808     return c;
00809 }
00810 
00838 void QCassandra::setCurrentContext(QSharedPointer<QCassandraContext> c)
00839 {
00840     // emit the change only if not the same context
00841     if(f_current_context != c) {
00842         f_private->setContext(c->contextName());
00843         // we save the current context only AFTER the call to setContext()
00844         // in case it throws (and then the current context would be wrong)
00845         f_current_context = c;
00846     }
00847 }
00848 
00856 void QCassandra::clearCurrentContextIf(const QCassandraContext& c)
00857 {
00858     if(f_current_context == &c) {
00859         f_current_context.clear();
00860     }
00861 }
00862 
00880 const QCassandraContexts& QCassandra::contexts() const
00881 {
00882     if(!f_contexts_read) {
00883         f_contexts_read = true;
00884         f_private->contexts();
00885     }
00886     return f_contexts;
00887 }
00888 
00899 QSharedPointer<QCassandraContext> QCassandra::findContext(const QString& context_name) const
00900 {
00901     QCassandraContexts::const_iterator ci(f_contexts.find(context_name));
00902     if(ci == f_contexts.end()) {
00903         QSharedPointer<QCassandraContext> null;
00904         return null;
00905     }
00906     return *ci;
00907 }
00908 
00927 QCassandraContext& QCassandra::operator [] (const QString& context_name)
00928 {
00929     QCassandraContext *context = findContext(context_name).data();
00930     if(context == NULL) {
00931         throw std::runtime_error("named context was not found, cannot return a reference");
00932     }
00933 
00934     return *context;
00935 }
00936 
00955 const QCassandraContext& QCassandra::operator [] (const QString& context_name) const
00956 {
00957     const QCassandraContext *context = findContext(context_name).data();
00958     if(context == NULL) {
00959         throw std::runtime_error("named context was not found, cannot return a reference");
00960     }
00961 
00962     return *context;
00963 }
00964 
00974 void QCassandra::dropContext(const QString& context_name)
00975 {
00976     QSharedPointer<QCassandraContext> c(context(context_name));
00977 
00978     // first do the context drop in Cassandra
00979     c->drop();
00980 
00981     // now unparent so the memory is returned to the user
00982     c->unparent();
00983 
00984     // forget about this context in the QCassandra object
00985     f_contexts.remove(context_name);
00986 }
00987 
01001 consistency_level_t QCassandra::defaultConsistencyLevel() const
01002 {
01003     return f_default_consistency_level;
01004 }
01005 
01019 void QCassandra::setDefaultConsistencyLevel(consistency_level_t default_consistency_level)
01020 {
01021     // make sure the consistency level exists
01022     if(default_consistency_level != CONSISTENCY_LEVEL_ONE
01023     && default_consistency_level != CONSISTENCY_LEVEL_QUORUM
01024     && default_consistency_level != CONSISTENCY_LEVEL_LOCAL_QUORUM
01025     && default_consistency_level != CONSISTENCY_LEVEL_EACH_QUORUM
01026     && default_consistency_level != CONSISTENCY_LEVEL_ALL
01027     && default_consistency_level != CONSISTENCY_LEVEL_ANY
01028     && default_consistency_level != CONSISTENCY_LEVEL_TWO
01029     && default_consistency_level != CONSISTENCY_LEVEL_THREE) {
01030         throw std::runtime_error("invalid default server consistency level");
01031     }
01032 
01033     f_default_consistency_level = default_consistency_level;
01034 }
01035 
01042 int QCassandra::versionMajor()
01043 {
01044     return QT_CASSANDRA_LIBRARY_VERSION_MAJOR;
01045 }
01046 
01053 int QCassandra::versionMinor()
01054 {
01055     return QT_CASSANDRA_LIBRARY_VERSION_MINOR;
01056 }
01057 
01064 int QCassandra::versionPatch()
01065 {
01066     return QT_CASSANDRA_LIBRARY_VERSION_PATCH;
01067 }
01068 
01077 const char *QCassandra::version()
01078 {
01079     return QT_CASSANDRA_LIBRARY_VERSION_STRING;
01080 }
01081 
01090 int64_t QCassandra::timeofday()
01091 {
01092     struct timeval tv;
01093 
01094     // we ignore timezone as it can also generate an error
01095     gettimeofday(&tv, NULL);
01096 
01097     return static_cast<int64_t>(tv.tv_sec) * 1000000 + static_cast<int64_t>(tv.tv_usec);
01098 }
01099 
01100 } // namespace QtCassandra
01101 // vim: ts=4 sw=4 et
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

This document is part of the libQtCassandra Project.

Copyright by Made to Order Software Corp.