99 lines
2.9 KiB
C++
99 lines
2.9 KiB
C++
#pragma once
|
|
|
|
#include "bts_export.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <stdint.h>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace bts {
|
|
namespace utils {
|
|
|
|
typedef std::string Puid;
|
|
|
|
// because posix file lock semantics are per process (not per thread)
|
|
// this interface must be static see:
|
|
// http://www.boost.org/doc/libs/1_49_0/doc/html/interprocess/synchronization_mechanisms.html
|
|
|
|
// generate Probably Unique Ids. The ids are constructed using the
|
|
// machine's ip, the unix time (seconds since 1970) and a machine
|
|
// unique counter. id's are 20 bytes long and can contain [a-z], [A-Z]
|
|
// [0-9] and -
|
|
BTS_EXPORT std::vector<Puid> gen_puids(std::string const &scope, int n_puids);
|
|
BTS_EXPORT Puid gen_puid(std::string const &scope);
|
|
|
|
// generate machine unique ids (no guarantees of uniqueness across
|
|
// machines)
|
|
BTS_EXPORT std::vector<uint64_t> gen_ids(std::string const &scope, int n_ids);
|
|
// Similar to the above but instead of copying the ids to a vector
|
|
// this simply returns the first free id (x) and skips ahead by n_ids
|
|
// so that the next gen_id call will will be x+n_ids
|
|
BTS_EXPORT uint64_t reserve_ids(std::string const &scope, int n_ids);
|
|
// generate a single id
|
|
BTS_EXPORT uint64_t gen_id(std::string const &scope);
|
|
|
|
// convert a uint32 to a 6 byte human readeable string containing only
|
|
// alpha numerical characters.
|
|
BTS_EXPORT std::string to_base_62(uint32_t id);
|
|
BTS_EXPORT void
|
|
to_base_62(std::vector<uint32_t> const &ids, std::vector<char> *out);
|
|
|
|
#ifndef _WIN32
|
|
class BTS_EXPORT IDGenerator {
|
|
public:
|
|
IDGenerator(std::string const &scope, uint32_t batch_size)
|
|
: _batch_size(batch_size), _next(0), _end(0), _scope(scope), _lock() {
|
|
bts_assert((int)batch_size > 0);
|
|
refill();
|
|
}
|
|
|
|
// next should only be called from a single thread
|
|
uint64_t next() {
|
|
if (_remaning() == 0) {
|
|
std::lock_guard _(_lock);
|
|
if (!_next_block.has_value())
|
|
_get_next_block();
|
|
|
|
bts_assert(_next_block.has_value());
|
|
_next = _next_block.value().first;
|
|
_end = _next_block.value().second;
|
|
_next_block.reset();
|
|
}
|
|
bts_assert(_remaning() > 0);
|
|
return _next++;
|
|
}
|
|
|
|
// refill can be called from any thread
|
|
void refill() {
|
|
std::lock_guard _(_lock);
|
|
if (!_next_block.has_value())
|
|
_get_next_block();
|
|
}
|
|
|
|
private:
|
|
uint64_t _remaning() {
|
|
bts_assert(_end >= _next);
|
|
return _end - _next;
|
|
}
|
|
|
|
void _get_next_block() {
|
|
auto block_start = reserve_ids(_scope, _batch_size);
|
|
auto block_end = block_start + _batch_size;
|
|
_next_block = std::make_pair(block_start, block_end);
|
|
}
|
|
|
|
const uint32_t _batch_size;
|
|
uint64_t _next;
|
|
uint64_t _end;
|
|
const std::string _scope;
|
|
std::mutex _lock;
|
|
std::optional<std::pair<uint64_t, uint64_t>> _next_block;
|
|
};
|
|
#endif
|
|
|
|
} // namespace utils
|
|
} // namespace bts
|