Search code examples
erlangdets

Why dets file_size doesnot reduce after deleting all the keys


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.


Solution

  • 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.