Annotate

mod_storage_mongodb/mod_storage_mongodb.lua @ 508:9831506dcfd6

mod_storage_mongodb: move database connecting to inside driver:open
author James Callahan <james@chatid.com>
date Wed, 14 Dec 2011 12:56:07 +1100
parent 507:46f578da4ff0
child 509:bf2ad6d6c778
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
507
46f578da4ff0 mod_storage_mongodb: assert the configuration options
James Callahan <james@chatid.com>
parents: 506
diff changeset
4 local params = assert ( module:get_option("mongodb") , "mongodb configuration not found" );
504
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
5
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
6 local mongo = require "mongo";
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
7
508
9831506dcfd6 mod_storage_mongodb: move database connecting to inside driver:open
James Callahan <james@chatid.com>
parents: 507
diff changeset
8 local conn
504
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
9
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
10 local keyval_store = {};
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
11 keyval_store.__index = keyval_store;
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 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
14 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
15 local store = self.store;
504
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
16
507
46f578da4ff0 mod_storage_mongodb: assert the configuration options
James Callahan <james@chatid.com>
parents: 506
diff changeset
17 -- The database name can't have a period in it (hence it can't be a host/ip)
504
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
18 local namespace = params.dbname .. "." .. host;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
19 local v = { _id = { store = store ; username = username } };
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
20
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
21 local cursor , err = conn:query ( namespace , v );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
22 if not cursor then return nil , err end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
23
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
24 local r , err = cursor:next ( );
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
25 if not r then return nil , err end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
26 return r.data;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
27 end
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
28
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
29 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
30 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
31 local store = self.store;
504
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
32
507
46f578da4ff0 mod_storage_mongodb: assert the configuration options
James Callahan <james@chatid.com>
parents: 506
diff changeset
33 -- The database name can't have a period in it (hence it can't be a host/ip)
504
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)
508
9831506dcfd6 mod_storage_mongodb: move database connecting to inside driver:open
James Callahan <james@chatid.com>
parents: 507
diff changeset
48 if not conn then
9831506dcfd6 mod_storage_mongodb: move database connecting to inside driver:open
James Callahan <james@chatid.com>
parents: 507
diff changeset
49 conn = assert ( mongo.Connection.New ( true ) );
9831506dcfd6 mod_storage_mongodb: move database connecting to inside driver:open
James Callahan <james@chatid.com>
parents: 507
diff changeset
50 assert ( conn:connect ( params.server ) );
9831506dcfd6 mod_storage_mongodb: move database connecting to inside driver:open
James Callahan <james@chatid.com>
parents: 507
diff changeset
51 assert ( conn:auth ( params ) );
9831506dcfd6 mod_storage_mongodb: move database connecting to inside driver:open
James Callahan <james@chatid.com>
parents: 507
diff changeset
52 end
9831506dcfd6 mod_storage_mongodb: move database connecting to inside driver:open
James Callahan <james@chatid.com>
parents: 507
diff changeset
53
504
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
54 if not typ then -- default key-value store
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
55 return setmetatable({ store = store }, keyval_store);
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
56 end;
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
57 return nil, "unsupported-store";
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
58 end
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
59
0e9b43db7a2c mod_storage_mondodb: Add module
James Callahan <james@chatid.com>
parents:
diff changeset
60 module:add_item("data-driver", driver);