1

Improved index generation for dictionary

Former-commit-id: ad93c9a556ac1aea89499a03f5a14a210057585e
This commit is contained in:
Alex Yatskov 2013-11-08 14:29:54 -08:00
parent 988916820a
commit 2e94404b0c

View File

@ -28,19 +28,17 @@ class Dictionary:
def findTerm(self, word, partial=False): def findTerm(self, word, partial=False):
self.requireIndex('Terms', 'expression')
self.requireIndex('Terms', 'reading')
cursor = self.db.cursor() cursor = self.db.cursor()
if not self.hasIndex('TermIndex'):
cursor.execute('CREATE INDEX TermIndex ON Terms(expression, reading)')
self.db.commit()
cursor.execute('SELECT * FROM Terms WHERE expression {0} ? OR reading=?'.format('LIKE' if partial else '='), (word, word)) cursor.execute('SELECT * FROM Terms WHERE expression {0} ? OR reading=?'.format('LIKE' if partial else '='), (word, word))
return cursor.fetchall() return cursor.fetchall()
def findCharacter(self, character): def findCharacter(self, character):
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute('SELECT * FROM Kanji WHERE character=?', character) cursor.execute('SELECT * FROM Kanji WHERE character=? LIMIT 1', character)
return cursor.fetchone() return cursor.fetchone()
@ -60,7 +58,7 @@ class Dictionary:
def findRadicalsByCharacter(self, character): def findRadicalsByCharacter(self, character):
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute('SELECT radicals FROM Radicals WHERE character=?', character) cursor.execute('SELECT radicals FROM Radicals WHERE character=? LIMIT 1', character)
columns = cursor.fetchone() columns = cursor.fetchone()
if columns is None: if columns is None:
@ -80,6 +78,18 @@ class Dictionary:
return map(operator.itemgetter(0), columns) return map(operator.itemgetter(0), columns)
def requireIndex(self, table, column):
name = 'index_{0}_{1}'.format(table, column)
if not self.hasIndex(name):
self.buildIndex(name, table, column)
def buildIndex(self, name, table, column):
cursor = self.db.cursor()
cursor.execute('CREATE INDEX {0} ON {1}({2})'.format(name, table, column))
self.db.commit()
def hasIndex(self, name): def hasIndex(self, name):
if name in self.indices: if name in self.indices:
return True return True