PageRenderTime 10ms CodeModel.GetById 6ms RepoModel.GetById 0ms 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. -export([init/2,
  20. drop/0,
  21. terminate/0]).
  22. -include("uce.hrl").
  23. -include("mongodb.hrl").
  24. create_indexes(0, _Domain) ->
  25. ok;
  26. create_indexes(1, Domain) ->
  27. Modules = [uce_event_mongodb, uce_user_mongodb, uce_role_mongodb],
  28. [Module:index(Domain) || Module <- Modules].
  29. %%--------------------------------------------------------------------
  30. %% @spec (Domain::list, MongoPoolInfos::list) -> any()
  31. %% @doc Initialize mongodb dedicated connections pool for the given domain (vhost).
  32. %% @end
  33. %%--------------------------------------------------------------------
  34. init(Domain, MongoPoolInfos) ->
  35. catch application:start(emongo),
  36. [Size, Host, Port, Name, Index] = utils:get_values(MongoPoolInfos,
  37. [{size, "1"},
  38. {host, "localhost"},
  39. {port, ?DEFAULT_MONGODB_PORT},
  40. {database, ?DEFAULT_MONGODB_NAME},
  41. {index, 1}]),
  42. emongo:add_pool(Domain, Host, Port, Name, Size),
  43. create_indexes(Index, Domain).
  44. %%--------------------------------------------------------------------
  45. %% @spec () -> any()
  46. %% @doc Disconnect from mongodb by dropping all connections pool.
  47. %% @end
  48. %%--------------------------------------------------------------------
  49. drop() ->
  50. lists:foreach(fun({Domain, _}) ->
  51. catch emongo:drop_database(Domain)
  52. end,
  53. config:get('hosts')).
  54. %%--------------------------------------------------------------------
  55. %% @spec () -> ok
  56. %% @doc Terminate.
  57. %% @end
  58. %%--------------------------------------------------------------------
  59. terminate() ->
  60. ok.