diff --git a/3d_party/mongo-c-driver/install.sh b/3d_party/mongo-c-driver/install.sh new file mode 100755 index 0000000000000000000000000000000000000000..fc9ec2e391234f1ffb867019c844dc2f3e77201a --- /dev/null +++ b/3d_party/mongo-c-driver/install.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +cd $1 +wget https://github.com/mongodb/mongo-c-driver/releases/download/1.9.0/mongo-c-driver-1.9.0.tar.gz +tar xzf mongo-c-driver-1.9.0.tar.gz +cd mongo-c-driver-1.9.0 +./configure --disable-automatic-init-and-cleanup --enable-static=yes --enable-shared=no +make +#sudo make install + + + + diff --git a/CMakeLists.txt b/CMakeLists.txt index bb5c947b864274b122c9b257d9905b6b33f22299..28d1e733254cea0a7caa79e9134728b5bdd95aaf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ include(testing_cpp) if(BUILD_WORKER_TOOLS) set (BUILD_MONGODB_CLIENTLIB ON) endif() + add_subdirectory(common/cpp) add_subdirectory(producer/api) diff --git a/common/cpp/CMakeLists.txt b/common/cpp/CMakeLists.txt index 75587b54394fd0397147b955141d022a15b71b18..a63ecbbd38585bfe57827cbae0dedff7ef73f11d 100644 --- a/common/cpp/CMakeLists.txt +++ b/common/cpp/CMakeLists.txt @@ -4,4 +4,5 @@ if(BUILD_MONGODB_CLIENTLIB) add_subdirectory(src/database) endif() -install(DIRECTORY ${HIDRA2_CXX_COMMON_INCLUDE_DIR} DESTINATION include) \ No newline at end of file +install(DIRECTORY ${HIDRA2_CXX_COMMON_INCLUDE_DIR}/common + DESTINATION ${CMAKE_INSTALL_PREFIX}/include ) diff --git a/common/cpp/include/database/mongodb_client.h b/common/cpp/include/database/mongodb_client.h index 7ae6dadd1465aa1e4e24d0779b12529a0588d369..26fd802568439ab765676eed2e06c76d9d91c241 100644 --- a/common/cpp/include/database/mongodb_client.h +++ b/common/cpp/include/database/mongodb_client.h @@ -7,10 +7,10 @@ namespace hidra2 { class MongoDB final: public Database { public: - virtual DBError Connect(const std::string& address, const std::string& database, - const std::string& collection ) override; - virtual DBError Import(const FileInfos& files) const override; - virtual ~MongoDB() override; + DBError Connect(const std::string& address, const std::string& database, + const std::string& collection ) override; + DBError Import(const FileInfos& files) const override; + ~MongoDB() override; }; diff --git a/common/cpp/src/database/CMakeLists.txt b/common/cpp/src/database/CMakeLists.txt index 592a79029f2f1eb7deade9963290b520bd1a81d1..40528481495136b00f6d7e8ea0b574b08d5bca0d 100644 --- a/common/cpp/src/database/CMakeLists.txt +++ b/common/cpp/src/database/CMakeLists.txt @@ -5,7 +5,14 @@ set(SOURCE_FILES ################################ # Library ################################ -add_library(${TARGET_NAME} OBJECT ${SOURCE_FILES}) -target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR}) +find_package (libmongoc-static-1.0 1.9 REQUIRED) +message ("-- mongoc found version \"${MONGOC_STATIC_VERSION}\"") +message ("-- mongoc include path \"${MONGOC_STATIC_INCLUDE_DIRS}\"") +message ("-- mongoc libraries \"${MONGOC_STATIC_LIBRARIES}\"") +add_library(${TARGET_NAME} STATIC ${SOURCE_FILES}) +target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR} + PRIVATE "${MONGOC_STATIC_INCLUDE_DIRS}") +target_link_libraries (${TARGET_NAME} PRIVATE "${MONGOC_STATIC_LIBRARIES}") +target_compile_definitions (${TARGET_NAME} PRIVATE "${MONGOC_STATIC_DEFINITIONS}") diff --git a/common/cpp/src/database/mongodb_client.cpp b/common/cpp/src/database/mongodb_client.cpp index a45169fe070bbd026b1b3b3b16dd7cc73b912146..3910786fa3489fa624eb5adf24fb55341deea8fa 100644 --- a/common/cpp/src/database/mongodb_client.cpp +++ b/common/cpp/src/database/mongodb_client.cpp @@ -1,9 +1,79 @@ +#include "mongoc.h" + + #include "database/mongodb_client.h" namespace hidra2 { -DBError MongoDB::Connect(const std::string& address, const std::string& database, - const std::string& collection ) { +DBError MongoDB::Connect(const std::string& address, const std::string& database_name, + const std::string& collection_name ) { + const char* uri_str = address.c_str(); + mongoc_client_t* client; + mongoc_database_t* database; + mongoc_collection_t* collection; + bson_t* command, reply, *insert; + bson_error_t error; + char* str; + bool retval; + + /* + * Required to initialize libmongoc's internals + */ + mongoc_init (); + + /* + * Create a new client instance + */ + client = mongoc_client_new (uri_str); + + /* + * Register the application name so we can track it in the profile logs + * on the server. This can also be done from the URI (see other examples). + */ + mongoc_client_set_appname (client, "connect-example"); + + /* + * Get a handle on the database "db_name" and collection "coll_name" + */ + database = mongoc_client_get_database (client, database_name.c_str()); + collection = mongoc_client_get_collection (client, database_name.c_str(), collection_name.c_str()); + + /* + * Do work. This example pings the database, prints the result as JSON and + * performs an insert + */ + command = BCON_NEW ("ping", BCON_INT32 (1)); + + retval = mongoc_client_command_simple ( + client, "admin", command, NULL, &reply, &error); + + if (!retval) { + fprintf (stderr, "%s\n", error.message); + return DBError::kConnectionError; + } + + str = bson_as_json (&reply, NULL); + printf ("%s\n", str); + + insert = BCON_NEW ("hello", BCON_UTF8 ("world")); + + if (!mongoc_collection_insert_one (collection, insert, NULL, NULL, &error)) { + fprintf (stderr, "%s\n", error.message); + } + + bson_destroy (insert); + bson_destroy (&reply); + bson_destroy (command); + bson_free (str); + + /* + * Release our handles and clean up libmongoc + */ + mongoc_collection_destroy (collection); + mongoc_database_destroy (database); + mongoc_client_destroy (client); + mongoc_cleanup (); + return DBError::kNoError; } diff --git a/tests/worker/folder_to_db/check_linux.sh b/tests/worker/folder_to_db/check_linux.sh index 3f3eae30637c1c8bf3667a757979453f8a938140..c651c836064bcaf878cac26633017bb7a4728023 100644 --- a/tests/worker/folder_to_db/check_linux.sh +++ b/tests/worker/folder_to_db/check_linux.sh @@ -5,6 +5,6 @@ set -e mkdir -p test touch test/1 -$1 test 123 +$1 test mongodb://127.0.0.1 rm -rf test diff --git a/worker/api/cpp/CMakeLists.txt b/worker/api/cpp/CMakeLists.txt index 97ab1f315d2cdc732fc269db94d831b8f09820ba..46b83b837855d6bd9eefa9baef46d5253e625fe2 100644 --- a/worker/api/cpp/CMakeLists.txt +++ b/worker/api/cpp/CMakeLists.txt @@ -9,7 +9,6 @@ set(SOURCE_FILES include/worker/data_broker.h src/data_broker.cpp src/folder_dat add_library(${TARGET_NAME} STATIC ${SOURCE_FILES} $<TARGET_OBJECTS:system_io>) target_include_directories(${TARGET_NAME} PUBLIC include ${HIDRA2_CXX_COMMON_INCLUDE_DIR}) -#target_link_libraries(${TARGET_NAME} $<TARGET_OBJECTS:common>) set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX) diff --git a/worker/tools/folder_to_db/CMakeLists.txt b/worker/tools/folder_to_db/CMakeLists.txt index ebd8940178f88b8afea8387c35edd343983fd99a..3c55901baeac98158115c077705664219971af6d 100644 --- a/worker/tools/folder_to_db/CMakeLists.txt +++ b/worker/tools/folder_to_db/CMakeLists.txt @@ -6,10 +6,8 @@ set(SOURCE_FILES src/folder_db_importer.cpp) ################################ # Library ################################ -add_library(${TARGET_NAME} STATIC ${SOURCE_FILES} $<TARGET_OBJECTS:system_io> - $<TARGET_OBJECTS:mongo_db>) - -target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR}) +add_library(${TARGET_NAME} STATIC ${SOURCE_FILES} $<TARGET_OBJECTS:system_io>) +target_link_libraries(${TARGET_NAME} mongo_db) add_executable(${TARGET_NAME}-bin src/main.cpp) set_target_properties(${TARGET_NAME}-bin @@ -25,3 +23,5 @@ set(TEST_LIBRARIES "${TARGET_NAME}") gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}") + +install(TARGETS ${TARGET_NAME}-bin DESTINATION bin) diff --git a/worker/tools/folder_to_db/unittests/test_folder_to_db.cpp b/worker/tools/folder_to_db/unittests/test_folder_to_db.cpp index 0f136e31d98b0e8b71b6ee83de9adce6f51d3f28..b77a8ac3626896bfd4927f7a8cd61ce90a2ca6fe 100644 --- a/worker/tools/folder_to_db/unittests/test_folder_to_db.cpp +++ b/worker/tools/folder_to_db/unittests/test_folder_to_db.cpp @@ -133,6 +133,7 @@ MATCHER_P(CompareFileInfos, file_infos, "") { if (arg[i].size != file_infos[i].size) return false; if (arg[i].base_name != file_infos[i].base_name) return false; } + return true; } FileInfos CreateTestFileInfos() {