Java in-memory database-like object

user1017413 picture user1017413 · Nov 8, 2013 · Viewed 12.5k times · Source

I'm looking at a project that will benefit from an in-memory database-like object. I will have perhaps one or two thousand objects with the same structure, all inheriting from an abstract class. There will be a couple of string fields, an int, perhaps an enum or two (or maybe even a set of enums), and then a set of Strings. There would need to be a transient boolean field as well, but that likely wouldn't be a concern.

These objects would be instantiated and constructed from preset data, although additional ones can be created beyond that if need be. They may be stored in an XML file or something similar. I'd rather not hardwire the entire thing, of course, and using a local database like SQLite feels like overkill.

Storing these objects would be relatively simple if not for one thing: I want the user to easily be able to find the object they want from ANY of the values, most of which would be unique. This rules out a HashMap unless I want to wrap a massive bunch of them, which is hardly ideal. That leaves me looking for a sort of indexed, in-memory database-like object that supports retrieval via any field of the object. It may not have to store the objects directly, but could assemble them upon retrieval, or retrieve a "row" based on one field, get another field from the same "row" that serves as a sort of key, and then retrieve the object from a single HashMap based on that key.

In short, the idea is to easily and quickly retrieve objects with the same fields based on any field they contain. I've seen a variety of different libraries and such that may do this sort of thing, but there's a real myriad of these things out there. Whatever may work would need to be free and compatible with a variety of open licenses.

Answer

scottb picture scottb · Nov 8, 2013

For an "in memory database-like object" please don't re-invent the wheel. However simple or complex your needs are, using a library that has already been through years (or decades) of debugging and optimization is absolutely the right thing to do if it can be made to suit your needs.

SQLite is a fine choice. It is not Java, however, and requires a separate JDBC driver (no JDBC driver for SQLite is maintained by the SQlite project).

Another RDBMS which is small footprint, reasonably lightweight, and robust is HSQLDB which includes a version 4.1 JDBC driver as part of the project. It is 100% Java. It provides native support for in memory tables. Any time I use the word "database" and "thousands of objects", my thoughts immediately go to HSQLDB.

The project is managed by the HSQLDB development group and is available here: http://www.hsqldb.org