我正在将数据移入和移出dets,我有一个选择:我可以选择:
1)在访问前立即打开dets,并在访问后立即关闭,或者
%% Based on Armstrong, Programming Erlang, page 279
open() ->
File = ?NAMEDB,
case dets:open_file(?MODULE, [{file, File}, {keypos,2}]) of
{ok, ?MODULE} ->
io:format("dets opened:~p~n", [File]);
{error,_Reason} ->
io:format("cannot open dets table~n"),
exit(eDetsOpen)
end.
%% Open db, get record, close db, return name
name(Record) ->
open(),
#name{honorific=Honorific, fname=FName, mname=MName, lname=LName, suffix=Suffix} = Record,
close(),
format_name([Honorific, FName, MName, LName, Suffix]).2)将dets链接到在发生崩溃时重新打开的supervisor;例如,使用supervisor通过gen-server访问dets,如下所示:
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
start_child(Value, LeaseTime) ->
supervisor:start_child(?SERVER, [Value, LeaseTime]).
init([]) ->
Names = {lit_names, {lit_names, start_link, []},
temporary, brutal_kill, worker, [zpt_gridz_scratchpad]},
Children = [Names],
RestartStrategy = {simple_one_for_one, 0, 1},
{ok, {RestartStrategy, Children}}.哪一个是最好的?或者还有更好的选择吗?
非常感谢,
LRP
发布于 2014-08-13 21:28:19
我想这完全取决于你的使用模式。在我看来,你有几个选择:
ram_file选项一起使用,以获得更好的性能,具体取决于您的使用情况。第二个可能在一个随意的用例中是最好的。事实上,several processes can open the same DETS table simultaneously
如果两个进程通过提供相同的名称和参数打开同一个表,则该表将有两个用户。如果一个用户关闭了该表,它仍然保持打开状态,直到第二个用户关闭该表。
第三种情况可能是最慢的,不应该选择,除非您确实有很好的理由这样做(数据需要以特定的顺序写入或根据其他数据进行原子验证)。
https://stackoverflow.com/questions/18751045
复制相似问题