syn_backbone.erl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2019 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. -module(syn_backbone).
  27. %% API
  28. -export([init/0]).
  29. -export([deinit/0]).
  30. %% includes
  31. -include("syn.hrl").
  32. %% ===================================================================
  33. %% API
  34. %% ===================================================================
  35. -spec init() -> ok | {error, Reason :: any()}.
  36. init() ->
  37. case create_registry_table() of
  38. {atomic, ok} ->
  39. case create_groups_table() of
  40. {atomic, ok} -> ok;
  41. {aborted, Reason} -> {error, {could_not_create_syn_groups_table, Reason}}
  42. end;
  43. {aborted, Reason} ->
  44. {error, {could_not_create_syn_registry_table, Reason}}
  45. end.
  46. -spec deinit() -> ok.
  47. deinit() ->
  48. mnesia:delete_table(syn_registry_table),
  49. mnesia:delete_table(syn_groups_table),
  50. ok.
  51. %% ===================================================================
  52. %% Internal
  53. %% ===================================================================
  54. -spec create_registry_table() -> {atomic, ok} | {aborted, Reason :: any()}.
  55. create_registry_table() ->
  56. mnesia:create_table(syn_registry_table, [
  57. {type, set},
  58. {attributes, record_info(fields, syn_registry_table)},
  59. {index, [#syn_registry_table.pid]},
  60. {storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
  61. ]).
  62. -spec create_groups_table() -> {atomic, ok} | {aborted, Reason :: any()}.
  63. create_groups_table() ->
  64. mnesia:create_table(syn_groups_table, [
  65. {type, bag},
  66. {attributes, record_info(fields, syn_groups_table)},
  67. {index, [#syn_groups_table.pid, #syn_groups_table.node]},
  68. {storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
  69. ]).