@@ -6,7 +6,6 @@ use std::{
6
6
future:: Future ,
7
7
} ;
8
8
use tikv_client_proto:: kvrpcpb;
9
- use tokio:: sync:: { Mutex , MutexGuard } ;
10
9
11
10
#[ derive( Default ) ]
12
11
struct InnerBuffer {
@@ -31,17 +30,17 @@ impl InnerBuffer {
31
30
/// A caching layer which buffers reads and writes in a transaction.
32
31
#[ derive( Default ) ]
33
32
pub struct Buffer {
34
- mutations : Mutex < InnerBuffer > ,
33
+ inner : InnerBuffer ,
35
34
}
36
35
37
36
impl Buffer {
38
37
/// Get the primary key of the buffer.
39
38
pub async fn get_primary_key ( & self ) -> Option < Key > {
40
- self . mutations . lock ( ) . await . primary_key . clone ( )
39
+ self . inner . primary_key . clone ( )
41
40
}
42
41
/// Get the primary key of the buffer, if not exists, use `key` as the primary key.
43
- pub async fn get_primary_key_or ( & self , key : & Key ) -> Key {
44
- self . mutations . lock ( ) . await . get_primary_key_or ( key) . clone ( )
42
+ pub async fn get_primary_key_or ( & mut self , key : & Key ) -> Key {
43
+ self . inner . get_primary_key_or ( key) . clone ( )
45
44
}
46
45
47
46
/// Get a value from the buffer.
@@ -55,7 +54,7 @@ impl Buffer {
55
54
56
55
/// Get a value from the buffer. If the value is not present, run `f` to get
57
56
/// the value.
58
- pub async fn get_or_else < F , Fut > ( & self , key : Key , f : F ) -> Result < Option < Value > >
57
+ pub async fn get_or_else < F , Fut > ( & mut self , key : Key , f : F ) -> Result < Option < Value > >
59
58
where
60
59
F : FnOnce ( Key ) -> Fut ,
61
60
Fut : Future < Output = Result < Option < Value > > > ,
@@ -64,8 +63,7 @@ impl Buffer {
64
63
MutationValue :: Determined ( value) => Ok ( value) ,
65
64
MutationValue :: Undetermined => {
66
65
let value = f ( key. clone ( ) ) . await ?;
67
- let mut mutations = self . mutations . lock ( ) . await ;
68
- Self :: update_cache ( & mut mutations, key, value. clone ( ) ) ;
66
+ Self :: update_cache ( & mut self . inner , key, value. clone ( ) ) ;
69
67
Ok ( value)
70
68
}
71
69
}
@@ -76,7 +74,7 @@ impl Buffer {
76
74
///
77
75
/// only used for snapshot read (i.e. not for `batch_get_for_update`)
78
76
pub async fn batch_get_or_else < F , Fut > (
79
- & self ,
77
+ & mut self ,
80
78
keys : impl Iterator < Item = Key > ,
81
79
f : F ,
82
80
) -> Result < impl Iterator < Item = KvPair > >
@@ -85,15 +83,15 @@ impl Buffer {
85
83
Fut : Future < Output = Result < Vec < KvPair > > > ,
86
84
{
87
85
let ( cached_results, undetermined_keys) = {
88
- let mutations = self . mutations . lock ( ) . await ;
89
86
// Partition the keys into those we have buffered and those we have to
90
87
// get from the store.
91
88
let ( undetermined_keys, cached_results) : (
92
89
Vec < ( Key , MutationValue ) > ,
93
90
Vec < ( Key , MutationValue ) > ,
94
91
) = keys
95
92
. map ( |key| {
96
- let value = mutations
93
+ let value = self
94
+ . inner
97
95
. entry_map
98
96
. get ( & key)
99
97
. map ( BufferEntry :: get_value)
@@ -111,11 +109,10 @@ impl Buffer {
111
109
} ;
112
110
113
111
let fetched_results = f ( Box :: new ( undetermined_keys) ) . await ?;
114
- let mut mutations = self . mutations . lock ( ) . await ;
115
112
for kvpair in & fetched_results {
116
113
let key = kvpair. 0 . clone ( ) ;
117
114
let value = Some ( kvpair. 1 . clone ( ) ) ;
118
- Self :: update_cache ( & mut mutations , key, value) ;
115
+ Self :: update_cache ( & mut self . inner , key, value) ;
119
116
}
120
117
121
118
let results = cached_results. chain ( fetched_results. into_iter ( ) ) ;
@@ -124,7 +121,7 @@ impl Buffer {
124
121
125
122
/// Run `f` to fetch entries in `range` from TiKV. Combine them with mutations in local buffer. Returns the results.
126
123
pub async fn scan_and_fetch < F , Fut > (
127
- & self ,
124
+ & mut self ,
128
125
range : BoundRange ,
129
126
limit : u32 ,
130
127
f : F ,
@@ -134,8 +131,7 @@ impl Buffer {
134
131
Fut : Future < Output = Result < Vec < KvPair > > > ,
135
132
{
136
133
// read from local buffer
137
- let mut mutations = self . mutations . lock ( ) . await ;
138
- let mutation_range = mutations. entry_map . range ( range. clone ( ) ) ;
134
+ let mutation_range = self . inner . entry_map . range ( range. clone ( ) ) ;
139
135
140
136
// fetch from TiKV
141
137
// fetch more entries because some of them may be deleted.
@@ -166,7 +162,7 @@ impl Buffer {
166
162
167
163
// update local buffer
168
164
for ( k, v) in & results {
169
- Self :: update_cache ( & mut mutations , k. clone ( ) , Some ( v. clone ( ) ) ) ;
165
+ Self :: update_cache ( & mut self . inner , k. clone ( ) , Some ( v. clone ( ) ) ) ;
170
166
}
171
167
172
168
let mut res = results
@@ -179,10 +175,10 @@ impl Buffer {
179
175
}
180
176
181
177
/// Lock the given key if necessary.
182
- pub async fn lock ( & self , key : Key ) {
183
- let mutations = & mut self . mutations . lock ( ) . await ;
184
- mutations . primary_key . get_or_insert ( key . clone ( ) ) ;
185
- let value = mutations
178
+ pub async fn lock ( & mut self , key : Key ) {
179
+ self . inner . primary_key . get_or_insert ( key . clone ( ) ) ;
180
+ let value = self
181
+ . inner
186
182
. entry_map
187
183
. entry ( key)
188
184
// Mutated keys don't need a lock.
@@ -194,25 +190,19 @@ impl Buffer {
194
190
}
195
191
196
192
/// Insert a value into the buffer (does not write through).
197
- pub async fn put ( & self , key : Key , value : Value ) {
198
- self . mutations
199
- . lock ( )
200
- . await
201
- . insert ( key, BufferEntry :: Put ( value) ) ;
193
+ pub async fn put ( & mut self , key : Key , value : Value ) {
194
+ self . inner . insert ( key, BufferEntry :: Put ( value) ) ;
202
195
}
203
196
204
197
/// Mark a value as Insert mutation into the buffer (does not write through).
205
- pub async fn insert ( & self , key : Key , value : Value ) {
206
- self . mutations
207
- . lock ( )
208
- . await
209
- . insert ( key, BufferEntry :: Insert ( value) ) ;
198
+ pub async fn insert ( & mut self , key : Key , value : Value ) {
199
+ self . inner . insert ( key, BufferEntry :: Insert ( value) ) ;
210
200
}
211
201
212
202
/// Mark a value as deleted.
213
- pub async fn delete ( & self , key : Key ) {
214
- let mut mutations = self . mutations . lock ( ) . await ;
215
- let value = mutations
203
+ pub async fn delete ( & mut self , key : Key ) {
204
+ let value = self
205
+ . inner
216
206
. entry_map
217
207
. entry ( key. clone ( ) )
218
208
. or_insert ( BufferEntry :: Del ) ;
@@ -224,31 +214,27 @@ impl Buffer {
224
214
new_value = BufferEntry :: Del
225
215
}
226
216
227
- mutations . insert ( key, new_value) ;
217
+ self . inner . insert ( key, new_value) ;
228
218
}
229
219
230
220
/// Converts the buffered mutations to the proto buffer version
231
221
pub async fn to_proto_mutations ( & self ) -> Vec < kvrpcpb:: Mutation > {
232
- self . mutations
233
- . lock ( )
234
- . await
222
+ self . inner
235
223
. entry_map
236
224
. iter ( )
237
225
. filter_map ( |( key, mutation) | mutation. to_proto_with_key ( key) )
238
226
. collect ( )
239
227
}
240
228
241
229
async fn get_from_mutations ( & self , key : & Key ) -> MutationValue {
242
- self . mutations
243
- . lock ( )
244
- . await
230
+ self . inner
245
231
. entry_map
246
232
. get ( & key)
247
233
. map ( BufferEntry :: get_value)
248
234
. unwrap_or ( MutationValue :: Undetermined )
249
235
}
250
236
251
- fn update_cache ( buffer : & mut MutexGuard < InnerBuffer > , key : Key , value : Option < Value > ) {
237
+ fn update_cache ( buffer : & mut InnerBuffer , key : Key , value : Option < Value > ) {
252
238
match buffer. entry_map . get ( & key) {
253
239
Some ( BufferEntry :: Locked ( None ) ) => {
254
240
buffer
@@ -378,7 +364,7 @@ mod tests {
378
364
#[ tokio:: test]
379
365
#[ allow( unreachable_code) ]
380
366
async fn set_and_get_from_buffer ( ) {
381
- let buffer = Buffer :: default ( ) ;
367
+ let mut buffer = Buffer :: default ( ) ;
382
368
buffer
383
369
. put ( b"key1" . to_vec ( ) . into ( ) , b"value1" . to_vec ( ) )
384
370
. await ;
@@ -411,7 +397,7 @@ mod tests {
411
397
#[ tokio:: test]
412
398
#[ allow( unreachable_code) ]
413
399
async fn insert_and_get_from_buffer ( ) {
414
- let buffer = Buffer :: default ( ) ;
400
+ let mut buffer = Buffer :: default ( ) ;
415
401
buffer
416
402
. insert ( b"key1" . to_vec ( ) . into ( ) , b"value1" . to_vec ( ) )
417
403
. await ;
@@ -453,13 +439,13 @@ mod tests {
453
439
let v2: Value = b"value2" . to_vec ( ) ;
454
440
let v2_ = v2. clone ( ) ;
455
441
456
- let buffer = Buffer :: default ( ) ;
442
+ let mut buffer = Buffer :: default ( ) ;
457
443
let r1 = block_on ( buffer. get_or_else ( k1. clone ( ) , move |_| ready ( Ok ( Some ( v1_) ) ) ) ) ;
458
444
let r2 = block_on ( buffer. get_or_else ( k1. clone ( ) , move |_| ready ( panic ! ( ) ) ) ) ;
459
445
assert_eq ! ( r1. unwrap( ) . unwrap( ) , v1) ;
460
446
assert_eq ! ( r2. unwrap( ) . unwrap( ) , v1) ;
461
447
462
- let buffer = Buffer :: default ( ) ;
448
+ let mut buffer = Buffer :: default ( ) ;
463
449
let r1 = block_on (
464
450
buffer. batch_get_or_else ( vec ! [ k1. clone( ) , k2. clone( ) ] . into_iter ( ) , move |_| {
465
451
ready ( Ok ( vec ! [ ( k1_, v1__) . into( ) , ( k2_, v2_) . into( ) ] ) )
0 commit comments