-
Notifications
You must be signed in to change notification settings - Fork 60
Producing a Snapshot
The first thing we need to do to produce blob files is to instantiate a FastBlobStateEngine:
Once we instantiate a state engine, we need to create the data state on our machine. The data state should contain all of the instances of objects which provide a complete, up-to-date view of your data. Your method for instantiating this from your sources of truth will vary. For each top level object you wish to be contained in your data state, you must add it to the state engine:
Note that adding an object to the state engine requires two parameters. First, we need to tell the state engine what type of object this is. This is the name specified in the type’s NFTypeSerializer. Second, we need to give the state engine the actual object.
It’s worth taking some time here to go over a few details about what is happening when we make this call. When we add an object to the state engine, it will create a serialized representation for that object, and any objects which are reachable from that object. If the serialized representation at any level in the object hierarchy is identical to the serialized representation of a previously added object, then the previously added serialized representation will be used.
Thread Safety: The addition of objects to a FastBlobStateEngine is thread safe. You can safely add objects from multiple threads without explicit synchronization.
The FastBlobStateEngine will not retain references to objects which are passed to it. Instead, it will retain only the serialized representation of those objects. Consequently, if you no longer retain references to objects after you add them to the state engine, the space they occupy will be reclaimed by the garbage collector.
Back to our example. After you have added all of your Objects to the state engine, you must notify the state engine that you have finished the data state:
And once you have done that, you can use a FastBlobWriter to write a blob snapshot and/or delta. Instantiate the FastBlobWriter with the state engine:
You must define where these blobs will go, and then create a DataOutputStream to that location. The FastBlobWriter will stream the data appropriately. Please note that the FastBlobWriter will not close the streams for you. This is on purpose; it makes it easy to stream a blob as part of a larger message:
Now we’ve written our first blob file. Let’s recap the example, without so many noisy comments:
This example is also available as a runnable JUnit test. Check out /src/examples/java in the zeno project.
Continue on to the next page “Producing a Delta”, to learn about how we can make our data change over time.