PStore for Data Storage

For those who aren’t as familiar with databases yet, PStore is a native ruby gem that’s actually pretty useful for storing smaller datasets.

If you think about it, persistence of data can occur simply through reading from and writing to a file each and every time a script runs with the relevant information. Now, this is pretty inefficient, but for simple data structures like a hash that needs to be updated, it can work pretty well.

The basic function of PStore is to make this process of reading and writing to files easier.

For a recent project, we built a program in Terminal for tracking user votes to rank NFL players that were pulled from an API database of players. The dataset we needed to store and persist was very simple — a hash with keys consisting of player names that point to that respective player’s number of votes (which would be updated every time the program ran).

It looks something like this:

{“Aaron Rodgers” => 4, “Tom Brady” => 3, “Peyton Manning” => 2}

A hash with data this simple made PStore a solid candidate for data persistence.

The code below illustrates how to initiate a PStore object and write data to it. The first step is to create a PStore object using‘file_name’), which will create a new file ‘file_name’ where the data will persist.

@@qb_vote_hash ||=‘qb.txt’)

Next, we populated the PStore with an initial hash that would be updated each time the program ran. To do this, we simply iterated over an array of existing hashes and populated the PStore object with only the data from the existing hashes that we needed to persist.

Every PStore interaction in Ruby needs to occur as a block in a transaction.

Updating the PStore object is very straightforward — just treat it like a simple hash but embed it in a transaction as shown below.

Iterating through the PStore object to return data as output is also very straightforward if you know how to iterate through a hash.

One obstacle we faced was sorting the PStore object once new votes were inserted. Hashes have a sort method, but PStore objects don’t appear to be easily sorted. To get around this, we converted the PStore object to a hash, by iterating through the PStore object and returning a new hash that was identical to the PStore object. Having the object in hash form made it easy to sort and return the player rankings back to the user.

The main point here is that working with PStore is very simple once you understand the transaction and commit syntax associated with it. If you run into any obstacles that PStore can’t handle and worst comes to worst, just convert the PStore object to a hash, make whatever changes you need to, and re-populate the PStore object once the changes have been made.

Of course, it’s probably much more efficient to use SQL in the first place, but for small hash-like datasets, PStore can work pretty well.