|
| 1 | +Serialization |
| 2 | +========= |
| 3 | + |
| 4 | +Serializing your data structures using ``immer::persist`` allows you |
| 5 | +preserve the *structural sharing* across sessions of your application. |
| 6 | + |
| 7 | +This has multiple practical use cases, like storing the undo history |
| 8 | +or the clipboard of a complex application, or applying advanced |
| 9 | +logging techniques. |
| 10 | + |
| 11 | +The library serializes multiple containers together via the notion of |
| 12 | +a :ref:`pool<pools>`. These pools are produced automatically and |
| 13 | +represent in the JSON the internal structure (trees) that implement |
| 14 | +the Immer containers. |
| 15 | + |
| 16 | +Example |
| 17 | +------- |
| 18 | +.. _first-example: |
| 19 | + |
| 20 | +For this example, we'll use a ``document`` type that contains two |
| 21 | +``immer`` vectors. |
| 22 | + |
| 23 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 24 | + :language: c++ |
| 25 | + :start-after: intro/start-types |
| 26 | + :end-before: intro/end-types |
| 27 | + |
| 28 | +Let's say we have two vectors ``v1`` and ``v2``, where ``v2`` is |
| 29 | +derived from ``v1`` so that it shares data with it: |
| 30 | + |
| 31 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 32 | + :language: c++ |
| 33 | + :start-after: intro/start-prepare-value |
| 34 | + :end-before: intro/end-prepare-value |
| 35 | + |
| 36 | +We can serialize the document using ``cereal`` with this: |
| 37 | + |
| 38 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 39 | + :language: c++ |
| 40 | + :start-after: intro/start-serialize-with-cereal |
| 41 | + :end-before: intro/end-serialize-with-cereal |
| 42 | + |
| 43 | +Generating a JSON like this one: |
| 44 | + |
| 45 | +.. code-block:: c++ |
| 46 | + |
| 47 | + {"value0": {"ints": [1, 2, 3], "ints2": [1, 2, 3, 4, 5, 6]}} |
| 48 | + |
| 49 | +As you can see, ``ints`` and ``ints2`` contain the full linearization |
| 50 | +of each vector. The structural sharing between these two data |
| 51 | +structures is not represented in its serialized form. |
| 52 | + |
| 53 | +Using pools |
| 54 | +----------- |
| 55 | + |
| 56 | +First, let's make the ``document`` struct compatible with |
| 57 | +``boost::hana``. This way, the ``persist`` library can automatically |
| 58 | +determine what :ref:`pool<pools>` types are needed, and to name the |
| 59 | +pools. |
| 60 | + |
| 61 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 62 | + :language: c++ |
| 63 | + :start-after: intro/start-adapt-document-for-hana |
| 64 | + :end-before: intro/end-adapt-document-for-hana |
| 65 | + |
| 66 | +Then using ``immer::persist`` we can serialize it with: |
| 67 | + |
| 68 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 69 | + :language: c++ |
| 70 | + :start-after: intro/start-serialize-with-persist |
| 71 | + :end-before: intro/end-serialize-with-persist |
| 72 | + |
| 73 | +Which generates some JSON like this: |
| 74 | + |
| 75 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 76 | + :language: c++ |
| 77 | + :start-after: include:intro/start-persist-json |
| 78 | + :end-before: include:intro/end-persist-json |
| 79 | + |
| 80 | +As you can see, the value is serialized with every ``immer`` container |
| 81 | +replaced by an identifier. This identifier is a key into a |
| 82 | +:ref:`pool<pools>`, which is serialized just after. |
| 83 | + |
| 84 | +.. note:: |
| 85 | + Currently, ``immer-persist`` makes a distiction between |
| 86 | + pools used for saving containers (*output* pools) and for loading |
| 87 | + containers (*input* pools), similar to ``cereal`` with its |
| 88 | + ``InputArchive`` and ``OutputArchive`` distiction. |
| 89 | + |
| 90 | +Currently, ``immer-persist`` focuses on JSON as the serialization |
| 91 | +format and uses the ``cereal`` library internally. In principle, other |
| 92 | +formats and serialization libraries could be supported in the future. |
| 93 | +sharing across sessions. |
| 94 | + |
| 95 | +You can see in the out that the nodes of the trees that make up the |
| 96 | +``immer`` containers are directly represented in the JSON and, because |
| 97 | +we are representing all the containers as a whole, those nodes that |
| 98 | +are referenced in multiple trees can be stored only once. That same |
| 99 | +structure is preserved when reading the pool back from disk and |
| 100 | +reconstructing the vectors (and other containers) from it, thus |
| 101 | +allowing us to preserve the structural sharing across sessions. |
| 102 | + |
| 103 | +Custom policies |
| 104 | +---------- |
| 105 | + |
| 106 | +We can use policy to control the names of the pools for each container. |
| 107 | + |
| 108 | +For this example, let's define a new document type ``doc_2``. It will |
| 109 | +also contain another type ``extra_data`` with a ``vector`` of |
| 110 | +``strings`` in it. To demonstrate the responsibilities of the policy, |
| 111 | +the ``doc_2`` type will not be a ``boost::hana::Struct`` and will not |
| 112 | +allow for compile-time reflection. |
| 113 | + |
| 114 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 115 | + :language: c++ |
| 116 | + :start-after: include:start-doc_2-type |
| 117 | + :end-before: include:end-doc_2-type |
| 118 | + |
| 119 | +We define the ``doc_2_policy`` as following: |
| 120 | + |
| 121 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 122 | + :language: c++ |
| 123 | + :start-after: include:start-doc_2_policy |
| 124 | + :end-before: include:end-doc_2_policy |
| 125 | + |
| 126 | +The ``get_pool_types`` function returns the types of containers that |
| 127 | +should be serialized with pools, in this case it's both ``vector`` of |
| 128 | +``ints`` and ``strings``. The ``save`` and ``load`` functions control |
| 129 | +the name of the document node, in this case it is ``doc2_value``. And |
| 130 | +the ``get_pool_name`` overloaded functions supply the name of the pool |
| 131 | +for each corresponding ``immer`` container. To create and serialize a |
| 132 | +value of ``doc_2``, you can use the following approach: |
| 133 | + |
| 134 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 135 | + :language: c++ |
| 136 | + :start-after: include:start-doc_2-cereal_save_with_pools |
| 137 | + :end-before: include:end-doc_2-cereal_save_with_pools |
| 138 | + |
| 139 | +The serialized JSON looks like this: |
| 140 | + |
| 141 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 142 | + :language: c++ |
| 143 | + :start-after: include:start-doc_2-json |
| 144 | + :end-before: include:end-doc_2-json |
| 145 | + |
| 146 | +And it can also be loaded from JSON like this: |
| 147 | + |
| 148 | +.. literalinclude:: ../../test/extra/persist/test_for_docs.cpp |
| 149 | + :language: c++ |
| 150 | + :start-after: include:start-doc_2-load |
| 151 | + :end-before: include:end-doc_2-load |
| 152 | + |
| 153 | +This example also demonstrates a scenario in which the main document |
| 154 | +type ``doc_2`` contains another type ``extra_data`` with a |
| 155 | +``vector``. As you can see in the resulting JSON, nested types are |
| 156 | +also serialized with pools: ``"extra": {"comments": 1}``. Only the ID |
| 157 | +of the ``comments`` ``vector`` is serialized instead of its content. |
0 commit comments