'Gets the localpart of the next generated user ID.
Generated user IDs are integers, and we aim for them to be as small as
we can. Unfortunately, it\'s possible some of them are already taken by
existing users, and there may be gaps in the already taken range. This
function returns the start of the first allocatable gap. This is to
avoid the case of ID 10000000 being pre-allocated, so us wasting the
first (and shortest) many generated user IDs.'

@defer.inlineCallbacks
def find_next_generated_user_id_localpart(self):

    def _find_next_generated_user_id(txn):
        txn.execute('SELECT    name    FROM    users')
        rows = self.cursor_to_dict(txn)
        regex = re.compile('^@(\\d+):')
        found = set()
        for r in rows:
            user_id = r['name']
            match = regex.search(user_id)
            if match:
                found.add(int(match.group(1)))
        for i in xrange((len(found) + 1)):
            if (i not in found):
                return i
    defer.returnValue((yield self.runInteraction('find_next_generated_user_id', _find_next_generated_user_id)))
