@@ -205,40 +205,16 @@ defmodule Concord do
205205 result =
206206 cond do
207207 expected != nil ->
208- # Direct CAS — no functions in the command
209- case command ( { :put_if , key , compressed_value , expires_at , expected } , timeout ) do
210- { :ok , :ok , _ } -> :ok
211- { :ok , { :error , reason } , _ } -> { :error , reason }
212- { :timeout , _ } -> { :error , :timeout }
213- { :error , :noproc } -> { :error , :cluster_not_ready }
214- { :error , reason } -> { :error , reason }
215- end
208+ unwrap_command_result ( { :put_if , key , compressed_value , expires_at , expected } , timeout )
216209
217210 condition_fn != nil ->
218- # Evaluate condition pre-consensus, then CAS
219- case get ( key , Keyword . take ( opts , [ :token , :timeout , :consistency ] ) ) do
220- { :ok , current_value } ->
221- if condition_fn . ( current_value ) do
222- case command (
223- { :put_if , key , compressed_value , expires_at , current_value } ,
224- timeout
225- ) do
226- { :ok , :ok , _ } -> :ok
227- { :ok , { :error , reason } , _ } -> { :error , reason }
228- { :timeout , _ } -> { :error , :timeout }
229- { :error , :noproc } -> { :error , :cluster_not_ready }
230- { :error , reason } -> { :error , reason }
231- end
232- else
233- { :error , :condition_failed }
234- end
235-
236- { :error , :not_found } ->
237- { :error , :not_found }
238-
239- { :error , reason } ->
240- { :error , reason }
241- end
211+ evaluate_condition_then_cas (
212+ key ,
213+ fn current -> { :put_if , key , compressed_value , expires_at , current } end ,
214+ condition_fn ,
215+ opts ,
216+ timeout
217+ )
242218
243219 true ->
244220 { :error , :missing_condition }
@@ -288,39 +264,19 @@ defmodule Concord do
288264
289265 start_time = System . monotonic_time ( )
290266
291- # Evaluate condition functions pre-consensus (same pattern as put_if)
292267 result =
293268 cond do
294269 expected != nil ->
295- case command ( { :delete_if , key , expected , nil } , timeout ) do
296- { :ok , :ok , _ } -> :ok
297- { :ok , { :error , reason } , _ } -> { :error , reason }
298- { :timeout , _ } -> { :error , :timeout }
299- { :error , :noproc } -> { :error , :cluster_not_ready }
300- { :error , reason } -> { :error , reason }
301- end
270+ unwrap_command_result ( { :delete_if , key , expected , nil } , timeout )
302271
303272 condition_fn != nil ->
304- case get ( key , Keyword . take ( opts , [ :token , :timeout , :consistency ] ) ) do
305- { :ok , current_value } ->
306- if condition_fn . ( current_value ) do
307- case command ( { :delete_if , key , current_value , nil } , timeout ) do
308- { :ok , :ok , _ } -> :ok
309- { :ok , { :error , reason } , _ } -> { :error , reason }
310- { :timeout , _ } -> { :error , :timeout }
311- { :error , :noproc } -> { :error , :cluster_not_ready }
312- { :error , reason } -> { :error , reason }
313- end
314- else
315- { :error , :condition_failed }
316- end
317-
318- { :error , :not_found } ->
319- { :error , :not_found }
320-
321- { :error , reason } ->
322- { :error , reason }
323- end
273+ evaluate_condition_then_cas (
274+ key ,
275+ fn current -> { :delete_if , key , current , nil } end ,
276+ condition_fn ,
277+ opts ,
278+ timeout
279+ )
324280
325281 true ->
326282 { :error , :missing_condition }
@@ -806,6 +762,32 @@ defmodule Concord do
806762 :ra . process_command ( server_id ( ) , cmd , timeout )
807763 end
808764
765+ defp unwrap_command_result ( cmd , timeout ) do
766+ case command ( cmd , timeout ) do
767+ { :ok , :ok , _ } -> :ok
768+ { :ok , { :error , reason } , _ } -> { :error , reason }
769+ { :timeout , _ } -> { :error , :timeout }
770+ { :error , :noproc } -> { :error , :cluster_not_ready }
771+ { :error , reason } -> { :error , reason }
772+ end
773+ end
774+
775+ # Evaluate a condition function pre-consensus, then issue a CAS command.
776+ # Keeps anonymous functions out of the Raft log.
777+ defp evaluate_condition_then_cas ( key , build_cmd , condition_fn , opts , timeout ) do
778+ case get ( key , Keyword . take ( opts , [ :token , :timeout , :consistency ] ) ) do
779+ { :ok , current_value } ->
780+ if condition_fn . ( current_value ) do
781+ unwrap_command_result ( build_cmd . ( current_value ) , timeout )
782+ else
783+ { :error , :condition_failed }
784+ end
785+
786+ { :error , reason } ->
787+ { :error , reason }
788+ end
789+ end
790+
809791 defp query ( query , timeout , consistency ) do
810792 query_fun = fun ( query )
811793
0 commit comments