Programmable quantum memory in atomic ensembles