diff --git a/lib/resty/mongol/ll.lua b/lib/resty/mongol/ll.lua index ec06645..49312c9 100644 --- a/lib/resty/mongol/ll.lua +++ b/lib/resty/mongol/ll.lua @@ -31,7 +31,6 @@ local num_to_le_uint = function ( n , bytes ) for i=1 , bytes do b [ i ] , n = n % 2^8 , floor ( n / 2^8 ) end - assert ( n == 0 ) return strchar ( unpack ( b ) ) end local num_to_le_int = function ( n , bytes ) @@ -57,7 +56,6 @@ local num_to_be_uint = function ( n , bytes ) for i=bytes , 1 , -1 do b [ i ] , n = n % 2^8 , floor ( n / 2^8 ) end - assert ( n == 0 ) return strchar ( unpack ( b ) ) end diff --git a/lib/resty/mongol/object_id.lua b/lib/resty/mongol/object_id.lua index fc3f2bb..88f6b52 100755 --- a/lib/resty/mongol/object_id.lua +++ b/lib/resty/mongol/object_id.lua @@ -46,20 +46,27 @@ local object_id_mt = { } local machineid -if hasposix then - machineid = posix.uname("%n") -else - machineid = assert(io.popen("uname -n")):read("*l") +local function get_os_machineid() + if hasposix then + machineid = posix.uname("%n") + else + machineid = assert(io.popen("uname -n")):read("*l") + end + machineid = ngx.md5_bin(machineid):sub(1, 3) + return machineid end -machineid = ngx.md5_bin(machineid):sub(1, 3) -local pid = num_to_le_uint(ngx.var.pid, 2) +local pid +local function get_os_pid() + pid = num_to_le_uint(ngx.var.pid, 2) + return pid +end local inc = 0 -local function generate_id ( ) +local function generate_id() inc = inc + 1 -- "A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON" - return num_to_be_uint ( os.time ( ) , 4 ) .. machineid .. pid .. num_to_be_uint ( inc , 3 ) + return num_to_be_uint(os.time(), 4) .. (machineid or get_os_machineid()) .. (pid or get_os_pid()) .. num_to_be_uint(inc, 3) end local function new_object_id(str)