Code

more metakit fixes
[roundup.git] / roundup / backends / indexer_rdbms.py
1 ''' This implements the full-text indexer over two RDBMS tables. The first
2 is a mapping of words to occurance IDs. The second maps the IDs to (Class,
3 propname, itemid) instances.
4 '''
5 import re
7 from indexer_dbm import Indexer
9 class Indexer(Indexer):
10     disallows = {'THE':1, 'THIS':1, 'ZZZ':1, 'THAT':1, 'WITH':1}
11     def __init__(self, db):
12         self.db = db
13         self.reindex = 0
15     def close(self):
16         '''close the indexing database'''
17         # just nuke the circular reference
18         self.db = None
19   
20     def force_reindex(self):
21         '''Force a reindexing of the database.  This essentially
22         empties the tables ids and index and sets a flag so
23         that the databases are reindexed'''
24         self.reindex = 1
26     def should_reindex(self):
27         '''returns True if the indexes need to be rebuilt'''
28         return self.reindex
30     def add_text(self, identifier, text, mime_type='text/plain'):
31         ''' "identifier" is  (classname, itemid, property) '''
32         if mime_type != 'text/plain':
33             return
35         # first, find the id of the (classname, itemid, property)
36         a = self.db.arg
37         sql = 'select _textid from __textids where _class=%s and '\
38             '_itemid=%s and _prop=%s'%(a, a, a)
39         self.db.cursor.execute(sql, identifier)
40         r = self.db.cursor.fetchone()
41         if not r:
42             id = self.db.newid('__textids')
43             sql = 'insert into __textids (_textid, _class, _itemid, _prop)'\
44                 ' values (%s, %s, %s, %s)'%(a, a, a, a)
45             self.db.cursor.execute(sql, (id, ) + identifier)
46             self.db.cursor.execute('select max(_textid) from __textids')
47             id = self.db.cursor.fetchone()[0]
48         else:
49             id = int(r[0])
50             # clear out any existing indexed values
51             sql = 'delete from __words where _textid=%s'%a
52             self.db.cursor.execute(sql, (id, ))
54         # ok, find all the words in the text
55         wordlist = re.findall(r'\b\w{2,25}\b', str(text).upper())
56         words = {}
57         for word in wordlist:
58             if not self.disallows.has_key(word):
59                 words[word] = 1
60         words = words.keys()
62         # for each word, add an entry in the db
63         for word in words:
64             # don't dupe
65             sql = 'select * from __words where _word=%s and _textid=%s'%(a, a)
66             self.db.cursor.execute(sql, (word, id))
67             if self.db.cursor.fetchall():
68                 continue
69             sql = 'insert into __words (_word, _textid) values (%s, %s)'%(a, a)
70             self.db.cursor.execute(sql, (word, id))
72     def find(self, wordlist):
73         '''look up all the words in the wordlist.
74         If none are found return an empty dictionary
75         * more rules here
76         '''        
77         l = [word.upper() for word in wordlist if 26 > len(word) > 2]
79         a = ','.join([self.db.arg] * len(l))
80         sql = 'select distinct(_textid) from __words where _word in (%s)'%a
81         self.db.cursor.execute(sql, tuple(l))
82         r = self.db.cursor.fetchall()
83         if not r:
84             return {}
85         a = ','.join([self.db.arg] * len(r))
86         sql = 'select _class, _itemid, _prop from __textids '\
87             'where _textid in (%s)'%a
88         self.db.cursor.execute(sql, tuple([int(id) for (id,) in r]))
89         # self.search_index has the results as {some id: identifier} ...
90         # sigh
91         r = {}
92         k = 0
93         for c,n,p in self.db.cursor.fetchall():
94             key = (str(c), str(n), str(p))
95             r[k] = key
96             k += 1
97         return r
99     def save_index(self):
100         # the normal RDBMS backend transaction mechanisms will handle this
101         pass
103     def rollback(self):
104         # the normal RDBMS backend transaction mechanisms will handle this
105         pass