BNRPersistence

One of the things that's bugged me about Pear Note for a long time is that search took way too long. Search is one of those things that should be instant while you're typing. The reason it was slower was that it was querying Spotlight as you type. Spotlight has gotten faster through the years, but it's still not in the league I wanted for speed. So, I started trying out ways to speed this up. Soon, it became apparent that the way to go was to keep my own database of Pear Note documents to search, rather than rely on Spotlight on the fly.

The obvious choice for a local database on the Mac would be to use Core Data. I've never used it before, but it's supported by Apple and would likely suffice. However, a few months ago, I attended NSConference in Atlanta, and heard Aaron Hillegass talk about his new project, BNRPersistence. I'd previously heard Aaron talk about some of the issues with Core Data on a podcast, and at NSConference Aaron unveiled this project to do something about it.

BNRPersistence is a set of local persistence classes built on top of Tokyo Cabinet and Tokyo Dystopia. Tokyo Cabinet is one of the no-SQL databases that are gaining popularity these days. Aaron demoed BNRPersistence at NSConference and I took one thing away from his demo - it's crazy fast. Orders of magnitude faster than Core Data.

So, I set out to try both BNRPersistence and Core Data out. That's when I discovered BNRPersistence's next major advantage - it's really simple. By the time I had started wrapping my head around managed object contexts and data models in Core Data, I had everything working in BNRPersistence. Objects being stored just need a pair of methods (very similar to complying with NSCoding), and almost the entire API for interacting with the object store is described in the short README. Once I had things working, I confirmed that indeed, BNRPersistence is crazy fast. So, I stopped delving into Core Data and started integrating BNRPersistence into Pear Note.

It definitely does show its immaturity at times, so there are some drawbacks:

  • It's not thread safe at all, so you have to make sure you lock appropriately or stick to a single thread for store access.
  • There aren't many convenience methods, but that contributes to the simplicity of the API.
  • Getting Tokyo Cabinet and Tokyo Dystopia installed and usable for multiple architectures was a bit of a pain, but I'll take a bit of administrative pain if it makes the code simpler and easier to write.
  • Tokyo Cabinet stores have a very large disk footprint for what they store. For me, this wasn't a big deal as I'm only storing a search cache in it, but I'd have to do some testing before using it for a document format.

All that said, I think BNRPersistence is a great project and am happy I chose to use it. Once Pear Note 2 comes out in a couple weeks, you can all see it in action and judge for yourself.

Comments

There are no comments yet