Suppose I've got a simple record definition:
假设我有一个简单的记录定义:
-record(data, {primary_key = '_', more_stuff = '_'}).
I want a simple function that adds one of these records to a mnesia database. But I want it to fail if there's already an entry with the same primary key.
我想要一个简单的函数,将其中一个记录添加到mnesia数据库中。但是如果已经有一个具有相同主键的条目,我希望它失败。
(In the following examples, assume I've already defined
(在以下示例中,假设我已经定义过
db_get_data(Key)->
Q = qlc:q([Datum
|| Datum = #data{primary_key = RecordKey}
<- mnesia:table(data),
RecordKey =:= Key]),
qlc:e(Q).
)
The following works, but strikes me as sort of ugly ...
以下作品,但让我觉得有点难看......
add_data(D) when is_record(D, data)->
{atomic, Result} = mnesia:transaction(fun()->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> {error, bzzt_duplicate_primary_key}
end
end),
case Result of
{error, _} = Error -> throw(Error);
_ -> result
end.
This works too, but is also ugly:
这也有效,但也很难看:
add_data(D) when is_record(D, data)->
{atomic, Result} = mnesia:transaction(fun()->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> throw({error, bzzt_duplicate_primary_key})
end
end).
It differs from the above in that the above throws
它与上述不同之处在于上述抛出
{error, bzzt_duplicate_primary_key},
whereas this one throws
而这一个抛出
{error, {badmatch, {aborted, {throw,{error, bzzt_duplicate_primary_key}}}}}
So: is there some convention for indicating this sort of error? Or is there a built-in way that I can get mnesia to throw this error for me?
那么:是否有一些约定来表明这种错误?或者是否有一种内置的方式,我可以让mnesia为我抛出这个错误?
1 个解决方案
#1
I think both of them are fine, if you only make your code more pretty, like:
我认为它们都很好,如果你只是让你的代码更漂亮,比如:
add_data(D) when is_record(D, data)->
Fun = fun() ->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> throw({error, bzzt_duplicate_primary_key})
end
end,
{atomic, Result} = mnesia:activity(transaction, Fun).
or
add_data(D) when is_record(D, data)->
Fun = fun() ->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> {error, bzzt_duplicate_primary_key}
end
end,
{atomic, Result} = mnesia:activity(transaction, Fun),
case Result of
{error, Error} -> throw(Error);
_ -> result
end.
Do you throw errors or return errors? I would return an error myself. We split out code out into mnesia work units - a module with a set of functions that perform basic mnesia activities not in transactions, and an api module which 'composes' the work units into mnesia transactions with functions that look very similar to the one above.
你抛出错误或返回错误?我会自己回复一个错误。我们将代码拆分为mnesia工作单元 - 一个具有一组函数的模块,这些函数执行不在事务中的基本mnesia活动,以及一个api模块,它将工作单元“组合”成mnesia事务,其功能看起来与上面的类似。
#1
I think both of them are fine, if you only make your code more pretty, like:
我认为它们都很好,如果你只是让你的代码更漂亮,比如:
add_data(D) when is_record(D, data)->
Fun = fun() ->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> throw({error, bzzt_duplicate_primary_key})
end
end,
{atomic, Result} = mnesia:activity(transaction, Fun).
or
add_data(D) when is_record(D, data)->
Fun = fun() ->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> {error, bzzt_duplicate_primary_key}
end
end,
{atomic, Result} = mnesia:activity(transaction, Fun),
case Result of
{error, Error} -> throw(Error);
_ -> result
end.
Do you throw errors or return errors? I would return an error myself. We split out code out into mnesia work units - a module with a set of functions that perform basic mnesia activities not in transactions, and an api module which 'composes' the work units into mnesia transactions with functions that look very similar to the one above.
你抛出错误或返回错误?我会自己回复一个错误。我们将代码拆分为mnesia工作单元 - 一个具有一组函数的模块,这些函数执行不在事务中的基本mnesia活动,以及一个api模块,它将工作单元“组合”成mnesia事务,其功能看起来与上面的类似。