Skip to content
Snippets Groups Projects
Commit 86d8aea4 authored by Sergey Yakubov's avatar Sergey Yakubov
Browse files

start mongodb client

parent f4f539dd
No related branches found
No related tags found
No related merge requests found
#!/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
...@@ -24,6 +24,7 @@ include(testing_cpp) ...@@ -24,6 +24,7 @@ include(testing_cpp)
if(BUILD_WORKER_TOOLS) if(BUILD_WORKER_TOOLS)
set (BUILD_MONGODB_CLIENTLIB ON) set (BUILD_MONGODB_CLIENTLIB ON)
endif() endif()
add_subdirectory(common/cpp) add_subdirectory(common/cpp)
add_subdirectory(producer/api) add_subdirectory(producer/api)
......
...@@ -4,4 +4,5 @@ if(BUILD_MONGODB_CLIENTLIB) ...@@ -4,4 +4,5 @@ if(BUILD_MONGODB_CLIENTLIB)
add_subdirectory(src/database) add_subdirectory(src/database)
endif() endif()
install(DIRECTORY ${HIDRA2_CXX_COMMON_INCLUDE_DIR} DESTINATION include) install(DIRECTORY ${HIDRA2_CXX_COMMON_INCLUDE_DIR}/common
\ No newline at end of file DESTINATION ${CMAKE_INSTALL_PREFIX}/include )
...@@ -7,10 +7,10 @@ namespace hidra2 { ...@@ -7,10 +7,10 @@ namespace hidra2 {
class MongoDB final: public Database { class MongoDB final: public Database {
public: public:
virtual DBError Connect(const std::string& address, const std::string& database, DBError Connect(const std::string& address, const std::string& database,
const std::string& collection ) override; const std::string& collection ) override;
virtual DBError Import(const FileInfos& files) const override; DBError Import(const FileInfos& files) const override;
virtual ~MongoDB() override; ~MongoDB() override;
}; };
......
...@@ -5,7 +5,14 @@ set(SOURCE_FILES ...@@ -5,7 +5,14 @@ set(SOURCE_FILES
################################ ################################
# Library # Library
################################ ################################
add_library(${TARGET_NAME} OBJECT ${SOURCE_FILES}) find_package (libmongoc-static-1.0 1.9 REQUIRED)
target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR}) 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}")
#include "mongoc.h"
#include "database/mongodb_client.h" #include "database/mongodb_client.h"
namespace hidra2 { namespace hidra2 {
DBError MongoDB::Connect(const std::string& address, const std::string& database, DBError MongoDB::Connect(const std::string& address, const std::string& database_name,
const std::string& collection ) { 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; return DBError::kNoError;
} }
......
...@@ -5,6 +5,6 @@ set -e ...@@ -5,6 +5,6 @@ set -e
mkdir -p test mkdir -p test
touch test/1 touch test/1
$1 test 123 $1 test mongodb://127.0.0.1
rm -rf test rm -rf test
...@@ -9,7 +9,6 @@ set(SOURCE_FILES include/worker/data_broker.h src/data_broker.cpp src/folder_dat ...@@ -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>) add_library(${TARGET_NAME} STATIC ${SOURCE_FILES} $<TARGET_OBJECTS:system_io>)
target_include_directories(${TARGET_NAME} PUBLIC include ${HIDRA2_CXX_COMMON_INCLUDE_DIR}) 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) set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
......
...@@ -6,10 +6,8 @@ set(SOURCE_FILES src/folder_db_importer.cpp) ...@@ -6,10 +6,8 @@ set(SOURCE_FILES src/folder_db_importer.cpp)
################################ ################################
# Library # Library
################################ ################################
add_library(${TARGET_NAME} STATIC ${SOURCE_FILES} $<TARGET_OBJECTS:system_io> add_library(${TARGET_NAME} STATIC ${SOURCE_FILES} $<TARGET_OBJECTS:system_io>)
$<TARGET_OBJECTS:mongo_db>) target_link_libraries(${TARGET_NAME} mongo_db)
target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR})
add_executable(${TARGET_NAME}-bin src/main.cpp) add_executable(${TARGET_NAME}-bin src/main.cpp)
set_target_properties(${TARGET_NAME}-bin set_target_properties(${TARGET_NAME}-bin
...@@ -25,3 +23,5 @@ set(TEST_LIBRARIES "${TARGET_NAME}") ...@@ -25,3 +23,5 @@ set(TEST_LIBRARIES "${TARGET_NAME}")
gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}") gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}")
install(TARGETS ${TARGET_NAME}-bin DESTINATION bin)
...@@ -133,6 +133,7 @@ MATCHER_P(CompareFileInfos, file_infos, "") { ...@@ -133,6 +133,7 @@ MATCHER_P(CompareFileInfos, file_infos, "") {
if (arg[i].size != file_infos[i].size) return false; if (arg[i].size != file_infos[i].size) return false;
if (arg[i].base_name != file_infos[i].base_name) return false; if (arg[i].base_name != file_infos[i].base_name) return false;
} }
return true;
} }
FileInfos CreateTestFileInfos() { FileInfos CreateTestFileInfos() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment