PageRenderTime 33ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/backends/db/mnesia/uce_file_mnesia.erl

http://github.com/AF83/ucengine
Erlang | 108 lines | 78 code | 13 blank | 17 comment | 3 complexity | dd09f6f2e8198c51ca90eddce9821432 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(uce_file_mnesia).
  19. -include_lib("stdlib/include/qlc.hrl").
  20. -include("uce.hrl").
  21. -behaviour(gen_uce_file).
  22. -export([init/0, drop/0]).
  23. -export([add/2, list/3, get/2, all/1, delete/2]).
  24. init() ->
  25. case mnesia:create_table(uce_file,
  26. [{disc_copies, [node()]},
  27. {type, set},
  28. {attributes, record_info(fields, uce_file)}]) of
  29. {atomic, ok} -> ok;
  30. {aborted, {already_exists, uce_file}} -> ok
  31. end.
  32. add(Domain, #uce_file{id=Id} = File) ->
  33. case mnesia:dirty_write(File#uce_file{id={Id, Domain}}) of
  34. ok ->
  35. {ok, Id};
  36. {aborted, _} ->
  37. throw({error, bad_parameters})
  38. end.
  39. list(Domain, Meeting, Order) ->
  40. FunOrder = case Order of
  41. asc ->
  42. fun(A, B) ->
  43. B#uce_file.datetime > A#uce_file.datetime
  44. end;
  45. desc ->
  46. fun(A, B) ->
  47. B#uce_file.datetime < A#uce_file.datetime
  48. end
  49. end,
  50. Transaction = fun() ->
  51. Query = qlc:q([File || File <- mnesia:table(uce_file), File#uce_file.location == Meeting, element(2, File#uce_file.id) == Domain]),
  52. qlc:eval(qlc:sort(Query, [{order, FunOrder}]))
  53. end,
  54. case mnesia:transaction(Transaction) of
  55. {atomic, Files} ->
  56. {ok, remove_domain_from_id(Files)};
  57. {aborded, Error} ->
  58. ?ERROR_MSG("ERROR ~p~n", [Error]),
  59. throw({error, Error})
  60. end.
  61. all(Domain) ->
  62. case mnesia:dirty_match_object(#uce_file{id={'_', Domain},
  63. name='_',
  64. location='_',
  65. uri='_',
  66. datetime='_',
  67. mime='_',
  68. metadata='_'}) of
  69. Files when is_list(Files) ->
  70. {ok, remove_domain_from_id(Files)};
  71. {aborted, _} ->
  72. throw({error, bad_parameters})
  73. end.
  74. get(Domain, Id) ->
  75. case mnesia:dirty_read(uce_file, {Id, Domain}) of
  76. [File] ->
  77. {ok, remove_domain_from_id(File)};
  78. [] ->
  79. throw({error, not_found});
  80. {aborted, _} ->
  81. throw({error, bad_parameters})
  82. end.
  83. delete(Domain, Id) ->
  84. case mnesia:transaction(fun() ->
  85. mnesia:delete({uce_file, {Id, Domain}})
  86. end) of
  87. {atomic, ok} ->
  88. {ok, deleted};
  89. {aborted, _} ->
  90. throw({error, bad_paramaters})
  91. end.
  92. drop() ->
  93. mnesia:clear_table(uce_file).
  94. remove_domain_from_id(Files) ->
  95. ?REMOVE_ID_FROM_RECORD(Files, uce_file).