annotate mod_storage_mongodb/mod_storage_mongodb.lua @ 506:0e07810550c8

mod_storage_mongodb: Use _global as host when none provided
author James Callahan <james@chatid.com>
date Tue, 13 Dec 2011 12:30:01 +1100
parents b6d2ac386120
children 46f578da4ff0
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)
506
0e07810550c8 mod_storage_mongodb: Use _global as host when none provided
James Callahan <james@chatid.com>
parents: 505
diff changeset
17 local host = module.host or "_global";
0e07810550c8 mod_storage_mongodb: Use _global as host when none provided
James Callahan <james@chatid.com>
parents: 505
diff changeset
18 local store = self.store;
504
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
19
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
20 local namespace = params.dbname .. "." .. host;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
21 local v = { _id = { store = store ; username = username } };
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
22
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
23 local cursor , err = conn:query ( namespace , v );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
24 if not cursor then return nil , err end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
25
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
26 local r , err = cursor:next ( );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
27 if not r then return nil , err end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
28 return r.data;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
29 end
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
30
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
31 function keyval_store:set(username, data)
506
0e07810550c8 mod_storage_mongodb: Use _global as host when none provided
James Callahan <james@chatid.com>
parents: 505
diff changeset
32 local host = module.host or "_global";
0e07810550c8 mod_storage_mongodb: Use _global as host when none provided
James Callahan <james@chatid.com>
parents: 505
diff changeset
33 local store = self.store;
504
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
34
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
35 local namespace = params.dbname .. "." .. host;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
36 local v = { _id = { store = store ; username = username } };
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
37
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
38 if next(data) ~= nil then -- set data
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
39 v.data = data;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
40 return conn:insert ( namespace , v );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
41 else -- delete data
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
42 return conn:remove ( namespace , v );
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 end
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
45
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
46 local driver = { name = "mongodb" };
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
47
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
48 function driver:open(store, typ)
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
49 if not typ then -- default key-value store
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
50 return setmetatable({ store = store }, keyval_store);
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
51 end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
52 return nil, "unsupported-store";
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
53 end
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
54
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
55 module:add_item("data-driver", driver);