Bug fixes and implementation refinement around `ts_ext_data
`.
* Transformed the test database to match the new data model. Added ``ts_ext_data`` table and moved ``ts_user.ext_data`` values to it. * Added D0022: cascade delete ``ts_ext_data`` when ``ts_entry`` record is deleted. * Completed refactor of ``ts_api`` functions to account for extended data: * ``post_entry/3`` * ``put_entry/3`` * Created ``ts_entry:write/2`` to write extended data atomically with the entry. * Fixed copy/paste bug in ``ts_ext_data:create_table/1``. * Fixed implementation of ``ts_ext_data:set_property/3`` to be explicit about taking a record, not a reference. It then extracts the reference and passes it to the underlying implementation in ``ts_ext_data:do_set_property/3``. * Refactored ``ts_ext_data:do_set_property/3`` to take a record *reference*, not the record itself. * Refactored the ``ts_ext_data:get_property/2`` and ``ts_ext_data:get_properties/1`` functions to return key value tuple pairs, not ``ts_ext_data{}`` records.
This commit is contained in:
@ -4,41 +4,44 @@
|
||||
-include("ts_db_records.hrl").
|
||||
|
||||
create_table(TableOpts) ->
|
||||
mnesia:create_table(ts_entry,
|
||||
mnesia:create_table(ts_ext_data,
|
||||
TableOpts ++ [{attributes, record_info(fields, ts_ext_data)},
|
||||
{type, ordered_set}]).
|
||||
|
||||
% set last timeline
|
||||
set_property(Ref=#ts_user{}, last_timeline, LastTimelineId) ->
|
||||
do_set_property(Ref, last_timeline, LastTimelineId);
|
||||
set_property(Rec=#ts_user{}, last_timeline, LastTimelineId) ->
|
||||
do_set_property(Rec#ts_user.username, last_timeline, LastTimelineId);
|
||||
|
||||
% Set exclusion_list for a User account
|
||||
set_property(Ref=#ts_user{}, entry_exclusions, ExclusionList) ->
|
||||
do_set_property(Ref, entry_exclusions, string:join(ExclusionList, "|"));
|
||||
set_property(Rec=#ts_user{}, entry_exclusions, ExclusionList) ->
|
||||
do_set_property(Rec#ts_user.username, entry_exclusions, ExclusionList);
|
||||
|
||||
% Set exclusion_list for a Timeline entry
|
||||
set_property(Ref=#ts_timeline{}, entry_exclusions, ExclusionList) ->
|
||||
do_set_property(Ref, entry_exclusions, string:join(ExclusionList, "|"));
|
||||
set_property(Rec=#ts_timeline{}, entry_exclusions, ExclusionList) ->
|
||||
do_set_property(Rec#ts_timeline.ref, entry_exclusions, ExclusionList);
|
||||
|
||||
set_property(Ref, Key, Value) ->
|
||||
set_property(Rec, Key, _Value) ->
|
||||
throw(io_lib:format("Property '~s' not available for a ~s record.",
|
||||
[Key, element(1, Ref)])).
|
||||
[Key, element(1, Rec)])).
|
||||
|
||||
get_property(Ref, PropKey) ->
|
||||
{atomic, Result} = mnesia:transaction(fun() ->
|
||||
case mnesia:read(ts_ext_data, {Ref, PropKey}) of
|
||||
[] -> not_set;
|
||||
[Property] -> Property
|
||||
[Property] -> Property#ts_ext_data.value
|
||||
end
|
||||
end),
|
||||
Result.
|
||||
|
||||
get_properties(Ref) ->
|
||||
get_properties(Rec) ->
|
||||
Ref = element(2, Rec),
|
||||
{atomic, Result} = mnesia:transaction(fun() ->
|
||||
MatchHead = #ts_ext_data{ref = {Ref, '_'}, _='_'},
|
||||
mnesia:select(ts_ext_data, [{MatchHead, [], ['$_']}])
|
||||
end),
|
||||
Result.
|
||||
lists:map(fun(ExtData = #ts_ext_data{}) ->
|
||||
{Ref, Key} = ExtData#ts_ext_data.ref,
|
||||
{Key, ExtData#ts_ext_data.value} end, Result).
|
||||
|
||||
do_set_property(Ref, PropKey, Val) ->
|
||||
{atomic, Result} = mnesia:transaction(fun() ->
|
||||
|
Reference in New Issue
Block a user