Fix surname index for autocompletion

Fix check tool and start with importgrdb upgrade


svn: r15949
This commit is contained in:
Benny Malengier
2010-10-02 16:01:34 +00:00
parent 0ca48dd811
commit 371205ec70
5 changed files with 172 additions and 34 deletions

View File

@@ -46,7 +46,7 @@ import logging
#
#-------------------------------------------------------------------------
from gen.lib import (MediaObject, Person, Family, Source, Event, Place,
Repository, Note, GenderStats, Researcher)
Repository, Note, GenderStats, Researcher, NameOriginType)
from gen.db.dbconst import *
from gen.utils.callback import Callback
from gen.db import (BsddbBaseCursor, DbReadBase)
@@ -67,6 +67,41 @@ _SIGBASE = ('person', 'family', 'source', 'event',
DBERRS = (db.DBRunRecoveryError, db.DBAccessError,
db.DBPageNotFoundError, db.DBInvalidArgError)
#-------------------------------------------------------------------------
#
# Helper functions
#
#-------------------------------------------------------------------------
def find_surname(key, data):
"""
Creating a surname from raw data of a person, to use for sort and index
"""
return __index_surname(data[3][5])
def find_surname_name(key, data):
"""
Creating a surname from raw name, to use for sort and index
"""
return __index_surname(data[5])
def __index_surname(surn_list):
"""
All non pa/matronymic surnames are used in indexing.
pa/matronymic not as they change for every generation!
"""
if surn_list:
surn = " ".join([x[0] for x in surn_list if not (x[3][0] in [
NameOriginType.PATRONYMIC, NameOriginType.MATRONYMIC]) ])
else:
surn = ""
return str(surn)
#-------------------------------------------------------------------------
#
# class DbBookmarks
#
#-------------------------------------------------------------------------
class DbBookmarks(object):
def __init__(self, default=[]):
self.bookmarks = list(default) # want a copy (not an alias)
@@ -1356,12 +1391,8 @@ class DbBsddbRead(DbReadBase, Callback):
return self.__has_handle(self.source_map, handle)
def __sortbyperson_key(self, person):
surnlist = self.person_map.get(str(person))[3][5]
if surnlist:
surn = " ".join([x[0] for x in surnlist])
else:
surn = ""
return locale.strxfrm(surn)
return locale.strxfrm(find_surname(str(person),
self.person_map.get(str(person))))
def __sortbyplace(self, first, second):
return locale.strcoll(self.place_map.get(str(first))[2],

View File

@@ -25,17 +25,17 @@ from __future__ import with_statement
"""
methods to upgrade a database from version 13 to current version
"""
from bsddb import db
from gen.db import BSDDBTxn
from gen.lib.nameorigintype import NameOriginType
from gen.db.write import _mkname, SURNAMES
def gramps_upgrade_15(self):
"""Upgrade database from version 14 to 15. This upgrade adds:
* tagging
* surname list
"""
length = len(self.person_map)
length = len(self.person_map)+10
self.set_total(length)
# ---------------------------------
@@ -94,7 +94,13 @@ def gramps_upgrade_15(self):
)
with BSDDBTxn(self.env, self.person_map) as txn:
txn.put(str(handle), new_person)
self.update()
self.update(length)
#surname is now different, remove secondary index with names
_db = db.DB(self.env)
try:
_db.remove(_mkname(self.full_name, SURNAMES), SURNAMES)
except db.DBNoSuchFileError:
pass
# Bump up database version. Separate transaction to save metadata.
with BSDDBTxn(self.env, self.metadata) as txn:

View File

@@ -52,7 +52,7 @@ from gen.lib import (GenderStats, Person, Family, Event, Place, Source,
MediaObject, Repository, Note)
from gen.db import (DbBsddbRead, DbWriteBase, BSDDBTxn,
DbTxn, BsddbBaseCursor, DbVersionError,
DbUpgradeRequiredError,
DbUpgradeRequiredError, find_surname, find_surname_name,
DbUndoBSDDB as DbUndo)
from gen.db.dbconst import *
from gen.utils.callback import Callback
@@ -120,10 +120,7 @@ KEY_TO_CLASS_MAP = {PERSON_KEY: Person.__name__,
#
# Helper functions
#
#-------------------------------------------------------------------------
def find_surname(key, data):
return str(data[3][5])
#-------------------------------------------------------------------------
def find_idmap(key, data):
return str(data[1])
@@ -1315,7 +1312,8 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
"""
Build surname list for use in autocompletion
"""
self.surname_list = sorted(map(unicode, set(self.surnames.keys())), key=locale.strxfrm)
self.surname_list = sorted(map(unicode, set(self.surnames.keys())),
key=locale.strxfrm)
def add_to_surname_list(self, person, batch_transaction):
"""
@@ -1323,7 +1321,8 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
"""
if batch_transaction:
return
name = unicode(person.get_primary_name().get_surname())
name = unicode(find_surname_name(person.handle,
person.get_primary_name().serialize()))
i = bisect.bisect(self.surname_list, name)
if 0 < i <= len(self.surname_list):
if self.surname_list[i-1] != name:
@@ -1340,7 +1339,8 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
If not then we need to remove the name from the list.
The function must be overridden in the derived class.
"""
name = str(person.get_primary_name().get_surname())
name = str(find_surname_name(person.handle,
person.get_primary_name().serialize()))
try:
cursor = self.surnames.cursor(txn=self.txn)
cursor.set(name)
@@ -1400,7 +1400,10 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
self.genderStats.count_person(person)
# Update surname list if necessary
if (old_person.primary_name.surname !=person.primary_name.surname):
if (find_surname_name(old_person.handle,
old_person.primary_name.serialize()) !=
find_surname_name(person.handle,
person.primary_name.serialize())):
self.remove_from_surname_list(old_person)
self.add_to_surname_list(person, transaction.batch)
else: