This code is the same as in db/builder.cc. Can it be abstracted to it's own function?