Merge pull request #33 from aeternity/uw-batch-on-demand

Begin dirty activity with batch ref dummy
This commit is contained in:
Ulf Wiger 2022-10-26 13:16:48 +02:00 committed by GitHub
commit b65e82ed71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -105,6 +105,8 @@
-include("mnesia_rocksdb.hrl"). -include("mnesia_rocksdb.hrl").
-include("mnesia_rocksdb_int.hrl"). -include("mnesia_rocksdb_int.hrl").
-define(BATCH_REF_DUMMY, '$mrdb_batch_ref_dummy').
-type tab_name() :: atom(). -type tab_name() :: atom().
-type alias() :: atom(). -type alias() :: atom().
-type admin_tab() :: {admin, alias()}. -type admin_tab() :: {admin, alias()}.
@ -269,7 +271,7 @@ activity(Type, Alias, F) ->
#{ alias => Alias #{ alias => Alias
, db_ref => DbRef }, TxCtxt); , db_ref => DbRef }, TxCtxt);
batch -> batch ->
Batch = get_batch_(DbRef), Batch = init_batch_ref(DbRef),
#{ activity => #{ type => batch #{ activity => #{ type => batch
, handle => Batch } , handle => Batch }
, alias => Alias , alias => Alias
@ -1007,18 +1009,20 @@ get_batch(#{db_ref := DbRef, batch := BatchRef}) ->
get_batch(_) -> get_batch(_) ->
{error, badarg}. {error, badarg}.
get_batch_(DbRef) -> init_batch_ref(DbRef) ->
Ref = make_ref(), Ref = make_ref(),
{ok, Batch} = rdb_batch(), pdict_put({mrdb_batch, Ref}, #{DbRef => ?BATCH_REF_DUMMY}),
pdict_put({mrdb_batch, Ref}, #{DbRef => Batch}),
Ref. Ref.
get_batch_(DbRef) -> Ref = make_ref(), {ok, Batch} = rdb_batch(),
pdict_put({mrdb_batch, Ref}, #{DbRef => Batch}), Ref.
get_batch_(DbRef, BatchRef) -> get_batch_(DbRef, BatchRef) ->
Key = batch_ref_key(BatchRef), Key = batch_ref_key(BatchRef),
case pdict_get(Key) of case pdict_get(Key) of
undefined -> undefined ->
error(stale_batch_ref); error(stale_batch_ref);
#{DbRef := Batch} -> #{DbRef := Batch} when Batch =/= ?BATCH_REF_DUMMY ->
Batch; Batch;
Map -> Map ->
{ok, Batch} = rdb_batch(), {ok, Batch} = rdb_batch(),
@ -1047,7 +1051,9 @@ write_batches(BatchRef, Opts) ->
pdict_erase(Key), pdict_erase(Key),
ret_batch_write_acc( ret_batch_write_acc(
maps:fold( maps:fold(
fun(DbRef, Batch, Acc) -> fun(_, ?BATCH_REF_DUMMY, Acc) ->
Acc;
(DbRef, Batch, Acc) ->
case rocksdb:write_batch(DbRef, Batch, Opts) of case rocksdb:write_batch(DbRef, Batch, Opts) of
ok -> ok ->
Acc; Acc;