CBD-648: Refactor insertion algorithm

This commit prepares for the deletion algorithm. The refactoring
makes it easier to add the deletion algorithm as it has much in
common with the insertion algorithm.

Change-Id: Ie2ea65085b3e493679feb27009f97c24e54adba7
Reviewed-on: http://review.couchbase.org/22337
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>