@@ -474,6 +474,10 @@ impl LsmStorageInner {
474474 compaction_filters. push ( compaction_filter) ;
475475 }
476476
477+ pub fn mvcc ( & self ) -> & LsmMvccInner {
478+ self . mvcc . as_ref ( ) . unwrap ( )
479+ }
480+
477481 /// Get a key from the storage. In day 7, this can be further optimized by using a bloom filter.
478482 pub fn get ( & self , key : & [ u8 ] ) -> Result < Option < Bytes > > {
479483 let snapshot;
@@ -572,17 +576,20 @@ impl LsmStorageInner {
572576
573577 /// Write a batch of data into the storage. Implement in week 2 day 7.
574578 pub fn write_batch < T : AsRef < [ u8 ] > > ( & self , batch : & [ WriteBatchRecord < T > ] ) -> Result < ( ) > {
579+ self . mvcc ( ) . write_lock . lock ( ) ;
580+ let ts = self . mvcc ( ) . latest_commit_ts ( ) + 1 ;
575581 for record in batch {
576582 match record {
577583 WriteBatchRecord :: Put ( key, value) => {
578584 let key = key. as_ref ( ) ;
579585 let value = value. as_ref ( ) ;
580586 assert ! ( !key. is_empty( ) , "key can not be empty" ) ;
581587 assert ! ( !value. is_empty( ) , "value can not be empty" ) ;
588+ let key_slice = KeySlice :: from_slice_with_ts ( key, ts) ;
582589 let size;
583590 {
584591 let storage_guard = self . state . read ( ) ;
585- storage_guard. memtable . put ( key , value) ?;
592+ storage_guard. memtable . put ( key_slice , value) ?;
586593 size = storage_guard. memtable . approximate_size ( ) ;
587594 }
588595
@@ -591,15 +598,17 @@ impl LsmStorageInner {
591598 WriteBatchRecord :: Del ( key) => {
592599 let key = key. as_ref ( ) ;
593600 assert ! ( !key. is_empty( ) , "key can not be empty" ) ;
601+ let key_slice = KeySlice :: from_slice_with_ts ( key, ts) ;
594602 let size = {
595603 let state_guard = self . state . read ( ) ;
596- state_guard. memtable . put ( key , b"" ) ?;
604+ state_guard. memtable . put ( key_slice , b"" ) ?;
597605 state_guard. memtable . approximate_size ( )
598606 } ;
599607 self . try_freeze ( size) ?;
600608 }
601609 }
602610 }
611+ self . mvcc ( ) . update_commit_ts ( ts) ;
603612 Ok ( ( ) )
604613 }
605614
0 commit comments