'Optimized version of add_dataset above that minimizes database
interactions when adding many datasets to history at once under
certain circumstances.'

def __add_datasets_optimized(self, datasets, genome_build=None):

    n = len(datasets)
    base_hid = self._next_hid(n=n)
    set_genome = (genome_build not in [None, '?'])
    for (i, dataset) in enumerate(datasets):
        dataset.hid = (base_hid + i)
        dataset.history = self
        if set_genome:
            self.genome_build = genome_build
    for dataset in datasets:
        dataset.history_id = self.id
    return datasets
