Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 1 | .. _module-pw_blob_store: |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 2 | |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 3 | ============= |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 4 | pw_blob_store |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 5 | ============= |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 6 | ``pw_blob_store`` is a storage container library for storing a single blob of |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 7 | data. ``BlobStore`` is a flash-backed persistent storage system with integrated |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 8 | data integrity checking that serves as a lightweight alternative to a file |
| 9 | system. |
| 10 | |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 11 | ===== |
| 12 | Usage |
| 13 | ===== |
| 14 | Most operations on a ``BlobStore`` are done using ``BlobReader`` and |
| 15 | ``BlobWriter`` objects that have been constructed using a ``BlobStore``. Though |
| 16 | a ``BlobStore`` may have multiple open ``BlobReader`` objects, no other |
| 17 | readers/writers may be active if a ``BlobWriter`` is opened on a blob store. |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 18 | |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 19 | ---------------------- |
| 20 | Writing to a BlobStore |
| 21 | ---------------------- |
| 22 | ``BlobWriter`` objects are ``pw::stream::Writer`` compatible, but do not support |
| 23 | reading any of the blob's contents. Opening a ``BlobWriter`` on a ``BlobStore`` |
| 24 | that contains data will discard any existing data if ``Discard()``, ``Write |
| 25 | ()``, or ``Erase()`` are called. There is currently no mechanism to allow |
| 26 | appending to existing data. |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 27 | |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 28 | .. code-block:: cpp |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 29 | |
Armando Montanez | 0e8aff8 | 2021-09-30 13:21:12 -0700 | [diff] [blame] | 30 | BlobStore::BlobWriterWithBuffer writer(my_blob_store); |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 31 | writer.Open(); |
| 32 | writer.Write(my_data); |
| 33 | |
| 34 | // ... |
| 35 | |
| 36 | // A close is implied when a BlobWriter is destroyed. Manually closing a |
| 37 | // BlobWriter enables error handling on Close() failure. |
| 38 | writer.Close(); |
| 39 | |
| 40 | Erasing a BlobStore |
| 41 | =================== |
| 42 | There are two distinctly different mechanisms to "erase" the contents of a BlobStore: |
| 43 | |
| 44 | #. ``Discard()``: Discards any ongoing writes and ensures ``BlobReader`` objects |
| 45 | see the ``BlobStore`` as empty. This is the fastest way to logically erase a |
| 46 | ``BlobStore``. |
| 47 | #. ``Erase()``: Performs an explicit flash erase of the ``BlobStore``'s |
| 48 | underlying partition. This is useful for manually controlling when a flash |
| 49 | erase is performed before a ``BlobWriter`` starts to write data (as flash |
| 50 | erase operations may be time-consuming). |
| 51 | |
| 52 | Naming a BlobStore's contents |
| 53 | ============================= |
| 54 | Data in a ``BlobStore`` May be named similarly to a file. This enables |
| 55 | identification of a BlobStore's contents in cases where different data may be |
| 56 | stored to a shared blob store. This requires an additional RAM buffer that can |
| 57 | be used to encode the BlobStore's KVS metadata entry. Calling |
| 58 | ``MaxFileNameLength()`` on a ``BlobWriter`` will provide the max file name |
| 59 | length based on the ``BlobWriter``'s metadata encode buffer size. |
| 60 | |
| 61 | ``SetFileName()`` performs a copy of the provided file name, meaning it's safe |
| 62 | for the ``std::string_view`` to be invalidated after the function returns. |
| 63 | |
| 64 | .. code-block:: cpp |
| 65 | |
Armando Montanez | 0e8aff8 | 2021-09-30 13:21:12 -0700 | [diff] [blame] | 66 | constexpr size_t kMaxFileNameLength = 48; |
| 67 | BlobStore::BlobWriterWithBuffer<kMaxFileNameLength> writer(my_blob_store); |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 68 | writer.Open(); |
| 69 | writer.SetFileName("stonks.jpg"); |
| 70 | writer.Write(my_data); |
| 71 | // ... |
| 72 | writer.Close(); |
| 73 | |
| 74 | ------------------------ |
| 75 | Reading from a BlobStore |
| 76 | ------------------------ |
| 77 | |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 78 | 0) Create BlobReader instance |
| 79 | 1) BlobReader::Open(). |
| 80 | 2) Read data using BlobReader::Read() or |
Yecheng Zhao | 4a6deb4 | 2021-09-01 15:23:00 -0700 | [diff] [blame] | 81 | BlobReader::GetMemoryMappedBlob(). BlobReader is seekable. Use |
| 82 | BlobReader::Seek() to read from a desired offset. |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 83 | 3) BlobReader::Close(). |
| 84 | |
Armando Montanez | 0aa452b | 2021-09-29 17:21:33 -0700 | [diff] [blame] | 85 | ========================== |
| 86 | FileSystem RPC integration |
| 87 | ========================== |
| 88 | ``pw_blob_store`` provides an optional ``FileSystemEntry`` implementation for |
| 89 | use with ``pw_file``'s ``FlatFileSystemService``. This simplifies the process of |
| 90 | enumerating ``BlobStore`` objects as files via ``pw_file``'s ``FileSystem`` RPC |
| 91 | service. |
| 92 | |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 93 | =========== |
David Rogers | 17793d6 | 2021-02-05 03:29:02 -0800 | [diff] [blame] | 94 | Size report |
Armando Montanez | 28ad6f4 | 2021-08-30 16:23:57 -0700 | [diff] [blame] | 95 | =========== |
David Rogers | 17793d6 | 2021-02-05 03:29:02 -0800 | [diff] [blame] | 96 | The following size report showcases the memory usage of the blob store. |
| 97 | |
| 98 | .. include:: blob_size |
| 99 | |
| 100 | |
David Rogers | 2d19502 | 2020-07-16 14:07:47 -0700 | [diff] [blame] | 101 | .. note:: |
| 102 | The documentation for this module is currently incomplete. |