@ -94,14 +94,25 @@ int main() {
s = txn_db - > Put ( write_options , " abc " , " xyz " ) ;
s = txn_db - > Put ( write_options , " abc " , " xyz " ) ;
assert ( s . ok ( ) ) ;
assert ( s . ok ( ) ) ;
// Read the latest committed value.
s = txn - > Get ( read_options , " abc " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " xyz " ) ;
// Read the snapshotted value.
read_options . snapshot = snapshot ;
s = txn - > Get ( read_options , " abc " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " def " ) ;
// Attempt to read a key using the snapshot. This will fail since
// Attempt to read a key using the snapshot. This will fail since
// the previous write outside this txn conflicts with this read.
// the previous write outside this txn conflicts with this read.
read_options . snapshot = snapshot ;
s = txn - > GetForUpdate ( read_options , " abc " , & value ) ;
s = txn - > GetForUpdate ( read_options , " abc " , & value ) ;
assert ( s . IsBusy ( ) ) ;
assert ( s . IsBusy ( ) ) ;
txn - > Rollback ( ) ;
txn - > Rollback ( ) ;
// Snapshot will be released upon deleting the transaction.
delete txn ;
delete txn ;
// Clear snapshot from read options since it is no longer valid
// Clear snapshot from read options since it is no longer valid
read_options . snapshot = nullptr ;
read_options . snapshot = nullptr ;
@ -125,10 +136,13 @@ int main() {
// Do some reads and writes to key "x"
// Do some reads and writes to key "x"
read_options . snapshot = txn_db - > GetSnapshot ( ) ;
read_options . snapshot = txn_db - > GetSnapshot ( ) ;
s = txn - > Get ( read_options , " x " , & value ) ;
s = txn - > Get ( read_options , " x " , & value ) ;
txn - > Put ( " x " , " x " ) ;
assert ( s . IsNotFound ( ) ) ;
s = txn - > Put ( " x " , " x " ) ;
assert ( s . ok ( ) ) ;
// Do a write outside of the transaction to key "y"
// Do a write outside of the transaction to key "y"
s = txn_db - > Put ( write_options , " y " , " y " ) ;
s = txn_db - > Put ( write_options , " y " , " y1 " ) ;
assert ( s . ok ( ) ) ;
// Set a new snapshot in the transaction
// Set a new snapshot in the transaction
txn - > SetSnapshot ( ) ;
txn - > SetSnapshot ( ) ;
@ -139,7 +153,10 @@ int main() {
// Since the snapshot was advanced, the write done outside of the
// Since the snapshot was advanced, the write done outside of the
// transaction does not conflict.
// transaction does not conflict.
s = txn - > GetForUpdate ( read_options , " y " , & value ) ;
s = txn - > GetForUpdate ( read_options , " y " , & value ) ;
txn - > Put ( " y " , " y " ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " y1 " ) ;
s = txn - > Put ( " y " , " y2 " ) ;
assert ( s . ok ( ) ) ;
// Decide we want to revert the last write from this transaction.
// Decide we want to revert the last write from this transaction.
txn - > RollbackToSavePoint ( ) ;
txn - > RollbackToSavePoint ( ) ;
@ -151,6 +168,15 @@ int main() {
// Clear snapshot from read options since it is no longer valid
// Clear snapshot from read options since it is no longer valid
read_options . snapshot = nullptr ;
read_options . snapshot = nullptr ;
// db state is at the save point.
s = txn_db - > Get ( read_options , " x " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " x " ) ;
s = txn_db - > Get ( read_options , " y " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " y1 " ) ;
// Cleanup
// Cleanup
delete txn_db ;
delete txn_db ;
DestroyDB ( kDBPath , options ) ;
DestroyDB ( kDBPath , options ) ;