diff options
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"; +} + |