I’m working on a little project that implements an event queue. I could have happily implemented it in a List, but I’ve wanted the power to query it in all sorts of unusual ways so I ended up just writing it to a database. But this week I’ve come across JoSQL - a very cool little library for querying java collections using SQL style semantics.
Now this little library isn’t a database tool, it’s a query tool for java.util.Collections in memory (the name is a little unfortunate - but makes sense once you’ve used it). Once you wrap your head around it, things get very funky. To query my event queue (an ArrayList) for events of a particular value, I can do stuff like:
Query q = new Query(); q.parse("SELECT * from event.queue.Event where key = '" + eventCode + "'"); QueryResults qr = q.execute(eventQueue); if (qr.getResults() != null) return (Event[]) qr.getResults().toArray(new MonitorEvent[0]);
Of course this is something you could write by hand by iterating your collections and performing your custom checks on each iteration, but using JoSQL is just way more productive and intuitive - and you can express what you’re after very concisely.
You can event do ORDER BY
and all sorts of SQL goodness. Very cool stuff. And how performant is it? Well, compared with the database I was persisting this stuff to… let’s just say I’m a long way ahead…
Big props to the guys at JoSQL. Very funky and productive library!