@ -46,21 +46,34 @@ int main() {
assert ( s . IsNotFound ( ) ) ;
assert ( s . IsNotFound ( ) ) ;
// Write a key in this transaction
// Write a key in this transaction
txn - > Put ( " abc " , " def " ) ;
s = txn - > Put ( " abc " , " xyz " ) ;
assert ( s . ok ( ) ) ;
// Read a key OUTSIDE this transaction. Does not affect txn.
// Read a key OUTSIDE this transaction. Does not affect txn.
s = db - > Get ( read_options , " abc " , & value ) ;
s = db - > Get ( read_options , " abc " , & value ) ;
assert ( s . IsNotFound ( ) ) ;
// Write a key OUTSIDE of this transaction.
// Write a key OUTSIDE of this transaction.
// Does not affect txn since this is an unrelated key. If we wrote key 'abc'
// Does not affect txn since this is an unrelated key. If we wrote key 'abc'
// here, the transaction would fail to commit.
// here, the transaction would fail to commit.
s = db - > Put ( write_options , " xyz " , " zzz " ) ;
s = db - > Put ( write_options , " xyz " , " zzz " ) ;
assert ( s . ok ( ) ) ;
s = db - > Put ( write_options , " abc " , " def " ) ;
assert ( s . ok ( ) ) ;
// Commit transaction
// Commit transaction
s = txn - > Commit ( ) ;
s = txn - > Commit ( ) ;
assert ( s . ok ( ) ) ;
assert ( s . IsBusy ( ) ) ;
delete txn ;
delete txn ;
s = db - > Get ( read_options , " xyz " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " zzz " ) ;
s = db - > Get ( read_options , " abc " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " def " ) ;
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//
//
// "Repeatable Read" (Snapshot Isolation) Example
// "Repeatable Read" (Snapshot Isolation) Example
@ -75,11 +88,13 @@ int main() {
const Snapshot * snapshot = txn - > GetSnapshot ( ) ;
const Snapshot * snapshot = txn - > GetSnapshot ( ) ;
// Write a key OUTSIDE of transaction
// Write a key OUTSIDE of transaction
db - > Put ( write_options , " abc " , " xyz " ) ;
s = db - > Put ( write_options , " abc " , " xyz " ) ;
assert ( s . ok ( ) ) ;
// Read a key using the snapshot
// Read a key using the snapshot
read_options . snapshot = snapshot ;
read_options . snapshot = snapshot ;
s = txn - > GetForUpdate ( read_options , " abc " , & value ) ;
s = txn - > GetForUpdate ( read_options , " abc " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " def " ) ;
assert ( value = = " def " ) ;
// Attempt to commit transaction
// Attempt to commit transaction
@ -94,6 +109,10 @@ int main() {
read_options . snapshot = nullptr ;
read_options . snapshot = nullptr ;
snapshot = nullptr ;
snapshot = nullptr ;
s = db - > Get ( read_options , " abc " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " xyz " ) ;
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//
//
// "Read Committed" (Monotonic Atomic Views) Example
// "Read Committed" (Monotonic Atomic Views) Example
@ -112,10 +131,18 @@ int main() {
// Do some reads and writes to key "x"
// Do some reads and writes to key "x"
read_options . snapshot = db - > GetSnapshot ( ) ;
read_options . snapshot = 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 ( ) ) ;
// The transaction hasn't committed, so the write is not visible
// outside of txn.
s = db - > Get ( read_options , " x " , & value ) ;
assert ( s . IsNotFound ( ) ) ;
// Do a write outside of the transaction to key "y"
// Do a write outside of the transaction to key "y"
s = db - > Put ( write_options , " y " , " y " ) ;
s = db - > Put ( write_options , " y " , " z " ) ;
assert ( s . ok ( ) ) ;
// Set a new snapshot in the transaction
// Set a new snapshot in the transaction
txn - > SetSnapshot ( ) ;
txn - > SetSnapshot ( ) ;
@ -123,6 +150,8 @@ int main() {
// Do some reads and writes to key "y"
// Do some reads and writes to key "y"
s = txn - > GetForUpdate ( read_options , " y " , & value ) ;
s = txn - > GetForUpdate ( read_options , " y " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " z " ) ;
txn - > Put ( " y " , " y " ) ;
txn - > Put ( " y " , " y " ) ;
// Commit. Since the snapshot was advanced, the write done outside of the
// Commit. Since the snapshot was advanced, the write done outside of the
@ -133,6 +162,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 ;
// txn is committed, read the latest values.
s = db - > Get ( read_options , " x " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " x " ) ;
s = db - > Get ( read_options , " y " , & value ) ;
assert ( s . ok ( ) ) ;
assert ( value = = " y " ) ;
// Cleanup
// Cleanup
delete txn_db ;
delete txn_db ;
DestroyDB ( kDBPath , options ) ;
DestroyDB ( kDBPath , options ) ;