1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2001-2016. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 20%%% File : bench_trans.hrl 21%%% Author : Hakan Mattsson <hakan@cslab.ericsson.se> 22%%% Purpose : Implement the transactions in Canadian database benchmark (LMC/UU-01:025) 23%%% Created : 21 Jun 2001 by Hakan Mattsson <hakan@cslab.ericsson.se> 24%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 25 26-module(bench_trans). 27-author('hakan@cslab.ericsson.se'). 28 29-include("bench.hrl"). 30 31-export([ 32 update_current_location/5, 33 read_current_location/2, 34 read_session_details/4, 35 create_session_to_server/6, 36 delete_session_from_server/5, 37 number_to_suffix/1, 38 number_to_key/2 39 ]). 40 41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 42%% The transactions 43%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 45%% ------------------------------------------------------------------- 46%% T1 47%% ------------------------------------------------------------------- 48 49update_current_location(Wlock, SubscrId, Location, ChangedBy, ChangedTime) -> 50 Suffix = number_to_suffix(SubscrId), 51 [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, Wlock), 52 Subscr2 = Subscr#subscriber{location = Location, 53 changed_by = ChangedBy, 54 changed_time = ChangedTime}, 55 mnesia:write(subscriber, Subscr2, Wlock), 56 {do_commit, false, [ok]}. 57 58%% ------------------------------------------------------------------- 59%% T2 60%% ------------------------------------------------------------------- 61 62read_current_location(_Wlock, SubscrId) -> 63 Suffix = number_to_suffix(SubscrId), 64 [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, read), 65 66 Name = Subscr#subscriber.subscriber_name, 67 Location = Subscr#subscriber.location, 68 ChangedBy = Subscr#subscriber.changed_by, 69 ChangedTime = Subscr#subscriber.changed_time, 70 {do_commit, false, [Name, Location, ChangedBy, ChangedTime]}. 71 72%% ------------------------------------------------------------------- 73%% T3 74%% ------------------------------------------------------------------- 75 76read_session_details(Wlock, SubscrId, ServerBit, ServerId) -> 77 Suffix = number_to_suffix(SubscrId), 78 [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, read), 79 %%[Group] = mnesia:read(group, Subscr#subscriber.group_id, read), 80 [Group] = mnesia:dirty_read(group, Subscr#subscriber.group_id), 81 82 IsAllowed = ((Group#group.allow_read band ServerBit) == ServerBit), 83 IsActive = ((Subscr#subscriber.active_sessions band ServerBit) == ServerBit), 84 ExecuteBranch = (IsAllowed and IsActive), 85 86 case ExecuteBranch of 87 true -> 88 SessionKey = {SubscrId, ServerId}, 89 [Session] = mnesia:read({session, Suffix}, SessionKey, read), 90 91 ServerKey = {ServerId, Suffix}, 92 [Server] = mnesia:read({server, Suffix}, ServerKey, Wlock), 93 Server2 = Server#server{no_of_read = Server#server.no_of_read + 1}, 94 mnesia:write(server, Server2, Wlock), 95 {do_commit, ExecuteBranch, [Session#session.session_details]}; 96 false -> 97 {do_commit, ExecuteBranch, []} 98 end. 99 100%% ------------------------------------------------------------------- 101%% T4 102%% ------------------------------------------------------------------- 103 104create_session_to_server(Wlock, SubscrId, ServerBit, ServerId, Details, DoRollback) -> 105 Suffix = number_to_suffix(SubscrId), 106 [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, Wlock), 107 %%[Group] = mnesia:read(group, Subscr#subscriber.group_id, read), 108 [Group] = mnesia:dirty_read(group, Subscr#subscriber.group_id), 109 110 IsAllowed = ((Group#group.allow_insert band ServerBit) == ServerBit), 111 IsInactive = ((Subscr#subscriber.active_sessions band ServerBit) == 0), 112 ExecuteBranch = (IsAllowed and IsInactive), 113 case ExecuteBranch of 114 true -> 115 SessionKey = {SubscrId, ServerId}, 116 Session = #session{session_key = SessionKey, 117 session_details = Details, 118 suffix = Suffix}, 119 mnesia:write(session, Session, Wlock), 120 Active = (Subscr#subscriber.active_sessions bor ServerBit), 121 Subscr2 = Subscr#subscriber{active_sessions = Active}, 122 mnesia:write(subscriber, Subscr2, Wlock), 123 124 ServerKey = {ServerId, Suffix}, 125 [Server] = mnesia:read({server, Suffix}, ServerKey, Wlock), 126 Server2 = Server#server{no_of_insert = Server#server.no_of_insert + 1}, 127 mnesia:write(server, Server2, Wlock); 128 false -> 129 ignore 130 end, 131 case DoRollback of 132 true -> 133 mnesia:abort({do_rollback, ExecuteBranch, []}); 134 false -> 135 {do_commit, ExecuteBranch, []} 136 end. 137 138%% ------------------------------------------------------------------- 139%% T5 140%% ------------------------------------------------------------------- 141 142delete_session_from_server(Wlock, SubscrId, ServerBit, ServerId, DoRollback) -> 143 Suffix = number_to_suffix(SubscrId), 144 [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, Wlock), 145 %%[Group] = mnesia:read(group, Subscr#subscriber.group_id, read), 146 [Group] = mnesia:dirty_read(group, Subscr#subscriber.group_id), 147 148 IsAllowed = ((Group#group.allow_delete band ServerBit) == ServerBit), 149 IsActive = ((Subscr#subscriber.active_sessions band ServerBit) == ServerBit), 150 ExecuteBranch = (IsAllowed and IsActive), 151 case ExecuteBranch of 152 true -> 153 SessionKey = {SubscrId, ServerId}, 154 mnesia:delete({session, Suffix}, SessionKey, Wlock), 155 Active = (Subscr#subscriber.active_sessions bxor ServerBit), 156 Subscr2 = Subscr#subscriber{active_sessions = Active}, 157 mnesia:write(subscriber, Subscr2, Wlock), 158 159 ServerKey = {ServerId, Suffix}, 160 [Server] = mnesia:read({server, Suffix}, ServerKey, Wlock), 161 Server2 = Server#server{no_of_delete = Server#server.no_of_delete + 1}, 162 mnesia:write(server, Server2, Wlock); 163 false -> 164 ignore 165 end, 166 case DoRollback of 167 true -> 168 mnesia:abort({do_rollback, ExecuteBranch, []}); 169 false -> 170 {do_commit, ExecuteBranch, []} 171 end. 172 173number_to_suffix(SubscrId) when is_integer(SubscrId) -> 174 SubscrId rem 100; 175number_to_suffix(<<_:8/binary, TimesTen:8/integer, TimesOne:8/integer>>) -> 176 ((TimesTen - $0) * 10) + (TimesOne - $0). 177 178number_to_key(Id, C) when is_integer(Id) -> 179 case C#config.use_binary_subscriber_key of 180 true -> 181 list_to_binary(string:right(integer_to_list(Id), 10, $0)); 182 false -> 183 Id 184 end. 185 186