Skip to content

Commit

Permalink
get gearbox/store and gearbox/t/store working
Browse files Browse the repository at this point in the history
- no longer need to explicitly dlopen soci plugins, soci now deals with it correctly
  • Loading branch information
coryb committed May 12, 2013
1 parent 342fc27 commit eaee1ae
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 87 deletions.
21 changes: 19 additions & 2 deletions Makefile.am
Expand Up @@ -36,7 +36,7 @@ bin_PROGRAMS+=gearbox/core/gearbox_json
lib_LTLIBRARIES+=gearbox/core/libgearbox_core.la

gearbox_core_libgearbox_core_la_CXXFLAGS = $(YAJL_CFLAGS) $(CURL_CFLAGS) $(BOOST_CPPFLAGS) $(LOG4CXX_CFLAGS) $(UUID_CFLAGS)
gearbox_core_libgearbox_core_la_LDFLAGS = $(YAJL_LIBS) $(CURL_LIBS) $(BOOST_LDFLAGS) $(BOOST_FILESYSTEM_LIB) $(LOG4CXX_LIBS) $(BOOST_REGEX_LIB) -lcrypto $(UUID_LIBS) $(BOOST_IOSTREAMS_LIB)
gearbox_core_libgearbox_core_la_LDFLAGS = $(YAJL_LIBS) $(CURL_LIBS) $(BOOST_LDFLAGS) $(BOOST_FILESYSTEM_LIB) $(LOG4CXX_LIBS) $(BOOST_REGEX_LIB) $(CRYPTO_LIB) $(UUID_LIBS) $(BOOST_IOSTREAMS_LIB)
gearbox_core_libgearbox_core_includes = \
gearbox/core/ConfigFile.h \
gearbox/core/Errors.h \
Expand Down Expand Up @@ -89,7 +89,7 @@ gearbox_core_gearbox_json_LDFLAGS = $(BOOST_LDFLAGS) $(BOOST_PROGRAM_OPTIONS_LIB
gearbox_core_gearbox_json_LDADD = gearbox/core/libgearbox_core.la

###############################################
### gearbox/core
### gearbox/job
###############################################
lib_LTLIBRARIES+=gearbox/job/libgearbox_job.la

Expand Down Expand Up @@ -133,3 +133,20 @@ gearbox_job_libgearbox_job_la_SOURCES = $(gearbox_job_libgearbox_job_includes) \
gearbox/job/StatusManager.cc \
gearbox/job/TransientStatusImpl.cc \
$(NULL)

###############################################
### gearbox/store
###############################################
lib_LTLIBRARIES+=gearbox/store/libgearbox_store.la

gearbox_store_libgearbox_store_la_CXXFLAGS =
gearbox_store_libgearbox_store_la_LDFLAGS = $(SOCI_CORE_LIB)
gearbox_store_libgearbox_store_la_LIBADD = gearbox/core/libgearbox_core.la
gearbox_store_libgearbox_store_includes = \
gearbox/store/dbconn.h \
$(NULL)

gearbox_store_libgearbox_store_la_SOURCES = $(gearbox_store_libgearbox_store_includes) \
gearbox/store/dbconn.cc \
gearbox/store/LoggedStatement.cc \
$(NULL)
12 changes: 12 additions & 0 deletions auto/testAutoMake.sh
Expand Up @@ -53,3 +53,15 @@ ${name}_SOURCES=$test
EOF
done

addTests gearbox/t/store
for test in gearbox/t/store/*.t.cc; do
name=$(amName $test)
cat <<EOF
${name}_CXXFLAGS=${test_CXXFLAGS} -DTESTDIR='"\$(abs_top_srcdir)/gearbox/t/store"'
${name}_LDFLAGS=\$(BOOST_LDFLAGS) \$(BOOST_SYSTEM_LIB)
${name}_LDADD=\$(LIBTAP) gearbox/store/libgearbox_store.la
${name}_SOURCES=$test
EOF
done
14 changes: 11 additions & 3 deletions configure.ac
Expand Up @@ -66,10 +66,18 @@ PKG_CHECK_MODULES([UUID], [uuid], [], [exit 1])
PKG_CHECK_MODULES([GEARMAN], [gearmand], [], [exit 1])

# Checks for libraries.
# FIXME: Replace `main' with a function in `-lcrypto':
AC_CHECK_LIB([crypto], [main])
AC_CHECK_LIB([crypto], [main], [
AC_ARG_VAR([CRYPTO_LIB])
CRYPTO_LIB=-lcrypto
], [exit 1])

# FIXME: Replace `main' with a function in `-lzookeeper_st':
AC_CHECK_LIB([zookeeper_st], [main])
AC_CHECK_LIB([zookeeper_st], [main], [], [])

AC_CHECK_LIB([soci_core], [main], [
AC_ARG_VAR([SOCI_CORE_LIB])
SOCI_CORE_LIB=-lsoci_core
], [exit 1])

# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h fcntl.h limits.h stdint.h stdlib.h string.h sys/file.h sys/ioctl.h sys/socket.h sys/time.h unistd.h])
Expand Down
57 changes: 2 additions & 55 deletions gearbox/store/dbconn.cc
Expand Up @@ -13,7 +13,6 @@
#include <string>
#include <sstream>
#include <stdexcept>
#include <dlfcn.h>

#include <boost/lexical_cast.hpp>

Expand Down Expand Up @@ -110,8 +109,6 @@ namespace Gearbox {
std::string sock;
soci::session * sess;
std::string type;
const soci::backend_factory * backend;
void * dl;
Private(const std::string & type)
: dbname("test"),
user("root"),
Expand All @@ -120,64 +117,14 @@ namespace Gearbox {
port(""),
sock(""),
sess(NULL),
type(type),
backend(NULL),
dl(NULL) {}
type(type) {}
};

Connection::Connection(const std::string & type) : impl(new Private(type)) {
try {
std::string factory_name("factory_" + type);

typedef soci::backend_factory const * (*factory_t)(void);
factory_t f = (factory_t)dlsym(RTLD_DEFAULT, factory_name.c_str());
char * err = dlerror();
if( err ) {
// libsoci_core should have the "soci_create_session" symbol,
// so first we need to find the symbol address,
// then find the file name that the symbol comes from
// then replace "core" with db driver type, to hopefully
// end up with something like: /usr/lib/libsoci_sqlite3-3.0.0.so
void * addr = dlsym(RTLD_DEFAULT, "soci_create_session");
if( addr ) {
Dl_info dli;
if( dladdr(addr, &dli) ) {
std::string soname(dli.dli_fname);
size_t s = soname.find("soci_core");
if( s != std::string::npos ) {
soname.replace(s, 9, "soci_" + type);
_DEBUG("Attempting to load " << soname);
this->impl->dl = dlopen(soname.c_str(), RTLD_NOW | RTLD_GLOBAL);
if( !this->impl->dl ) {
const char * err = dlerror();
_WARN("Failed to load soci module: " << err);
gbTHROW( ERR_INTERNAL_SERVER_ERROR(
"Unable to load soci module for " + type + ": " + err
) );
}
}
}
}

// hopefully by now we have the soname loaded so we can find the factory symbol
f = (factory_t)dlsym(RTLD_DEFAULT, factory_name.c_str());
char * err = dlerror();
if( err ) {
_FATAL("Failed to find " << factory_name << " symbol after loading: " << err);
gbTHROW( ERR_INTERNAL_SERVER_ERROR("Unable to load soci module for " + type ) );
}
}
this->impl->backend = f();
}
catch( ... ) {
delete impl;
throw;
}
};

Connection::~Connection() {
if( this->impl->sess ) delete this->impl->sess;
if( this->impl->dl ) dlclose(this->impl->dl);
delete impl;
}

Expand Down Expand Up @@ -275,7 +222,7 @@ namespace Gearbox {
}
}

this->impl->sess = new soci::session(*this->impl->backend, this->connection_string());
this->impl->sess = new soci::session(this->impl->type, this->connection_string());
return *this->impl->sess;
}

Expand Down
50 changes: 25 additions & 25 deletions gearbox/store/dbconn.h
Expand Up @@ -162,31 +162,31 @@ namespace Gearbox {
}
}

namespace soci {
namespace details {
template <>
struct exchange_traits<unsigned int>
{
typedef basic_type_tag type_family;
enum { x_type = x_unsigned_long };
};
#ifndef __i386__
template <>
struct exchange_traits<int64_t>
{
typedef basic_type_tag type_family;
enum { x_type = x_long_long };
};
#else
template <>
struct exchange_traits<time_t>
{
typedef basic_type_tag type_family;
enum { x_type = x_unsigned_long };
};
#endif
}
}
// namespace soci {
// namespace details {
// template <>
// struct exchange_traits<unsigned int>
// {
// typedef basic_type_tag type_family;
// enum { x_type = x_unsigned_long };
// };
// #ifndef __i386__
// template <>
// struct exchange_traits<int64_t>
// {
// typedef basic_type_tag type_family;
// enum { x_type = x_long_long };
// };
// #else
// template <>
// struct exchange_traits<time_t>
// {
// typedef basic_type_tag type_family;
// enum { x_type = x_unsigned_long };
// };
// #endif
// }
// }



Expand Down
1 change: 1 addition & 0 deletions gearbox/t/store/LoggedStatement.t.cc
Expand Up @@ -9,6 +9,7 @@ using namespace Gearbox;
using namespace Gearbox::Database;

int main() {
chdir(TESTDIR);
TEST_START(23);
OK( run("./mkdb") == 0 );
log_init("./unit.conf");
Expand Down
5 changes: 3 additions & 2 deletions gearbox/t/store/dbconn.t.cc
Expand Up @@ -14,11 +14,12 @@ struct TestConnection : public Connection {
};

int main() {
chdir(TESTDIR);
TEST_START(25);
log_init("./unit.conf");

THROWS_LIKE( TestConnection("bogus"),
"INTERNAL_SERVER_ERROR \\[500\\]: Unable to load soci module for bogus: ([^ ]+)/libsoci_bogus-3.0.0.so: cannot open shared object file: No such file or directory" );
THROWS( TestConnection("bogus").get_session(),
"Failed to find shared library for backend bogus" );

THROWS( Connection::get("bogus"),
"Database connection bogus has not been registered!" );
Expand Down

0 comments on commit eaee1ae

Please sign in to comment.