#44 fixes: api signatures and cache version-miss (#48)

* fix: Fix API signatures

* fix: Fix get_snap version_not_found case
This commit is contained in:
Yaroslav Rogov 2021-07-08 10:59:13 +03:00 committed by GitHub
parent 53924f4de4
commit 1a8404ae52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 17 deletions

View File

@ -68,6 +68,10 @@
-type object_filter() :: fun((object_type(), domain_object()) -> boolean()).
-type object_folder(Acc) :: fun((object_type(), domain_object(), Acc) -> Acc).
-type domain_object() :: dmsl_domain_thrift:'DomainObject'().
%% HACK: this is type required for checkout_objects_by_type:
%% domain_object is any object from union, tagged with it's name
%% yet there's no way to extract typespecs for untagged objects
-type untagged_domain_object() :: tuple().
-type versioned_object() :: dmsl_domain_config_thrift:'VersionedObject'().
-type domain() :: dmsl_domain_thrift:'Domain'().
-type history() :: dmsl_domain_config_thrift:'History'().
@ -118,15 +122,15 @@ checkout_versioned_object(Reference, ObjectReference, Opts) ->
Version = ref_to_version(Reference),
#'VersionedObject'{version = Version, object = checkout_object(Reference, ObjectReference, Opts)}.
-spec checkout_objects_by_type(object_type()) -> [domain_object()] | no_return().
-spec checkout_objects_by_type(object_type()) -> [untagged_domain_object()] | no_return().
checkout_objects_by_type(ObjectType) ->
checkout_objects_by_type(latest, ObjectType).
-spec checkout_objects_by_type(version(), object_type()) -> [domain_object()] | no_return().
-spec checkout_objects_by_type(version(), object_type()) -> [untagged_domain_object()] | no_return().
checkout_objects_by_type(Reference, ObjectType) ->
checkout_objects_by_type(Reference, ObjectType, #{}).
-spec checkout_objects_by_type(version(), object_type(), opts()) -> [domain_object()] | no_return().
-spec checkout_objects_by_type(version(), object_type(), opts()) -> [untagged_domain_object()] | no_return().
checkout_objects_by_type(Reference, ObjectType, Opts) ->
Version = ref_to_version(Reference),
unwrap(dmt_client_cache:get_objects_by_type(Version, ObjectType, Opts)).
@ -162,23 +166,23 @@ checkout_fold_objects(Reference, Folder, Acc, Opts) ->
Version = ref_to_version(Reference),
unwrap(dmt_client_cache:fold_objects(Version, Folder, Acc, Opts)).
-spec commit(version(), commit()) -> version() | no_return().
-spec commit(vsn(), commit()) -> vsn() | no_return().
commit(Version, Commit) ->
commit(Version, Commit, #{}).
-spec commit(version(), commit(), opts()) -> version() | no_return().
-spec commit(vsn(), commit(), opts()) -> vsn() | no_return().
commit(Version, Commit, Opts) ->
dmt_client_backend:commit(Version, Commit, Opts).
-spec get_last_version() -> version().
-spec get_last_version() -> vsn().
get_last_version() ->
dmt_client_cache:get_last_version().
-spec pull_range(version(), limit()) -> history() | no_return().
-spec pull_range(vsn(), limit()) -> history() | no_return().
pull_range(Version, Limit) ->
pull_range(Version, Limit, #{}).
-spec pull_range(version(), limit(), opts()) -> history() | no_return().
-spec pull_range(vsn(), limit(), opts()) -> history() | no_return().
pull_range(Version, Limit, Opts) ->
dmt_client_backend:pull_range(Version, Limit, Opts).

View File

@ -216,7 +216,7 @@ ensure_version(Version, Opts) ->
-spec do_get(dmt_client:vsn()) -> {ok, dmt_client:snapshot()} | {error, version_not_found}.
do_get(Version) ->
case get_snap(Version) of
case fetch_snap(Version) of
{ok, Snap} ->
build_snapshot(Snap);
{error, version_not_found} = Error ->
@ -226,8 +226,10 @@ do_get(Version) ->
-spec do_get_object(dmt_client:vsn(), dmt_client:object_ref()) ->
{ok, dmt_client:domain_object()} | {error, version_not_found | object_not_found}.
do_get_object(Version, ObjectRef) ->
{ok, #snap{tid = TID}} = get_snap(Version),
try ets:lookup(TID, ObjectRef) of
try
Snap = get_snap(Version),
ets:lookup(Snap#snap.tid, ObjectRef)
of
[#object{obj = Object}] ->
{ok, Object};
[] ->
@ -241,11 +243,13 @@ do_get_object(Version, ObjectRef) ->
end.
do_get_objects_by_type(Version, ObjectType) ->
{ok, #snap{tid = TID}} = get_snap(Version),
MatchSpec = [
{{object, '_', {ObjectType, '$1'}}, [], ['$1']}
],
try ets:select(TID, MatchSpec) of
try
Snap = get_snap(Version),
ets:select(Snap#snap.tid, MatchSpec)
of
Result ->
{ok, Result}
catch
@ -255,11 +259,13 @@ do_get_objects_by_type(Version, ObjectType) ->
end.
do_fold_objects(Version, Folder, Acc) ->
{ok, #snap{tid = TID}} = get_snap(Version),
MappedFolder = fun({object, {Type, _Ref}, {Type, Object}}, AccIn) ->
Folder(Type, Object, AccIn)
end,
try ets:foldl(MappedFolder, Acc, TID) of
try
Snap = get_snap(Version),
ets:foldl(MappedFolder, Acc, Snap#snap.tid)
of
Result ->
{ok, Result}
catch
@ -270,7 +276,7 @@ do_fold_objects(Version, Folder, Acc) ->
-spec put_snapshot(dmt_client:snapshot()) -> ok.
put_snapshot(#'Snapshot'{version = Version, domain = Domain}) ->
case get_snap(Version) of
case fetch_snap(Version) of
{ok, _Snap} ->
ok;
{error, version_not_found} ->
@ -296,8 +302,15 @@ put_domain_to_table(TID, Domain) ->
Domain
).
-spec get_snap(dmt_client:vsn()) -> {ok, snap()} | {error, version_not_found}.
-spec get_snap(dmt_client:vsn()) -> snap() | no_return().
get_snap(Version) ->
case fetch_snap(Version) of
{ok, Snap} -> Snap;
{error, version_not_found} -> error(badarg)
end.
-spec fetch_snap(dmt_client:vsn()) -> {ok, snap()} | {error, version_not_found}.
fetch_snap(Version) ->
case ets:lookup(?TABLE, Version) of
[Snap] ->
_ = update_last_access(Version),