PageRenderTime 102ms CodeModel.GetById 61ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/ucengine/src/backends/db/mongodb/mongodb_db.erl

http://github.com/AF83/ucengine
Erlang | 66 lines | 28 code | 6 blank | 32 comment | 1 complexity | 89c7731eec57911fabfe1107515598fc MD5 | raw file
 1%%
 2%%  U.C.Engine - Unified Collaboration Engine
 3%%  Copyright (C) 2011 af83
 4%%
 5%%  This program is free software: you can redistribute it and/or modify
 6%%  it under the terms of the GNU Affero General Public License as published by
 7%%  the Free Software Foundation, either version 3 of the License, or
 8%%  (at your option) any later version.
 9%%
10%%  This program is distributed in the hope that it will be useful,
11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13%%  GNU Affero General Public License for more details.
14%%
15%%  You should have received a copy of the GNU Affero General Public License
16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17%%
18-module(mongodb_db).
19
20-export([init/2,
21         drop/0,
22         terminate/0]).
23
24-include("uce.hrl").
25-include("mongodb.hrl").
26
27create_indexes(0, _Domain) ->
28    ok;
29create_indexes(1, Domain) ->
30    Modules = [uce_event_mongodb, uce_user_mongodb, uce_role_mongodb],
31    [Module:index(Domain) || Module <- Modules].
32
33%%--------------------------------------------------------------------
34%% @spec (Domain::list, MongoPoolInfos::list) -> any()
35%% @doc Initialize mongodb dedicated connections pool for the given domain (vhost).
36%% @end
37%%--------------------------------------------------------------------
38init(Domain, MongoPoolInfos) ->
39    catch application:start(emongo),
40    [Size, Host, Port, Name, Index] = utils:get_values(MongoPoolInfos,
41                                                [{size, "1"},
42                                                 {host, "localhost"},
43                                                 {port, ?DEFAULT_MONGODB_PORT},
44                                                 {database, ?DEFAULT_MONGODB_NAME},
45                                                 {index, 1}]),
46    emongo:add_pool(Domain, Host, Port, Name, Size),
47    create_indexes(Index, Domain).
48
49%%--------------------------------------------------------------------
50%% @spec () -> any()
51%% @doc Disconnect from mongodb by dropping all connections pool.
52%% @end
53%%--------------------------------------------------------------------
54drop() ->
55    lists:foreach(fun({Domain, _}) ->
56                          catch emongo:drop_database(Domain)
57                  end,
58                  config:get('hosts')).
59
60%%--------------------------------------------------------------------
61%% @spec () -> ok
62%% @doc Terminate.
63%% @end
64%%--------------------------------------------------------------------
65terminate() ->
66    ok.