timestamper/src/id_counter.erl
Jonathan Bernard 6fe9184c8e Continuing work on the API.
The id_counter module now includes the record directly in the source.
Fixed many typos and small syntax errors.
Added ts_api:dispatch_user/2 to handle different HTTP methods.
Added method placeholder stubs to ts_api.
Implemented ts_api:list_entries/3.
Added ts_user record and ts_user module.
Implemented ts_entry:list/4, the more generic guts of the other list functions.
Created ts_entry:list_asc/3 and ts_entry:list_desc/3.
Fixed ts_json:encode_datetime/1.
2011-02-02 16:57:58 -06:00

24 lines
748 B
Erlang

-module(id_counter).
-export([create_table/1, next_counter/1, dirty_next_counter/1]).
-record(id_counter, {name, next_value = 0}).
%% Create the table structure for Mnesia
create_table(Opts) ->
mnesia:create_table(id_counter, Opts ++
[{attributes, record_info(fields, id_counter)}]).
%% Get the next id for a given name
next_counter(Name) ->
Rec = case mnesia:read({id_counter, Name}) of
[] -> #id_counter{name=Name, next_value=0};
[Val] -> Val
end,
NextRec = Rec#id_counter{next_value = Rec#id_counter.next_value+ 1},
ok = mnesia:write(NextRec),
Rec#id_counter.next_value.
%% Get the next id for a given name
dirty_next_counter(Name) ->
mnesia:dirty_update_counter(id_counter, Name, 1) - 1.