Permalink
Browse files

fixing date format and adding oid function

  • Loading branch information...
1 parent 035eba7 commit 01f9c3d9fea5b4aa66483ae71676a203961cd6ec Jacob Vorreuter committed Oct 5, 2009
Showing with 24 additions and 7 deletions.
  1. +15 −3 src/emongo.erl
  2. +9 −3 src/emongo_bson.erl
  3. +0 −1 src/emongo_conn.erl
View
@@ -26,15 +26,15 @@
-export([start_link/0, init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).
--export([pools/0, add_pool/5, find/2, find/3, find/4,
+-export([pools/0, oid/0, add_pool/5, find/2, find/3, find/4,
find_all/2, find_all/3, find_all/4, get_more/4,
get_more/5, find_one/3, find_one/4, kill_cursors/2,
insert/3, update/4, update/5, delete/2, delete/3,
count/2]).
-include("emongo.hrl").
--record(state, {pools}).
+-record(state, {pools, oid_index, hashed_hostn}).
%%====================================================================
%% API
@@ -49,6 +49,9 @@ start_link() ->
pools() ->
gen_server:call(?MODULE, pools, infinity).
+oid() ->
+ gen_server:call(?MODULE, oid, infinity).
+
add_pool(PoolId, Host, Port, Database, Size) ->
gen_server:call(?MODULE, {add_pool, PoolId, Host, Port, Database, Size}, infinity).
@@ -199,7 +202,9 @@ count(PoolId, Collection) ->
init(_) ->
process_flag(trap_exit, true),
Pools = initialize_pools(),
- {ok, #state{pools=Pools}}.
+ {ok, HN} = inet:gethostname(),
+ <<HashedHN:3/binary,_/binary>> = erlang:md5(HN),
+ {ok, #state{pools=Pools, oid_index=1, hashed_hostn=HashedHN}}.
%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
@@ -213,6 +218,13 @@ init(_) ->
handle_call(pools, _From, State) ->
{reply, State#state.pools, State};
+handle_call(oid, _From, State) ->
+ {Total_Wallclock_Time, _} = erlang:statistics(wall_clock),
+ Front = Total_Wallclock_Time rem 16#ffffffff,
+ <<_:20/binary,PID:2/binary,_/binary>> = term_to_binary(self()),
+ Index = State#state.oid_index rem 16#ffffff,
+ {reply, <<Front:32, (State#state.hashed_hostn)/binary, PID/binary, Index:24>>, State#state{oid_index = State#state.oid_index + 1}};
+
handle_call({add_pool, PoolId, Host, Port, Database, Size}, _From, #state{pools=Pools}=State) ->
{Result, Pools1} =
case proplists:is_defined(PoolId, Pools) of
View
@@ -39,8 +39,14 @@ encode_key_value(Key, Val) when is_float(Val) ->
%% STRING
encode_key_value(Key, Val) when is_binary(Val) orelse Val == [] orelse (is_list(Val) andalso length(Val) > 0 andalso is_integer(hd(Val))) ->
Key1 = encode_key(Key),
- Val1 = unicode:characters_to_binary(Val),
- <<2, Key1/binary, 0, (byte_size(Val1)+1):32/little-signed, Val1/binary, 0:8>>;
+ case unicode:characters_to_binary(Val) of
+ {error, Bin, RestData} ->
+ exit({cannot_convert_chars_to_binary, Val, Bin, RestData});
+ {incomplete, Bin1, Bin2} ->
+ exit({cannot_convert_chars_to_binary, Val, Bin1, Bin2});
+ Val1 ->
+ <<2, Key1/binary, 0, (byte_size(Val1)+1):32/little-signed, Val1/binary, 0:8>>
+ end;
%% NESTED OBJECT
encode_key_value(Key, [{_,_}|_]=Val) ->
@@ -94,7 +100,7 @@ encode_key_value(Key, {datetime, Val}) ->
Key1 = encode_key(Key),
Date1 = calendar:datetime_to_gregorian_seconds(Val),
Date2 = calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}),
- Epoch = Date1 - Date2,
+ Epoch = (Date1 - Date2) * 1000,
<<9, Key1/binary, 0, Epoch:64/little-signed>>;
encode_key_value(Key, {{Year, Month, Day}, {Hour, Min, Secs}}) when is_integer(Year), is_integer(Month), is_integer(Day), is_integer(Hour), is_integer(Min), is_integer(Secs) ->
View
@@ -66,7 +66,6 @@ loop(State, Leftover) ->
State1 = State#state{requests=[{ReqID, Request}|State#state.requests]},
loop(State1, Leftover);
{tcp, _Sock, Data} ->
- io:format("recv'd ~p~n", [Data]),
case emongo_packet:decode_response(<<Leftover/binary, Data/binary>>) of
undefined ->
loop(State, <<Leftover/binary, Data/binary>>);

0 comments on commit 01f9c3d

Please sign in to comment.