/Users/deen/code/yugabyte-db/src/yb/util/oid_generator.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include "yb/util/oid_generator.h" |
34 | | |
35 | | #include <mutex> |
36 | | #include <string> |
37 | | |
38 | | #include <boost/uuid/uuid_generators.hpp> |
39 | | |
40 | | #include "yb/gutil/strings/escaping.h" |
41 | | #include "yb/util/cast.h" |
42 | | #include "yb/util/locks.h" |
43 | | #include "yb/util/thread.h" |
44 | | |
45 | | namespace yb { |
46 | | |
47 | | namespace { |
48 | | |
49 | | class Generator { |
50 | | public: |
51 | 579k | std::string Next(bool binary_id) { |
52 | | // Use the thread id to select a random oid generator. |
53 | 579k | auto& entry = entries_[yb::Thread::UniqueThreadId() % kNumOidGenerators]; |
54 | 579k | boost::uuids::uuid oid; |
55 | 579k | { |
56 | 579k | std::lock_guard<LockType> lock(entry.lock); |
57 | 579k | oid = entry.generator(); |
58 | 579k | } |
59 | | |
60 | 483k | return binary_id ? string(to_char_ptr(oid.data), sizeof(oid.data)) |
61 | 95.9k | : b2a_hex(to_char_ptr(oid.data), sizeof(oid.data)); |
62 | 579k | } |
63 | | |
64 | | private: |
65 | | typedef simple_spinlock LockType; |
66 | | |
67 | | // Multiple instances of OID generators with corresponding locks are used to |
68 | | // avoid bottlenecking on a single lock. |
69 | | static const int kNumOidGenerators = 17; |
70 | | struct Entry { |
71 | | LockType lock; |
72 | | boost::uuids::random_generator generator; |
73 | | }; |
74 | | Entry entries_[kNumOidGenerators]; |
75 | | }; |
76 | | |
77 | | } // namespace |
78 | | |
79 | | // Generates a unique 32byte id, based on uuid v4. |
80 | | // This class is thread safe |
81 | | |
82 | 579k | std::string GenerateObjectId(bool binary_id) { |
83 | 579k | static Generator generator; |
84 | 579k | return generator.Next(binary_id); |
85 | 579k | } |
86 | | |
87 | | } // namespace yb |