blob: 29e82d9fe778881f91735d8ea11603305eafb113 [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
Armando Montanez28ad6f42021-08-30 16:23:57 -070011=====
12Usage
13=====
14Most 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
Armando Montanez28ad6f42021-08-30 16:23:57 -070019----------------------
20Writing to a BlobStore
21----------------------
22``BlobWriter`` objects are ``pw::stream::Writer`` compatible, but do not support
23reading any of the blob's contents. Opening a ``BlobWriter`` on a ``BlobStore``
24that contains data will discard any existing data if ``Discard()``, ``Write
25()``, or ``Erase()`` are called. There is currently no mechanism to allow
26appending to existing data.
David Rogers2d195022020-07-16 14:07:47 -070027
Armando Montanez28ad6f42021-08-30 16:23:57 -070028.. code-block:: cpp
David Rogers2d195022020-07-16 14:07:47 -070029
Armando Montanez0e8aff82021-09-30 13:21:12 -070030 BlobStore::BlobWriterWithBuffer writer(my_blob_store);
Armando Montanez28ad6f42021-08-30 16:23:57 -070031 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
40Erasing a BlobStore
41===================
42There 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
52Naming a BlobStore's contents
53=============================
54Data in a ``BlobStore`` May be named similarly to a file. This enables
55identification of a BlobStore's contents in cases where different data may be
56stored to a shared blob store. This requires an additional RAM buffer that can
57be used to encode the BlobStore's KVS metadata entry. Calling
58``MaxFileNameLength()`` on a ``BlobWriter`` will provide the max file name
59length based on the ``BlobWriter``'s metadata encode buffer size.
60
61``SetFileName()`` performs a copy of the provided file name, meaning it's safe
62for the ``std::string_view`` to be invalidated after the function returns.
63
64.. code-block:: cpp
65
Armando Montanez0e8aff82021-09-30 13:21:12 -070066 constexpr size_t kMaxFileNameLength = 48;
67 BlobStore::BlobWriterWithBuffer<kMaxFileNameLength> writer(my_blob_store);
Armando Montanez28ad6f42021-08-30 16:23:57 -070068 writer.Open();
69 writer.SetFileName("stonks.jpg");
70 writer.Write(my_data);
71 // ...
72 writer.Close();
73
74------------------------
75Reading from a BlobStore
76------------------------
77
David Rogers2d195022020-07-16 14:07:47 -070078 0) Create BlobReader instance
79 1) BlobReader::Open().
80 2) Read data using BlobReader::Read() or
Yecheng Zhao4a6deb42021-09-01 15:23:00 -070081 BlobReader::GetMemoryMappedBlob(). BlobReader is seekable. Use
82 BlobReader::Seek() to read from a desired offset.
David Rogers2d195022020-07-16 14:07:47 -070083 3) BlobReader::Close().
84
Armando Montanez0aa452b2021-09-29 17:21:33 -070085==========================
86FileSystem RPC integration
87==========================
88``pw_blob_store`` provides an optional ``FileSystemEntry`` implementation for
89use with ``pw_file``'s ``FlatFileSystemService``. This simplifies the process of
90enumerating ``BlobStore`` objects as files via ``pw_file``'s ``FileSystem`` RPC
91service.
92
Armando Montanez28ad6f42021-08-30 16:23:57 -070093===========
David Rogers17793d62021-02-05 03:29:02 -080094Size report
Armando Montanez28ad6f42021-08-30 16:23:57 -070095===========
David Rogers17793d62021-02-05 03:29:02 -080096The following size report showcases the memory usage of the blob store.
97
98.. include:: blob_size
99
100
David Rogers2d195022020-07-16 14:07:47 -0700101.. note::
102 The documentation for this module is currently incomplete.