IDGenerator.cpp 1.5 KB
Newer Older
X
Xu Peng 已提交
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
6 7
#include "IDGenerator.h"

8 9
#include <chrono>
#include <assert.h>
10
#include <iostream>
11

X
Xu Peng 已提交
12
namespace zilliz {
J
jinhai 已提交
13
namespace milvus {
X
Xu Peng 已提交
14
namespace engine {
15

16 17
IDGenerator::~IDGenerator() = default;

J
jinhai 已提交
18
constexpr size_t SimpleIDGenerator::MAX_IDS_PER_MICRO;
19

20
IDNumber SimpleIDGenerator::GetNextIDNumber() {
X
Xu Peng 已提交
21 22 23 24
    auto now = std::chrono::system_clock::now();
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(
            now.time_since_epoch()).count();
    return micros * MAX_IDS_PER_MICRO;
25 26
}

27
void SimpleIDGenerator::NextIDNumbers(size_t n, IDNumbers& ids) {
28
    if (n > MAX_IDS_PER_MICRO) {
29 30
        NextIDNumbers(n-MAX_IDS_PER_MICRO, ids);
        NextIDNumbers(MAX_IDS_PER_MICRO, ids);
31 32 33 34 35 36
        return;
    }
    if (n <= 0) {
        return;
    }

X
Xu Peng 已提交
37 38 39
    auto now = std::chrono::system_clock::now();
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(
            now.time_since_epoch()).count();
40 41 42
    micros *= MAX_IDS_PER_MICRO;

    for (int pos=0; pos<n; ++pos) {
43
        ids.push_back(micros+pos);
44
    }
45 46
}

47
void SimpleIDGenerator::GetNextIDNumbers(size_t n, IDNumbers& ids) {
48
    ids.clear();
49
    NextIDNumbers(n, ids);
50 51 52
}


X
Xu Peng 已提交
53
} // namespace engine
J
jinhai 已提交
54
} // namespace milvus
X
Xu Peng 已提交
55
} // namespace zilliz