Want a lightweight in memory database – try Boon!

So you have a million objects in memory, you’ve got them in a Map – so you can access them by key very quickly, but now you want to find all that match on another field. Well, they are in memory, so you can go through them all. However that is going to be slow.

[code]
static Collection

database = new ArrayList

();

Collection

results = new LinkedList

();
for (Map entry : database) {
if (entry.get(“colour”).equals(“red”)) {
results.add(entry);
}
}
[/code]

You could stick them in a DB (like MySQL or MongoDB) however it will still be pretty slow to query/convert to/from or you could write your own query engine from scratch. Admittedly its optimised for that, but it’s still a separate process and the data has been converted into your

If only you could index the collection by that other field…

Well you can – just put the collection into a Boon Repo and add an index on that field and it will take care of ensuring the index is kept up to date.

[code]
Repo<Integer, Map> dbRepo = Repos.builder()
.primaryKey(“name”)
.lookupIndex(“colour”)
.build(int.class, Map.class);

dbRepo.addAll(database);

List

result = dbRepo.query(eq(“colour”, colour));
[/code]

For these simple queries the search changes from O(n) to O(1) – bucket loads quicker!

It provides a rich set of criteria facilities that allows you to query in a variety of ways – equality, in, between, etc and these can be composed using and/or …

[code]
List

result = dbRepo.query(and( eq(“sport”, sport), eq(“job”, job)));
[/code]

It effectively provides an In memory object database, searchable, you define the indexes you want to use – doesn’t have to be unique.
– its pure Java (1.7) – although I have a 1.6 backport.

Ideal for the case where you have low frequency updates but lots of varied queries.

Its a little quirky when it comes to updating items – to update an item, you need to either delete/re-add it or get the existing object, clone it, amend it and re-add it.

[code]
managers = dbRepo.query(eq(“job”, “manager”));
Map updatedMgr = Maps.copy(managers.get(0)); // Maps.copy is a boon cloning util
updatedMgr.put(“colour”,”blue”);
dbRepo.modify(updatedMgr);
[/code]

I have found performance to vary somewhat on more complex queries – but I am sure that is something that will improve greatly.

As with all libraries, you need to use it and see if it helps with what you need.

Links to similar data repo articles
Boon’s Data Repo by Example
Boon Data Repo Indexed Collections and more
Background on Data Repo
What if Java Collections and Java Hierarchies were Easily Searchable?
– Unrelated, but… Boon beer

Thanks to Rick Hightower for giving us this boon!

Leave a Reply

Your email address will not be published. Required fields are marked *