Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
71086fc
Merge branch 'dome'
paynechu Aug 23, 2013
4758294
- fixed random crash by ll.lua's assert
paynechu Aug 23, 2013
9a2724d
- fixed to_bson not serialize lua table begin from 1
paynechu Aug 25, 2013
8317a43
Added 32bit & 64bit int support
Sep 18, 2013
3d59d51
to_bson userdata = null
Sep 18, 2013
abeb135
check for empty array with ascii 5 if so insert an empty array not ob…
Sep 18, 2013
87f214e
Added query example with order and regex
Sep 20, 2013
82093f7
fic bson & add more exaples
Sep 20, 2013
4c35e06
Update bson.lua
Sep 23, 2013
e053b39
More examples
Sep 23, 2013
8121adf
Start lua index from 1 but bson from 0
Oct 8, 2013
8bb5e7e
- added Array class support
paynechu Oct 24, 2013
8192d93
- fixed read_document, forgot to convert the array to start from 1
paynechu Oct 24, 2013
5497895
- fixed to_bson dead loop when handle general table
paynechu Oct 24, 2013
4cb7226
- removed general table support, because it's difficult work with JSO…
paynechu Oct 24, 2013
8332d8f
- adjusted object_id tostring and new to use base64
paynechu Oct 30, 2013
c267f93
- changed * to .
paynechu Oct 30, 2013
f813d4d
- refactored object_id to ObjectId
paynechu Oct 31, 2013
c803a32
- changed . to _
paynechu Oct 31, 2013
874e82e
- revised to return nil with error string instead of assert
paynechu Oct 31, 2013
adead55
- minor change for better coding
paynechu Oct 31, 2013
d440a49
- fixed incorrectly reversed + / and ! _
paynechu Nov 4, 2013
4b0dcfb
- FIX: cjson already referenced to __tojson for serialization instead…
paynechu Dec 19, 2013
07b00b8
update file_size after write
Jan 10, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ Notes
Known Issues
---------------------------
1. Auth bson message has reduntant value.
2. Could not inserting a null array, it always inserted as a document.
3. Gridfs_new api only create a meta info in file_col.
2. Gridfs_new api only create a meta info in file_col.

Example

Basic Usage
---------------------------
local mongo = require "resty.mongol"
conn = mongo:new()
Expand All @@ -252,7 +252,11 @@ Example

r = col:find_one({name="dog"})
ngx.say(r["name"])


See wiki for advanced examples


For Test Case
--------------------
#####mongo config:
Expand Down
104 changes: 104 additions & 0 deletions lib/resty/mongol/ObjectId.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
local type = type
local require = require
local assert = assert
local setmetatable = setmetatable

local string_byte = string.byte
local string_format = string.format
local table_insert = table.insert
local table_concat = table.concat
local os_time = os.time
local io_popen = io.popen

local hasposix, posix = pcall(require , "posix")

local ngx = ngx
local ngx_decode_base64 = ngx.decode_base64
local ngx_encode_base64 = ngx.encode_base64
local ngx_md5_bin = ngx.md5_bin

module(...)

local ll = require(_PACKAGE.."ll")
local num_to_le_uint = ll.num_to_le_uint
local num_to_be_uint = ll.num_to_be_uint
local be_uint_tonum = ll.be_uint_to_num

local machineid
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

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()
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 or _get_os_machineid()) .. (pid or _get_os_pid()) .. num_to_be_uint(inc, 3)
end

local function _new(cls, id)
if id then
if type(id) == 'string' then
local len = #id
if len == 12 then
elseif len == 16 then
id = ngx_decode_base64((id:gsub('!', '+'):gsub('_', '/')))
else
return nil, 'expecting string is 12 or 16 length'
end
else
return nil, 'expecting id is string or nil'
end
end
local o = { id = id or _generate_id() }
setmetatable(o, cls)
return o
end

setmetatable(_M, { __call = _new })

__index = _M

__tostring = function(ob)
return ngx_encode_base64(ob.id):gsub('%+', '!'):gsub('/', '_')
end

__tojson = __tostring

__eq = function (a, b)
return a.id == b.id
end

tostring = __tostring

function get_ts(ob)
return be_uint_to_num(ob.id, 1, 4)
end

function get_pid(ob)
return be_uint_to_num(ob.id, 8, 9)
end

function get_hostname(ob)
local t = {}
for i = 5, 7 do
table_insert(t, string_format("%02x", string_byte(ob.id, i, i)))
end
return table_concat(t)
end

function get_inc(ob)
return be_uint_to_num(ob.id, 10, 12)
end
220 changes: 0 additions & 220 deletions lib/resty/mongol/bson-lua/bson.lua

This file was deleted.

Loading