1
1
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2
2
3
3
use crate :: { BoundRange , Key , KvPair , Result , Value } ;
4
- use derive_new:: new;
5
4
use std:: {
6
5
collections:: { btree_map:: Entry , BTreeMap , HashMap } ,
7
6
future:: Future ,
8
7
} ;
9
8
use tikv_client_proto:: kvrpcpb;
10
9
11
- #[ derive( new) ]
12
- struct InnerBuffer {
13
- #[ new( default ) ]
10
+ /// A caching layer which buffers reads and writes in a transaction.
11
+ pub struct Buffer {
14
12
primary_key : Option < Key > ,
15
- #[ new( default ) ]
16
13
entry_map : BTreeMap < Key , BufferEntry > ,
17
14
is_pessimistic : bool ,
18
15
}
19
16
20
- impl InnerBuffer {
21
- fn insert ( & mut self , key : impl Into < Key > , entry : BufferEntry ) {
22
- let key = key. into ( ) ;
23
- if !matches ! ( entry, BufferEntry :: Cached ( _) | BufferEntry :: CheckNotExist ) {
24
- self . primary_key . get_or_insert_with ( || key. clone ( ) ) ;
25
- }
26
- self . entry_map . insert ( key, entry) ;
27
- }
28
-
29
- /// Set the primary key if it is not set
30
- pub fn primary_key_or ( & mut self , key : & Key ) {
31
- self . primary_key . get_or_insert ( key. clone ( ) ) ;
32
- }
33
- }
34
-
35
- /// A caching layer which buffers reads and writes in a transaction.
36
- pub struct Buffer {
37
- inner : InnerBuffer ,
38
- }
39
-
40
17
impl Buffer {
41
18
pub fn new ( is_pessimistic : bool ) -> Buffer {
42
19
Buffer {
43
- inner : InnerBuffer :: new ( is_pessimistic) ,
20
+ primary_key : None ,
21
+ entry_map : BTreeMap :: new ( ) ,
22
+ is_pessimistic,
44
23
}
45
24
}
46
25
47
26
/// Get the primary key of the buffer.
48
27
pub async fn get_primary_key ( & self ) -> Option < Key > {
49
- self . inner . primary_key . clone ( )
28
+ self . primary_key . clone ( )
50
29
}
51
30
52
31
/// Set the primary key if it is not set
53
32
pub async fn primary_key_or ( & mut self , key : & Key ) {
54
- self . inner . primary_key_or ( key) ;
33
+ self . primary_key . get_or_insert_with ( || key. clone ( ) ) ;
55
34
}
56
35
57
36
/// Get a value from the buffer.
@@ -74,7 +53,7 @@ impl Buffer {
74
53
MutationValue :: Determined ( value) => Ok ( value) ,
75
54
MutationValue :: Undetermined => {
76
55
let value = f ( key. clone ( ) ) . await ?;
77
- Self :: update_cache ( & mut self . inner , key, value. clone ( ) ) ;
56
+ self . update_cache ( key, value. clone ( ) ) ;
78
57
Ok ( value)
79
58
}
80
59
}
@@ -102,7 +81,6 @@ impl Buffer {
102
81
) = keys
103
82
. map ( |key| {
104
83
let value = self
105
- . inner
106
84
. entry_map
107
85
. get ( & key)
108
86
. map ( BufferEntry :: get_value)
@@ -123,7 +101,7 @@ impl Buffer {
123
101
for kvpair in & fetched_results {
124
102
let key = kvpair. 0 . clone ( ) ;
125
103
let value = Some ( kvpair. 1 . clone ( ) ) ;
126
- Self :: update_cache ( & mut self . inner , key, value) ;
104
+ self . update_cache ( key, value) ;
127
105
}
128
106
129
107
let results = cached_results. chain ( fetched_results. into_iter ( ) ) ;
@@ -142,7 +120,7 @@ impl Buffer {
142
120
Fut : Future < Output = Result < Vec < KvPair > > > ,
143
121
{
144
122
// read from local buffer
145
- let mutation_range = self . inner . entry_map . range ( range. clone ( ) ) ;
123
+ let mutation_range = self . entry_map . range ( range. clone ( ) ) ;
146
124
147
125
// fetch from TiKV
148
126
// fetch more entries because some of them may be deleted.
@@ -173,7 +151,7 @@ impl Buffer {
173
151
174
152
// update local buffer
175
153
for ( k, v) in & results {
176
- Self :: update_cache ( & mut self . inner , k. clone ( ) , Some ( v. clone ( ) ) ) ;
154
+ self . update_cache ( k. clone ( ) , Some ( v. clone ( ) ) ) ;
177
155
}
178
156
179
157
let mut res = results
@@ -187,9 +165,8 @@ impl Buffer {
187
165
188
166
/// Lock the given key if necessary.
189
167
pub async fn lock ( & mut self , key : Key ) {
190
- self . inner . primary_key . get_or_insert_with ( || key. clone ( ) ) ;
168
+ self . primary_key . get_or_insert_with ( || key. clone ( ) ) ;
191
169
let value = self
192
- . inner
193
170
. entry_map
194
171
. entry ( key)
195
172
// Mutated keys don't need a lock.
@@ -202,61 +179,57 @@ impl Buffer {
202
179
203
180
/// Insert a value into the buffer (does not write through).
204
181
pub async fn put ( & mut self , key : Key , value : Value ) {
205
- self . inner . insert ( key, BufferEntry :: Put ( value) ) ;
182
+ self . insert_entry ( key, BufferEntry :: Put ( value) ) ;
206
183
}
207
184
208
185
/// Mark a value as Insert mutation into the buffer (does not write through).
209
186
pub async fn insert ( & mut self , key : Key , value : Value ) {
210
- let mut entry = self . inner . entry_map . entry ( key. clone ( ) ) ;
187
+ let mut entry = self . entry_map . entry ( key. clone ( ) ) ;
211
188
match entry {
212
189
Entry :: Occupied ( ref mut o) if matches ! ( o. get( ) , BufferEntry :: Del ) => {
213
190
o. insert ( BufferEntry :: Put ( value) ) ;
214
191
}
215
- _ => self . inner . insert ( key, BufferEntry :: Insert ( value) ) ,
192
+ _ => self . insert_entry ( key, BufferEntry :: Insert ( value) ) ,
216
193
}
217
194
}
218
195
219
196
/// Mark a value as deleted.
220
197
pub async fn delete ( & mut self , key : Key ) {
221
- let is_pessimistic = self . inner . is_pessimistic ;
222
- let mut entry = self . inner . entry_map . entry ( key. clone ( ) ) ;
198
+ let is_pessimistic = self . is_pessimistic ;
199
+ let mut entry = self . entry_map . entry ( key. clone ( ) ) ;
223
200
224
201
match entry {
225
202
Entry :: Occupied ( ref mut o)
226
203
if matches ! ( o. get( ) , BufferEntry :: Insert ( _) ) && !is_pessimistic =>
227
204
{
228
205
o. insert ( BufferEntry :: CheckNotExist ) ;
229
206
}
230
- _ => self . inner . insert ( key, BufferEntry :: Del ) ,
207
+ _ => self . insert_entry ( key, BufferEntry :: Del ) ,
231
208
}
232
209
}
233
210
234
211
/// Converts the buffered mutations to the proto buffer version
235
212
pub async fn to_proto_mutations ( & self ) -> Vec < kvrpcpb:: Mutation > {
236
- self . inner
237
- . entry_map
213
+ self . entry_map
238
214
. iter ( )
239
215
. filter_map ( |( key, mutation) | mutation. to_proto_with_key ( key) )
240
216
. collect ( )
241
217
}
242
218
243
219
async fn get_from_mutations ( & self , key : & Key ) -> MutationValue {
244
- self . inner
245
- . entry_map
220
+ self . entry_map
246
221
. get ( & key)
247
222
. map ( BufferEntry :: get_value)
248
223
. unwrap_or ( MutationValue :: Undetermined )
249
224
}
250
225
251
- fn update_cache ( buffer : & mut InnerBuffer , key : Key , value : Option < Value > ) {
252
- match buffer . entry_map . get ( & key) {
226
+ fn update_cache ( & mut self , key : Key , value : Option < Value > ) {
227
+ match self . entry_map . get ( & key) {
253
228
Some ( BufferEntry :: Locked ( None ) ) => {
254
- buffer
255
- . entry_map
256
- . insert ( key, BufferEntry :: Locked ( Some ( value) ) ) ;
229
+ self . entry_map . insert ( key, BufferEntry :: Locked ( Some ( value) ) ) ;
257
230
}
258
231
None => {
259
- buffer . entry_map . insert ( key, BufferEntry :: Cached ( value) ) ;
232
+ self . entry_map . insert ( key, BufferEntry :: Cached ( value) ) ;
260
233
}
261
234
Some ( BufferEntry :: Cached ( v) ) | Some ( BufferEntry :: Locked ( Some ( v) ) ) => {
262
235
assert ! ( & value == v) ;
@@ -275,6 +248,14 @@ impl Buffer {
275
248
}
276
249
}
277
250
}
251
+
252
+ fn insert_entry ( & mut self , key : impl Into < Key > , entry : BufferEntry ) {
253
+ let key = key. into ( ) ;
254
+ if !matches ! ( entry, BufferEntry :: Cached ( _) | BufferEntry :: CheckNotExist ) {
255
+ self . primary_key . get_or_insert_with ( || key. clone ( ) ) ;
256
+ }
257
+ self . entry_map . insert ( key, entry) ;
258
+ }
278
259
}
279
260
280
261
// The state of a key-value pair in the buffer.
0 commit comments