Skip to content

Commit 0d34011

Browse files
turtledreamsZahidZafarArtursKadikis
authored
Implementing SQLite based request queue
* the husk * setPath fix * Co-authored-by: ArtursKadikis <[email protected]> * Reworking storage module API and creating implementation for the memory variant (#111) * - polymorphous behavior added - sqlite storge class added - code is building * storage modules integration stable state * - log added * storage unit tests added * unit tests updated * - virtual destructor - sqlite storage tests * files and class names updated * - storage unit tests improved - PR Changes - Code formatting * Log print updated while adding event. * cmakelist file updated * - Abstract methods signatures updated - Stable state * - new methods added - Unit test improved - stable state * - Unit tests add/updated - methods signatures improved - Fixed potential bugs * - logic updated * - Fixed issue with storage moduel - Unit tests * - request remove front code refactored * - Moved to new structure * -Unit tests fixed -Updated logic * memory deallocated * - Pointer to smart pointer * - RQpeekFront would return DataENtry with id = -1 and data = "" - Incremental Id Unit tests added * - make_shard used - peekAll and clearAll * removed logs * storage module unit tests updated * - unit test changes, * - Only unit tests added Note: Code isn't compiling * - unit test comments added - compile errors fixed - All unit tests are doing fine. * Code refactor * PR Changes, * test changes, * - missed requested chagnes * - RQ removed on the basis of id, not ref * Storage module methods documentation added. * request processing is functional * Method comments updated * - fixed unit tests * - fix bug - views test running. * - fixe a potential bug, --------- Co-authored-by: Zahid Zafar <> * View changes added (#113) * views * comments * extracted things (#114) * bits * Co-authored-by: ArtursKadikis <[email protected]> * Adding request queue db variant (#112) * - Request sql calls added * - code compile checks - storage unit tests logic updated * storage tests reworked * - sqlite storage unit tests fixed * - clearSDK - debugEQState - Views tests * - finishing test * - PR Requested changes - raw pointer to smart pointer * Requested changes * - crash tests fixed - sessions tests fixed * - set request queue size - safe check on configuration values - new configuration tests * Request module request threshold unit tests added. * - safety check for storage modules * - unit tests added in the storage module - PR changes - fixed event schema creation * Changelog added SDK Version updated * PR Changes * Update views.cpp * comments and name changes * Co-authored-by: ArtursKadikis <[email protected]> * new vacuum * its a me function --------- Co-authored-by: Zahid Zafar <> Co-authored-by: ArtursK <[email protected]> Co-authored-by: turtledreams <[email protected]> * Update CHANGELOG.md * Update CHANGELOG.md * database --------- Co-authored-by: Zahid Zafar <[email protected]> Co-authored-by: ArtursK <[email protected]>
1 parent e978b57 commit 0d34011

27 files changed

+1908
-80
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
22.09.0
2+
* ! Minor breaking change ! SDK configuration now can't be changed after initialization/start
3+
* Added a persistent requests queue when building the SDK with the 'COUNTLY_USE_SQLITE' flag
4+
* Fixed a bug where view's name was being overriden by segmentation provided.
5+
16
22.06.4
27
* Fixed a bug where the SDK 'mutex' was being locked twice when built with the 'COUNTLY_USE_SQLITE' flag.
38

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ set(COUNTLY_PUBLIC_HEADERS
3636

3737
${CMAKE_CURRENT_SOURCE_DIR}/include/countly/logger_module.hpp
3838
${CMAKE_CURRENT_SOURCE_DIR}/include/countly/crash_module.hpp
39-
${CMAKE_CURRENT_SOURCE_DIR}/include/countly/request_module.hpp
40-
${CMAKE_CURRENT_SOURCE_DIR}/include/countly/request_builder.hpp
4139
${CMAKE_CURRENT_SOURCE_DIR}/include/countly/views_module.hpp)
4240

4341
add_library(countly
@@ -48,6 +46,8 @@ add_library(countly
4846
${CMAKE_CURRENT_SOURCE_DIR}/src/request_module.cpp
4947
${CMAKE_CURRENT_SOURCE_DIR}/src/crash_module.cpp
5048
${CMAKE_CURRENT_SOURCE_DIR}/src/request_builder.cpp
49+
${CMAKE_CURRENT_SOURCE_DIR}/src/storage_module_db.cpp
50+
${CMAKE_CURRENT_SOURCE_DIR}/src/storage_module_memory.cpp
5151
${CMAKE_CURRENT_SOURCE_DIR}/src/event.cpp)
5252

5353
target_include_directories(countly
@@ -107,8 +107,10 @@ if(COUNTLY_BUILD_TESTS)
107107
${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp
108108
${CMAKE_CURRENT_SOURCE_DIR}/tests/views.cpp
109109
${CMAKE_CURRENT_SOURCE_DIR}/tests/session.cpp
110+
${CMAKE_CURRENT_SOURCE_DIR}/tests/storage.cpp
110111
${CMAKE_CURRENT_SOURCE_DIR}/tests/event.cpp
111112
${CMAKE_CURRENT_SOURCE_DIR}/tests/crash.cpp
113+
${CMAKE_CURRENT_SOURCE_DIR}/tests/request.cpp
112114
${CMAKE_CURRENT_SOURCE_DIR}/tests/config.cpp)
113115

114116
target_compile_definitions(countly-tests PRIVATE COUNTLY_BUILD_TESTS)

bin/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Build Scripts
2+
3+
Scripts provided here offers a convenient way to build sample app and the tests at different platforms.
4+
Still SQLite setup must be done separately at the "/vendor/sqlite" directory if SQLite is needed.
5+
Current cmake options set in the script that differs from the default are:
6+
7+
- COUNTLY_BUILD_SAMPLE = ON
8+
- COUNTLY_BUILD_TESTS = ON
9+
10+
To change any other options you can manually modify the scripts.
11+
12+
### Run
13+
14+
You can run the scripts simply by executing them from the root directory.
15+
16+
### Flow
17+
18+
When run the script would:
19+
20+
- Check if a build folder is present at the root and erase it if it exists
21+
- Run Cmake with mentioned options
22+
- Build countly-sample app
23+
- Build countly-test
24+
25+
After this point you can just go to the "/build" directory and execute those build files

bin/mac_build_script.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
if [ "${PWD##*/}" = "build" ]
4+
then
5+
echo "In build folder. Changing directory..."
6+
cd ..
7+
echo "Current directory: $PWD"
8+
else
9+
echo "Not in build folder. Current directory: $PWD"
10+
fi
11+
12+
echo "Deleting folder and its subfolders..."
13+
rm -rf build
14+
echo "Folder and subfolders deleted."
15+
16+
cmake -DCOUNTLY_BUILD_SAMPLE=1 -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_CUSTOM_SHA256=OFF -DCOUNTLY_USE_SQLITE=OFF -B build .
17+
18+
cd build
19+
make ./countly-sample
20+
make ./countly-tests

bin/windows_build_script.bat

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@echo off
2+
if /i "%CD:~-5%"=="build" (
3+
echo In build folder. Changing directory...
4+
cd ..
5+
echo Current directory: %CD%
6+
) else (
7+
echo Not in build folder. Current directory: %CD%
8+
)
9+
echo Deleting folder and its subfolders...
10+
rmdir /s /q build
11+
echo Folder and subfolders deleted.
12+
cmake -DCOUNTLY_BUILD_SAMPLE=1 -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_CUSTOM_SHA256=OFF -DCOUNTLY_USE_SQLITE=OFF -B build . -G "Unix Makefiles"
13+
cd build
14+
make ./countly-sample
15+
make ./countly-tests

examples/example_integration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ int main() {
3434
cout << "Sample App" << endl;
3535
Countly &ct = Countly::getInstance();
3636
ct.alwaysUsePost(true);
37+
ct.setLogger(printLog);
38+
ct.SetPath("databaseFileName.db"); // this will be only built into account if the correct configurations are set
3739
ct.setDeviceID("test-device-id");
3840
// ct.setSalt("test-salt");
39-
40-
ct.setLogger(printLog);
4141
// OS, OS_version, device, resolution, carrier, app_version);
4242
ct.SetMetrics("Windows 10", "10.22", "Mac", "800x600", "Carrier", "1.0");
4343
// Server and port

include/countly.hpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@
2222
#endif
2323
#include "countly/event.hpp"
2424
#include "countly/logger_module.hpp"
25+
#include "countly/storage_module_base.hpp"
2526
#include "countly/views_module.hpp"
27+
#include <countly/crash_module.hpp>
2628
#include <countly/request_builder.hpp>
2729
#include <countly/request_module.hpp>
28-
#include <countly/crash_module.hpp>
2930

3031
namespace cly {
3132
class Countly : public cly::CountlyDelegates {
3233
public:
3334
Countly();
3435

3536
~Countly();
37+
38+
// Returns the singleton instance of Countly
3639
static Countly &getInstance();
3740

3841
// Do not implicitly generate the copy constructor, this is a singleton.
@@ -43,6 +46,8 @@ class Countly : public cly::CountlyDelegates {
4346

4447
void alwaysUsePost(bool value);
4548

49+
void setMaxRequestQueueSize(unsigned int requestQueueSize);
50+
4651
void setSalt(const std::string &value);
4752

4853
void setLogger(void (*fun)(LogLevel level, const std::string &message));
@@ -93,6 +98,8 @@ class Countly : public cly::CountlyDelegates {
9398

9499
void addEvent(const cly::Event &event);
95100

101+
void addEventToSqlite(const cly::Event &event);
102+
96103
void setMaxEvents(size_t value);
97104

98105
void flushEvents(std::chrono::seconds timeout = std::chrono::seconds(30));
@@ -179,7 +186,14 @@ class Countly : public cly::CountlyDelegates {
179186
}
180187

181188
/* Provide 'updateInterval' in seconds. */
182-
inline void setAutomaticSessionUpdateInterval(unsigned short updateInterval) { configuration->sessionDuration = updateInterval; }
189+
inline void setAutomaticSessionUpdateInterval(unsigned short updateInterval) {
190+
if (is_sdk_initialized) {
191+
log(LogLevel::WARNING, "[Countly][setAutomaticSessionUpdateInterval] You can not set the session duration after SDK initialization.");
192+
return;
193+
}
194+
195+
configuration->sessionDuration = updateInterval;
196+
}
183197

184198
#ifdef COUNTLY_BUILD_TESTS
185199
/**
@@ -188,15 +202,7 @@ class Countly : public cly::CountlyDelegates {
188202
* You should not be using this method.
189203
* @return a vector object containing events.
190204
*/
191-
const std::vector<std::string> debugReturnStateOfEQ() {
192-
193-
#ifdef COUNTLY_USE_SQLITE
194-
return {};
195-
#else
196-
std::vector<std::string> v(event_queue.begin(), event_queue.end());
197-
return v;
198-
#endif
199-
}
205+
std::vector<std::string> debugReturnStateOfEQ();
200206

201207
/**
202208
* This function should not be used as it will be removed in a future release.
@@ -229,6 +235,9 @@ class Countly : public cly::CountlyDelegates {
229235
void _deleteThread();
230236
void _sendIndependantLocationRequest();
231237
void log(LogLevel level, const std::string &message);
238+
#ifdef COUNTLY_USE_SQLITE
239+
bool createEventTableSchema();
240+
#endif
232241

233242
/**
234243
* Helper methods to fetch remote config from the server.
@@ -257,11 +266,12 @@ class Countly : public cly::CountlyDelegates {
257266
std::unique_ptr<std::thread> thread;
258267
std::unique_ptr<cly::CrashModule> crash_module;
259268
std::unique_ptr<cly::ViewsModule> views_module;
269+
260270
std::shared_ptr<cly::CountlyConfiguration> configuration;
261271
std::shared_ptr<cly::LoggerModule> logger;
262-
263272
std::shared_ptr<cly::RequestBuilder> requestBuilder;
264273
std::shared_ptr<cly::RequestModule> requestModule;
274+
std::shared_ptr<cly::StorageModuleBase> storageModule;
265275
std::shared_ptr<std::mutex> mutex = std::make_shared<std::mutex>();
266276

267277
bool is_queue_being_processed = false;

include/countly/constants.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#ifndef COUNTLY_CONSTANTS_HPP_
22
#define COUNTLY_CONSTANTS_HPP_
33

4+
#include "nlohmann/json.hpp"
45
#include <cassert>
56
#include <chrono>
7+
#include <climits>
68
#include <functional>
79
#include <map>
810
#include <memory>
911
#include <random>
1012
#include <sstream>
1113
#include <string>
12-
#include <climits>
13-
#include "nlohmann/json.hpp"
1414

1515
#define COUNTLY_SDK_NAME "cpp-native-unknown"
16-
#define COUNTLY_SDK_VERSION "22.06.4"
16+
#define COUNTLY_SDK_VERSION "22.09.0"
1717
#define COUNTLY_POST_THRESHOLD 2000
1818
#define COUNTLY_KEEPALIVE_INTERVAL 3000
1919
#define COUNTLY_MAX_EVENTS_DEFAULT 200
@@ -31,7 +31,7 @@ const std::default_random_engine generator(std::chrono::system_clock::now().time
3131
const std::uniform_int_distribution<int> distribution(1, INT_MAX);
3232

3333
/**
34-
* Convert map into a string.
34+
* Formats the given arguments into a string buffer.
3535
*
3636
* @param format formatting string
3737
* @param args arguments to be formatted
@@ -49,7 +49,7 @@ template <typename... Args> static std::string format_string(const std::string &
4949
}
5050

5151
/**
52-
* Convert map into a string.
52+
* Gives a string representation of the size of a map.
5353
*
5454
* @param m a map containing key-value pairs
5555
* @return a string object holding size of map.

include/countly/countly_configuration.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ struct CountlyConfiguration {
2727
*/
2828
std::string salt;
2929

30+
/**
31+
* Path to the database.
32+
*/
33+
#ifdef COUNTLY_USE_SQLITE
34+
std::string databasePath;
35+
#endif
36+
3037
/**
3138
* Sets the interval for the automatic update calls
3239
* min value 1 (1 second), max value 600 (10 minutes)

include/countly/request_module.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
#include "countly/countly_configuration.hpp"
99
#include "countly/logger_module.hpp"
1010
#include "countly/request_builder.hpp"
11+
#include "countly/storage_module_base.hpp"
1112

1213
namespace cly {
1314
class RequestModule {
1415

1516
public:
1617
~RequestModule();
17-
RequestModule(std::shared_ptr<CountlyConfiguration> config, std::shared_ptr<LoggerModule> logger, std::shared_ptr<RequestBuilder> requestBuilder);
18+
RequestModule(std::shared_ptr<CountlyConfiguration> config, std::shared_ptr<LoggerModule> logger, std::shared_ptr<RequestBuilder> requestBuilder, std::shared_ptr<StorageModuleBase> storageModule);
1819

1920
HTTPResponse sendHTTP(std::string path, std::string data);
2021

0 commit comments

Comments
 (0)