def thd(conn, no_recurse=False, identifier=identifier): tbl = self.db.model.users tbl_info = self.db.model.users_info self.checkLength(tbl.c.identifier, identifier) self.checkLength(tbl_info.c.attr_type, attr_type) self.checkLength(tbl_info.c.attr_data, attr_data) # try to find the user q = sa.select([tbl_info.c.uid], whereclause=and_(tbl_info.c.attr_type == attr_type, tbl_info.c.attr_data == attr_data)) rows = conn.execute(q).fetchall() if rows: return rows[0].uid if _race_hook is not None: _race_hook(conn) # try to do both of these inserts in a transaction, so that both # the new user and the corresponding attributes appear at the same # time from the perspective of other masters. transaction = conn.begin() inserted_user = False try: r = conn.execute(tbl.insert(), dict(identifier=identifier)) uid = r.inserted_primary_key[0] inserted_user = True conn.execute(tbl_info.insert(), dict(uid=uid, attr_type=attr_type, attr_data=attr_data)) transaction.commit() except (sa.exc.IntegrityError, sa.exc.ProgrammingError): transaction.rollback() # try it all over again, in case there was an overlapping, # identical call to findUserByAttr. If the identifier # collided, we'll try again indefinitely; otherwise, only once. if no_recurse: raise # if we failed to insert the user, then it's because the # identifier wasn't unique if not inserted_user: identifier = identifiers.incrementIdentifier( 256, identifier) else: no_recurse = True return thd(conn, no_recurse=no_recurse, identifier=identifier) return uid
def addLog(self, stepid, name, type): slug = identifiers.forceIdentifier(50, name) while True: try: logid = yield self.master.db.logs.addLog( stepid=stepid, name=name, slug=slug, type=type) except KeyError: slug = identifiers.incrementIdentifier(50, slug) continue self.generateEvent(logid, "new") return logid
def thd(conn, no_recurse=False, identifier=identifier): tbl = self.db.model.users tbl_info = self.db.model.users_info self.checkLength(tbl.c.identifier, identifier) self.checkLength(tbl_info.c.attr_type, attr_type) self.checkLength(tbl_info.c.attr_data, attr_data) # try to find the user q = sa.select([tbl_info.c.uid], whereclause=and_(tbl_info.c.attr_type == attr_type, tbl_info.c.attr_data == attr_data)) rows = conn.execute(q).fetchall() if rows: return rows[0].uid _race_hook and _race_hook(conn) # try to do both of these inserts in a transaction, so that both # the new user and the corresponding attributes appear at the same # time from the perspective of other masters. transaction = conn.begin() inserted_user = False try: r = conn.execute(tbl.insert(), dict(identifier=identifier)) uid = r.inserted_primary_key[0] inserted_user = True conn.execute(tbl_info.insert(), dict(uid=uid, attr_type=attr_type, attr_data=attr_data)) transaction.commit() except (sa.exc.IntegrityError, sa.exc.ProgrammingError): transaction.rollback() # try it all over again, in case there was an overlapping, # identical call to findUserByAttr. If the identifier # collided, we'll try again indefinitely; otherwise, only once. if no_recurse: raise # if we failed to insert the user, then it's because the # identifier wasn't unique if not inserted_user: identifier = identifiers.incrementIdentifier( 256, identifier) else: no_recurse = True return thd(conn, no_recurse=no_recurse, identifier=identifier) return uid
def addLog(self, stepid, name, type): slug = identifiers.forceIdentifier(50, name) while True: try: logid = yield self.master.db.logs.addLog( stepid=stepid, name=name, slug=slug, type=type) except KeyError: slug = identifiers.incrementIdentifier(50, slug) continue self.generateEvent(logid, "new") defer.returnValue(logid)
def test_incrementIdentifier_add_digits_too_long(self): self.assertEqualUnicode(identifiers.incrementIdentifier(6, u'aaa_99'), u'aa_100')
def test_incrementIdentifier_single_digit(self): self.assertEqualUnicode(identifiers.incrementIdentifier(100, u'aaa_2'), u'aaa_3')
def test_incrementIdentifier_simple_too_long(self): self.assertEqualUnicode(identifiers.incrementIdentifier(4, u'aaa'), u'aa_2')
def test_incrementIdentifier_simple(self): self.assertEqualUnicode(identifiers.incrementIdentifier(100, u'aaa'), u'aaa_2')
def test_incrementIdentifier_add_digits_out_of_space(self): self.assertRaises( ValueError, lambda: identifiers.incrementIdentifier(6, u'_99999'))
def test_incrementIdentifier_add_digits_out_of_space(self): with self.assertRaises(ValueError): identifiers.incrementIdentifier(6, '_99999')
def test_incrementIdentifier_add_digits(self): self.assertEqualUnicode(identifiers.incrementIdentifier(100, 'aaa_99'), 'aaa_100')
def test_incrementIdentifier_add_digits_too_long(self): self.assertEqualUnicode( identifiers.incrementIdentifier(6, u'aaa_99'), u'aa_100')
def test_incrementIdentifier_single_digit(self): self.assertEqualUnicode( identifiers.incrementIdentifier(100, u'aaa_2'), u'aaa_3')
def test_incrementIdentifier_simple_too_long(self): self.assertEqualUnicode( identifiers.incrementIdentifier(4, u'aaa'), u'aa_2')
def test_incrementIdentifier_simple(self): self.assertEqualUnicode( identifiers.incrementIdentifier(100, u'aaa'), u'aaa_2')
def test_incrementIdentifier_add_digits_out_of_space(self): self.assertRaises(ValueError, lambda: identifiers.incrementIdentifier(6, u'_99999'))