Commits

Jacob Vorreuter  committed 036c16c

fixing date format and adding oid function

  • Participants
  • Parent commits 405d7a5

Comments (0)

Files changed (3)

File src/emongo.erl

 -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,
 
 -include("emongo.hrl").
 
--record(state, {pools}).
+-record(state, {pools, oid_index, hashed_hostn}).
 
 %%====================================================================
 %% API
 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).
 	
 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} |
 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

File src/emongo_bson.erl

 %% 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) ->
 	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) ->

File src/emongo_conn.erl

 			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>>);