Who’s Been Naughty, Who’s Been Nice? Santa Gives You Java 11 Advice!
Ever wondered how Santa can deliver holiday gifts to all kids around the world? There are 2 billion
You do not need to wonder anymore. I will reveal the secret. He is using Java 11 and a modern stream ORM with superfast execution.
Even though Santa’s backing database is old and slow, he can analyze data in microseconds by using standard Java streams and in-JVM-memory technology. Santa’s database contains two tables;
Childwhich holds every child in the world, and
HolidayGiftthat specifies all the items available for production in Santa’s workshop. A child can only have one wish, such are the hash rules.
Viewing the Database as StreamsSpeedment is a modern stream based ORM which is able to view relational database tables as standard Java streams. As we all know, only nice children get gifts, so it is important to distinguish between those who’s been naughty and those who’s been nice. This is easily accomplished with the following code:
var niceChildren = children.stream()
This stream will yield a long list containing only the kids that have been nice. To enable Santa to optimize his delivery route, the list is sorted by country of residence.
Joining Child and HolidayGiftThis list seems incomplete though. How does Santa keep track of which gift goes to whom? Now the
HolidayGifttable will come in handy. Since some children provided Santa with their wish list, we can now join the two tables together to make a complete list containing all the nice children and their corresponding gift. It is important to include the children without any wish (they will get a random gift), therefore we make a left join.
var join = joinComponent
Speedment is using a builder pattern to create a
Join<T>object which can then be reused over and over again to create streams with elements of type
T. In this case, it is used to join
HolidayGift. The join only includes children that are nice and matches rows which contain the same value in the
This is how Santa deliver all packages:
join.stream()As can be seen, Santa can easily deliver all the packages with parallel sleighs, carried by reindeers.
This will render the stream to an efficient SQL query but unfortunately, it is not quick enough to make it in time.
Using In-JVM-Memory AccelerationNow to the fun part. Santa is activating the in-JVM-memory acceleration component in Speedment, called DataStore. This is done in the following way:
var santasWorkshop = new ApplicationBuilder()This startup configuration is the only needed adjustment to the application. All stream constructs above remain the same. When the application is started, a snapshot of the database is pulled into the JVM and is stored off-heap. Because the data is stored off-heap, it will not influence garbage collection and the amount of data is only limited by available RAM. Nothing prevents Santa from loading terabytes of data since he is using a cloud service and can easily expand his RAM. Now the application will run order of magnitudes faster and Santa will be able to deliver all packages in time.
// Activate DataStore
// Load a snapshot of the database into off-heap memory
Run Your Own Projects with In-JVM-Memory AccelerationIf you want to try for yourself how fast a database application can be, there is an Initializer that can be found here. Just tick in your desired database type (Oracle, MySQL, MariaDB, PostgreSQL, Microsoft SQL Server, DB2 or AS400) and you will get a POM and an application template automatically generated for you.
If you need more help setting up your project, check out the Speedment GitHub page or explore the user guide.