IDGenerator.cpp 1.9 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

S
starlord 已提交
18
#include "db/IDGenerator.h"
19

20
#include <assert.h>
S
starlord 已提交
21
#include <chrono>
22
#include <iostream>
23

J
jinhai 已提交
24
namespace milvus {
X
Xu Peng 已提交
25
namespace engine {
26

27 28
IDGenerator::~IDGenerator() = default;

J
jinhai 已提交
29
constexpr size_t SimpleIDGenerator::MAX_IDS_PER_MICRO;
30

S
starlord 已提交
31 32
IDNumber
SimpleIDGenerator::GetNextIDNumber() {
X
Xu Peng 已提交
33
    auto now = std::chrono::system_clock::now();
S
starlord 已提交
34
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
X
Xu Peng 已提交
35
    return micros * MAX_IDS_PER_MICRO;
36 37
}

S
starlord 已提交
38
void
S
starlord 已提交
39
SimpleIDGenerator::NextIDNumbers(size_t n, IDNumbers& ids) {
40
    if (n > MAX_IDS_PER_MICRO) {
S
starlord 已提交
41
        NextIDNumbers(n - MAX_IDS_PER_MICRO, ids);
42
        NextIDNumbers(MAX_IDS_PER_MICRO, ids);
43 44 45 46 47 48
        return;
    }
    if (n <= 0) {
        return;
    }

X
Xu Peng 已提交
49
    auto now = std::chrono::system_clock::now();
S
starlord 已提交
50
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
51 52
    micros *= MAX_IDS_PER_MICRO;

S
starlord 已提交
53 54
    for (int pos = 0; pos < n; ++pos) {
        ids.push_back(micros + pos);
55
    }
56 57
}

S
starlord 已提交
58
void
S
starlord 已提交
59
SimpleIDGenerator::GetNextIDNumbers(size_t n, IDNumbers& ids) {
60
    ids.clear();
61
    NextIDNumbers(n, ids);
62 63
}

S
starlord 已提交
64 65
}  // namespace engine
}  // namespace milvus