diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-02 21:00:10 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-02 21:00:10 +0100 |
commit | 676e924f7c5bd780250a80187bfcb74550348af7 (patch) | |
tree | a9ad983eb5ef2e29713d5630270496f24df36a67 /storage.cpp | |
parent | b1c99b0c3b0a8d65f237f507ad238ce441233b3f (diff) |
Added tests
Diffstat (limited to 'storage.cpp')
-rw-r--r-- | storage.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/storage.cpp b/storage.cpp index 6fb5b64..e7e9248 100644 --- a/storage.cpp +++ b/storage.cpp @@ -4,6 +4,7 @@ #include <chrono> #include <iostream> +#include <random> #include <SQLiteCpp/SQLiteCpp.h> @@ -164,3 +165,35 @@ void Storage::setRow(const std::string& id, const std::string& document, int rev throw std::runtime_error("Unable to insert row with id "s + id); } +uint32_t Storage::checksum32(const std::string& s) +{ + uint32_t result{0}; + for (unsigned int i = 0; i < s.size(); i++) { + result = ((result >> 1) | ((result & 1) << 31)) ^ (s[i] & 0xFF); + } + return result & 0x7FFFFFFF; +} + +std::string Storage::generate_id() +{ + static std::random_device r; + static std::default_random_engine e1(r()); + static std::uniform_int_distribution<int> uniform_dist(0, 35); + + // limit tries + for (int j = 0; j < 100000; j++) { + std::string result; + for (int i = 0; i < 6; i++) { + char c{static_cast<char>('0' + uniform_dist(e1))}; + if (c > '9') + c = c - '9' + 'a'; + result.push_back(c); + } + + if (!exists(result)) + return result; + } + + return "endofcodes"; +} + |