You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2020/08/06 19:06:03 UTC
[couchdb] 01/01: Pluggable persist_fun
This is an automated email from the ASF dual-hosted git repository.
rnewson pushed a commit to branch prototype/fdb-layer-ebtree-persist-fun
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit ee213c9c995c2be9d103e32c0e2f9d01477a446e
Author: Robert Newson <rn...@apache.org>
AuthorDate: Thu Aug 6 20:04:58 2020 +0100
Pluggable persist_fun
---
src/ebtree/src/ebtree.erl | 29 +++++++++++++++++++++++------
1 file changed, 23 insertions(+), 6 deletions(-)
diff --git a/src/ebtree/src/ebtree.erl b/src/ebtree/src/ebtree.erl
index c31f503..bacb42e 100644
--- a/src/ebtree/src/ebtree.erl
+++ b/src/ebtree/src/ebtree.erl
@@ -46,7 +46,8 @@
max,
collate_fun,
reduce_fun,
- encode_fun
+ encode_fun,
+ persist_fun
}).
-define(META, 0).
@@ -83,12 +84,14 @@ open(Db, Prefix, Order, Options) when is_binary(Prefix), is_integer(Order), Orde
ReduceFun = proplists:get_value(reduce_fun, Options, fun reduce_noop/2),
CollateFun = proplists:get_value(collate_fun, Options, fun collate_raw/2),
EncodeFun = proplists:get_value(encode_fun, Options, fun encode_erlang/3),
+ PersistFun = proplists:get_value(persist_fun, Options, fun simple_persist/3),
Tree = #tree{
prefix = Prefix,
reduce_fun = ReduceFun,
collate_fun = CollateFun,
- encode_fun = EncodeFun
+ encode_fun = EncodeFun,
+ persist_fun = PersistFun
},
erlfdb:transactional(Db, fun(Tx) ->
@@ -772,8 +775,8 @@ meta_key(Prefix, MetaKey) when is_binary(Prefix) ->
get_node(Tx, #tree{} = Tree, Id) ->
Key = node_key(Tree#tree.prefix, Id),
- Future = erlfdb:get(Tx, Key),
- Value = erlfdb:wait(Future),
+ #tree{persist_fun = PersistFun} = Tree,
+ Value = PersistFun(Tx, get, Key),
decode_node(Tree, Id, Key, Value).
@@ -785,7 +788,8 @@ clear_nodes(Tx, #tree{} = Tree, Nodes) ->
clear_node(Tx, #tree{} = Tree, #node{} = Node) ->
Key = node_key(Tree#tree.prefix, Node#node.id),
- erlfdb:clear(Tx, Key).
+ #tree{persist_fun = PersistFun} = Tree,
+ PersistFun(Tx, clear, Key).
set_nodes(Tx, #tree{} = Tree, Nodes) ->
@@ -805,7 +809,8 @@ set_node(Tx, #tree{} = Tree, #node{} = Node) ->
validate_node(Tree, Node),
Key = node_key(Tree#tree.prefix, Node#node.id),
Value = encode_node(Tree, Key, Node),
- erlfdb:set(Tx, Key, Value).
+ #tree{persist_fun = PersistFun} = Tree,
+ PersistFun(Tx, set, [Key, Value]).
node_key(Prefix, Id) when is_binary(Prefix), is_integer(Id) ->
@@ -1006,6 +1011,18 @@ encode_erlang(encode, _Key, Value) ->
encode_erlang(decode, _Key, Value) ->
binary_to_term(Value, [safe]).
+%% persist function
+
+simple_persist(Tx, set, [Key, Value]) ->
+ erlfdb:set(Tx, Key, Value);
+
+simple_persist(Tx, get, Key) ->
+ erlfdb:wait(erlfdb:get(Tx, Key));
+
+simple_persist(Tx, clear, Key) ->
+ erlfdb:clear(Tx, Key).
+
+
%% private functions
init_order(#tree{} = Tree, Order)