orber_ifr_utils.erl
来自「OTP是开放电信平台的简称」· ERL 代码 · 共 437 行 · 第 1/2 页
ERL
437 行
get_field(Objref,FieldName) -> Object = get_object(Objref), select(Object,FieldName).%%%----------------------------------------------------------------------%%% Atomically set the value of a field in a record in the DBset_field(Objref,FieldName,Value) -> _F = fun() -> Object = get_object(Objref), New_object = construct(Object,FieldName,Value), mnesia:write(New_object) end, write_result(ifr_transaction_write(_F)).%%%----------------------------------------------------------------------%%% Check a write transactionwrite_result({atomic,ok}) -> ok;write_result(Wres) -> orber:dbg("[~p] orber_ifr_utils:write_result(~p);~n", [?LINE, Wres], ?DEBUG_LEVEL), corba:raise(#'INTF_REPOS'{completion_status=?COMPLETED_NO}).%%%----------------------------------------------------------------------%%% Extract the data from a readread_result({atomic,[Qres]}) -> Qres;read_result({atomic,[]}) -> [];read_result(Qres) -> orber:dbg("[~p] orber_ifr_utils:read_result(~p);~n", [?LINE, Qres], ?DEBUG_LEVEL), corba:raise(#'INTF_REPOS'{completion_status=?COMPLETED_NO}).%%%----------------------------------------------------------------------%%% Execute a transaction or a dirty read/write.%%%%%% Since nested transctions will upgrade the inner activity to the%%% same kind as the outer, we cannot use the check the result in the%%% above simplistic manner. Therefore we will not mix transaction%%% with async_dirty (or any of the other transaction-like%%% activities). A rather extensive rewrite of the query extraction%%% code must be done first.ifr_transaction_read(Fun) -> % read synchronously Tr = mnesia:transaction(Fun), {atomic, _} = Tr, Tr.ifr_transaction_write(Fun) -> % write synchronously Tr = mnesia:transaction(Fun), {atomic, _} = Tr, Tr.ifr_transaction_read_write(Fun) -> % write synchronously Tr = mnesia:transaction(Fun), {atomic, _} = Tr, Tr.%%%----------------------------------------------------------------------%%% Make an object reference from an objectmakeref(Obj) -> [ObjType, ObjID | _] = tuple_to_list(Obj), {ObjType, ObjID}.%%%----------------------------------------------------------------------%%% Make a unique tag.%%%%%% The call to term_to_binary is made to hide the representation of the%%% unique tag. I do this because the tuple generated takes a lot of space%%% when I dump the database. A binary is simply printed as #Bin, which%%% is much less obtrusive.%%% The code has been moved to a macro defined in orber_ifr.hrl, so we%%% can use a simpler uniqification code when debugging.unique() -> term_to_binary({node(), now()}).%%%----------------------------------------------------------------------%%% Check for an existing object with the Id of the object which is%%% about to be created.existence_check({ObjType, ObjID}, Id) -> Rep = case ObjType of ir_Repository -> {ObjType, ObjID}; _ -> orber_ifr_contained:'_get_containing_repository'({ObjType, ObjID}) end, case orber_ifr_repository:lookup_id(Rep, Id) of [] -> ok; What -> orber:dbg("[~p] orber_ifr_utils:existence_check(~p, ~p, ~p);~n" "Name clash(?): ~p", [?LINE, ObjType, ObjID, Id, What], ?DEBUG_LEVEL), corba:raise(#'INTF_REPOS'{completion_status=?COMPLETED_NO}) end.existence_check(Id, Tab, FieldNum) -> case mnesia:dirty_index_read(Tab, Id, FieldNum) of [] -> ok; What -> orber:dbg("[~p] orber_ifr_utils:existence_check(~p, ~p, ~p);~n" "Name clash(?): ~p", [?LINE, Id, Tab, FieldNum, What], ?DEBUG_LEVEL), corba:raise(#'INTF_REPOS'{completion_status=?COMPLETED_NO}) end.%%======================================================================%% Database initializationinit_DB(Timeout, Options) -> init_DB(Timeout, Options, false).init_DB(Timeout, Options, LightIFR) -> Func = case Options of {localCopy, IFR_storage_type} when LightIFR == true -> ?ifr_light_record_tuple_list_local(IFR_storage_type); {localCopy, IFR_storage_type} -> ?ifr_record_tuple_list_local(IFR_storage_type); _ when LightIFR == true -> ?ifr_light_record_tuple_list(Options); _ -> ?ifr_record_tuple_list(Options) end, create_tables(Func), Wait = wait_for_tables(LightIFR, Timeout), db_error_check([Wait],"Database table waiting failed.").wait_for_tables(true, Timeout) -> mnesia:wait_for_tables(?ifr_light_object_list, Timeout);wait_for_tables(_, Timeout) -> mnesia:wait_for_tables(?ifr_object_list, Timeout).db_error_check(Checkval,_Message) -> case lists:any(fun(X) -> X/= ok end, Checkval) of true -> corba:raise(#'INTF_REPOS'{completion_status=?COMPLETED_NO}); false -> ok end. create_tables([{T,F}|Rest]) -> case F() of ok -> create_tables2(Rest); {aborted,{already_exists,_}} -> exit({error, "Orber Mnesia Table(s) already exist. Cannot install Orber."}); Reason -> orber:dbg("[~p] orber_ifr_utils:create_tables(~p);~n" "Failed to create the Mnesia table.~n" "Reason: ~p", [?LINE, T, Reason], ?DEBUG_LEVEL), exit({error, "Unable to create Mnesia Table"}) end.create_tables2([]) -> ok;create_tables2([{T,F}|Rest]) -> case F() of ok -> create_tables2(Rest); Reason -> orber:dbg("[~p] orber_ifr_utils:create_tables2(~p);~n" "Failed to create the Mnesia table.~n" "Reason: ~p", [?LINE, T, Reason], ?DEBUG_LEVEL), corba:raise(#'INTF_REPOS'{completion_status=?COMPLETED_NO}) end.%%%----------------------------------------------------------------------%%% Create an interface repository. This function should only be called%%% once, after the database has been set up and initialized.create_repository() -> case orber:light_ifr() of true -> #orber_light_ifr_ref{data = #lightdata{scope = "", id = ""}}; false -> _R = fun() -> Pat = mnesia:table_info(ir_Repository, wild_pattern), case [X#ir_Repository.ir_Internal_ID || X <- mnesia:match_object(Pat)] of [] -> PrimitiveDefs = create_primitivedefs(), New = #ir_Repository{ir_Internal_ID = unique(), def_kind = dk_Repository, contents = [], primitivedefs = PrimitiveDefs}, mnesia:write(New), {ir_Repository,New#ir_Repository.ir_Internal_ID}; [Rep_ID] -> {ir_Repository,Rep_ID}; Error -> mnesia:abort(Error) end end, case mnesia:transaction(_R) of {atomic, RepRef} -> RepRef; {aborted, Error} -> orber:dbg("[~p] orber_ifr_utils:create_repository() failed;~n" "Reason: ~p", [?LINE, Error], ?DEBUG_LEVEL), corba:raise(#'INTF_REPOS'{completion_status=?COMPLETED_NO}) end end.create_primitivedefs() -> lists:map(fun(Pk) -> orber_ifr_repository:create_primitivedef(Pk, false) end, [pk_void,pk_short,pk_long,pk_longlong,pk_ulonglong,pk_ushort,pk_ulong, pk_float,pk_double,pk_boolean,pk_char,pk_wchar,pk_octet,pk_any, pk_TypeCode,pk_Principal,pk_string,pk_wstring,pk_objref]).
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?