summaryrefslogtreecommitdiff
path: root/lib/mnesia/examples/bench/bench_trans.erl
blob: 73934d4fb58973f4f49d4dae2779f61e851316c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2001-2016. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% File    : bench_trans.hrl
%%% Author  : Hakan Mattsson <hakan@cslab.ericsson.se>
%%% Purpose : Implement the transactions in Canadian database benchmark (LMC/UU-01:025)
%%% Created : 21 Jun 2001 by Hakan Mattsson <hakan@cslab.ericsson.se>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(bench_trans).
-author('hakan@cslab.ericsson.se').

-include("bench.hrl").

-export([
	 update_current_location/5,
	 read_current_location/2,
	 read_session_details/4,
	 create_session_to_server/6,
	 delete_session_from_server/5,
	 number_to_suffix/1,
	 number_to_key/2
	]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% The transactions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% -------------------------------------------------------------------
%% T1
%% -------------------------------------------------------------------

update_current_location(Wlock, SubscrId, Location, ChangedBy, ChangedTime) ->
    Suffix   = number_to_suffix(SubscrId),
    [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, Wlock),
    Subscr2  = Subscr#subscriber{location     = Location,
                                 changed_by   = ChangedBy,
                                 changed_time = ChangedTime},
    mnesia:write(subscriber, Subscr2, Wlock),
    {do_commit, false, [ok]}.

%% -------------------------------------------------------------------
%% T2
%% -------------------------------------------------------------------

read_current_location(_Wlock, SubscrId) ->
    Suffix   = number_to_suffix(SubscrId),
    [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, read),

    Name          = Subscr#subscriber.subscriber_name,
    Location      = Subscr#subscriber.location,
    ChangedBy     = Subscr#subscriber.changed_by,
    ChangedTime   = Subscr#subscriber.changed_time,
    {do_commit, false, [Name, Location, ChangedBy, ChangedTime]}.

%% -------------------------------------------------------------------
%% T3
%% -------------------------------------------------------------------

read_session_details(Wlock, SubscrId, ServerBit, ServerId) ->
    Suffix   = number_to_suffix(SubscrId),
    [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, read),
    %%[Group]  = mnesia:read(group, Subscr#subscriber.group_id, read),
    [Group]  = mnesia:dirty_read(group, Subscr#subscriber.group_id),

    IsAllowed     = ((Group#group.allow_read band ServerBit) == ServerBit),
    IsActive      = ((Subscr#subscriber.active_sessions band ServerBit) == ServerBit),    
    ExecuteBranch = (IsAllowed and IsActive),

    case ExecuteBranch of
        true ->
            SessionKey = {SubscrId, ServerId},
            [Session] = mnesia:read({session, Suffix}, SessionKey, read),

            ServerKey = {ServerId, Suffix},
            [Server] = mnesia:read({server, Suffix}, ServerKey, Wlock),
            Server2 = Server#server{no_of_read = Server#server.no_of_read + 1},
            mnesia:write(server, Server2, Wlock),
            {do_commit, ExecuteBranch, [Session#session.session_details]};
        false  ->
            {do_commit, ExecuteBranch, []}
    end.

%% -------------------------------------------------------------------
%% T4
%% -------------------------------------------------------------------

create_session_to_server(Wlock, SubscrId, ServerBit, ServerId, Details, DoRollback) ->
    Suffix   = number_to_suffix(SubscrId),
    [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, Wlock),
    %%[Group]  = mnesia:read(group, Subscr#subscriber.group_id, read),
    [Group]  = mnesia:dirty_read(group, Subscr#subscriber.group_id),

    IsAllowed     = ((Group#group.allow_insert band ServerBit) == ServerBit),
    IsInactive    = ((Subscr#subscriber.active_sessions band ServerBit) == 0),
    ExecuteBranch = (IsAllowed and IsInactive),
    case ExecuteBranch of
        true ->
            SessionKey = {SubscrId, ServerId},
            Session = #session{session_key     = SessionKey,
                               session_details = Details,
                               suffix          = Suffix},
            mnesia:write(session, Session, Wlock),
            Active = (Subscr#subscriber.active_sessions bor ServerBit),
            Subscr2 = Subscr#subscriber{active_sessions = Active},
            mnesia:write(subscriber, Subscr2, Wlock),

            ServerKey = {ServerId, Suffix},
            [Server] = mnesia:read({server, Suffix}, ServerKey, Wlock),
            Server2 = Server#server{no_of_insert = Server#server.no_of_insert + 1},
            mnesia:write(server, Server2, Wlock);
        false ->
            ignore
    end,
    case DoRollback of
        true ->
            mnesia:abort({do_rollback, ExecuteBranch, []});
        false ->
            {do_commit, ExecuteBranch, []}
    end.

%% -------------------------------------------------------------------
%% T5
%% -------------------------------------------------------------------

delete_session_from_server(Wlock, SubscrId, ServerBit, ServerId, DoRollback) ->
    Suffix   = number_to_suffix(SubscrId),
    [Subscr] = mnesia:read({subscriber, Suffix}, SubscrId, Wlock),
    %%[Group]  = mnesia:read(group, Subscr#subscriber.group_id, read),
    [Group]  = mnesia:dirty_read(group, Subscr#subscriber.group_id),

    IsAllowed     = ((Group#group.allow_delete band ServerBit) == ServerBit),
    IsActive      = ((Subscr#subscriber.active_sessions band ServerBit) == ServerBit),    
    ExecuteBranch = (IsAllowed and IsActive),
    case ExecuteBranch of
        true ->
            SessionKey = {SubscrId, ServerId},
            mnesia:delete({session, Suffix}, SessionKey, Wlock),
            Active = (Subscr#subscriber.active_sessions bxor ServerBit),
            Subscr2 = Subscr#subscriber{active_sessions = Active},
            mnesia:write(subscriber, Subscr2, Wlock),

            ServerKey = {ServerId, Suffix},
            [Server] = mnesia:read({server, Suffix}, ServerKey, Wlock),
            Server2 = Server#server{no_of_delete = Server#server.no_of_delete + 1},
            mnesia:write(server, Server2, Wlock);
        false ->
            ignore
    end,
    case DoRollback of
        true ->
            mnesia:abort({do_rollback, ExecuteBranch, []});
        false ->
            {do_commit, ExecuteBranch, []}
    end.

number_to_suffix(SubscrId) when is_integer(SubscrId) ->
    SubscrId rem 100;
number_to_suffix(<<_:8/binary, TimesTen:8/integer, TimesOne:8/integer>>) ->
    ((TimesTen - $0) * 10) + (TimesOne - $0).

number_to_key(Id, C) when is_integer(Id) ->
    case C#config.use_binary_subscriber_key of
	true ->
	    list_to_binary(string:right(integer_to_list(Id), 10, $0));
	false ->
	    Id
    end.