iilyak commented on a change in pull request #2662: Use `couch_rate` 
application for `couch_view`
URL: https://github.com/apache/couchdb/pull/2662#discussion_r396421664
 
 

 ##########
 File path: src/couch_rate/src/couch_rate_ets.erl
 ##########
 @@ -0,0 +1,126 @@
+% 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_rate_ets).
+
+-include("couch_rate.hrl").
+
+-export([
+    create_tables/0,
+    delete_tables/0,
+    create_if_missing/2,
+    new/2,
+    lookup/2,
+    update/3
+]).
+
+
+-define(SHARDS_N, 16).
+
+-type id() :: term().
+-type state() :: term().
+-type result() :: term().
+-type store_state() :: term().
+
+-compile({parse_transform, lookup_pt}).
+
+%% The following directive would generate following code
+%% table_name(0) -> couch_rate_ets_0;
+%% table_name(1) -> couch_rate_ets_1;
+%% ...
+%% table_name(15) -> couch_rate_ets_15;
+%% table_name(Id) -> {error, Id}.
+-lookup({table_name, ?MODULE, ?SHARDS_N}).
+
+
+-spec create_if_missing(couch_rate:id(), state()) ->
+    ok
+    | {error, term()}.
+
+create_if_missing(Id, State) ->
+    Tid = term_to_table(Id),
+    case ets:lookup(Tid, Id) of
+        [_ | _] -> ok;
+        _ -> ets:insert(Tid, {Id, State})
+    end,
+    ok.
+
+
+-spec new(couch_rate:id(), state()) ->
+    ok
+    | {error, term()}.
+
+new(Id, State) ->
+    Tid = term_to_table(Id),
+    case ets:insert_new(Tid, {Id, State}) of
+        true -> ok;
+        false -> {error, #{reason => already_exists, id => Id}}
+    end.
+
+
+-spec update(id(), store_state(), fun(
+            (id(), state()) -> {state(), result()}
+        )) ->
+            result()
+            | {error, term()}.
+
+update(Id, ok, Fun) ->
 
 Review comment:
   Yes it is a store state as returned from 
https://github.com/apache/couchdb/pull/2662/files/6ffebb3f932db171403aa7768a6c87038c765c07#diff-e3d46220ad28a76b3f69c12cfadda22bR52.
 I'll change `ok` to `_StoreState` and update the type spec for 
`create_if_missing` and `new` since it should be `-> state().` instead of `-> 
ok.`

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to