Skip to content

Commit

Permalink
destroy_rev no longer updates MTIME, fixes #1448
Browse files Browse the repository at this point in the history
previous code when destroying a middle revision
the update to PARENTID was making the next revision
into the latest
  • Loading branch information
bylsmad committed May 28, 2023
1 parent f7ecf94 commit f2d368c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/moin/storage/middleware/_tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
HASH_ALGORITHM, CONTENT, COMMENT, LATEST_REVS,
ALL_REVS, NAMESPACE, NAMERE, NAMEPREFIX,
CONTENTTYPE, ITEMTYPE, ITEMLINKS, REV_NUMBER,
PARENTID)
PARENTID, MTIME)

from moin.constants.namespaces import NAMESPACE_USERS

Expand Down Expand Up @@ -100,18 +100,26 @@ def _store_three_revs(self, acl=None):

def test_destroy_revision(self):
item, item_name, revid0, revid1, revid2 = self._store_three_revs()
query = Term(NAME_EXACT, item_name)
metas = {m[REVID]: m for m in flaskg.storage.search_meta(query, idx_name=ALL_REVS)}
rev1_mtime = metas[revid1][MTIME]
# destroy a non-current revision:
item.destroy_revision(revid0)
# check if the revision was destroyed:
item = self.imw[item_name]
query = Term(NAME_EXACT, item_name)
metas = {m[REVID]: m for m in flaskg.storage.search_meta(query, idx_name=ALL_REVS)}
revids = list(metas.keys())
print("after destroy revid0", revids)
assert sorted(revids) == sorted([revid1, revid2])
# validate parent id of remaining revision is updated
assert PARENTID not in metas[revid1]
# validate mtime not updated
assert rev1_mtime == metas[revid1][MTIME]
# validate revid2 is still the current one
metas = {m[REVID]: m for m in flaskg.storage.search_meta(query)}
assert 1 == len(metas)
assert revid2 in metas
# destroy a current revision:
item = self.imw[item_name]
item.destroy_revision(revid2)
# check if the revision was destroyed:
item = self.imw[item_name]
Expand Down
32 changes: 23 additions & 9 deletions src/moin/storage/middleware/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,28 +554,42 @@ def move_index(self):
index_dir, index_dir_tmp = params[0], params_tmp[0]
os.rename(index_dir_tmp, index_dir)

def index_revision(self, meta, content, backend_name, async_=True):
def index_revision(self, meta, content, backend_name, async_=True, force_latest=True):
"""
Index a single revision, add it to all-revs and latest-revs index.
:param meta: metadata dict
:param content: preprocessed (filtered) indexable content
:param async_: if True, use the AsyncWriter, otherwise use normal writer
:param force_latest: True - unconditionally store this rev in LATEST_REVS
False - store in LATEST_REVS if this rev MTIME is most recent
overrides async_ parameter to False
"""
if force_latest:
async_ = False # must wait for storage in ALL_REVS before check for latest
doc = backend_to_index(meta, content, self.schemas[ALL_REVS], self.wikiname, backend_name)
if async_:
writer = AsyncWriter(self.ix[ALL_REVS])
else:
writer = self.ix[ALL_REVS].writer()
with writer as writer:
writer.update_document(**doc) # update, because store_revision() may give us an existing revid
doc = backend_to_index(meta, content, self.schemas[LATEST_REVS], self.wikiname, backend_name)
if async_:
writer = AsyncWriter(self.ix[LATEST_REVS])
if force_latest:
is_latest = True
else:
writer = self.ix[LATEST_REVS].writer()
with writer as writer:
writer.update_document(**doc)
with self.ix[ALL_REVS].searcher() as searcher:
latest_revid = searcher.search(Term(ITEMID, doc[ITEMID]),
sortedby=FieldFacet(MTIME, reverse=True),
limit=1)[0][REVID]
is_latest = (doc[REVID] == latest_revid)
if is_latest:
doc = backend_to_index(meta, content, self.schemas[LATEST_REVS], self.wikiname, backend_name)
if async_:
writer = AsyncWriter(self.ix[LATEST_REVS])
else:
writer = self.ix[LATEST_REVS].writer()
with writer as writer:
writer.update_document(**doc)

def remove_revision(self, revid, async_=True):
"""
Expand Down Expand Up @@ -1240,7 +1254,7 @@ def store_revision(self, meta, data, overwrite=False,
data.seek(0) # rewind file
backend_name, revid = backend.store(meta, data)
meta[REVID] = revid
self.indexer.index_revision(meta, content, backend_name)
self.indexer.index_revision(meta, content, backend_name, force_latest=not overwrite)
if not overwrite:
self._current = get_indexer(self.indexer._document, revid=revid)
if return_rev:
Expand Down Expand Up @@ -1274,7 +1288,7 @@ def destroy_revision(self, revid):
child_meta[PARENTID] = my_parent
else:
del child_meta[PARENTID]
self.store_revision(child_meta, child_rev.data, overwrite=True)
self.store_revision(child_meta, child_rev.data, overwrite=True, trusted=True)

def destroy_all_revisions(self):
"""
Expand Down

0 comments on commit f2d368c

Please sign in to comment.