nickva commented on a change in pull request #3127:
URL: https://github.com/apache/couchdb/pull/3127#discussion_r485341066



##########
File path: src/couch_views/src/couch_views_batch_impl.erl
##########
@@ -0,0 +1,147 @@
+% 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_views_batch_impl).
+
+-behavior(couch_views_batch).
+
+
+-export([
+    start/2,
+    success/4,
+    failure/2
+]).
+
+
+-include("couch_mrview/include/couch_mrview.hrl").
+
+
+-record(batch_st, {
+    start_time,
+    size,
+    state = search,
+    search_incr,
+    sense_incr,
+    max_tx_size,
+    max_tx_time_msec,
+    threshold_penalty
+}).
+
+
+-spec start(
+        Mrst::#mrst{},
+        State::term()
+    ) -> {NewState::term(), BatchSize::pos_integer()}.
+start(Mrst, undefined) ->
+    St = #batch_st{
+        size = get_config("batch_initial_size", "100"),
+        search_incr = get_config("batch_search_increment", "500"),
+        sense_incr = get_config("batch_sense_increment", "100"),
+        max_tx_size = get_config("batch_max_tx_size", "9000000"),
+        max_tx_time_msec = get_config("batch_max_tx_time_msec", "4500"),
+        threshold_penalty = get_config("batch_threshold_penalty", "0.2")
+    },
+    start(Mrst, validate_opts(St));
+
+start(_Mrst, #batch_st{size = Size} = St) ->
+    NewSt = St#batch_st{
+        start_time = erlang:monotonic_time()
+    },
+    {NewSt, Size}.
+
+
+-spec success(
+        Mrst::#mrst{},
+        TxSize::non_neg_integer(),
+        DocsRead::non_neg_integer(),
+        State::term()
+    ) -> NewState::term().
+success(_Mrst, TxSize, _DocsRead, #batch_st{} = St) ->
+    #batch_st{
+        start_time = StartTime,
+        size = Size,
+        state = State,
+        search_incr = SearchIncr,
+        sense_incr = SenseIncr,
+        max_tx_size = MaxTxSize,
+        max_tx_time_msec = MaxTxTime,
+        threshold_penalty = ThresholdPenalty
+    } = St,
+
+    TxTimeNative = erlang:monotonic_time() - StartTime,
+    TxTime = erlang:convert_time_unit(TxTimeNative, native, millisecond),
+
+    {NewSize, NewState} = case TxSize > MaxTxSize orelse TxTime > MaxTxTime of
+        true ->
+            {round(Size * (1.0 - ThresholdPenalty)), sense};
+        false when State == search ->
+            {Size + SearchIncr, State};
+        false when State == sense ->
+            {Size + SenseIncr, State}
+    end,
+
+    St#batch_st{
+        size = erlang:max(1, NewSize),
+        state = NewState
+    }.
+
+
+-spec failure(Mrst::#mrst{}, State::term()) -> NewState::term().
+failure(_Mrst, #batch_st{} = St) ->
+    St#batch_st{
+        size = erlang:max(1, St#batch_st.size div 2),
+        state = sense
+    }.
+
+
+validate_opts(St) ->
+    #batch_st{
+        size = Size,
+        search_incr = SearchIncr,
+        sense_incr = SenseIncr,
+        max_tx_size = MaxTxSize,
+        max_tx_time_msec = MaxTxTime,
+        threshold_penalty = Penalty
+    } = St,
+    St#batch_st{
+        size = non_neg_integer(Size, batch_initial_size),
+        search_incr = non_neg_integer(SearchIncr, batch_search_increment),
+        sense_incr = non_neg_integer(SenseIncr, batch_sense_increment),
+        max_tx_size = non_neg_integer(MaxTxSize, batch_max_tx_size),
+        max_tx_time_msec = non_neg_integer(MaxTxTime, batch_max_tx_time_msec),
+        threshold_penalty = float_0_to_1(Penalty, batch_threshold_penalty)

Review comment:
       Would this be shorter to combine with `get_config/2`. Maybe something 
like:
   
   ```
   St = #batch_st{
       size = get_config(batch_initial_size, "100", fun non_neg_integer/2),
       search_incr = get_config(batch_search_increment, "500", fun 
non_neg_integer/2),
       ...
   }
   ```
   
   Then have 
   
   ```
   get_config(Key, Default, ValFun) when is_function(ValFun, 2) ->
       Str = config:get("couch_views", atom_to_list(Key), Default),
       ValFun(Str, Key).
   ```
   
   Up to you if you feel like it would eliminate the extra unpacking from 
validation...




----------------------------------------------------------------
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]


Reply via email to