blob: a418f58af475f5ad741ceda5ae3504263fb95a9f [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_blob_store:
David Rogers2d195022020-07-16 14:07:47 -07002
Armando Montanez28ad6f42021-08-30 16:23:57 -07003=============
David Rogers2d195022020-07-16 14:07:47 -07004pw_blob_store
Armando Montanez28ad6f42021-08-30 16:23:57 -07005=============
David Rogers2d195022020-07-16 14:07:47 -07006``pw_blob_store`` is a storage container library for storing a single blob of
Armando Montanez28ad6f42021-08-30 16:23:57 -07007data. ``BlobStore`` is a flash-backed persistent storage system with integrated
David Rogers2d195022020-07-16 14:07:47 -07008data integrity checking that serves as a lightweight alternative to a file
9system.
10
Ted Pudlik1b69a4e2021-11-13 00:30:12 +000011-----
Armando Montanez28ad6f42021-08-30 16:23:57 -070012Usage
Ted Pudlik1b69a4e2021-11-13 00:30:12 +000013-----
Armando Montanez28ad6f42021-08-30 16:23:57 -070014Most operations on a ``BlobStore`` are done using ``BlobReader`` and
15``BlobWriter`` objects that have been constructed using a ``BlobStore``. Though
16a ``BlobStore`` may have multiple open ``BlobReader`` objects, no other
17readers/writers may be active if a ``BlobWriter`` is opened on a blob store.
David Rogers2d195022020-07-16 14:07:47 -070018
David Rogers2835cf62022-03-10 20:23:20 -080019The data state of a blob can be checked using the ``HasData()`` method.
20The method returns true if the blob is currenty valid and has at least one data
21byte. This allows checking if a blob has stored data without needing to
22instantiate and open a reader or writer.
23
David Rogers96571412021-11-05 03:30:35 -070024Write buffer
25============
26
27BlobStore uses a write buffer to allow writes smaller than and/or unaligned to
28the flash write aligment. BlobStore also supports using the write buffer for
29deferred writes that can be enqueued and written to flash at a later time or by
30a different thread/context.
31
32BlobStore can be used with a zero-size write buffer to reduce memory
33requirements. When using zero-size write buffer, the user is required to write
34maintain write sizes that are a multiple of the flash write size the blob is
35configured for.
36
37If a non-zero sized write buffer is used, the write buffer size must be a
38multiple of the flash write size.
39
Armando Montanez28ad6f42021-08-30 16:23:57 -070040Writing to a BlobStore
41----------------------
42``BlobWriter`` objects are ``pw::stream::Writer`` compatible, but do not support
43reading any of the blob's contents. Opening a ``BlobWriter`` on a ``BlobStore``
44that contains data will discard any existing data if ``Discard()``, ``Write
45()``, or ``Erase()`` are called. There is currently no mechanism to allow
46appending to existing data.
David Rogers2d195022020-07-16 14:07:47 -070047
Armando Montanez28ad6f42021-08-30 16:23:57 -070048.. code-block:: cpp
David Rogers2d195022020-07-16 14:07:47 -070049
Armando Montanez0e8aff82021-09-30 13:21:12 -070050 BlobStore::BlobWriterWithBuffer writer(my_blob_store);
Armando Montanez28ad6f42021-08-30 16:23:57 -070051 writer.Open();
52 writer.Write(my_data);
53
54 // ...
55
56 // A close is implied when a BlobWriter is destroyed. Manually closing a
57 // BlobWriter enables error handling on Close() failure.
58 writer.Close();
59
60Erasing a BlobStore
61===================
62There are two distinctly different mechanisms to "erase" the contents of a BlobStore:
63
64#. ``Discard()``: Discards any ongoing writes and ensures ``BlobReader`` objects
65 see the ``BlobStore`` as empty. This is the fastest way to logically erase a
66 ``BlobStore``.
67#. ``Erase()``: Performs an explicit flash erase of the ``BlobStore``'s
68 underlying partition. This is useful for manually controlling when a flash
69 erase is performed before a ``BlobWriter`` starts to write data (as flash
70 erase operations may be time-consuming).
71
72Naming a BlobStore's contents
73=============================
74Data in a ``BlobStore`` May be named similarly to a file. This enables
75identification of a BlobStore's contents in cases where different data may be
76stored to a shared blob store. This requires an additional RAM buffer that can
77be used to encode the BlobStore's KVS metadata entry. Calling
78``MaxFileNameLength()`` on a ``BlobWriter`` will provide the max file name
79length based on the ``BlobWriter``'s metadata encode buffer size.
80
81``SetFileName()`` performs a copy of the provided file name, meaning it's safe
82for the ``std::string_view`` to be invalidated after the function returns.
83
84.. code-block:: cpp
85
Armando Montanez0e8aff82021-09-30 13:21:12 -070086 constexpr size_t kMaxFileNameLength = 48;
87 BlobStore::BlobWriterWithBuffer<kMaxFileNameLength> writer(my_blob_store);
Armando Montanez28ad6f42021-08-30 16:23:57 -070088 writer.Open();
89 writer.SetFileName("stonks.jpg");
90 writer.Write(my_data);
91 // ...
92 writer.Close();
93
Armando Montanez28ad6f42021-08-30 16:23:57 -070094Reading from a BlobStore
95------------------------
David Rogers2835cf62022-03-10 20:23:20 -080096A ``BlobStore`` may have multiple open ``BlobReader`` objects. No other
97readers/writers may be open/active if a ``BlobWriter`` is opened on a blob
98store.
Armando Montanez28ad6f42021-08-30 16:23:57 -070099
David Rogers2d195022020-07-16 14:07:47 -0700100 0) Create BlobReader instance
101 1) BlobReader::Open().
102 2) Read data using BlobReader::Read() or
Yecheng Zhao4a6deb42021-09-01 15:23:00 -0700103 BlobReader::GetMemoryMappedBlob(). BlobReader is seekable. Use
104 BlobReader::Seek() to read from a desired offset.
David Rogers2d195022020-07-16 14:07:47 -0700105 3) BlobReader::Close().
106
Ted Pudlik1b69a4e2021-11-13 00:30:12 +0000107--------------------------
Armando Montanez0aa452b2021-09-29 17:21:33 -0700108FileSystem RPC integration
Ted Pudlik1b69a4e2021-11-13 00:30:12 +0000109--------------------------
Armando Montanez0aa452b2021-09-29 17:21:33 -0700110``pw_blob_store`` provides an optional ``FileSystemEntry`` implementation for
111use with ``pw_file``'s ``FlatFileSystemService``. This simplifies the process of
112enumerating ``BlobStore`` objects as files via ``pw_file``'s ``FileSystem`` RPC
113service.
114
Ted Pudlik1b69a4e2021-11-13 00:30:12 +0000115-----------
David Rogers17793d62021-02-05 03:29:02 -0800116Size report
Ted Pudlik1b69a4e2021-11-13 00:30:12 +0000117-----------
David Rogers17793d62021-02-05 03:29:02 -0800118The following size report showcases the memory usage of the blob store.
119
120.. include:: blob_size
121
122
David Rogers2d195022020-07-16 14:07:47 -0700123.. note::
124 The documentation for this module is currently incomplete.