Reworked backend Cursors; don't emit changes when changing in batch mode

This commit is contained in:
Doug Blank 2015-05-14 09:07:10 -04:00
parent 6da7f78cba
commit 1118ce449c
4 changed files with 121 additions and 2124 deletions

View File

File diff suppressed because it is too large Load Diff

View File

@ -130,27 +130,33 @@ class Cursor(object):
def __init__(self, map, func): def __init__(self, map, func):
self.map = map self.map = map
self.func = func self.func = func
self._iter = self.__iter__()
def __enter__(self): def __enter__(self):
return self return self
def __iter__(self): def __iter__(self):
return self.__next__()
def __next__(self):
for item in self.map.keys(): for item in self.map.keys():
yield (bytes(item, "utf-8"), self.func(item)) yield (bytes(item, "utf-8"), self.func(item))
def __next__(self):
try:
return self._iter.__next__()
except StopIteration:
return None
def __exit__(self, *args, **kwargs): def __exit__(self, *args, **kwargs):
pass pass
def iter(self): def iter(self):
for item in self.map.keys(): for item in self.map.keys():
yield (bytes(item, "utf-8"), self.func(item)) yield (bytes(item, "utf-8"), self.func(item))
yield None
def first(self): def first(self):
self._iter = self.__iter__() self._iter = self.__iter__()
return self.next() try:
return next(self._iter)
except:
return
def next(self): def next(self):
try: try:
return next(self._iter) return next(self._iter)
except: except:
return None return
def close(self): def close(self):
pass pass
@ -1099,73 +1105,83 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback):
return obj.handle return obj.handle
def commit_person(self, person, trans, change_time=None): def commit_person(self, person, trans, change_time=None):
if person.handle in self.person_map: if not trans.batch:
self.emit("person-update", ([person.handle],)) if person.handle in self.person_map:
else: self.emit("person-update", ([person.handle],))
self.emit("person-add", ([person.handle],)) else:
self.emit("person-add", ([person.handle],))
self.person_map[person.handle] = person self.person_map[person.handle] = person
def commit_family(self, family, trans, change_time=None): def commit_family(self, family, trans, change_time=None):
if family.handle in self.family_map: if not trans.batch:
self.emit("family-update", ([family.handle],)) if family.handle in self.family_map:
else: self.emit("family-update", ([family.handle],))
self.emit("family-add", ([family.handle],)) else:
self.emit("family-add", ([family.handle],))
self.family_map[family.handle] = family self.family_map[family.handle] = family
def commit_citation(self, citation, trans, change_time=None): def commit_citation(self, citation, trans, change_time=None):
if citation.handle in self.citation_map: if not trans.batch:
self.emit("citation-update", ([citation.handle],)) if citation.handle in self.citation_map:
else: self.emit("citation-update", ([citation.handle],))
self.emit("citation-add", ([citation.handle],)) else:
self.emit("citation-add", ([citation.handle],))
self.citation_map[citation.handle] = citation self.citation_map[citation.handle] = citation
def commit_source(self, source, trans, change_time=None): def commit_source(self, source, trans, change_time=None):
if source.handle in self.source_map: if not trans.batch:
self.emit("source-update", ([source.handle],)) if source.handle in self.source_map:
else: self.emit("source-update", ([source.handle],))
self.emit("source-add", ([source.handle],)) else:
self.emit("source-add", ([source.handle],))
self.source_map[source.handle] = source self.source_map[source.handle] = source
def commit_repository(self, repository, trans, change_time=None): def commit_repository(self, repository, trans, change_time=None):
if repository.handle in self.repository_map: if not trans.batch:
self.emit("repository-update", ([repository.handle],)) if repository.handle in self.repository_map:
else: self.emit("repository-update", ([repository.handle],))
self.emit("repository-add", ([repository.handle],)) else:
self.emit("repository-add", ([repository.handle],))
self.repository_map[repository.handle] = repository self.repository_map[repository.handle] = repository
def commit_note(self, note, trans, change_time=None): def commit_note(self, note, trans, change_time=None):
if note.handle in self.note_map: if not trans.batch:
self.emit("note-update", ([note.handle],)) if note.handle in self.note_map:
else: self.emit("note-update", ([note.handle],))
self.emit("note-add", ([note.handle],)) else:
self.emit("note-add", ([note.handle],))
self.note_map[note.handle] = note self.note_map[note.handle] = note
def commit_place(self, place, trans, change_time=None): def commit_place(self, place, trans, change_time=None):
if place.handle in self.place_map: if not trans.batch:
self.emit("place-update", ([place.handle],)) if place.handle in self.place_map:
else: self.emit("place-update", ([place.handle],))
self.emit("place-add", ([place.handle],)) else:
self.emit("place-add", ([place.handle],))
self.place_map[place.handle] = place self.place_map[place.handle] = place
def commit_event(self, event, trans, change_time=None): def commit_event(self, event, trans, change_time=None):
if event.handle in self.event_map: if not trans.batch:
self.emit("event-update", ([event.handle],)) if event.handle in self.event_map:
else: self.emit("event-update", ([event.handle],))
self.emit("event-add", ([event.handle],)) else:
self.emit("event-add", ([event.handle],))
self.event_map[event.handle] = event self.event_map[event.handle] = event
def commit_tag(self, tag, trans, change_time=None): def commit_tag(self, tag, trans, change_time=None):
if tag.handle in self.tag_map: if not trans.batch:
self.emit("tag-update", ([tag.handle],)) if tag.handle in self.tag_map:
else: self.emit("tag-update", ([tag.handle],))
self.emit("tag-add", ([tag.handle],)) else:
self.emit("tag-add", ([tag.handle],))
self.tag_map[tag.handle] = tag self.tag_map[tag.handle] = tag
def commit_media_object(self, media, transaction, change_time=None): def commit_media_object(self, media, transaction, change_time=None):
if media.handle in self.media_map: if not trans.batch:
self.emit("media-update", ([media.handle],)) if media.handle in self.media_map:
else: self.emit("media-update", ([media.handle],))
self.emit("media-add", ([media.handle],)) else:
self.emit("media-add", ([media.handle],))
self.media_map[media.handle] = media self.media_map[media.handle] = media
def get_gramps_ids(self, obj_key): def get_gramps_ids(self, obj_key):

View File

@ -127,13 +127,17 @@ class Cursor(object):
def __init__(self, model, func): def __init__(self, model, func):
self.model = model self.model = model
self.func = func self.func = func
self._iter = self.__iter__()
def __enter__(self): def __enter__(self):
return self return self
def __iter__(self): def __iter__(self):
return self.__next__()
def __next__(self):
for item in self.model.all(): for item in self.model.all():
yield (bytes(item.handle, "utf-8"), self.func(item.handle)) yield (bytes(item.handle, "utf-8"), self.func(item.handle))
def __next__(self):
try:
return self._iter.__next__()
except StopIteration:
return None
def __exit__(self, *args, **kwargs): def __exit__(self, *args, **kwargs):
pass pass
def iter(self): def iter(self):
@ -142,7 +146,10 @@ class Cursor(object):
yield None yield None
def first(self): def first(self):
self._iter = self.__iter__() self._iter = self.__iter__()
return self.next() try:
return next(self._iter)
except:
return
def next(self): def next(self):
try: try:
return next(self._iter) return next(self._iter)
@ -1494,10 +1501,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_person(person.serialize()) self.dji.add_person(person.serialize())
self.dji.add_person_detail(person.serialize()) self.dji.add_person_detail(person.serialize())
if items.count() > 0: if not trans.batch:
self.emit("person-update", ([person.handle],)) if items.count() > 0:
else: self.emit("person-update", ([person.handle],))
self.emit("person-add", ([person.handle],)) else:
self.emit("person-add", ([person.handle],))
def commit_family(self, family, trans, change_time=None): def commit_family(self, family, trans, change_time=None):
if self.use_import_cache: if self.use_import_cache:
@ -1509,10 +1517,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_family(family.serialize()) self.dji.add_family(family.serialize())
self.dji.add_family_detail(family.serialize()) self.dji.add_family_detail(family.serialize())
if items.count() > 0: if not trans.batch:
self.emit("family-update", ([family.handle],)) if items.count() > 0:
else: self.emit("family-update", ([family.handle],))
self.emit("family-add", ([family.handle],)) else:
self.emit("family-add", ([family.handle],))
def commit_citation(self, citation, trans, change_time=None): def commit_citation(self, citation, trans, change_time=None):
if self.use_import_cache: if self.use_import_cache:
@ -1524,10 +1533,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_citation(citation.serialize()) self.dji.add_citation(citation.serialize())
self.dji.add_citation_detail(citation.serialize()) self.dji.add_citation_detail(citation.serialize())
if items.count() > 0: if not trans.batch:
self.emit("citation-update", ([citation.handle],)) if items.count() > 0:
else: self.emit("citation-update", ([citation.handle],))
self.emit("citation-add", ([citation.handle],)) else:
self.emit("citation-add", ([citation.handle],))
def commit_source(self, source, trans, change_time=None): def commit_source(self, source, trans, change_time=None):
if self.use_import_cache: if self.use_import_cache:
@ -1539,10 +1549,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_source(source.serialize()) self.dji.add_source(source.serialize())
self.dji.add_source_detail(source.serialize()) self.dji.add_source_detail(source.serialize())
if items.count() > 0: if not trans.batch:
self.emit("source-update", ([source.handle],)) if items.count() > 0:
else: self.emit("source-update", ([source.handle],))
self.emit("source-add", ([source.handle],)) else:
self.emit("source-add", ([source.handle],))
def commit_repository(self, repository, trans, change_time=None): def commit_repository(self, repository, trans, change_time=None):
if self.use_import_cache: if self.use_import_cache:
@ -1554,10 +1565,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_repository(repository.serialize()) self.dji.add_repository(repository.serialize())
self.dji.add_repository_detail(repository.serialize()) self.dji.add_repository_detail(repository.serialize())
if items.count() > 0: if not trans.batch:
self.emit("repository-update", ([repository.handle],)) if items.count() > 0:
else: self.emit("repository-update", ([repository.handle],))
self.emit("repository-add", ([repository.handle],)) else:
self.emit("repository-add", ([repository.handle],))
def commit_note(self, note, trans, change_time=None): def commit_note(self, note, trans, change_time=None):
if self.use_import_cache: if self.use_import_cache:
@ -1569,10 +1581,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_note(note.serialize()) self.dji.add_note(note.serialize())
self.dji.add_note_detail(note.serialize()) self.dji.add_note_detail(note.serialize())
if items.count() > 0: if not trans.batch:
self.emit("note-update", ([note.handle],)) if items.count() > 0:
else: self.emit("note-update", ([note.handle],))
self.emit("note-add", ([note.handle],)) else:
self.emit("note-add", ([note.handle],))
def commit_place(self, place, trans, change_time=None): def commit_place(self, place, trans, change_time=None):
if self.use_import_cache: if self.use_import_cache:
@ -1584,10 +1597,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_place(place.serialize()) self.dji.add_place(place.serialize())
self.dji.add_place_detail(place.serialize()) self.dji.add_place_detail(place.serialize())
if items.count() > 0: if not trans.batch:
self.emit("place-update", ([place.handle],)) if items.count() > 0:
else: self.emit("place-update", ([place.handle],))
self.emit("place-add", ([place.handle],)) else:
self.emit("place-add", ([place.handle],))
def commit_event(self, event, trans, change_time=None): def commit_event(self, event, trans, change_time=None):
if self.use_import_cache: if self.use_import_cache:
@ -1599,10 +1613,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_event(event.serialize()) self.dji.add_event(event.serialize())
self.dji.add_event_detail(event.serialize()) self.dji.add_event_detail(event.serialize())
if items.count() > 0: if not trans.batch:
self.emit("event-update", ([event.handle],)) if items.count() > 0:
else: self.emit("event-update", ([event.handle],))
self.emit("event-add", ([event.handle],)) else:
self.emit("event-add", ([event.handle],))
def commit_tag(self, tag, trans, change_time=None): def commit_tag(self, tag, trans, change_time=None):
if self.use_import_cache: if self.use_import_cache:
@ -1614,10 +1629,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_tag(tag.serialize()) self.dji.add_tag(tag.serialize())
self.dji.add_tag_detail(tag.serialize()) self.dji.add_tag_detail(tag.serialize())
if items.count() > 0: if not trans.batch:
self.emit("tag-update", ([tag.handle],)) if items.count() > 0:
else: self.emit("tag-update", ([tag.handle],))
self.emit("tag-add", ([tag.handle],)) else:
self.emit("tag-add", ([tag.handle],))
def commit_media_object(self, media, transaction, change_time=None): def commit_media_object(self, media, transaction, change_time=None):
""" """
@ -1633,10 +1649,11 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback):
items[0].delete() items[0].delete()
self.dji.add_media(media.serialize()) self.dji.add_media(media.serialize())
self.dji.add_media_detail(media.serialize()) self.dji.add_media_detail(media.serialize())
if items.count() > 0: if not trans.batch:
self.emit("media-update", ([media.handle],)) if items.count() > 0:
else: self.emit("media-update", ([media.handle],))
self.emit("media-add", ([media.handle],)) else:
self.emit("media-add", ([media.handle],))
def get_gramps_ids(self, obj_key): def get_gramps_ids(self, obj_key):
key2table = { key2table = {