IDGenerator.h 1.9 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

X
Xu Peng 已提交
12
#pragma once
13

X
Xu Peng 已提交
14
#include "Types.h"
J
Jin Hai 已提交
15
#include "utils/Status.h"
16

17 18 19
#include <cstddef>
#include <vector>

J
jinhai 已提交
20
namespace milvus {
X
Xu Peng 已提交
21
namespace engine {
22 23

class IDGenerator {
J
jinhai 已提交
24
 public:
S
starlord 已提交
25 26
    virtual IDNumber
    GetNextIDNumber() = 0;
27

J
Jin Hai 已提交
28
    virtual Status
S
starlord 已提交
29
    GetNextIDNumbers(size_t n, IDNumbers& ids) = 0;
30

S
starlord 已提交
31 32
    virtual ~IDGenerator() = 0;
};  // IDGenerator
33 34

class SimpleIDGenerator : public IDGenerator {
J
jinhai 已提交
35 36 37 38 39 40
 public:
    ~SimpleIDGenerator() override = default;

    IDNumber
    GetNextIDNumber() override;

J
Jin Hai 已提交
41
    Status
S
starlord 已提交
42
    GetNextIDNumbers(size_t n, IDNumbers& ids) override;
J
jinhai 已提交
43 44

 private:
J
Jin Hai 已提交
45
    Status
S
starlord 已提交
46
    NextIDNumbers(size_t n, IDNumbers& ids);
47

J
jinhai 已提交
48
    static constexpr size_t MAX_IDS_PER_MICRO = 1000;
S
starlord 已提交
49
};  // SimpleIDGenerator
50

J
Jin Hai 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
class SafeIDGenerator : public IDGenerator {
 public:
    static SafeIDGenerator&
    GetInstance() {
        static SafeIDGenerator instance;
        return instance;
    }

    ~SafeIDGenerator() override = default;

    IDNumber
    GetNextIDNumber() override;

    Status
    GetNextIDNumbers(size_t n, IDNumbers& ids) override;

 private:
    SafeIDGenerator() = default;

    Status
    NextIDNumbers(size_t n, IDNumbers& ids);

    static constexpr size_t MAX_IDS_PER_MICRO = 1000;

    std::mutex mtx_;
    int64_t time_stamp_ms_ = 0;
};

S
starlord 已提交
79 80
}  // namespace engine
}  // namespace milvus