Eclipse Store: Up and Running
What Is Eclipse Store?
First, I would like to briefly explain what Eclipse Store actually is. Simply put, this is a mechanism for storing Java object trees. This initially sounds very theoretical, but it is much simpler than you might think.
When I started developing Java applications in 1996, I dreamed of being able to easily store the objects I created in my application. I found the assumption that serialization could be used to write everything to the hard drive as a byte stream very good. Unfortunately, the reality looked different or still looks different today. You were quickly confronted with various technologies, all with their own characteristics. I still remember very clearly the first steps in which the application's data had to be persisted via a JDBC interface. Who doesn't know the JDBC-ODBC interface from back then? A typical application now had JDBC connections, mapping layers that implement everything in SQL, and, of course, caches in various places so that the application's responsiveness remained tolerable for the user. It's not easy when dealing with a more complex data model.
With Eclipse Store, we now have a tool that eliminates most of these technology layers. So we have finally achieved what was promised to us in 1996.
Let's now come to the implementation of our own project. For this, we need the maven dependencies. For this example, I used the first final version. This is based on Microstream Version 8 and is the basis for the Eclipse project. As far as I know, Microstream is not being actively developed further.
<dependencies>
<dependency>
<groupId>org.eclipse.store</groupId>
<artifactId>storage-embedded</artifactId>
<version>{maven-version}</version>
</dependency>
</dependencies>
Now that we have added the necessary dependencies to the project, we can start with the first lines of source code.
Basic First Steps
The Storage Manager
We will now look at the essential elements of architecture. The linchpin of the persistence solution is the "Storage Manager." This is the interface through which the behavior can be configured. All entities are also transferred to persistence, retrieved, and deleted. The storage manager itself is available in various forms. We will focus on embedded storage here for now. This implementation is an in-memory solution that accesses the local hard drive directly. You shouldn't let the name confuse you. As is usual with RDBMS systems, it is not a tiny solution that allows you to do a little testing. The implementation of embedded storage is absolutely suitable for production.
So, an instance of this implementation is needed. The easiest way to get this is to call:
final EmbeddedStorageManager storageManager = EmbeddedStorage.start();
The chosen implementation depends on which dependencies have been defined in the pom.xml. In our case, it's the embedded implementation. We will discuss various other implementations in the following parts. We will also take a closer look at how the behavior can be configured later. These parameters are irrelevant for the first steps and can safely be ignored.
Storage Root
The storage manager now needs to know the root of the graphs to be stored. You can imagine this as described below.
If we only have one element that needs to be stored, then no further wrapper is necessary. However, if we have two parts that we want to save, we need a container to hold both elements. This container is then the root through which initial access to all elements can occur. This can be a list or a class specifically tailored to your needs. This container must now be made available for persistence.
Let's look at an example. Two different strings should be stored here. A list is used as a container. This means that the instance of the list is the StorageRoot element. This must now be declared as an anchor point.
EmbeddedStorageManager storageManager = EmbeddedStorage.start();
storageManager.setRoot(new ArrayList<>());
storageManager.storeRoot();
The storeRoot()
instruction causes this modification to be saved at the root. We will see this storeRoot()
method call more often. This persists in all changes in the graph, starting from the root. In our case, the root itself is saved.
How can another element be added to this container? There is no need to keep a reference to this container. Instead, we can ask the Storage Manager for this reference. What should not be forgotten at this point? If you overwrite the root, all previously saved elements will be deleted. These are then no longer under the supervision of persistence.
List<String> root = (List<String>) storageManager.root();
A downer at this point. Unfortunately, the root()
method is not typed. Here, you have to know exactly what type the container is. We'll look at how to deal with this better. I ask for patience here.
Any number of elements can now be added or removed from this list. To save the changes, the StorageManager
is informed of this. Let's look at this in a little more detail. We will expand and modify the previous source code for this. Instead of simple strings, a separate class with two attributes is now used. The first attribute is again a string and represents the data value. The second attribute is an instance of the type LocaDateTime
and is an automatically set timestamp. We can already see here that it is no longer a simple attribute. The constructor takes over the data value, which can be modified later. The timestamp
cannot be set explicitly. There is only one getter here. Such constructs can also be processed by EclipseStore without any further action.
public class DataElement {
private String value;
private LocalDateTime timestamp;
public DataElement(String value) {
this.value = value;
this.timestamp = LocalDateTime.now();
}
public void setValue(String value) {
this.value = value;
this.timestamp = LocalDateTime.now();
}
public String getValue() {
return value;
}
public LocalDateTime getTimestamp() {
return timestamp;
}
@Override
public String toString() {
return "DataElement{" +
"value='" + value + '\'' +
", timestamp=" + timestamp +
'}';
}
}
Case I: We Add Elements to the Container
In our case, the simplest case is to add one or more elements to the root element, a list of type DataElement
. This is done by adding the desired elements to the list and calling the storeRoot()
method at the root level or saving the elements yourself.
EmbeddedStorageManager storageManager = EmbeddedStorage.start();
storageManager.setRoot(new ArrayList<DataElement>());
storageManager.storeRoot();
List<DataElement> root = (List<DataElement>) storageManager.root();
root.add(new DataElement("Nr 1"));
root.add(new DataElement("Nr 2"));
storageManager.storeRoot();
And here is the method in which the elements are specified directly.
DataElement d3 = new DataElement("Nr 3");
DataElement d4 = new DataElement("Nr 4");
root.add(d3);
root.add(d4);
storageManager.storeAll(d3, d4);
Case II: We Remove Elements From the Container
The opposite operation of adding elements is deleting them. Here, too, the process is very straightforward. However, looking at the different options is unusual at the beginning. First, it's the most intuitive version. Here, the element is removed from the holding object, the root itself. This reduced instance is then passed on to the StorageManager
with the request to save this modification.
root.remove(d3);
storageManager.store(root);
However, you can also use the removed element yourself. For this purpose, the element previously removed from the holding instance is passed to the StorageManager
for storage. This approach works but is anything but intuitive. At this point, for reasons of comprehensibility, you should not write it. Your colleagues will be grateful for that.
root.remove(d4);
storageManager.store(d4);
Case III: We Modify the Elements Within the Container
Since elements can now be added and removed, the only thing missing is the modification. Here, too, the procedure is analogous to adding. The instance is modified and then passed to the StorageManager
for storage. The saving process can, of course, also take place via a holding element. For readability reasons, however, saving the modified elements yourself is strongly advisable. Exception can, of course, be when you change a lot of elements within a holding instance.
d3.setValue("Nr 3. modified");
storageManager.store(d3);
Conclusion
We have now seen how easy it is to get started with EclipseStore. Elements can now be saved without any further effort. This way, you can realize the first small applications. But that's not the end of it. The following parts will address the intricacies and challenges of more complex applications. So it remains exciting.
Happy Coding
Sven