From 6b3e40fd7362ee56dba1aa8004e3b2b6f21b89d7 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Wed, 11 Jun 2008 00:59:03 +0000 Subject: [PATCH] use type-appropriate methods for size and map for dict and set structures Reviewed By: cpiro Test Plan: TODO Revert Plan: sure git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@666426 13f79535-47bb-0310-9956-ffa450edef68 --- lib/alterl/src/thrift_client.erl | 20 +++++------ lib/alterl/src/thrift_protocol.erl | 53 ++++++++++++++---------------- 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/lib/alterl/src/thrift_client.erl b/lib/alterl/src/thrift_client.erl index d76efdfb..c1d43aae 100644 --- a/lib/alterl/src/thrift_client.erl +++ b/lib/alterl/src/thrift_client.erl @@ -32,7 +32,6 @@ start_link(Host, Port, Service) when is_integer(Port), is_atom(Service) -> gen_server:start_link(?MODULE, [Host, Port, Service], []). - call(Client, Function, Args) when is_pid(Client), is_atom(Function), is_list(Args) -> case gen_server:call(Client, {call, Function, Args}) of @@ -40,9 +39,6 @@ call(Client, Function, Args) R = {error, _} -> R; {exception, Exception} -> throw(Exception) end. - - - %%==================================================================== %% gen_server callbacks @@ -60,10 +56,11 @@ init([Host, Port, Service]) -> [binary, {packet, 0}, {active, false}, - {nodelay, true}]), - {ok, Transport} = thrift_socket_transport:new(Sock), + {nodelay, true} + ]), + {ok, Transport} = thrift_socket_transport:new(Sock), {ok, BufTransport} = thrift_buffered_transport:new(Transport), - {ok, Protocol} = thrift_binary_protocol:new(BufTransport), + {ok, Protocol} = thrift_binary_protocol:new(BufTransport), {ok, #state{service = Service, protocol = Protocol, seqid = 0}}. @@ -95,7 +92,7 @@ handle_call({call, Function, Args}, _From, State = #state{service = Service, _ -> throw({error, {function_clause, ST}}) end end, - + {reply, Result, State}. @@ -159,7 +156,6 @@ send_function_call(#state{protocol = Proto, thrift_protocol:flush_transport(Proto), ok. - receive_function_result(State = #state{protocol = Proto, service = Service}, Function) -> @@ -178,10 +174,10 @@ read_result(State = #state{protocol = Proto, case thrift_protocol:read(Proto, message_begin) of #protocol_message_begin{seqid = RetSeqId} when RetSeqId =/= SeqId -> {error, {bad_seq_id, SeqId}}; - + #protocol_message_begin{type = ?tMessageType_EXCEPTION} -> handle_application_exception(State); - + #protocol_message_begin{type = ?tMessageType_REPLY} -> handle_reply(State, Function, ReplyType) end. @@ -210,7 +206,7 @@ handle_reply(State = #state{protocol = Proto, end, ok = thrift_protocol:read(Proto, message_end), Result. - + handle_application_exception(State = #state{protocol = Proto}) -> {ok, Exception} = thrift_protocol:read(Proto, diff --git a/lib/alterl/src/thrift_protocol.erl b/lib/alterl/src/thrift_protocol.erl index 7db79291..f1b2bcc9 100644 --- a/lib/alterl/src/thrift_protocol.erl +++ b/lib/alterl/src/thrift_protocol.erl @@ -5,10 +5,10 @@ read/2, skip/2, flush_transport/1, + typeid_to_atom/1 + ]). - typeid_to_atom/1, - - behaviour_info/1]). +-export([behaviour_info/1]). -include("thrift_constants.hrl"). -include("thrift_protocol.hrl"). @@ -23,12 +23,10 @@ behaviour_info(callbacks) -> ]; behaviour_info(_Else) -> undefined. - new(Module, Data) when is_atom(Module) -> {ok, #protocol{module = Module, data = Data}}. - flush_transport(#protocol{module = Module, data = Data}) -> Module:flush_transport(Data). @@ -46,7 +44,6 @@ typeid_to_atom(?tType_STRUCT) -> struct; typeid_to_atom(?tType_MAP) -> map; typeid_to_atom(?tType_SET) -> set; typeid_to_atom(?tType_LIST) -> list. - term_to_typeid(void) -> ?tType_VOID; term_to_typeid(bool) -> ?tType_BOOL; @@ -61,7 +58,6 @@ term_to_typeid({map, _, _}) -> ?tType_MAP; term_to_typeid({set, _}) -> ?tType_SET; term_to_typeid({list, _}) -> ?tType_LIST. - %% Structure is like: %% [{Fid, Type}, ...] read(IProto, {struct, Structure}) when is_list(Structure) -> @@ -97,7 +93,7 @@ read(IProto, {struct, {Module, StructureName}}) when is_atom(Module), read(IProto, {list, Type}) -> #protocol_list_begin{etype = EType, size = Size} = read(IProto, list_begin), - List = [Result || {ok, Result} <- + List = [Result || {ok, Result} <- [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]], ok = read(IProto, list_end), {ok, List}; @@ -106,7 +102,7 @@ read(IProto, {map, KeyType, ValType}) -> #protocol_map_begin{size = Size} = read(IProto, map_begin), - List = [{Key, Val} || {{ok, Key}, {ok, Val}} <- + List = [{Key, Val} || {{ok, Key}, {ok, Val}} <- [{read(IProto, KeyType), read(IProto, ValType)} || _X <- lists:duplicate(Size, 0)]], ok = read(IProto, map_end), @@ -116,7 +112,7 @@ read(IProto, {set, Type}) -> #protocol_set_begin{etype = _EType, size = Size} = read(IProto, set_begin), - List = [Result || {ok, Result} <- + List = [Result || {ok, Result} <- [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]], ok = read(IProto, set_end), {ok, sets:from_list(List)}; @@ -166,7 +162,7 @@ skip(Proto, set) -> skip(Proto, list) -> List = read(Proto, list_begin), ok = skip_list_loop(Proto, List), - ok = read(Proto, list_end); + ok = read(Proto, list_end); skip(Proto, Type) when is_atom(Type) -> _Ignore = read(Proto, Type), @@ -219,7 +215,7 @@ skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype, %%-------------------------------------------------------------------- %% Function: write(OProto, {Type, Data}) -> ok -%% +%% %% Type = {struct, StructDef} | %% {list, Type} | %% {map, KeyType, ValType} | @@ -233,7 +229,7 @@ skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype, %% | set() -- for set %% | term() -- for base types %% -%% Description: +%% Description: %%-------------------------------------------------------------------- write(Proto, {{struct, StructDef}, Data}) when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 -> @@ -266,33 +262,33 @@ write(Proto, {{list, Type}, Data}) ok; write(Proto, {{map, KeyType, ValType}, Data}) -> - DataList = dict:to_list(Data), ok = write(Proto, #protocol_map_begin{ ktype = term_to_typeid(KeyType), vtype = term_to_typeid(ValType), - size = length(DataList) - }), - lists:foreach(fun({KeyData, ValData}) -> - ok = write(Proto, {KeyType, KeyData}), - ok = write(Proto, {ValType, ValData}) - end, - DataList), + size = dict:size(Data) + }), + dict:fold(fun(KeyData, ValData, _Acc) -> + ok = write(Proto, {KeyType, KeyData}), + ok = write(Proto, {ValType, ValData}) + end, + _AccO = ok, + Data), ok = write(Proto, map_end), ok; write(Proto, {{set, Type}, Data}) -> true = sets:is_set(Data), - DataList = sets:to_list(Data), ok = write(Proto, #protocol_set_begin{ etype = term_to_typeid(Type), - size = length(DataList) - }), - lists:foreach(fun(Elem) -> - ok = write(Proto, {Type, Elem}) - end, - DataList), + size = sets:size(Data) + }), + sets:fold(fun(Elem, _Acc) -> + ok = write(Proto, {Type, Elem}) + end, + _Acc0 = ok, + Data), ok = write(Proto, set_end), ok; @@ -300,7 +296,6 @@ write(#protocol{module = Module, data = ModuleData}, Data) -> Module:write(ModuleData, Data). - struct_write_loop(Proto, [{Fid, Type} | RestStructDef], [Data | RestData]) -> case Data of undefined -> -- 2.17.1