File tree Expand file tree Collapse file tree 1 file changed +17
-7
lines changed Expand file tree Collapse file tree 1 file changed +17
-7
lines changed Original file line number Diff line number Diff line change 30
30
from errors import InvalidId
31
31
32
32
33
+ def _machine_and_pid_bytes ():
34
+ """Get the machine and pid portion of an ObjectId.
35
+ """
36
+ # 3 bytes machine
37
+ machine_hash = _md5func ()
38
+ machine_hash .update (socket .gethostname ())
39
+ bytes = machine_hash .digest ()[0 :3 ]
40
+
41
+ # 2 bytes pid
42
+ bytes += struct .pack (">H" , os .getpid () % 0xFFFF )
43
+ return bytes
44
+
45
+
33
46
class ObjectId (object ):
34
47
"""A Mongo ObjectId.
35
48
"""
36
49
37
50
_inc = 0
38
51
_inc_lock = threading .Lock ()
39
52
53
+ _machine_and_pid_bytes = _machine_and_pid_bytes ()
54
+
40
55
def __init__ (self , id = None ):
41
56
"""Initialize a new ObjectId.
42
57
@@ -62,13 +77,8 @@ def __generate(self):
62
77
# 4 bytes current time
63
78
oid += struct .pack (">i" , int (time .time ()))
64
79
65
- # 3 bytes machine
66
- machine_hash = _md5func ()
67
- machine_hash .update (socket .gethostname ())
68
- oid += machine_hash .digest ()[0 :3 ]
69
-
70
- # 2 bytes pid
71
- oid += struct .pack (">H" , os .getpid () % 0xFFFF )
80
+ # 3 bytes machine + 2 bytes pid
81
+ oid += ObjectId ._machine_and_pid_bytes
72
82
73
83
# 3 bytes inc
74
84
ObjectId ._inc_lock .acquire ()
You can’t perform that action at this time.
0 commit comments