Closing a dets

Tobias Lindahl tobias.lindahl@REDACTED
Wed Mar 23 14:31:10 CET 2005


Actually, while constructing an counterexample I realised that the problem
is not related to shutting down the node. It seems to be related to
converting a sufficently large ets table to dets.

The following example works when the ets is small, but breaks when it
grows larger.

Tobias

===== Example =====

-module(t).

-export([t/2]).

t(Name, Size) ->
  Ets = build_ets(Size),
  {ok, Dets} = dets:open_file(Name, []),
  ok = dets:from_ets(Dets, Ets),
  ok = dets:sync(Dets),
  ok = dets:close(Dets),
  ets:delete(Ets).

build_ets(Size) ->
  Ets = ets:new(t_ets, [public, set]),
  build_ets(Ets, Size).

build_ets(Ets, N) when N < 0->
  Ets;
build_ets(Ets, N) ->
  ets:insert(Ets, {N, N+1}),
  build_ets(Ets, N-1).


Eshell V5.5  (abort with ^G)
1> t:t("/tmp/foo", 100).
true
2> {ok, Dets} = dets:open_file("/tmp/foo", [{access, read}]).
{ok,"/tmp/foo"}
3> dets:lookup(Dets, 1).
[{1,2}]

Eshell V5.5  (abort with ^G)
1> t:t("/tmp/foo", 1000).
true
2> {ok, Dets} = dets:open_file("/tmp/foo", [{access, read}]).
{ok,"/tmp/foo"}
3> dets:lookup(Dets, 1).
{error,{file_error,"/tmp/foo",{0,ebadf}}}



On Wed, 23 Mar 2005, Hans Bolinder wrote:

> [Tobias Lindahl:]
> > Is there a way of waiting for a dets to be properly closed after
> > dets:close/1 has been called. The problem is that I want to shut down the
> > erlang node with an exit status (using halt(ExitStatus)), but wait until
> > the dets is properly closed.
>
> You should call dets:sync/1 before calling dets:close/1 if you want to
> be sure that data have been written to disk (dets:sync/1 calls
> file:sync/1 which calls fsync(3C)). (This may take some time if you
> close many Dets tables.)
>
> But I assume what you want is to be sure that no Dets table will need
> to be repaired when opened next time. For that it should suffice to
> call dets:close/1 without calling dets:sync/1.
>
> Or have you found a counterexample?
>
> Best regards,
>
> Hans Bolinder, Erlang/OTP
>



More information about the erlang-questions mailing list