Skip to content

Commit c048195

Browse files
author
Gordon B. McMillan
committed
Fix (re)indexing & find in back_metakit.
Fix testTransactions in the metakit test in test_db.
1 parent a8abc88 commit c048195

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

roundup/backends/back_metakit.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def rollback(self):
8787
self._db = metakit.storage(self.dbnm, 1)
8888
self.hist = self._db.view('history')
8989
self.tables = self._db.view('tables')
90+
self.indexer.rollback()
9091
self.indexer.datadb = self._db
9192
self.dirty = 0
9293
def clearCache(self):
@@ -492,7 +493,7 @@ def set(self, nodeid, **propvalues):
492493
changes[key] = oldvalue
493494
if hasattr(prop, 'isfilename') and prop.isfilename:
494495
propvalues[key] = os.path.basename(value)
495-
if prop.indexme and value is not None:
496+
if prop.indexme:
496497
self.db.indexer.add_text((self.classname, nodeid, key),
497498
value, 'text/plain')
498499

@@ -680,16 +681,24 @@ def find(self, **propspec):
680681
vws = []
681682
for propname, ids in propspec:
682683
if type(ids) is _STRINGTYPE:
683-
ids = {ids:1}
684+
ids = {int(ids):1}
685+
else:
686+
d = {}
687+
for id in ids.keys():
688+
d[int(id)] = 1
689+
ids = d
684690
prop = self.ruprops[propname]
685691
view = self.getview()
686692
if isinstance(prop, hyperdb.Multilink):
687-
view = view.flatten(getattr(view, propname))
688693
def ff(row, nm=propname, ids=ids):
689-
return ids.has_key(str(row.fid))
694+
sv = getattr(row, nm)
695+
for sr in sv:
696+
if ids.has_key(sr.fid):
697+
return 1
698+
return 0
690699
else:
691700
def ff(row, nm=propname, ids=ids):
692-
return ids.has_key(str(getattr(row, nm)))
701+
return ids.has_key(getattr(row, nm))
693702
ndxview = view.filter(ff)
694703
vws.append(ndxview.unique())
695704

@@ -700,7 +709,7 @@ def ff(row, nm=propname, ids=ids):
700709
ndxview = vws[0]
701710
for v in vws[1:]:
702711
ndxview = ndxview.union(v)
703-
view = view.remapwith(ndxview)
712+
view = self.getview().remapwith(ndxview)
704713
rslt = []
705714
for row in view:
706715
rslt.append(str(row.id))
@@ -1170,12 +1179,13 @@ def __init__(self, db, classname, **properties):
11701179
properties['superseder'] = hyperdb.Multilink(classname)
11711180
Class.__init__(self, db, classname, **properties)
11721181

1173-
CURVERSION = 1
1182+
CURVERSION = 2
11741183

11751184
class Indexer(indexer.Indexer):
11761185
disallows = {'THE':1, 'THIS':1, 'ZZZ':1, 'THAT':1, 'WITH':1}
11771186
def __init__(self, path, datadb):
1178-
self.db = metakit.storage(os.path.join(path, 'index.mk4'), 1)
1187+
self.path = os.path.join(path, 'index.mk4')
1188+
self.db = metakit.storage(self.path, 1)
11791189
self.datadb = datadb
11801190
self.reindex = 0
11811191
v = self.db.view('version')
@@ -1188,7 +1198,7 @@ def __init__(self, path, datadb):
11881198
v[0].vers = CURVERSION
11891199
self.reindex = 1
11901200
if self.reindex:
1191-
self.db.getas('ids[tblid:I,nodeid:I,propid:I]')
1201+
self.db.getas('ids[tblid:I,nodeid:I,propid:I,ignore:I]')
11921202
self.db.getas('index[word:S,hits[pos:I]]')
11931203
self.db.commit()
11941204
self.reindex = 1
@@ -1225,12 +1235,16 @@ def add_text(self, identifier, text, mime_type='text/plain'):
12251235
raise KeyError, "unknown class %r"%classname
12261236
nodeid = int(nodeid)
12271237
propid = self._getpropid(classname, property)
1228-
pos = self.db.view('ids').append(tblid=tblid,nodeid=nodeid,propid=propid)
1238+
ids = self.db.view('ids')
1239+
oldpos = ids.find(tblid=tblid,nodeid=nodeid,propid=propid,ignore=0)
1240+
if oldpos > -1:
1241+
ids[oldpos].ignore = 1
1242+
self.changed = 1
1243+
pos = ids.append(tblid=tblid,nodeid=nodeid,propid=propid)
12291244

1230-
wordlist = re.findall(r'\b\w{3,25}\b', text)
1245+
wordlist = re.findall(r'\b\w{2,25}\b', text.upper())
12311246
words = {}
12321247
for word in wordlist:
1233-
word = word.upper()
12341248
if not self.disallows.has_key(word):
12351249
words[word] = 1
12361250
words = words.keys()
@@ -1239,16 +1253,16 @@ def add_text(self, identifier, text, mime_type='text/plain'):
12391253
for word in words:
12401254
ndx = index.find(word=word)
12411255
if ndx < 0:
1242-
ndx = index.append(word=word)
1243-
hits = index[ndx].hits
1244-
if len(hits)==0 or hits.find(pos=pos) < 0:
1245-
hits.append(pos=pos)
1246-
self.changed = 1
1256+
index.append(word=word)
1257+
ndx = index.find(word=word)
1258+
index[ndx].hits.append(pos=pos)
1259+
self.changed = 1
12471260

12481261
def find(self, wordlist):
12491262
hits = None
12501263
index = self.db.view('index').ordered(1)
12511264
for word in wordlist:
1265+
word = word.upper()
12521266
if not 2 < len(word) < 26:
12531267
continue
12541268
ndx = index.find(word=word)
@@ -1267,12 +1281,18 @@ def find(self, wordlist):
12671281
tbls = self.datadb.view('tables')
12681282
for i in range(len(ids)):
12691283
hit = ids[i]
1270-
classname = tbls[hit.tblid].name
1271-
nodeid = str(hit.nodeid)
1272-
property = self._getpropname(classname, hit.propid)
1273-
rslt[i] = (classname, nodeid, property)
1284+
if not hit.ignore:
1285+
classname = tbls[hit.tblid].name
1286+
nodeid = str(hit.nodeid)
1287+
property = self._getpropname(classname, hit.propid)
1288+
rslt[i] = (classname, nodeid, property)
12741289
return rslt
12751290
def save_index(self):
12761291
if self.changed:
12771292
self.db.commit()
12781293
self.changed = 0
1294+
def rollback(self):
1295+
if self.changed:
1296+
self.db.rollback()
1297+
self.db = metakit.storage(self.path, 1)
1298+
self.changed = 0

test/test_db.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_db.py,v 1.56 2002-09-26 03:04:24 richard Exp $
18+
# $Id: test_db.py,v 1.57 2002-10-02 19:15:46 gmcm Exp $
1919

2020
import unittest, os, shutil, time
2121

@@ -738,15 +738,21 @@ def testTransactions(self):
738738
self.assertNotEqual(num_issues, len(self.db.issue.list()))
739739
self.db.file.create(name="test", type="text/plain", content="hi")
740740
self.db.rollback()
741+
num_files = len(self.db.file.list())
741742
for i in range(10):
742743
self.db.file.create(name="test", type="text/plain",
743744
content="hi %d"%(i))
744745
self.db.commit()
745746
# TODO: would be good to be able to ensure the file is not on disk after
746747
# a rollback...
748+
num_files2 = len(self.db.file.list())
747749
self.assertNotEqual(num_files, num_files2)
748750
self.db.file.create(name="test", type="text/plain", content="hi")
751+
num_rfiles = len(os.listdir(self.db.config.DATABASE + '/files/file/0'))
749752
self.db.rollback()
753+
num_rfiles2 = len(os.listdir(self.db.config.DATABASE + '/files/file/0'))
754+
self.assertEqual(num_files2, len(self.db.file.list()))
755+
self.assertEqual(num_rfiles2, num_rfiles-1)
750756

751757
class metakitReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
752758
def setUp(self):

0 commit comments

Comments
 (0)