@@ -21,37 +21,38 @@ struct InnerBuffer {
21
21
impl InnerBuffer {
22
22
fn insert ( & mut self , key : impl Into < Key > , entry : BufferEntry ) {
23
23
let key = key. into ( ) ;
24
- if !matches ! ( entry, BufferEntry :: Cached ( _) ) {
24
+ if !matches ! ( entry, BufferEntry :: Cached ( _) | BufferEntry :: CheckNotExist ) {
25
25
self . primary_key . get_or_insert_with ( || key. clone ( ) ) ;
26
26
}
27
27
self . entry_map . insert ( key, entry) ;
28
28
}
29
29
30
- pub fn get_primary_key_or ( & mut self , key : & Key ) -> & Key {
31
- self . primary_key . get_or_insert ( key. clone ( ) )
30
+ /// Set the primary key if it is not set
31
+ pub fn primary_key_or ( & mut self , key : & Key ) {
32
+ self . primary_key . get_or_insert ( key. clone ( ) ) ;
32
33
}
33
34
}
34
35
35
36
/// A caching layer which buffers reads and writes in a transaction.
36
37
pub struct Buffer {
37
- mutations : Mutex < InnerBuffer > ,
38
+ inner : Mutex < InnerBuffer > ,
38
39
}
39
40
40
41
impl Buffer {
41
42
pub fn new ( is_pessimistic : bool ) -> Buffer {
42
43
Buffer {
43
- mutations : Mutex :: new ( InnerBuffer :: new ( is_pessimistic) ) ,
44
+ inner : Mutex :: new ( InnerBuffer :: new ( is_pessimistic) ) ,
44
45
}
45
46
}
46
47
47
48
/// Get the primary key of the buffer.
48
49
pub async fn get_primary_key ( & self ) -> Option < Key > {
49
- self . mutations . lock ( ) . await . primary_key . clone ( )
50
+ self . inner . lock ( ) . await . primary_key . clone ( )
50
51
}
51
52
52
- /// Get the primary key of the buffer, if not exists, use `key` as the primary key.
53
- pub async fn get_primary_key_or ( & self , key : & Key ) -> Key {
54
- self . mutations . lock ( ) . await . get_primary_key_or ( key) . clone ( )
53
+ /// Set the primary key if it is not set
54
+ pub async fn primary_key_or ( & self , key : & Key ) {
55
+ self . inner . lock ( ) . await . primary_key_or ( key) ;
55
56
}
56
57
57
58
/// Get a value from the buffer.
@@ -74,7 +75,7 @@ impl Buffer {
74
75
MutationValue :: Determined ( value) => Ok ( value) ,
75
76
MutationValue :: Undetermined => {
76
77
let value = f ( key. clone ( ) ) . await ?;
77
- let mut mutations = self . mutations . lock ( ) . await ;
78
+ let mut mutations = self . inner . lock ( ) . await ;
78
79
Self :: update_cache ( & mut mutations, key, value. clone ( ) ) ;
79
80
Ok ( value)
80
81
}
@@ -95,7 +96,7 @@ impl Buffer {
95
96
Fut : Future < Output = Result < Vec < KvPair > > > ,
96
97
{
97
98
let ( cached_results, undetermined_keys) = {
98
- let mutations = self . mutations . lock ( ) . await ;
99
+ let mutations = self . inner . lock ( ) . await ;
99
100
// Partition the keys into those we have buffered and those we have to
100
101
// get from the store.
101
102
let ( undetermined_keys, cached_results) : (
@@ -121,7 +122,7 @@ impl Buffer {
121
122
} ;
122
123
123
124
let fetched_results = f ( Box :: new ( undetermined_keys) ) . await ?;
124
- let mut mutations = self . mutations . lock ( ) . await ;
125
+ let mut mutations = self . inner . lock ( ) . await ;
125
126
for kvpair in & fetched_results {
126
127
let key = kvpair. 0 . clone ( ) ;
127
128
let value = Some ( kvpair. 1 . clone ( ) ) ;
@@ -144,7 +145,7 @@ impl Buffer {
144
145
Fut : Future < Output = Result < Vec < KvPair > > > ,
145
146
{
146
147
// read from local buffer
147
- let mut mutations = self . mutations . lock ( ) . await ;
148
+ let mut mutations = self . inner . lock ( ) . await ;
148
149
let mutation_range = mutations. entry_map . range ( range. clone ( ) ) ;
149
150
150
151
// fetch from TiKV
@@ -190,8 +191,8 @@ impl Buffer {
190
191
191
192
/// Lock the given key if necessary.
192
193
pub async fn lock ( & self , key : Key ) {
193
- let mutations = & mut self . mutations . lock ( ) . await ;
194
- mutations. primary_key . get_or_insert ( key. clone ( ) ) ;
194
+ let mutations = & mut self . inner . lock ( ) . await ;
195
+ mutations. primary_key . get_or_insert_with ( || key. clone ( ) ) ;
195
196
let value = mutations
196
197
. entry_map
197
198
. entry ( key)
@@ -205,15 +206,12 @@ impl Buffer {
205
206
206
207
/// Insert a value into the buffer (does not write through).
207
208
pub async fn put ( & self , key : Key , value : Value ) {
208
- self . mutations
209
- . lock ( )
210
- . await
211
- . insert ( key, BufferEntry :: Put ( value) ) ;
209
+ self . inner . lock ( ) . await . insert ( key, BufferEntry :: Put ( value) ) ;
212
210
}
213
211
214
212
/// Mark a value as Insert mutation into the buffer (does not write through).
215
213
pub async fn insert ( & self , key : Key , value : Value ) {
216
- let mut mutations = self . mutations . lock ( ) . await ;
214
+ let mut mutations = self . inner . lock ( ) . await ;
217
215
let mut entry = mutations. entry_map . entry ( key. clone ( ) ) ;
218
216
match entry {
219
217
Entry :: Occupied ( ref mut o) if matches ! ( o. get( ) , BufferEntry :: Del ) => {
@@ -225,7 +223,7 @@ impl Buffer {
225
223
226
224
/// Mark a value as deleted.
227
225
pub async fn delete ( & self , key : Key ) {
228
- let mut mutations = self . mutations . lock ( ) . await ;
226
+ let mut mutations = self . inner . lock ( ) . await ;
229
227
let is_pessimistic = mutations. is_pessimistic ;
230
228
let mut entry = mutations. entry_map . entry ( key. clone ( ) ) ;
231
229
@@ -241,7 +239,7 @@ impl Buffer {
241
239
242
240
/// Converts the buffered mutations to the proto buffer version
243
241
pub async fn to_proto_mutations ( & self ) -> Vec < kvrpcpb:: Mutation > {
244
- self . mutations
242
+ self . inner
245
243
. lock ( )
246
244
. await
247
245
. entry_map
@@ -251,7 +249,7 @@ impl Buffer {
251
249
}
252
250
253
251
async fn get_from_mutations ( & self , key : & Key ) -> MutationValue {
254
- self . mutations
252
+ self . inner
255
253
. lock ( )
256
254
. await
257
255
. entry_map
0 commit comments