fix(db): fix id generator runtime error

Former-commit-id: fc964ebe4af0d31527b9b6ec01558d84199efbff
pull/191/head
Xu Peng 2019-04-15 20:45:21 +08:00 committed by xj.lin
parent 803686b8c6
commit fd8365a098
2 changed files with 6 additions and 6 deletions

View File

@ -1,5 +1,6 @@
#include <chrono>
#include <assert.h>
#include <iostream>
#include "id_generators.h"
@ -17,18 +18,17 @@ IDNumber SimpleIDGenerator::getNextIDNumber() {
return micros * MAX_IDS_PER_MICRO;
}
IDNumbers&& SimpleIDGenerator::getNextIDNumbers(size_t n) {
void SimpleIDGenerator::getNextIDNumbers(size_t n, IDNumbers& ids) {
assert(n < MAX_IDS_PER_MICRO);
auto now = std::chrono::system_clock::now();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(
now.time_since_epoch()).count();
micros *= MAX_IDS_PER_MICRO;
IDNumbers ids = IDNumbers(n);
ids.clear();
for (int pos=0; pos<n; ++pos) {
ids[pos] = micros + pos;
ids.push_back(micros+pos);
}
return std::move(ids);
}

View File

@ -10,7 +10,7 @@ namespace engine {
class IDGenerator {
public:
virtual IDNumber getNextIDNumber() = 0;
virtual IDNumbers&& getNextIDNumbers(size_t n_) = 0;
virtual void getNextIDNumbers(size_t n, IDNumbers& ids) = 0;
virtual ~IDGenerator();
@ -20,7 +20,7 @@ public:
class SimpleIDGenerator : public IDGenerator {
public:
virtual IDNumber getNextIDNumber() override;
virtual IDNumbers&& getNextIDNumbers(size_t n_) override;
virtual void getNextIDNumbers(size_t n, IDNumbers& ids) override;
private:
const size_t MAX_IDS_PER_MICRO = 1000;