Fix/commit latest (#52)

* fix: add pre-commit cache update for latest

* test: Remove unnecessary update

* refactor: Add updating_ref_to_version

* feat: Add /3 versions of convenience methods
This commit is contained in:
Yaroslav Rogov 2021-07-23 16:15:54 +03:00 committed by GitHub
parent f6e181e86f
commit 3f66402843
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 10 deletions

View File

@ -34,12 +34,16 @@
-export([pull_range/3]).
-export([insert/1]).
-export([insert/2]).
-export([insert/3]).
-export([update/1]).
-export([update/2]).
-export([update/3]).
-export([upsert/1]).
-export([upsert/2]).
-export([upsert/3]).
-export([remove/1]).
-export([remove/2]).
-export([remove/3]).
%% Health check API
-export([health_check/0]).
@ -194,7 +198,11 @@ commit(Reference, Commit) ->
-spec commit(version(), commit(), opts()) -> vsn() | no_return().
commit(Reference, Commit, Opts) ->
Version = ref_to_version(Reference),
Version = updating_ref_to_version(Reference),
do_commit(Version, Commit, Opts).
%% Without Pre-commit cache update version of commit with explicit version
do_commit(Version, Commit, Opts) ->
Result = dmt_client_backend:commit(Version, Commit, Opts),
_NewVersion = unwrap(dmt_client_cache:update()),
Result.
@ -230,6 +238,10 @@ insert(ObjectOrObjects) ->
insert(Reference, Object) when not is_list(Object) ->
insert(Reference, [Object]);
insert(Reference, Objects) ->
insert(Reference, Objects, #{}).
-spec insert(version(), domain_object() | [domain_object()], opts()) -> vsn() | no_return().
insert(Reference, Objects, Opts) ->
Commit = #'Commit'{
ops = [
{insert, #'InsertOp'{
@ -238,7 +250,7 @@ insert(Reference, Objects) ->
|| Object <- Objects
]
},
commit(Reference, Commit).
commit(Reference, Commit, Opts).
-spec update(domain_object() | [domain_object()]) -> vsn() | no_return().
update(NewObjectOrObjects) ->
@ -248,7 +260,11 @@ update(NewObjectOrObjects) ->
update(Reference, NewObject) when not is_list(NewObject) ->
update(Reference, [NewObject]);
update(Reference, NewObjects) ->
Version = ref_to_version(Reference),
update(Reference, NewObjects, #{}).
-spec update(version(), domain_object() | [domain_object()], opts()) -> vsn() | no_return().
update(Reference, NewObjects, Opts) ->
Version = updating_ref_to_version(Reference),
Commit = #'Commit'{
ops = [
{update, #'UpdateOp'{
@ -256,10 +272,11 @@ update(Reference, NewObjects) ->
new_object = NewObject
}}
|| NewObject = {Tag, {_ObjectName, Ref, _Data}} <- NewObjects,
OldObject <- [checkout_object(Version, {Tag, Ref})]
OldObject <- [checkout_object(Version, {Tag, Ref}, Opts)]
]
},
commit(Version, Commit).
%% Don't need pre-commit update: done in the beginning
do_commit(Version, Commit, Opts).
-spec upsert(domain_object() | [domain_object()]) -> vsn() | no_return().
upsert(NewObjectOrObjects) ->
@ -269,11 +286,15 @@ upsert(NewObjectOrObjects) ->
upsert(Reference, NewObject) when not is_list(NewObject) ->
upsert(Reference, [NewObject]);
upsert(Reference, NewObjects) ->
Version = ref_to_version(Reference),
upsert(Reference, NewObjects, #{}).
-spec upsert(version(), domain_object() | [domain_object()], opts()) -> vsn() | no_return().
upsert(Reference, NewObjects, Opts) ->
Version = updating_ref_to_version(Reference),
Commit = #'Commit'{
ops = lists:foldl(
fun(NewObject = {Tag, {ObjectName, Ref, _Data}}, Ops) ->
case unwrap_find(do_checkout_object(Reference, {Tag, Ref}, #{})) of
case unwrap_find(do_checkout_object(Version, {Tag, Ref}, Opts)) of
{ok, NewObject} ->
Ops;
{ok, OldObject = {Tag, {ObjectName, Ref, _OldData}}} ->
@ -297,7 +318,8 @@ upsert(Reference, NewObjects) ->
NewObjects
)
},
commit(Version, Commit).
%% Don't need pre-commit update: done in the beginning
do_commit(Version, Commit, Opts).
-spec remove(domain_object() | [domain_object()]) -> vsn() | no_return().
remove(ObjectOrObjects) ->
@ -307,6 +329,10 @@ remove(ObjectOrObjects) ->
remove(Reference, Object) when not is_list(Object) ->
remove(Reference, [Object]);
remove(Reference, Objects) ->
remove(Reference, Objects, #{}).
-spec remove(version(), domain_object() | [domain_object()], opts()) -> vsn() | no_return().
remove(Reference, Objects, Opts) ->
Commit = #'Commit'{
ops = [
{remove, #'RemoveOp'{
@ -315,7 +341,7 @@ remove(Reference, Objects) ->
|| Object <- Objects
]
},
commit(Reference, Commit).
commit(Reference, Commit, Opts).
%% Health check API
@ -358,6 +384,12 @@ unwrap_find({error, {woody_error, _} = Error}) -> erlang:error(Error);
unwrap_find({error, version_not_found = Reason}) -> erlang:error(Reason);
unwrap_find(Other) -> Other.
-spec updating_ref_to_version(version()) -> vsn().
updating_ref_to_version(latest) ->
unwrap(dmt_client_cache:update());
updating_ref_to_version(Ref) ->
ref_to_version(Ref).
-spec ref_to_version(version()) -> vsn().
ref_to_version(Version) when is_integer(Version) ->
Version;

View File

@ -63,7 +63,6 @@ insert_and_all_checkouts(_C) ->
#'Snapshot'{version = Version1} = dmt_client:checkout(),
Version2 = dmt_client:insert(Object),
true = Version1 < Version2,
_ = dmt_client_cache:update(),
#'Snapshot'{version = Version2} = dmt_client:checkout(),
Object = dmt_client:checkout_object(Ref),
#'VersionedObject'{version = Version2, object = Object} = dmt_client:checkout_versioned_object(latest, Ref).