annotate mod_storage_mondodb/mod_storage_mongodb.lua @ 504:0e9b43db7a2c

mod_storage_mondodb: Add module
author James Callahan <james@chatid.com>
date Mon, 12 Dec 2011 21:39:08 +1100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
504
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
1 local next = next;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
2 local setmetatable = setmetatable;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
3
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
4 local log = require "util.logger".init("mongodb");
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
5 local params = module:get_option("mongodb");
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
6
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
7 local mongo = require "mongo";
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
8
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
9 local conn = mongo.Connection.New ( true );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
10 conn:connect ( params.server );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
11 conn:auth ( params );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
12
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
13 local keyval_store = {};
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
14 keyval_store.__index = keyval_store;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
15
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
16 function keyval_store:get(username)
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
17 local host, store = module.host, self.store;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
18
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
19 local namespace = params.dbname .. "." .. host;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
20 local v = { _id = { store = store ; username = username } };
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
21
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
22 local cursor , err = conn:query ( namespace , v );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
23 if not cursor then return nil , err end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
24
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
25 local r , err = cursor:next ( );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
26 if not r then return nil , err end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
27 return r.data;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
28 end
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
29
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
30 function keyval_store:set(username, data)
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
31 local host, store = module.host, self.store;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
32 if not host then return nil , "mongodb cannot currently be used for host-less data" end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
33
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
34 local namespace = params.dbname .. "." .. host;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
35 local v = { _id = { store = store ; username = username } };
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
36
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
37 if next(data) ~= nil then -- set data
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
38 v.data = data;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
39 return conn:insert ( namespace , v );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
40 else -- delete data
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
41 return conn:remove ( namespace , v );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
42 end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
43 end
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
44
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
45 local driver = { name = "mongodb" };
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
46
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
47 function driver:open(store, typ)
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
48 if not typ then -- default key-value store
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
49 return setmetatable({ store = store }, keyval_store);
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
50 end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
51 return nil, "unsupported-store";
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
52 end
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
53
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
54 module:add_item("data-driver", driver);