New insert player function for HHC import

Added getSqlPlayerIDs to Database.py - returns a hash {playername:sqlid}

Function uses a caching hash in Database.py to reduce the number of round trips to the database needed just to fetch the player ids for later inserts.

Need to do a performance comparison on a larger import.
This commit is contained in:
Worros 2009-08-02 12:19:33 +08:00
parent 40a0fe428b
commit ff2cca361c
3 changed files with 45 additions and 2 deletions

View File

@ -182,6 +182,9 @@ class Database:
else:
self.sql = sql
self.pcache = None # PlayerId cache
self.cachemiss = 0 # Delete me later - using to count player cache misses
# config while trying out new hudcache mechanism
self.use_date_in_hudcache = True
@ -981,6 +984,30 @@ class Database:
print "Error during fdb.lock_for_insert:", str(sys.exc_value)
#end def lock_for_insert
def getSqlPlayerIDs(self, pnames, siteid):
result = {}
if(self.pcache == None):
self.pcache = LambdaDict(lambda key:self.insertPlayer(key, siteid))
for player in pnames:
result[player] = self.pcache[player]
return result
def insertPlayer(self, name, site_id):
self.cachemiss += 1
result = None
c = self.get_cursor()
c.execute ("SELECT id FROM Players WHERE name=%s", (name,))
tmp=c.fetchall()
if (len(tmp)==0): #new player
c.execute ("INSERT INTO Players (name, siteId) VALUES (%s, %s)", (name, site_id))
c.execute ("SELECT id FROM Players WHERE name=%s", (name,))
tmp=c.fetchall()
#print "recognisePlayerIDs, names[i]:",names[i],"tmp:",tmp
print "DEBUG: cache misses: %s" %self.cachemiss
return tmp[0][0]
def store_the_hand(self, h):
"""Take a HandToWrite object and store it in the db"""
@ -1797,3 +1824,17 @@ if __name__=="__main__":
print "press enter to continue"
sys.stdin.readline()
#Code borrowed from http://push.cx/2008/caching-dictionaries-in-python-vs-ruby
class LambdaDict(dict):
def __init__(self, l):
super(LambdaDict, self).__init__()
self.l = l
def __getitem__(self, key):
if key in self:
return self.get(key)
else:
self.__setitem__(key, self.l(key))
return self.get(key)

View File

@ -40,10 +40,12 @@ class Hand(object):
LCS = {'H':'h', 'D':'d', 'C':'c', 'S':'s'}
SYMBOL = {'USD': '$', 'EUR': u'$', 'T$': '', 'play': ''}
MS = {'horse' : 'HORSE', '8game' : '8-Game', 'hose' : 'HOSE', 'ha': 'HA'}
SITEIDS = {'Fulltilt':1, 'PokerStars':2, 'Everleaf':3, 'Win2day':4, 'OnGame':5, 'UltimateBet':6, 'Betfair':7}
def __init__(self, sitename, gametype, handText, builtFrom = "HHC"):
self.sitename = sitename
self.siteId = self.SITEIDS[sitename]
self.stats = DerivedStats.DerivedStats(self)
self.gametype = gametype
self.starttime = 0

View File

@ -401,8 +401,8 @@ class Importer:
self.pos_in_file[file] = hhc.getLastCharacterRead()
for hand in handlist:
hand.prepInsert()
hand.insert()
#hand.prepInsert()
hand.insert(self.database)
else:
# conversion didn't work
# TODO: appropriate response?