Transaction support for Blobs
=============================

We need a database with a blob supporting storage::

    >>> import ZODB.blob, transaction
    >>> blob_dir = 'blobs'
    >>> blob_storage = create_storage(blob_dir=blob_dir)
    >>> database = ZODB.DB(blob_storage)
    >>> connection1 = database.open()
    >>> root1 = connection1.root()

Putting a Blob into a Connection works like any other Persistent object::

    >>> blob1 = ZODB.blob.Blob()
    >>> with blob1.open('w') as file:
    ...     _ = file.write(b'this is blob 1')
    >>> root1['blob1'] = blob1
    >>> 'blob1' in root1
    True

Aborting a blob add leaves the blob unchanged:

    >>> transaction.abort()
    >>> 'blob1' in root1
    False

    >>> blob1._p_oid
    >>> blob1._p_jar
    >>> with blob1.open() as fp:
    ...     fp.read()
    b'this is blob 1'

It doesn't clear the file because there is no previously committed version:

    >>> fname = blob1._p_blob_uncommitted
    >>> import os
    >>> os.path.exists(fname)
    True

Let's put the blob back into the root and commit the change:

    >>> root1['blob1'] = blob1
    >>> transaction.commit()

Now, if we make a change and abort it, we'll return to the committed
state:

    >>> os.path.exists(fname)
    False
    >>> blob1._p_blob_uncommitted

    >>> with blob1.open('w') as file:
    ...     _ = file.write(b'this is new blob 1')
    >>> with blob1.open() as fp:
    ...     fp.read()
    b'this is new blob 1'
    >>> fname = blob1._p_blob_uncommitted
    >>> os.path.exists(fname)
    True

    >>> transaction.abort()
    >>> os.path.exists(fname)
    False
    >>> blob1._p_blob_uncommitted

    >>> with blob1.open() as fp:
    ...     fp.read()
    b'this is blob 1'

Opening a blob gives us a filehandle.  Getting data out of the
resulting filehandle is accomplished via the filehandle's read method::

    >>> connection2 = database.open()
    >>> root2 = connection2.root()
    >>> blob1a = root2['blob1']

    >>> blob1afh1 = blob1a.open("r")
    >>> blob1afh1.read()
    b'this is blob 1'

Let's make another filehandle for read only to blob1a. Each file
handle has a reference to the (same) underlying blob::

    >>> blob1afh2 = blob1a.open("r")
    >>> blob1afh2.blob is blob1afh1.blob
    True

Let's close the first filehandle we got from the blob::

    >>> blob1afh1.close()

Let's abort this transaction, and ensure that the filehandles that we
opened are still open::

    >>> transaction.abort()
    >>> blob1afh2.read()
    b'this is blob 1'

    >>> blob1afh2.close()

If we open a blob for append, writing any number of bytes to the
blobfile should result in the blob being marked "dirty" in the
connection (we just aborted above, so the object should be "clean"
when we start)::

    >>> bool(blob1a._p_changed)
    False
    >>> with blob1a.open('r') as fp:
    ...     fp.read()
    b'this is blob 1'
    >>> with blob1a.open('a') as blob1afh3:
    ...     assert(bool(blob1a._p_changed))
    ...     _ = blob1afh3.write(b'woot!')
    >>> blob1afh3.close()

We can open more than one blob object during the course of a single
transaction::

    >>> blob2 = ZODB.blob.Blob()
    >>> with blob2.open('w') as file:
    ...     _ = file.write(b'this is blob 3')
    >>> root2['blob2'] = blob2
    >>> transaction.commit()

Since we committed the current transaction above, the aggregate
changes we've made to blob, blob1a (these refer to the same object) and
blob2 (a different object) should be evident::

    >>> with blob1.open('r') as fp:
    ...     fp.read()
    b'this is blob 1woot!'
    >>> with blob1a.open('r') as fp:
    ...     fp.read()
    b'this is blob 1woot!'
    >>> with blob2.open('r') as fp:
    ...     fp.read()
    b'this is blob 3'

We shouldn't be able to persist a blob filehandle at commit time
(although the exception which is raised when an object cannot be
pickled appears to be particulary unhelpful for casual users at the
moment)::

    >>> with blob1.open('r') as f:
    ...     root1['wontwork'] = f
    ...     transaction.commit()
    Traceback (most recent call last):
    ...
    TypeError: ...

Abort for good measure::

    >>> transaction.abort()

Attempting to change a blob simultaneously from two different
connections should result in a write conflict error::

    >>> tm1 = transaction.TransactionManager()
    >>> tm2 = transaction.TransactionManager()
    >>> root3 = database.open(transaction_manager=tm1).root()
    >>> root4 = database.open(transaction_manager=tm2).root()
    >>> blob1c3 = root3['blob1']
    >>> blob1c4 = root4['blob1']
    >>> with blob1c3.open('a') as blob1c3fh1:
    ...     _ = blob1c3fh1.write(b'this is from connection 3')
    >>> with blob1c4.open('a') as blob1c4fh1:
    ...     _ = blob1c4fh1.write(b'this is from connection 4')
    >>> tm1.commit()
    >>> with root3['blob1'].open('r') as fp:
    ...     fp.read()
    b'this is blob 1woot!this is from connection 3'
    >>> tm2.commit()
    Traceback (most recent call last):
        ...
    ZODB.POSException.ConflictError: database conflict error (oid 0x01, class ZODB.blob.Blob...)

After the conflict, the winning transaction's result is visible on both
connections::

    >>> with root3['blob1'].open('r') as fp:
    ...     fp.read()
    b'this is blob 1woot!this is from connection 3'
    >>> tm2.abort()
    >>> with root4['blob1'].open('r') as fp:
    ...     fp.read()
    b'this is blob 1woot!this is from connection 3'

You can't commit a transaction while blob files are open:

    >>> f = root3['blob1'].open('w')
    >>> tm1.commit()
    Traceback (most recent call last):
    ...
    ValueError: Can't commit with opened blobs.

    >>> f.close()
    >>> tm1.abort()
    >>> f = root3['blob1'].open('w')
    >>> f.close()

    >>> f = root3['blob1'].open('r')
    >>> tm1.commit()
    Traceback (most recent call last):
    ...
    ValueError: Can't commit with opened blobs.
    >>> f.close()
    >>> tm1.abort()

Savepoints and Blobs
--------------------

We do support optimistic savepoints:

    >>> connection5 = database.open()
    >>> root5 = connection5.root()
    >>> blob = ZODB.blob.Blob()
    >>> with blob.open("w") as blob_fh:
    ...     _ = blob_fh.write(b"I'm a happy blob.")
    >>> root5['blob'] = blob
    >>> transaction.commit()
    >>> with root5['blob'].open("r") as fp:
    ...     fp.read()
    b"I'm a happy blob."
    >>> with root5['blob'].open("a") as blob_fh:
    ...     _ = blob_fh.write(b" And I'm singing.")
    >>> with root5['blob'].open("r") as fp:
    ...     fp.read()
    b"I'm a happy blob. And I'm singing."
    >>> savepoint = transaction.savepoint(optimistic=True)

    >>> with root5['blob'].open("r") as fp:
    ...     fp.read()
    b"I'm a happy blob. And I'm singing."

Savepoints store the blobs in temporary directories in the temporary
directory of the blob storage:

    >>> len([name for name in os.listdir(os.path.join(blob_dir, 'tmp'))
    ...      if name.startswith('savepoint')])
    1

After committing the transaction, the temporary savepoint files are moved to
the committed location again:

    >>> transaction.commit()
    >>> len([name for name in os.listdir(os.path.join(blob_dir, 'tmp'))
    ...      if name.startswith('savepoint')])
    0

We support non-optimistic savepoints too:

    >>> with root5['blob'].open("a") as file:
    ...     _ = file.write(b" And I'm dancing.")
    >>> with root5['blob'].open("r") as fp:
    ...     fp.read()
    b"I'm a happy blob. And I'm singing. And I'm dancing."
    >>> savepoint = transaction.savepoint()

Again, the savepoint creates a new savepoints directory:

    >>> len([name for name in os.listdir(os.path.join(blob_dir, 'tmp'))
    ...      if name.startswith('savepoint')])
    1

    >>> with root5['blob'].open("w") as file:
    ...     _ = file.write(b" And the weather is beautiful.")
    >>> savepoint.rollback()

    >>> with root5['blob'].open("r") as fp:
    ...     fp.read()
    b"I'm a happy blob. And I'm singing. And I'm dancing."
    >>> transaction.abort()

The savepoint blob directory gets cleaned up on an abort:

    >>> len([name for name in os.listdir(os.path.join(blob_dir, 'tmp'))
    ...      if name.startswith('savepoint')])
    0

Reading Blobs outside of a transaction
--------------------------------------

If you want to read from a Blob outside of transaction boundaries (e.g. to
stream a file to the browser), committed method to get the name of a
file that can be opened.

    >>> connection6 = database.open()
    >>> root6 = connection6.root()
    >>> blob = ZODB.blob.Blob()
    >>> with blob.open("w") as blob_fh:
    ...     _ = blob_fh.write(b"I'm a happy blob.")
    >>> root6['blob'] = blob
    >>> transaction.commit()
    >>> with open(blob.committed()) as fp:
    ...     fp.read()
    "I'm a happy blob."

We can also read committed data by calling open with a 'c' flag:

    >>> f = blob.open('c')

This just returns a regular file object:

    >>> type(f) == file_type
    True

and doesn't prevent us from opening the blob for writing:

    >>> with blob.open('w') as file:
    ...     _ = file.write(b'x')
    >>> with blob.open() as fp:
    ...     fp.read()
    b'x'

    >>> f.read()
    b"I'm a happy blob."

    >>> f.close()
    >>> transaction.abort()

An exception is raised if we call committed on a blob that has
uncommitted changes:

    >>> blob = ZODB.blob.Blob()
    >>> blob.committed()
    Traceback (most recent call last):
    ...
    ZODB.interfaces.BlobError: Uncommitted changes

    >>> blob.open('c')
    Traceback (most recent call last):
    ...
    ZODB.interfaces.BlobError: Uncommitted changes

    >>> with blob.open('w') as file:
    ...     _ = file.write(b"I'm a happy blob.")
    >>> root6['blob6'] = blob
    >>> blob.committed()
    Traceback (most recent call last):
    ...
    ZODB.interfaces.BlobError: Uncommitted changes

    >>> blob.open('c')
    Traceback (most recent call last):
    ...
    ZODB.interfaces.BlobError: Uncommitted changes

    >>> s = transaction.savepoint()
    >>> blob.committed()
    Traceback (most recent call last):
    ...
    ZODB.interfaces.BlobError: Uncommitted changes

    >>> blob.open('c')
    Traceback (most recent call last):
    ...
    ZODB.interfaces.BlobError: Uncommitted changes

    >>> transaction.commit()
    >>> with open(blob.committed()) as fp:
    ...     fp.read()
    "I'm a happy blob."

You can't open a committed blob file for writing:

    >>> try:
    ...     open(blob.committed(), 'w') # doctest: +ELLIPSIS
    ... except PermissionError:
    ...     print('Error raised.')
    Error raised.

tpc_abort
---------

If a transaction is aborted in the middle of 2-phase commit, any data
stored are discarded.

    >>> olddata, oldserial = blob_storage.load(blob._p_oid, '')
    >>> from ZODB.Connection import TransactionMetaData
    >>> t = TransactionMetaData()
    >>> blob_storage.tpc_begin(t)
    >>> with open('blobfile', 'wb') as file:
    ...     _ = file.write(b'This data should go away')
    >>> blob_storage.storeBlob(blob._p_oid, oldserial, olddata, 'blobfile',
    ...                             '', t)
    >>> new_oid = blob_storage.new_oid()
    >>> with open('blobfile2', 'wb') as file:
    ...     _ = file.write(b'This data should go away too')
    >>> blob_storage.storeBlob(new_oid, '\0'*8, olddata, 'blobfile2',
    ...                             '', t)
    >>> bool(blob_storage.tpc_vote(t))
    False
    >>> blob_storage.tpc_abort(t)

Now, the serial for the existing blob should be the same:

    >>> blob_storage.load(blob._p_oid, '') == (olddata, oldserial)
    True

The old data should be unaffected:

    >>> with open(blob_storage.loadBlob(blob._p_oid, oldserial)) as fp:
    ...     fp.read()
    "I'm a happy blob."

Similarly, the new object wasn't added to the storage:

    >>> blob_storage.load(new_oid, '')
    Traceback (most recent call last):
    ...
    ZODB.POSException.POSKeyError: 0x...

.. clean up

    >>> tm1.abort()
    >>> tm2.abort()
    >>> database.close()
