@@ -197,12 +197,13 @@ where
197197 K : From < [ u8 ; KEY_LEN ] > + Into < [ u8 ; KEY_LEN ] > ,
198198 V : From < [ u8 ; VAL_LEN ] > + Into < [ u8 ; VAL_LEN ] > ,
199199{
200- fn commit_transaction ( & mut self ) -> u64 {
201- if !self . pending . is_empty ( ) {
202- self . dirty . push ( mem:: take ( & mut self . pending ) ) ;
203- self . save ( ) . expect ( "Cannot save log file" ) ;
200+ fn commit_transaction ( & mut self ) -> Option < u64 > {
201+ if self . pending . is_empty ( ) {
202+ return None ;
204203 }
205- self . transaction_count ( ) - 1
204+ self . dirty . push ( mem:: take ( & mut self . pending ) ) ;
205+ self . save ( ) . expect ( "Cannot save the log file" ) ;
206+ Some ( self . transaction_count ( ) - 1 )
206207 }
207208
208209 fn abort_transaction ( & mut self ) { self . pending . clear ( ) ; }
@@ -281,7 +282,7 @@ mod tests {
281282 #[ test]
282283 fn abort ( ) {
283284 let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
284- let mut db = Db :: create_new ( dir. path ( ) , "happy_path " ) . unwrap ( ) ;
285+ let mut db = Db :: create_new ( dir. path ( ) , "abort " ) . unwrap ( ) ;
285286
286287 normal_ops ( & mut db) ;
287288 db. abort_transaction ( ) ;
@@ -292,17 +293,26 @@ mod tests {
292293 assert_eq ! ( db. keys( ) . count( ) , 0 ) ;
293294 assert_eq ! ( db. transaction_count( ) , 0 ) ;
294295
295- let data = fs:: read ( dir. path ( ) . join ( "happy_path .log" ) ) . unwrap ( ) ;
296+ let data = fs:: read ( dir. path ( ) . join ( "abort .log" ) ) . unwrap ( ) ;
296297 assert_eq ! ( data, b"DUMBTEST\0 \x01 \0 \0 \0 \0 \0 \0 \0 \0 " ) ;
297298 }
298299
300+ #[ test]
301+ fn empty_commit ( ) {
302+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
303+ let mut db = Db :: create_new ( dir. path ( ) , "dir" ) . unwrap ( ) ;
304+
305+ // No pending transaction
306+ assert_eq ! ( db. commit_transaction( ) , None ) ;
307+ }
308+
299309 #[ test]
300310 fn commit ( ) {
301311 let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
302- let mut db = Db :: create_new ( dir. path ( ) , "happy_transactions " ) . unwrap ( ) ;
312+ let mut db = Db :: create_new ( dir. path ( ) , "commit " ) . unwrap ( ) ;
303313
304314 normal_ops ( & mut db) ;
305- assert_eq ! ( db. commit_transaction( ) , 0 ) ;
315+ assert_eq ! ( db. commit_transaction( ) , Some ( 0 ) ) ;
306316
307317 // Check that commitment hasn't changed anything
308318 assert_eq ! ( db. get_expect( 1 . into( ) ) . 0 , 4 ) ;
@@ -315,12 +325,12 @@ mod tests {
315325
316326 // Insert another item
317327 db. insert_only ( 3 . into ( ) , 5 . into ( ) ) ;
318- assert_eq ! ( db. commit_transaction( ) , 1 ) ;
328+ assert_eq ! ( db. commit_transaction( ) , Some ( 1 ) ) ;
319329 assert_eq ! ( db. transaction_count( ) , 2 ) ;
320330 assert_eq ! ( db. transaction_keys( 0 ) . collect:: <HashSet <_>>( ) , set![ 0 . into( ) , 1 . into( ) ] ) ;
321331 assert_eq ! ( db. transaction_keys( 1 ) . collect:: <HashSet <_>>( ) , set![ 3 . into( ) ] ) ;
322332
323- let db = Db :: open ( dir. path ( ) , "happy_transactions " ) . unwrap ( ) ;
333+ let db = Db :: open ( dir. path ( ) , "commit " ) . unwrap ( ) ;
324334
325335 // Check that commitment hasn't changed anything
326336 assert_eq ! ( db. get_expect( 1 . into( ) ) . 0 , 4 ) ;
@@ -340,10 +350,10 @@ mod tests {
340350
341351 db. insert_only ( 0 . into ( ) , 1 . into ( ) ) ;
342352 db. insert_only ( 0 . into ( ) , 1 . into ( ) ) ;
343- assert_eq ! ( db. commit_transaction( ) , 0 ) ;
353+ assert_eq ! ( db. commit_transaction( ) , Some ( 0 ) ) ;
344354
345355 db. insert_only ( 0 . into ( ) , 1 . into ( ) ) ;
346- assert_eq ! ( db. commit_transaction( ) , 0 ) ;
356+ assert_eq ! ( db. commit_transaction( ) , None ) ;
347357
348358 assert_eq ! ( db. transaction_count( ) , 1 ) ;
349359 }
@@ -355,10 +365,10 @@ mod tests {
355365 let mut db = Db :: create_new ( dir. path ( ) , "unique_keys" ) . unwrap ( ) ;
356366
357367 db. insert_only ( 0 . into ( ) , 1 . into ( ) ) ;
358- assert_eq ! ( db. commit_transaction( ) , 0 ) ;
368+ assert_eq ! ( db. commit_transaction( ) , Some ( 0 ) ) ;
359369
360370 db. insert_only ( 0 . into ( ) , 2 . into ( ) ) ;
361- assert_eq ! ( db. commit_transaction( ) , 1 ) ;
371+ assert_eq ! ( db. commit_transaction( ) , Some ( 1 ) ) ;
362372 }
363373
364374 #[ test]
0 commit comments