/src/antidote_pb_process.erl

https://github.com/SyncFree/antidote · Erlang · 134 lines · 89 code · 17 blank · 28 comment · 1 complexity · f41ed72ce46f669825c87b3a8589d75a MD5 · raw file

  1. %% -------------------------------------------------------------------
  2. %%
  3. %% Copyright <2013-2018> <
  4. %% Technische Universität Kaiserslautern, Germany
  5. %% Université Pierre et Marie Curie / Sorbonne-Université, France
  6. %% Universidade NOVA de Lisboa, Portugal
  7. %% Université catholique de Louvain (UCL), Belgique
  8. %% INESC TEC, Portugal
  9. %% >
  10. %%
  11. %% This file is provided to you under the Apache License,
  12. %% Version 2.0 (the "License"); you may not use this file
  13. %% except in compliance with the License. You may obtain
  14. %% a copy of the License at
  15. %%
  16. %% http://www.apache.org/licenses/LICENSE-2.0
  17. %%
  18. %% Unless required by applicable law or agreed to in writing,
  19. %% software distributed under the License is distributed on an
  20. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  21. %% KIND, either expressed or implied. See the License for the
  22. %% specific language governing permissions and limitations
  23. %% under the License.
  24. %%
  25. %% List of the contributors to the development of Antidote: see AUTHORS file.
  26. %% Description and complete License: see LICENSE file.
  27. %% -------------------------------------------------------------------
  28. -module(antidote_pb_process).
  29. -ifdef(TEST).
  30. -compile([export_all]).
  31. -include_lib("eunit/include/eunit.hrl").
  32. -endif.
  33. -include("antidote.hrl").
  34. -include_lib("kernel/include/logger.hrl").
  35. -export([process/1]).
  36. -spec from_bin(binary()) -> snapshot_time() | ignore | txid().
  37. from_bin(Clock) ->
  38. case Clock of
  39. undefined -> ignore;
  40. _ -> binary_to_term(Clock)
  41. end.
  42. -spec encode_clock(snapshot_time() | txid()) -> binary().
  43. encode_clock(TxId) ->
  44. term_to_binary(TxId).
  45. -spec process(antidote_pb_codec:request()) -> antidote_pb_codec:response_in().
  46. process({start_transaction, Clock, Properties}) ->
  47. Response = antidote:start_transaction(from_bin(Clock), Properties),
  48. case Response of
  49. {ok, TxId} -> {start_transaction_response, {ok, encode_clock(TxId)}};
  50. {error, Reason} -> {start_transaction_response, {error, Reason}}
  51. end;
  52. process({abort_transaction, TxId}) ->
  53. Response = antidote:abort_transaction(from_bin(TxId)),
  54. case Response of
  55. ok -> {operation_response, ok};
  56. {error, Reason} -> {operation_response, {error, Reason}}
  57. %% TODO: client initiated abort is not implemented yet
  58. end;
  59. process({commit_transaction, TxId}) ->
  60. Response = antidote:commit_transaction(from_bin(TxId)),
  61. case Response of
  62. {ok, CommitTime} -> {commit_transaction_response, {ok, encode_clock(CommitTime)}};
  63. {error, Reason} -> {commit_transaction_response, {error, Reason}}
  64. end;
  65. process({update_objects, Updates, TxId}) ->
  66. Response = antidote:update_objects(Updates, from_bin(TxId)),
  67. case Response of
  68. {error, Reason} -> {operation_response, {error, Reason}};
  69. ok -> {operation_response, ok}
  70. end;
  71. process({static_update_objects, Clock, Properties, Updates}) ->
  72. Response = antidote:update_objects(from_bin(Clock), Properties, Updates),
  73. case Response of
  74. {ok, CommitTime} -> {commit_transaction_response, {ok, encode_clock(CommitTime)}};
  75. {error, Reason} -> {commit_transaction_response, {error, Reason}}
  76. end;
  77. process({read_objects, Objects, TxId}) ->
  78. Response = antidote:read_objects(Objects, from_bin(TxId)),
  79. case Response of
  80. {ok, Results} -> {read_objects_response, {ok, lists:zip(Objects, Results)}};
  81. {error, Reason} -> {read_objects_response, {error, Reason}}
  82. end;
  83. process({static_read_objects, Clock, Properties, Objects}) ->
  84. Response = antidote:read_objects(from_bin(Clock), Properties, Objects),
  85. case Response of
  86. {ok, Results, CommitTime} -> {static_read_objects_response, {lists:zip(Objects, Results), encode_clock(CommitTime)}};
  87. {error, Reason} -> {static_read_objects_response, {error, Reason}}
  88. end;
  89. process({create_dc, NodeNames}) ->
  90. try
  91. ok = antidote_dc_manager:create_dc(NodeNames),
  92. {create_dc_response, ok}
  93. catch
  94. Error:Reason ->
  95. ?LOG_ERROR("Create DC failed ~p : ~p", [Error, Reason]),
  96. {create_dc_response, {error, aborted}}
  97. end;
  98. process(get_connection_descriptor) ->
  99. try
  100. {ok, Descriptor} = antidote_dc_manager:get_connection_descriptor(),
  101. {get_connection_descriptor_response, {ok, term_to_binary(Descriptor)}}
  102. catch
  103. Error:Reason ->
  104. ?LOG_ERROR("Get Conection Descriptor failed ~p : ~p", [Error, Reason]),
  105. {get_connection_descriptor_response, {error, aborted}}
  106. end;
  107. process({connect_to_dcs, BinDescriptors}) ->
  108. try
  109. Descriptors = [binary_to_term(D) || D <- BinDescriptors],
  110. ?LOG_INFO("Connection to DCs: ~p", [Descriptors]),
  111. ok = antidote_dc_manager:subscribe_updates_from(Descriptors),
  112. {connect_to_dcs_response, ok}
  113. catch
  114. Error:Reason ->
  115. ?LOG_ERROR("Connect to DCs failed ~p : ~p", [Error, Reason]),
  116. {connect_to_dcs_response, {error, aborted}}
  117. end.