Refactor datastore

DO NOT MERGE.

This is a refactoring of the datastore layers to accommodate several datastore implementations (e.g. couchbase) and to reduce redundancy in the datastore code.