r.table('labels').getAll(owner,{index: 'owner'}).map(function(label){returnr.table('threads').getAll(label('id'),{index: 'labels'}).coerceTo('array').do(function(threads){returnlabel.merge({'total_threads_count': threads.count(),'unread_threads_count': threads.filter(function(thread){returnthread('is_read').not().and(thread('labels').contains(function(label){returnr.expr(spamTrashSent).map(function(row){returnrow('id');}).contains(label);}).not())}).count()});});});