Line data Source code
1 : // Snap Websites Server -- snap websites server
2 : // Copyright (c) 2011-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 :
18 :
19 : // self
20 : //
21 : #include "snapwebsites/snap_cassandra.h"
22 :
23 :
24 : // snapwebsites lib
25 : //
26 : #include "snapwebsites/snapwebsites.h"
27 : #include "snapwebsites/snap_tables.h"
28 : #include "snapwebsites/log.h"
29 :
30 :
31 : // casswrapper lib
32 : //
33 : #include <casswrapper/schema.h>
34 :
35 :
36 : // C lib
37 : //
38 : #include <unistd.h>
39 :
40 :
41 : // last include
42 : //
43 : #include <snapdev/poison.h>
44 :
45 :
46 :
47 :
48 :
49 : namespace snap
50 : {
51 :
52 :
53 0 : snap_cassandra::snap_cassandra()
54 : {
55 : // empty
56 0 : }
57 :
58 :
59 0 : snap_cassandra::~snap_cassandra()
60 : {
61 0 : }
62 :
63 :
64 0 : void snap_cassandra::connect()
65 : {
66 : // We now connect to our proxy instead. This allows us to have many
67 : // permanent connections to Cassandra (or some other data store) and
68 : // not have to have threads (at least the C/C++ driver forces us to
69 : // have threads for asynchronous and timeout handling...)
70 : //
71 0 : snap_config config("snapdbproxy");
72 0 : tcp_client_server::get_addr_port(config["listen"], f_snapdbproxy_addr, f_snapdbproxy_port, "tcp");
73 :
74 : //std::cerr << "snap proxy info: " << f_snapdbproxy_addr << " and " << f_snapdbproxy_port << "\n";
75 0 : f_cassandra = libdbproxy::libdbproxy::create();
76 0 : if(!f_cassandra)
77 : {
78 0 : QString const msg("could not create the libdbproxy instance.");
79 0 : SNAP_LOG_FATAL(msg);
80 0 : throw snap_cassandra_not_available_exception(msg);
81 : }
82 :
83 0 : if( !f_cassandra->connect(f_snapdbproxy_addr, f_snapdbproxy_port) )
84 : {
85 0 : QString const msg("could not connect libdbproxy to snapdbproxy.");
86 0 : SNAP_LOG_FATAL(msg);
87 0 : throw snap_cassandra_not_available_exception(msg);
88 : }
89 :
90 : // everything setup to QUORUM or we get really strange errors when under
91 : // load (without much load, it works like a charm with ONE).
92 : //
93 : // Note: the low level library forces everything to QUORUM anyway so
94 : // this call is not really useful as it stands.
95 : //
96 0 : f_cassandra->setDefaultConsistencyLevel(libdbproxy::CONSISTENCY_LEVEL_QUORUM);
97 0 : }
98 :
99 :
100 0 : void snap_cassandra::disconnect()
101 : {
102 0 : f_cassandra.reset();
103 0 : }
104 :
105 :
106 0 : libdbproxy::context::pointer_t snap_cassandra::get_snap_context()
107 : {
108 0 : if( f_cassandra == nullptr )
109 : {
110 0 : QString msg("You must connect to cassandra first!");
111 0 : SNAP_LOG_FATAL(msg);
112 0 : throw snap_cassandra_not_available_exception(msg);
113 : }
114 :
115 : // we need to read all the contexts in order to make sure the
116 : // findContext() works properly
117 : //
118 0 : f_cassandra->getContexts();
119 0 : QString const context_name(snap::get_name(snap::name_t::SNAP_NAME_CONTEXT));
120 0 : return f_cassandra->findContext(context_name);
121 : }
122 :
123 :
124 0 : QString snap_cassandra::get_snapdbproxy_addr() const
125 : {
126 0 : return f_snapdbproxy_addr;
127 : }
128 :
129 :
130 0 : int32_t snap_cassandra::get_snapdbproxy_port() const
131 : {
132 0 : return f_snapdbproxy_port;
133 : }
134 :
135 :
136 0 : bool snap_cassandra::is_connected() const
137 : {
138 0 : if(!f_cassandra)
139 : {
140 0 : return false;
141 : }
142 0 : return f_cassandra->isConnected();
143 : }
144 :
145 :
146 0 : libdbproxy::table::pointer_t snap_cassandra::get_table(QString const & table_name)
147 : {
148 0 : libdbproxy::context::pointer_t context(get_snap_context());
149 0 : if(!context)
150 : {
151 0 : throw snap_cassandra_not_available_exception("The snap_websites context is not available in this Cassandra database.");
152 : }
153 :
154 : // does table exist?
155 0 : libdbproxy::table::pointer_t table(context->findTable(table_name));
156 0 : if(!table)
157 : {
158 0 : SNAP_LOG_FATAL("could not find table \"")(table_name)("\" in Cassandra.");
159 0 : throw snap_cassandra_not_available_exception(QString("Table \"%1\" does not exist. Did you install a *-tables.xml file for it?").arg(table_name));
160 : }
161 :
162 0 : return table;
163 : }
164 :
165 :
166 :
167 :
168 :
169 6 : }
170 : // namespace snap
171 : // vim: ts=4 sw=4 et
|