Skip to content

Instantly share code, notes, and snippets.

@khanhhua
Created January 9, 2017 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khanhhua/cd83e823dab86787c043b80faea8b1e4 to your computer and use it in GitHub Desktop.
Save khanhhua/cd83e823dab86787c043b80faea8b1e4 to your computer and use it in GitHub Desktop.
%% Open a new table setting Name as primary key in tuples of {user, Name, Age}
{ok, Tab} = dets:open_file('demo.dat', [{type,set}, {keypos,2}]).
%% Inspect table info
dets:info(Tab).
dets:info(Tab, size).
%% Insert user Tom with ages 30 and 40
ok = dets:insert(Tab, {user, "Tom", 30}).
false = dets:insert_new(Tab, {user, "Tom", 40}).
%% .. Previous Tom will be overridden
ok = dets:insert(Tab, {user, "Tom", 40}).
%% List all items and project all attributes
dets:foldl(fun (X, L) -> [X|L] end, [], Tab).
dets:select(Tab, [{{user,'$1', '$2'},[],['$_']}]).
%% Selection and customize projection
%% - Select user "Tom" and project all attributes
dets:select(Tab, [{{user,'$1','$2'},[{'==','$1',"Tom"}],['$_']}]).
dets:select(Tab, ets:fun2ms(fun ({user, Name, Age}) -> {Name,Age} end)).
%% - Select user "Tom" and project Name attribute
dets:select(Tab, [{{user,'$1','$2'},[{'==','$1',"Tom"}],['$1']}]).
dets:select(Tab, ets:fun2ms(fun ({user, Name}) -> Name end)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment