@@ -23,7 +23,7 @@ defmodule Mongo.Session do
23
23
# * `slave_ok` true or false
24
24
# * `mongos` true or false
25
25
# * `implicit` true or false
26
- defstruct [ conn: nil , server_session: nil , slave_ok : false , mongos: false , implicit: false , opts: [ ] ]
26
+ defstruct [ conn: nil , slave_ok: false , mongos : false , server_session: nil , implicit: false , opts: [ ] ]
27
27
28
28
@ impl true
29
29
def callback_mode ( ) do
@@ -87,7 +87,11 @@ defmodule Mongo.Session do
87
87
88
88
@ impl true
89
89
def init ( { conn , server_session , slave_ok , mongos , type , opts } ) do
90
- data = % Session { conn: conn , server_session: server_session , slave_ok: slave_ok , mongos: mongos , implicit: ( type == :implict ) , opts: opts }
90
+ data = % Session { conn: conn ,
91
+ server_session: server_session ,
92
+ slave_ok: slave_ok ,
93
+ mongos: mongos ,
94
+ implicit: ( type == :implicit ) , opts: opts }
91
95
{ :ok , :no_transaction , data }
92
96
end
93
97
@@ -96,22 +100,24 @@ defmodule Mongo.Session do
96
100
{ :next_state , :starting_transaction , % Session { data | server_session: ServerSession . next_txn_num ( session ) } , { :reply , from , :ok } }
97
101
end
98
102
def handle_event ( { :call , from } , { :bind_session , cmd } , :no_transaction , % Session { conn: conn , server_session: % ServerSession { session_id: id } } ) do
99
- { :keep_state_and_data , { :reply , from , conn , Keyword . merge ( cmd , lsid: id ) } }
103
+ { :keep_state_and_data , { :reply , from , { :ok , conn , Keyword . merge ( cmd , lsid: % { id: id } ) } } }
100
104
end
101
105
def handle_event ( { :call , from } , { :bind_session , cmd } , :starting_transaction , % Session { conn: conn , server_session: % ServerSession { session_id: id , txn_num: txn_num } } = data ) do
102
106
result = Keyword . merge ( cmd ,
103
- lsid: id ,
104
- txnNumber: txn_num ,
107
+ readConcern: Keyword . get ( opts , :read_concern ) ,
108
+ lsid: % { id: id } ,
109
+ txnNumber: % BSON.LongNumber { value: txn_num } ,
105
110
startTransaction: true ,
106
- autocommit: false )
107
- { :next_state , :transaction_in_progress , data , { :reply , from , conn , result } }
111
+ autocommit: false ) |> filter_nils ( )
112
+ IO . puts inspect result
113
+ { :next_state , :transaction_in_progress , data , { :reply , from , { :ok , conn , result } } }
108
114
end
109
115
def handle_event ( { :call , from } , { :bind_session , cmd } , :transaction_in_progress , % Session { conn: conn , server_session: % ServerSession { session_id: id , txn_num: txn_num } } ) do
110
116
result = Keyword . merge ( cmd ,
111
- lsid: id ,
112
- txnNumber: txn_num ,
117
+ lsid: % { id: id } ,
118
+ txnNumber: % BSON.LongNumber { value: txn_num } ,
113
119
autocommit: false )
114
- { :keep_state_and_data , { :reply , from , conn , result } }
120
+ { :keep_state_and_data , { :reply , from , { :ok , conn , result } } }
115
121
end
116
122
117
123
def handle_event ( { :call , from } , { :commit_transaction } , :transaction_in_progress , data ) do
@@ -126,47 +132,41 @@ defmodule Mongo.Session do
126
132
def handle_event ( { :call , from } , { :end_session } , _state , _data ) do
127
133
{ :stop_and_reply , :normal , { :reply , from , :ok } }
128
134
end
135
+ def handle_event ( { :call , from } , { :server_session } , _state , % Session { server_session: session_server , implicit: implicit } ) do
136
+ { :keep_state_and_data , { :reply , from , { :ok , session_server , implicit } } }
137
+ end
129
138
130
- defp run_commit_command ( % { conn: conn , server_session: % ServerSession { session_id: id , txn_num: txn_num } } ) do
139
+ defp run_commit_command ( % { conn: conn , server_session: % ServerSession { session_id: id , txn_num: txn_num } , opts: opts } ) do
131
140
132
141
#{
133
- # commitTransaction : 1,
134
- # lsid : { id : <UUID> },
135
- # txnNumber : <Int64>,
136
- # autocommit : false,
137
- # writeConcern : {...},
138
- # maxTimeMS: <Int64>,
139
142
# recoveryToken : {...}
140
143
#}
141
144
142
145
cmd = [
143
146
commitTransaction: 1 ,
144
- lsid: id ,
145
- txnNumber: txn_num ,
147
+ lsid: % { id: id } ,
148
+ txnNumber: % BSON.LongNumber { value: txn_num } ,
146
149
autocommit: false ,
147
- writeConcern: % { w: 1 }
148
- ]
150
+ writeConcern: Keyword . get ( opts , :write_concern ) ,
151
+ maxTimeMS: Keyword . get ( opts , :max_commit_time_ms )
152
+ ] |> filter_nils ( )
149
153
150
154
Mongo . exec_command ( conn , cmd , database: "admin" )
151
155
end
152
156
153
- defp run_abort_command ( % { conn: conn , server_session: % ServerSession { session_id: id , txn_num: txn_num } } ) do
157
+ defp filter_nils ( keyword ) when is_list ( keyword ) do
158
+ Enum . reject ( keyword , fn { _key , value } -> is_nil ( value ) end )
159
+ end
154
160
155
- #{
156
- # abortTransaction : 1,
157
- # lsid : { id : <UUID> },
158
- # txnNumber : <Int64>,
159
- # autocommit : false,
160
- # writeConcern : {...}
161
- #}
161
+ defp run_abort_command ( % { conn: conn , server_session: % ServerSession { session_id: id , txn_num: txn_num } , opts: opts } ) do
162
162
163
163
cmd = [
164
164
abortTransaction: 1 ,
165
- lsid: id ,
166
- txnNumber: txn_num ,
165
+ lsid: % { id: id } ,
166
+ txnNumber: % BSON.LongNumber { value: txn_num } ,
167
167
autocommit: false ,
168
- writeConcern: % { w: 1 }
169
- ]
168
+ writeConcern: Keyword . get ( opts , :write_concern ) ,
169
+ ] |> filter_nils ( )
170
170
171
171
Mongo . exec_command ( conn , cmd , database: "admin" )
172
172
end
0 commit comments