Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn committed Mar 28, 2019
0 parents commit bb7c7af
Show file tree
Hide file tree
Showing 20 changed files with 1,044 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build/
*~
180 changes: 180 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
####################################
# General project definition
####################################

CMAKE_MINIMUM_REQUIRED(VERSION 3.5.2 FATAL_ERROR)

# Set cmake policy by version: https://cmake.org/cmake/help/latest/manual/cmake-policies.7.html
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.12)
endif()

# Define project
project(InfluxDB-Client
VERSION 0.0.1
DESCRIPTION "InfluxDB C++ client library"
LANGUAGES CXX
)

# Documentation dir
#add_subdirectory(doc)

# Add compiler flags for warnings and debug symbols
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra")

# Set fPIC for all targets
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Set CMAKE_INSTALL_LIBDIR explicitly to lib (to avoid lib64 on CC7)
set(CMAKE_INSTALL_LIBDIR lib)

# Set the default build type to "RelWithDebInfo"
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "RelWithDebInfo"
CACHE
STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel Coverage."
FORCE
)
endif()

# Add coverage flags to Debug build
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage")
endif()

####################################
# Dependencies
####################################

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

find_package(Boost REQUIRED COMPONENTS unit_test_framework system)
find_package(CURL REQUIRED MODULE)


####################################
# Library
####################################

set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/lib")
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/bin")
set(INCLUDE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/include")

set(SRCS
src/InfluxDB.cxx
src/Point.cxx
src/InfluxDBFactory.cxx
src/UDP.cxx
src/HTTP.cxx
src/UnixSocket.cxx
)

# Create library
add_library(InfluxDB SHARED ${SRCS})
target_include_directories(InfluxDB
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)

# Link targets
target_link_libraries(InfluxDB
PUBLIC
Boost::boost
PRIVATE
Boost::system
CURL::CURL
)

# Use C++17
target_compile_features(InfluxDB PUBLIC cxx_std_17)


####################################
# Tests
####################################

enable_testing()

set(TEST_SRCS
test/testFactory.cxx
)

foreach (test ${TEST_SRCS})
get_filename_component(test_name ${test} NAME)
string(REGEX REPLACE ".cxx" "" test_name ${test_name})

add_executable(${test_name} ${test})
target_link_libraries(${test_name}
PRIVATE
InfluxDB Boost::unit_test_framework
)
add_test(NAME ${test_name} COMMAND ${test_name})
set_tests_properties(${test_name} PROPERTIES TIMEOUT 60)
endforeach()


####################################
# Install
####################################

include(GNUInstallDirs)

# Build targets with install rpath on Mac to dramatically speed up installation
# https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" isSystemDir)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
if("${isSystemDir}" STREQUAL "-1")
set(CMAKE_INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR}")
endif()
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
endif()
unset(isSystemDir)

# Install library
install(TARGETS InfluxDB
EXPORT InfluxDBTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# Create version file
include(CMakePackageConfigHelpers)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/cmake/InfluxDBConfigVersion.cmake"
VERSION ${PACKAGE_VERSION}
COMPATIBILITY AnyNewerVersion
)

# Install headers
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/InfluxDB DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")

# Export targets
install(EXPORT InfluxDBTargets
FILE
InfluxDBTargets.cmake
NAMESPACE
InfluxData::
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/InfluxDB
)

# Configure and install Config files
configure_package_config_file(
cmake/InfluxDBConfig.cmake.in cmake/InfluxDBConfig.cmake
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/InfluxDB"
PATH_VARS CMAKE_INSTALL_PREFIX
)

install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/cmake/InfluxDBConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/InfluxDBConfigVersion.cmake"
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/InfluxDB
)
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# influxdb-cxx
InfluxDB C++ client library

## Installation

1. Requirements: boost::asio and cURL
2. `mkdir build; cd build`
3. `cmake ..; make -j`

## Sample usage
```cpp
auto influxdb = influxdb::InfluxDBFactory::Get("udp://127.0.0.1:1234");
influxdb->write(Point{"test"}
.addField("value", 10)
.addTag("host", "localhost")
);
```
67 changes: 67 additions & 0 deletions cmake/FindCURL.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

#.rst:
# FindCURL
# --------
#
# Find curl
#
# IMPORTED Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines :prop_tgt:`IMPORTED` target ``CURL::CURL``, if
# curl has been found.
#
# Find the native CURL headers and libraries.
#
# ::
#
# CURL_INCLUDE_DIRS - where to find curl/curl.h, etc.
# CURL_LIBRARIES - List of libraries when using curl.
# CURL_FOUND - True if curl found.
# CURL_VERSION_STRING - the version of curl found (since CMake 2.8.8)

# Look for the header file.
find_path(CURL_INCLUDE_DIR NAMES curl/curl.h)
mark_as_advanced(CURL_INCLUDE_DIR)

# Look for the library (sorted from most current/relevant entry to least).
find_library(CURL_LIBRARY NAMES
curl
# Windows MSVC prebuilts:
curllib
libcurl_imp
curllib_static
# Windows older "Win32 - MSVC" prebuilts (libcurl.lib, e.g. libcurl-7.15.5-win32-msvc.zip):
libcurl
)
mark_as_advanced(CURL_LIBRARY)

if(CURL_INCLUDE_DIR)
foreach(_curl_version_header curlver.h curl.h)
if(EXISTS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}")
file(STRINGS "${CURL_INCLUDE_DIR}/curl/${_curl_version_header}" curl_version_str REGEX "^#define[\t ]+LIBCURL_VERSION[\t ]+\".*\"")

string(REGEX REPLACE "^#define[\t ]+LIBCURL_VERSION[\t ]+\"([^\"]*)\".*" "\\1" CURL_VERSION_STRING "${curl_version_str}")
unset(curl_version_str)
break()
endif()
endforeach()
endif()

include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(CURL
REQUIRED_VARS CURL_LIBRARY CURL_INCLUDE_DIR
VERSION_VAR CURL_VERSION_STRING)

if(CURL_FOUND)
set(CURL_LIBRARIES ${CURL_LIBRARY})
set(CURL_INCLUDE_DIRS ${CURL_INCLUDE_DIR})

if(NOT TARGET CURL::CURL)
add_library(CURL::CURL UNKNOWN IMPORTED)
set_target_properties(CURL::CURL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}")
set_property(TARGET CURL::CURL APPEND PROPERTY IMPORTED_LOCATION "${CURL_LIBRARY}")
endif()
endif()
15 changes: 15 additions & 0 deletions cmake/InfluxDBConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@PACKAGE_INIT@

set(InfluxDB_VERSION @PROJECT_VERSION@)

get_filename_component(InfluxDB_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
include(CMakeFindDependencyMacro)

find_dependency(Boost)
find_dependency(CURL)

if(NOT TARGET InfluxData::InfluxDB)
include("${InfluxDB_CMAKE_DIR}/InfluxDBTargets.cmake")
endif()

message(STATUS "InfluxDB ${InfluxDB_VERSION} found")
72 changes: 72 additions & 0 deletions include/InfluxDB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
///
/// \author Adam Wegrzynek
///

#ifndef INFLUXDATA_INFLUXDB_H
#define INFLUXDATA_INFLUXDB_H

#include <chrono>
#include <memory>
#include <string>
#include <deque>

#include "Transport.h"
#include "Point.h"

namespace influxdb
{

class InfluxDB
{
public:
/// Disable copy constructor
InfluxDB & operator=(const InfluxDB&) = delete;

/// Disable copy constructor
InfluxDB(const InfluxDB&) = delete;

/// Constructor required valid transport
InfluxDB(std::unique_ptr<Transport> transport);

/// Flushes buffer
~InfluxDB();

/// Writes a metric
/// \param metric
void write(Point&& metric);

/// Flushes metric buffer (this can also happens when buffer is full)
void flushBuffer();

/// Enables metric buffering
/// \param size
void enableBuffering(const std::size_t size = 32);

/// Adds a global tag
/// \param name
/// \param value
void addGlobalTag(std::string_view name, std::string_view value);

private:
/// Buffer for points
std::deque<std::string> mBuffer;

/// Flag stating whether point buffering is enabled
bool mBuffering;

/// Buffer size
std::size_t mBufferSize;

/// Underlying transport UDP/HTTP/Unix socket
std::unique_ptr<Transport> mTransport;

/// Transmits string over transport
void transmit(std::string&& point);

/// List of global tags
std::string mGlobalTags;
};

} // namespace influxdb

#endif // INFLUXDATA_INFLUXDB_H
37 changes: 37 additions & 0 deletions include/InfluxDBFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
///
/// \author Adam Wegrzynek
///

#ifndef INFLUXDATA_INFLUXDB_FACTORY_H
#define INFLUXDATA_INFLUXDB_FACTORY_H

#include "InfluxDB.h"
#include "Transport.h"

namespace influxdb
{

/// \brief InfluxDB factory
class InfluxDBFactory
{
public:
/// Disables copy constructor
InfluxDBFactory & operator=(const InfluxDBFactory&) = delete;

/// Disables copy constructor
InfluxDBFactory(const InfluxDBFactory&) = delete;

/// InfluxDB factory
static std::unique_ptr<InfluxDB> Get(std::string url) noexcept(false);

private:
///\return backend based on provided URL
static std::unique_ptr<Transport> GetTransport(std::string url);

/// Private constructor disallows to create instance of Factory
InfluxDBFactory() = default;
};

} // namespace influxdb

#endif // INFLUXDATA_INFLUXDB_FACTORY_H
Loading

0 comments on commit bb7c7af

Please sign in to comment.