1
1
//! ObjectId
2
2
3
- use libc;
4
-
5
3
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
6
4
use std:: { error, fmt, io, result} ;
7
5
8
- use byteorder:: { BigEndian , ByteOrder , LittleEndian } ;
9
- use md5;
6
+ use byteorder:: { BigEndian , ByteOrder } ;
10
7
11
8
use hex:: { self , FromHexError } ;
12
9
13
10
use rand:: { thread_rng, Rng } ;
14
11
15
- use hostname:: get_hostname;
16
12
use time;
17
13
18
14
const TIMESTAMP_SIZE : usize = 4 ;
19
- const MACHINE_ID_SIZE : usize = 3 ;
20
- const PROCESS_ID_SIZE : usize = 2 ;
15
+ const PROCESS_ID_SIZE : usize = 5 ;
21
16
const COUNTER_SIZE : usize = 3 ;
22
17
23
18
const TIMESTAMP_OFFSET : usize = 0 ;
24
- const MACHINE_ID_OFFSET : usize = TIMESTAMP_OFFSET + TIMESTAMP_SIZE ;
25
- const PROCESS_ID_OFFSET : usize = MACHINE_ID_OFFSET + MACHINE_ID_SIZE ;
19
+ const PROCESS_ID_OFFSET : usize = TIMESTAMP_OFFSET + TIMESTAMP_SIZE ;
26
20
const COUNTER_OFFSET : usize = PROCESS_ID_OFFSET + PROCESS_ID_SIZE ;
27
21
28
22
const MAX_U24 : usize = 0xFF_FFFF ;
29
23
30
24
static OID_COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
31
- static mut MACHINE_BYTES : Option < [ u8 ; 3 ] > = None ;
32
25
33
26
/// Errors that can occur during OID construction and generation.
34
27
#[ derive( Debug ) ]
@@ -97,17 +90,13 @@ impl ObjectId {
97
90
/// for more information.
98
91
pub fn new ( ) -> Result < ObjectId > {
99
92
let timestamp = ObjectId :: gen_timestamp ( ) ;
100
- let machine_id = ObjectId :: gen_machine_id ( ) ?;
101
93
let process_id = ObjectId :: gen_process_id ( ) ;
102
94
let counter = ObjectId :: gen_count ( ) ?;
103
95
104
96
let mut buf: [ u8 ; 12 ] = [ 0 ; 12 ] ;
105
97
for i in 0 ..TIMESTAMP_SIZE {
106
98
buf[ TIMESTAMP_OFFSET + i] = timestamp[ i] ;
107
99
}
108
- for i in 0 ..MACHINE_ID_SIZE {
109
- buf[ MACHINE_ID_OFFSET + i] = machine_id[ i] ;
110
- }
111
100
for i in 0 ..PROCESS_ID_SIZE {
112
101
buf[ PROCESS_ID_OFFSET + i] = process_id[ i] ;
113
102
}
@@ -154,20 +143,6 @@ impl ObjectId {
154
143
BigEndian :: read_u32 ( & self . id )
155
144
}
156
145
157
- /// Retrieves the machine id associated with an ObjectId.
158
- pub fn machine_id ( & self ) -> u32 {
159
- let mut buf: [ u8 ; 4 ] = [ 0 ; 4 ] ;
160
- for i in 0 ..MACHINE_ID_SIZE {
161
- buf[ i] = self . id [ MACHINE_ID_OFFSET + i] ;
162
- }
163
- LittleEndian :: read_u32 ( & buf)
164
- }
165
-
166
- /// Retrieves the process id associated with an ObjectId.
167
- pub fn process_id ( & self ) -> u16 {
168
- LittleEndian :: read_u16 ( & self . id [ PROCESS_ID_OFFSET ..] )
169
- }
170
-
171
146
/// Retrieves the increment counter from an ObjectId.
172
147
pub fn counter ( & self ) -> u32 {
173
148
let mut buf: [ u8 ; 4 ] = [ 0 ; 4 ] ;
@@ -193,47 +168,11 @@ impl ObjectId {
193
168
buf
194
169
}
195
170
196
- // Generates a new machine id represented as an MD5-hashed 3-byte-encoded hostname string.
197
- // Represented in Little Endian.
198
- fn gen_machine_id ( ) -> Result < [ u8 ; 3 ] > {
199
- // Short-circuit if machine id has already been calculated.
200
- // Since the generated machine id is not variable, arising race conditions
201
- // will have the same MACHINE_BYTES result.
202
- unsafe {
203
- if let Some ( bytes) = MACHINE_BYTES . as_ref ( ) {
204
- return Ok ( * bytes) ;
205
- }
206
- }
207
-
208
- let hostname = get_hostname ( ) ;
209
- if hostname. is_none ( ) {
210
- return Err ( Error :: HostnameError ) ;
211
- }
212
-
213
- // Hash hostname string
214
- let digest = md5:: compute ( hostname. unwrap ( ) . as_str ( ) ) ;
215
- let hash = format ! ( "{:x}" , digest) ;
216
-
217
- // Re-convert string to bytes and grab first three
218
- let mut bytes = hash. bytes ( ) ;
219
- let mut vec: [ u8 ; 3 ] = [ 0 ; 3 ] ;
220
- for v in & mut vec {
221
- match bytes. next ( ) {
222
- Some ( b) => * v = b,
223
- None => break ,
224
- }
225
- }
226
-
227
- unsafe { MACHINE_BYTES = Some ( vec) } ;
228
- Ok ( vec)
229
- }
230
-
231
- // Gets the process ID and returns it as a 2-byte array.
232
- // Represented in Little Endian.
233
- fn gen_process_id ( ) -> [ u8 ; 2 ] {
234
- let pid = unsafe { libc:: getpid ( ) as u16 } ;
235
- let mut buf: [ u8 ; 2 ] = [ 0 ; 2 ] ;
236
- LittleEndian :: write_u16 ( & mut buf, pid) ;
171
+ // Generate a random 5-byte array.
172
+ fn gen_process_id ( ) -> [ u8 ; 5 ] {
173
+ let rng = thread_rng ( ) . gen_range ( 0 , MAX_U24 ) as u32 ;
174
+ let mut buf: [ u8 ; 5 ] = [ 0 ; 5 ] ;
175
+ BigEndian :: write_u32 ( & mut buf, rng) ;
237
176
buf
238
177
}
239
178
@@ -276,13 +215,6 @@ impl fmt::Debug for ObjectId {
276
215
}
277
216
}
278
217
279
- #[ test]
280
- fn pid_generation ( ) {
281
- let pid = unsafe { libc:: getpid ( ) as u16 } ;
282
- let generated = ObjectId :: gen_process_id ( ) ;
283
- assert_eq ! ( pid, LittleEndian :: read_u16( & generated) ) ;
284
- }
285
-
286
218
#[ test]
287
219
fn count_generated_is_big_endian ( ) {
288
220
let start = 1_122_866 ;
0 commit comments