nickva commented on code in PR #5602:
URL: https://github.com/apache/couchdb/pull/5602#discussion_r2317657477


##########
src/couch_srt/src/couch_srt_server.erl:
##########
@@ -0,0 +1,318 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_srt_server).
+
+-behaviour(gen_server).
+
+-export([
+    start_link/0,
+    init/1,
+    handle_call/3,
+    handle_cast/2
+]).
+
+-export([
+    create_pid_ref/0,
+    create_resource/1,
+    destroy_resource/1,
+    get_resource/1,
+    get_context_type/1,
+    inc/2,
+    inc/3,
+    match_resource/1,
+    new_context/2,
+    set_context_dbname/2,
+    set_context_handler_fun/2,
+    set_context_type/2,
+    set_context_username/2,
+    update_counter/3,
+    update_counter/4,
+    update_counters/2,
+    update_counters/3
+]).
+
+-include_lib("stdlib/include/ms_transform.hrl").
+-include_lib("couch_srt.hrl").
+
+-record(st, {}).
+
+%%
+%% Public API
+%%
+
+-spec create_pid_ref() -> pid_ref().
+create_pid_ref() ->
+    {self(), make_ref()}.
+
+%%
+%%
+%% Context lifecycle API
+%%
+
+-spec new_context(Type :: rctx_type(), Nonce :: nonce()) -> rctx().
+new_context(Type, Nonce) ->
+    #rctx{
+        nonce = Nonce,
+        pid_ref = create_pid_ref(),
+        type = Type
+    }.
+
+-spec set_context_dbname(DbName, PidRef) -> boolean() when
+    DbName :: dbname(), PidRef :: maybe_pid_ref().
+set_context_dbname(_, undefined) ->
+    false;
+set_context_dbname(DbName, PidRef) ->
+    update_element(PidRef, [{#rctx.dbname, DbName}]).
+
+-spec set_context_handler_fun({Mod, Func}, PidRef) -> boolean() when
+    Mod :: atom(), Func :: atom(), PidRef :: maybe_pid_ref().
+set_context_handler_fun(_, undefined) ->
+    false;
+set_context_handler_fun({Mod, Func}, PidRef) ->
+    case get_resource(PidRef) of
+        undefined ->
+            false;
+        #rctx{} = Rctx ->
+            %% TODO: #coordinator{} assumption needs to adapt for other types
+            case couch_srt_server:get_context_type(Rctx) of
+                #coordinator{} = Coordinator0 ->
+                    Coordinator = Coordinator0#coordinator{mod = Mod, func = 
Func},
+                    set_context_type(Coordinator, PidRef);
+                _ ->
+                    false
+            end
+    end.
+
+-spec set_context_username(UserName, PidRef) -> boolean() when
+    UserName :: username(), PidRef :: maybe_pid_ref().
+set_context_username(_, undefined) ->
+    false;
+set_context_username(UserName, PidRef) ->
+    update_element(PidRef, [{#rctx.username, UserName}]).
+
+-spec get_context_type(Rctx :: rctx()) -> rctx_type().
+get_context_type(#rctx{type = Type}) ->
+    Type.
+
+-spec set_context_type(Type, PidRef) -> boolean() when
+    Type :: rctx_type(), PidRef :: maybe_pid_ref().
+set_context_type(Type, PidRef) ->
+    update_element(PidRef, [{#rctx.type, Type}]).
+
+-spec create_resource(Rctx :: rctx()) -> boolean().
+create_resource(#rctx{} = Rctx) ->
+    (catch ets:insert(?CSRT_ETS, Rctx)) == true.
+
+-spec destroy_resource(PidRef :: maybe_pid_ref()) -> boolean().
+destroy_resource(undefined) ->
+    false;
+destroy_resource({_, _} = PidRef) ->
+    (catch ets:delete(?CSRT_ETS, PidRef)) == true.

Review Comment:
   It may be better to be explicit about handling missing ets table and catch 
error:badarg only



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to