Why dets file_size doesnot reduce after deleting all the keys

216 views Asked by At

I am having problems with the size of dets file.

> {ok,D1} = dets:open_file(sample_dets_file, [{type, set},{auto_save,3}]).
{ok,sample_dets_file}

> [dets:insert(D1,{{fid,X},"this is sample data"}) || X <- lists:seq(1,10000)].
> dets:info(D1).

[{type,set},
 {keypos,1},
 {size,10000},
 {file_size,769748},
 {filename,"sample_dets_file"}]

> [dets:delete(D1,{fid,X}) || X <- lists:seq(1,10000)].
> dets:info(D1).

[{type,set},
 {keypos,1},
 {size,0},
 {file_size,770092},
 {filename,"sample_dets_file"}]

After insert - {size,10000}, {file_size,769748}
After delete- {size,0}, {file_size,770092}.
The file size has not reduced even after table is empty. However when I execute delete_all_objects, file size returns to normal.

> dets:delete_all_objects(D1).
> dets:info(D1).

 [{type,set},
 {keypos,1},
 {size,0},
 {file_size,5464},
 {filename,"sample_dets_file"}]

I could not understand what additionally i need to do along with delete operation so as to reduce the file_size. The problem is, since dets file_size is limited to 4gb, even if objects are deleted, I reach the size limit of dets file.

2

There are 2 answers

0
RichardC On BEST ANSWER

The dets implementation doesn't try to shrink the file when entries are deleted, except when you do delete_all_objects. To shrink the file manually, you have to create a new temporary dets file, copy all entries to the new file, remove the old file, and rename the temporary file to replace the old.

0
A. Vitasek On

There is another option how to reduce size of the DETS table (after deletion).

Close this DETS table (dets:close) and open it whit an option {repair, force}.

See the documentation: http://www.erlang.org/doc/man/dets.htm

The only way to defragment a table is to close it and then open it again with the repair option set to force.

When there is no another process which have open this file, the file is compacted.

It works for me.