Skip to content

Commit 8795b1c

Browse files
committed
Make #kpayload_*{} records private
The public API is now: * `khepri_payload:none/0` * `khepri_payload:data/1` * `khepri_payload:sproc/1` That said, the caller should rarely have to use them: `khepri:put/1` tries to automatically detect if and how to wrap the given value. Therefore, one should be able to just use: khepri:put(StoreId, Path, #my_record{...}). Or: khepri:put(StoreId, Path, fun() -> ... end). Only the "no payload" case has no value beside the internal one. In this case, the caller should use: khepri:put(StoreId, Path, khepri_payload:none()). Or: khepri:clear_payload(StoreId, Path). The end goal is to reduce the chance that if we change the payload records, users are forced to recompile their code. It also makes the code maintenance easier for us. Note that in the process, the records are renamed `#p_*{}`.
1 parent 7e8ed85 commit 8795b1c

22 files changed

+500
-490
lines changed

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ khepri:transaction(
143143
%% There is less than 100 pieces of wood, or there is none
144144
%% at all (the node does not exist in Khepri). We need to
145145
%% request a new order.
146-
{ok, _} = khepri_tx:put(
147-
[order, wood],
148-
#kpayload_data{data = 1000}),
146+
{ok, _} = khepri_tx:put([order, wood], 1000),
149147
true
150148
end
151149
end).
@@ -174,10 +172,7 @@ the database itself and automatically execute it after some event occurs.
174172
on_action => Action} = Props
175173
end,
176174

177-
khepri:put(
178-
StoreId,
179-
StoredProcPath,
180-
#kpayload_sproc{sproc = Fun}))}.
175+
khepri:put(StoreId, StoredProcPath, Fun).
181176
```
182177

183178
2. Register a trigger using an event filter:

doc/overview.edoc

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,15 @@ A tree node may or may not have a payload. Khepri supports two types of
8989
payload, the <em>data payload</em> and the <em>stored procedure payload</em>.
9090
More payload types may be added in the future.
9191

92-
Payloads are represented using macros or helper functions:
92+
When passed to {@link khepri:put/2}, the type of the payload is autodetected.
93+
However if you need to prepare the payload before passing it to Khepri, you can
94+
use the following functions:
9395
<ul>
94-
<li>`?NO_PAYLOAD' and {@link khepri:no_payload/0}</li>
95-
<li>`#kpayload_data{data = Term}' and {@link khepri:data_payload/1}</li>
96-
<li>`#kpayload_sproc{sproc = Fun}' and {@link khepri:sproc_payload/1}</li>
96+
<li>{@link khepri_payload:none/0}</li>
97+
<li>{@link khepri_payload:data/1}</li>
98+
<li>{@link khepri_payload:sproc/1}</li>
9799
</ul>
98100

99-
Functions in {@link khepri_machine} have no assumption on the type of the
100-
payload because they are a low-level API. Therefore, it must be specified
101-
explicitly using the macros or helper functions mentionned above.
102-
103-
Most functions in {@link khepri}, being a higher-level API, target more
104-
specific use cases and detect the type of payload.
105-
106101
=== Properties ===
107102

108103
Properties are:
@@ -343,8 +338,7 @@ PathPattern = [stock,
343338
conditions = [
344339
<<"lime tree">>,
345340
#if_payload_version{version = PayloadVersion}]}],
346-
Payload = #kpayload_data{data = Term1},
347-
case khepri_machine:put(StoredId, PathPattern, Payload) of
341+
case khepri:put(StoredId, PathPattern, Term1) of
348342
{ok, _} ->
349343
ok; %% `Term1` was stored successfully.
350344
{error, {mismatching_node, _}} ->
@@ -375,14 +369,13 @@ The indicated stored procedure must have been stored in the tree first.
375369

376370
=== Storing an anonymous function ===
377371

378-
This is possible to store an anonymous function as the payload of a tree node
379-
using the {@link khepri_machine:payload_sproc()} record:
372+
This is possible to store an anonymous function as the payload of a tree node:
380373

381374
```
382-
khepri_machine:put(
375+
khepri:put(
383376
StoreId,
384377
StoredProcPath,
385-
#kpayload_sproc{sproc = fun() -> do_something() end}))}.
378+
fun() -> do_something() end).
386379
'''
387380

388381
The `StoredProcPath' can be <a href="#Addressing_a_tree_node">any path in the

include/khepri.hrl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@
3333
-define(IS_PATH_PATTERN(Path),
3434
(Path =:= [] orelse ?IS_PATH_CONDITION(hd(Path)))).
3535

36-
%% -------------------------------------------------------------------
37-
%% Payload types.
38-
%% -------------------------------------------------------------------
39-
40-
-define(NO_PAYLOAD, '$__NO_PAYLOAD__').
41-
-record(kpayload_data, {data :: khepri:data()}).
42-
-record(kpayload_sproc, {sproc :: khepri_fun:standalone_fun()}).
43-
44-
-define(IS_KHEPRI_PAYLOAD(Payload), (Payload =:= ?NO_PAYLOAD orelse
45-
is_record(Payload, kpayload_data) orelse
46-
is_record(Payload, kpayload_sproc))).
47-
4836
%% -------------------------------------------------------------------
4937
%% Path conditions.
5038
%% -------------------------------------------------------------------

src/internal.hrl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@
1616
-define(TX_STATE_KEY, khepri_tx_machine_state).
1717
-define(TX_PROPS, khepri_tx_properties).
1818

19+
-define(NO_PAYLOAD, '$__NO_PAYLOAD__').
20+
-record(p_data, {data :: khepri:data()}).
21+
-record(p_sproc, {sproc :: khepri_fun:standalone_fun()}).
22+
23+
-define(IS_KHEPRI_PAYLOAD(Payload), (Payload =:= ?NO_PAYLOAD orelse
24+
is_record(Payload, p_data) orelse
25+
is_record(Payload, p_sproc))).
26+
1927
%% Structure representing each node in the tree, including the root node.
2028
%% TODO: Rename stat to something more correct?
2129
-record(node, {stat = ?INIT_NODE_STAT :: khepri_machine:stat(),
22-
payload = ?NO_PAYLOAD :: khepri:payload(),
30+
payload = ?NO_PAYLOAD :: khepri_payload:payload(),
2331
child_nodes = #{} :: #{khepri_path:component() := #node{}}}).
2432

2533
%% State machine commands.
2634

2735
-record(put, {path :: khepri_path:pattern(),
28-
payload = ?NO_PAYLOAD :: khepri:payload(),
36+
payload = ?NO_PAYLOAD :: khepri_payload:payload(),
2937
extra = #{} :: #{keep_while =>
3038
khepri:keep_while_conds_map()}}).
3139

0 commit comments

Comments
 (0)