Chapter 9. Using Row Versions

When a row is initially inserted in the store, and each time it is updated, it is assigned a unique version token. The version is always returned by the method that wrote to the store (for example, Store.put()). The version information is also returned by methods that retrieve rows from the store.

There are two reasons why versions might be important.

  1. When an update or delete is to be performed, it may be important to only perform the operation if the row's value has not changed. This is particularly interesting in an application where there can be multiple threads or processes simultaneously operating on the row. In this case, read the row, examining its version when you do so. You can then perform a put operation, but only allow the put to proceed if the version has not changed (this is often referred to as a Compare and Set (CAS) or Read, Modify, Write (RMW) operation). You use Store.put_if_version() or Store.delete_if_version() to guarantee this.

  2. When a client reads data that was previously written, it may be important to ensure that the Oracle NoSQL Database node servicing the read operation has been updated with the information previously written. This can be accomplished by passing the version of the previously written data as a consistency parameter to the read operation. For more information on using consistency, see Consistency Guarantees.

Versions are handled as Python byte arrays. There is no class or other data type used to manage them. In some cases they are accessed by special methods, such as Row.get_version().

The following code fragment retrieves a row, and then writes that row back to the store only if the version has not changed:

def do_store_ops(store):

    key_d = {}

    try:
        row_list = store.index_iterator("myTable", "DoB", key_d, 
                                        False)
        if not row_list:
            logging.debug("Table retrieval failed")
        else:
            logging.debug("Table retrieval succeeded.")
            for r in row_list:
                version = r.get_version()

                ###
                ### do work on the row here
                ###

                store.put_if_version("myTable", r, version)
    except IllegalArgumentException, iae:
        logging.error("Table retrieval failed.")
        logging.error(iae.message)