Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jacob Vorreuter
committed
Sep 18, 2009
1 parent
d84d7bf
commit 549cbe5
Showing
7 changed files
with
218 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,12 @@ | |||
-record(pool, {id, host, port, database, size=1, conn_pids=[], req_id=1}). | |||
-record(header, {messageLength, requestID, responseTo, opCode}). | |||
-record(response, {header, responseFlag, cursorID, startingFrom, numberReturned, documents}). | |||
|
|||
-define(OP_REPLY, 1). | |||
-define(OP_MSG, 1000). | |||
-define(OP_UPDATE, 2001). | |||
-define(OP_INSERT, 2002). | |||
-define(OP_QUERY, 2004). | |||
-define(OP_GET_MORE, 2005). | |||
-define(OP_DELETE, 2006). | |||
-define(OP_KILL_CURSORS, 2007). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
[{emongo, [ | |||
{pools, [ | |||
{test1, [ | |||
{size, 1}, | |||
{host, "localhost"}, | |||
{port, 27017}, | |||
{database, "testdatabase"} | |||
]} | |||
]} | |||
]}]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,76 @@ | |||
%% Copyright (c) 2009 Jacob Vorreuter <jacob.vorreuter@gmail.com> | |||
%% | |||
%% Permission is hereby granted, free of charge, to any person | |||
%% obtaining a copy of this software and associated documentation | |||
%% files (the "Software"), to deal in the Software without | |||
%% restriction, including without limitation the rights to use, | |||
%% copy, modify, merge, publish, distribute, sublicense, and/or sell | |||
%% copies of the Software, and to permit persons to whom the | |||
%% Software is furnished to do so, subject to the following | |||
%% conditions: | |||
%% | |||
%% The above copyright notice and this permission notice shall be | |||
%% included in all copies or substantial portions of the Software. | |||
%% | |||
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |||
%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |||
%% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |||
%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |||
%% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |||
%% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |||
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |||
%% OTHER DEALINGS IN THE SOFTWARE. | |||
-module(emongo_conn). | |||
|
|||
-export([start_link/2, init/3, send/3, send_recv/3]). | |||
|
|||
-record(request, {req_id, requestor}). | |||
-record(state, {socket, requests}). | |||
|
|||
-include("emongo.hrl"). | |||
|
|||
start_link(Host, Port) -> | |||
proc_lib:start_link(?MODULE, init, [Host, Port, self()]). | |||
|
|||
init(Host, Port, Parent) -> | |||
Socket = open_socket(Host, Port), | |||
proc_lib:init_ack(Parent, self()), | |||
loop(#state{socket=Socket, requests=[]}). | |||
|
|||
send(Pid, ReqID, Packet) -> | |||
gen:call(Pid, '$emongo_conn_send', {ReqID, Packet}). | |||
|
|||
send_recv(Pid, ReqID, Packet) -> | |||
gen:call(Pid, '$emongo_conn_send_recv', {ReqID, Packet}). | |||
|
|||
loop(State) -> | |||
receive | |||
{'$emongo_conn_send', {From, Mref}, {_ReqID, Packet}} -> | |||
gen_tcp:send(State#state.socket, Packet), | |||
gen:reply({From, Mref}, ok), | |||
loop(State); | |||
{'$emongo_conn_send_recv', {From, Mref}, {ReqID, Packet}} -> | |||
gen_tcp:send(State#state.socket, Packet), | |||
gen:reply({From, Mref}, ok), | |||
Request = #request{req_id=ReqID, requestor={From, Mref}}, | |||
State1 = State#state{requests=[Request|State#state.requests]}, | |||
loop(State1); | |||
{tcp, _Sock, Data} -> | |||
Resp = emongo_packet:decode_response(Data), | |||
ResponseTo = (Resp#response.header)#header.responseTo, | |||
case proplists:get_value(ResponseTo, State#state.requests) of | |||
undefined -> | |||
ok; | |||
Requestor -> | |||
gen:reply(Requestor, Resp) | |||
end, | |||
loop(State) | |||
end. | |||
|
|||
open_socket(Host, Port) -> | |||
case gen_tcp:connect(Host, Port, [binary, {active, true}]) of | |||
{ok, Sock} -> | |||
Sock; | |||
{error, Reason} -> | |||
exit({failed_to_open_socket, Reason}) | |||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
%% Copyright (c) 2009 Jacob Vorreuter <jacob.vorreuter@gmail.com> | |||
%% | |||
%% Permission is hereby granted, free of charge, to any person | |||
%% obtaining a copy of this software and associated documentation | |||
%% files (the "Software"), to deal in the Software without | |||
%% restriction, including without limitation the rights to use, | |||
%% copy, modify, merge, publish, distribute, sublicense, and/or sell | |||
%% copies of the Software, and to permit persons to whom the | |||
%% Software is furnished to do so, subject to the following | |||
%% conditions: | |||
%% | |||
%% The above copyright notice and this permission notice shall be | |||
%% included in all copies or substantial portions of the Software. | |||
%% | |||
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |||
%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |||
%% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |||
%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |||
%% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |||
%% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |||
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |||
%% OTHER DEALINGS IN THE SOFTWARE. | |||
-module(emongo_packet). | |||
|
|||
-export([insert/4, decode_response/1]). | |||
|
|||
-include("emongo.hrl"). | |||
|
|||
insert(Database, Collection, ReqID, {obj, Props}) -> | |||
FullName = mongodb_bson:encode_cstring(lists:concat([Database, ".", Collection])), | |||
EncodedDocument = mongodb_bson:encode({obj, Props}), | |||
Message = <<0:32, FullName/binary, EncodedDocument/binary>>, | |||
Length = byte_size(Message), | |||
<<(Length+16):32/little-signed, ReqID:32/little-signed, 0:32, ?OP_INSERT:32/little-signed, Message/binary>>. | |||
|
|||
decode_response(<<Length:32/little-signed, ReqID:32/little-signed, RespTo:32/little-signed, Op:32/little-signed, Message/binary>>) -> | |||
<<RespFlag:32/little-signed, | |||
CursorID:64/little-signed, | |||
StartingFrom:32/little-signed, | |||
NumRet:32/little-signed, | |||
Documents/binary>> = Message, | |||
#response{ | |||
header = {header, Length, ReqID, RespTo, Op}, | |||
responseFlag = RespFlag, | |||
cursorID = CursorID, | |||
startingFrom = StartingFrom, | |||
numberReturned = NumRet, | |||
documents = mongodb_bson:decode(Documents) | |||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,10 +1,12 @@ | |||
#!/usr/local/bin/escript | #!/usr/local/bin/escript | ||
%% -*- erlang -*- | %% -*- erlang -*- | ||
%%! -pa ebin -sasl errlog_type error -boot start_sasl -noshell | %%! -pa ebin -sasl errlog_type error -boot start_sasl -noshell -config priv/example | ||
|
|
||
main(_) -> | main(_) -> | ||
etap:plan(unknown), | etap:plan(unknown), | ||
error_logger:tty(false), | error_logger:tty(false), | ||
etap_application:start_ok(emongo, "application 'emongo' started ok"), | etap_application:start_ok(emongo, "application 'emongo' started ok"), | ||
|
|
||
etap:is(length(emongo:pools()), 1, "one pool exists in state"), | |||
|
|||
etap:end_tests(). | etap:end_tests(). |