javakid1212
javakid1212

Reputation: 63

IMDB (In Memory Database) Vs Java Collections

My question is: Is there any way to implement database kind of functionality (which In Memory database provides) with java.util.collections (ArrayList/HashMap/3rd party LIB etc...) Let me put in little bit descriptive.

I have Model class Account with different 25 attributes, and i can store the data of lets say 5000 Accounts on ArrayList. Same way I can hold the data on In Memory Database by creating the table t_Account. Now I want to implement some basic operations as mentioned below

  1. Sorting in ascending/descending order based on each attribute. (e.g. Order By operation of SQL)

  2. Filtering out the specific Model Accounts based on different filter criteria of each attribute? (applying WHERE/AND conditions on table in SQL)

  3. Search of specific Account based on specific attribute? ( applying LIKE operations on table in SQL)

In short my main aim is to achieve the In Memory Database related features using Java, which should be as good as In Memory database operations?

Thanks in advance for your valuable time and suggestion...

Upvotes: 2

Views: 2706

Answers (2)

Bill Burdick
Bill Burdick

Reputation: 975

Both the HSQLDB and H2 pure Java SQL DBs can operate completely in-memory (and they are quite fast despite not being native).

Connection strings, HSQLDB: jdbc:hsqldb:mem:, H2: jdbc:h2:mem:. You can add an identifier after them to create in-memory databases that support multiple connections in the same VM.

The native embedded SQL DB, SQLite, can also operate completely in-memory and the Xerial SQLite driver uses the connection string jdbc:sqlite::memory:. Note that SQLite does not allow an identifier after :memory: and if you open two connections in the same VM to :memory, you will get two different in-memory databases.

I don't see a real need to use named in-memory connections because you can reuse the same connection object if you need to use it from different threads and all three of the above databases are thread-safe.

The Firebird embedded database does not provide in-memory databases.

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382102

Of course you can do part of what a database do in memory as long as you don't

  • want to implement complex querying

  • look for performances on big sets of data : complex indexes (including their maintenance) isn't an easy thing

But 5000 records is a very small set, so you can probably manage them in memory with custom functions and indexes without difficulty. Sorting, filtering, searching are easily implemented on small sets in any language.

You seem to use java ("ArrayList"), look at Collections.sort(). Sorting a small collection is fast as long as your comparison functions are fast.

On the other hand, using a small database like sqlite is easy too. If you're unsure you can start with the java solution and integrate external tools later if a profiling shows you cannot handle your in memory database efficiently enough for your need. If it fits easily in RAM and you don't have to persist the data, try to do it in memory. The first reason to use a database is the persistence. At contrary, if you have to keep the records between start-stops (or crashes) of your program, use a database.

Upvotes: 1

Related Questions