libmoost
/home/mhx/git/github/libmoost/src/kvstore/kyoto_tycoon_connection.cpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #include "../../include/moost/kvstore/kyoto_tycoon_connection.h"
00029 
00030 // below is just the linux specific implementation
00031 
00032 #ifndef WIN32
00033 
00034 #if defined (_STDINT_H)
00035 #define STDINT_H_3C996A98_ABAB_4d71_AC4B_633055150696
00036 #undef _STDINT_H
00037 #endif
00038 
00039 #include <ktremotedb.h>
00040 
00041 #ifdef STDINT_H_3C996A98_ABAB_4d71_AC4B_633055150696
00042 #undef STDINT_H_3C996A98_ABAB_4d71_AC4B_633055150696
00043 #if !defined (_STDINT_H)
00044 #define _STDINT_H
00045 #endif
00046 #else
00047 #undef _STDINT_H
00048 #endif
00049 
00050 namespace moost { namespace kvstore { namespace detail {
00051 
00052    KyotoTycoonConnection::~KyotoTycoonConnection()
00053    {
00054       try
00055       {
00056          close();
00057       }
00058       catch(...)
00059       {
00060          // ignore
00061       }
00062    }
00063 
00064    void KyotoTycoonConnection::open(const std::string& host, int port, int timeoutMs)
00065    {
00066       if (isOpen_)
00067       {
00068          throw std::runtime_error("The connection is already open");
00069       }
00070 
00071       if (!pDb_)
00072       {
00073          pDb_.reset(new kyototycoon::RemoteDB);
00074       }
00075 
00076       isOpen_ = pDb_->open(host, port, timeoutMs / 1000.0f);
00077 
00078       if (!isOpen_)
00079       {
00080          std::ostringstream oss;
00081          oss << "Couldn't open connection to " << host << ":" << port;
00082          throw_kt_exception(oss.str());
00083       }
00084    }
00085 
00086    void KyotoTycoonConnection::close()
00087    {
00088       if(isOpen_)
00089       {
00090          if (!pDb_->close())
00091             throw_kt_exception("Failed to close remote datastore");
00092          isOpen_ = false;
00093       }
00094    }
00095 
00096    boost::shared_array<char> KyotoTycoonConnection::get(const void* pkey, size_t ksize, size_t& vsize) const
00097    {
00098       assert_data_store_open();
00099       return boost::shared_array<char>(pDb_->get(reinterpret_cast<const char *>(pkey), ksize, &vsize));
00100    }
00101 
00102    void KyotoTycoonConnection::set(const void* pkey, size_t ksize, const void* pval, size_t vsize) const
00103    {
00104       assert_data_store_open();
00105       if (!pDb_->set(reinterpret_cast<const char *>(pkey), ksize, reinterpret_cast<const char *>(pval), vsize))
00106       {
00107          throw_kt_exception("Failed to set in remote datastore");
00108       }
00109    }
00110 
00111    void KyotoTycoonConnection::cache(const void* pkey, size_t ksize, const void* pval, size_t vsize, int64_t expirySecs) const
00112    {
00113       assert_data_store_open();
00114       if (!pDb_->set(reinterpret_cast<const char *>(pkey), ksize, reinterpret_cast<const char *>(pval), vsize, expirySecs))
00115       {
00116          throw_kt_exception("Failed to cache in remote datastore");
00117       }
00118    }
00119 
00120    void KyotoTycoonConnection::throw_kt_exception(const std::string& msg) const
00121    {
00122       throw std::runtime_error(msg + ": " + pDb_->error().name() + " (" + pDb_->error().message() + ")");
00123    }
00124 
00125    void KyotoTycoonConnection::assert_data_store_open() const
00126    {
00127       assert(isOpen_);
00128       if (!isOpen_)
00129          throw std::runtime_error("Kyoto Tycoon connection not open");
00130    }
00131 
00132 }}}  // end namespace
00133 
00134 #endif