Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 50 lines (37 sloc) 1.426 kb
e7bc02e edoc annotations in src comments
Tony Hannan authored
1 %@doc Init some internal global variables used by mongodb app
da8a3db MongoDB driver for Erlang
Tony Hannan authored
2 -module (mongodb_app).
3
4 -behaviour (application).
5 -export ([start/2, stop/1]).
6
7 -behaviour (supervisor).
8 -export ([init/1]).
9
10 -export ([gen_objectid/0, next_requestid/0]). % API
11
12 %% Behaviour callbacks
13
14 start (_, []) -> supervisor:start_link ({local, ?MODULE}, ?MODULE, []).
15
16 stop (_) -> ok.
17
18 %% Supervisor callbacks
19
e7bc02e edoc annotations in src comments
Tony Hannan authored
20 %@doc Create global vars which will be owned by this supervisor (and die with it)
da8a3db MongoDB driver for Erlang
Tony Hannan authored
21 init ([]) ->
f564cd8 Use OTP gen_server instead of own var/resource stdlib_ext
Tony Hannan authored
22 ets:new (?MODULE, [named_table, public]),
23 ets:insert (?MODULE, [
24 {oid_counter, 0},
25 {oid_machineprocid, oid_machineprocid()},
26 {requestid_counter, 0} ]),
da8a3db MongoDB driver for Erlang
Tony Hannan authored
27 {ok, {{one_for_one,3,10}, []}}.
28
29 %% API functions
30
31 -spec next_requestid () -> mongo_protocol:requestid(). % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
32 %@doc Fresh request id
f564cd8 Use OTP gen_server instead of own var/resource stdlib_ext
Tony Hannan authored
33 next_requestid() -> ets:update_counter (?MODULE, requestid_counter, 1).
da8a3db MongoDB driver for Erlang
Tony Hannan authored
34
35 -spec gen_objectid () -> bson:objectid(). % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
36 %@doc Fresh object id
da8a3db MongoDB driver for Erlang
Tony Hannan authored
37 gen_objectid() ->
0a8cc8c Documents are now tuples instead of lists
Tony Hannan authored
38 Now = bson:unixtime_to_secs (bson:timenow()),
f564cd8 Use OTP gen_server instead of own var/resource stdlib_ext
Tony Hannan authored
39 MPid = ets:lookup_element (?MODULE, oid_machineprocid, 2),
40 N = ets:update_counter (?MODULE, oid_counter, 1),
0a8cc8c Documents are now tuples instead of lists
Tony Hannan authored
41 bson:objectid (Now, MPid, N).
da8a3db MongoDB driver for Erlang
Tony Hannan authored
42
43 -spec oid_machineprocid () -> <<_:40>>. % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
44 %@doc Fetch hostname and os pid and compress into a 5 byte id
da8a3db MongoDB driver for Erlang
Tony Hannan authored
45 oid_machineprocid() ->
46 OSPid = list_to_integer (os:getpid()),
47 {ok, Hostname} = inet:gethostname(),
48 <<MachineId:3/binary, _/binary>> = erlang:md5 (Hostname),
49 <<MachineId:3/binary, OSPid:16/big>>.
Something went wrong with that request. Please try again.