Skip to content

Commit

Permalink
Merge pull request AppScale#3179 from cdonati/cleared-kinds
Browse files Browse the repository at this point in the history
Check if a kind has at least one entity
  • Loading branch information
obino authored Oct 11, 2019
2 parents a110f2d + ff694af commit b00cde4
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions AppDB/appscale/datastore/fdb/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,9 @@ def next_page(self):


class KindIterator(object):
def __init__(self, tr, project_dir, namespace):
def __init__(self, tr, tornado_fdb, project_dir, namespace):
self._tr = tr
self._tornado_fdb = tornado_fdb
self._project_dir = project_dir
self._namespace = namespace
self._done = False
Expand All @@ -375,13 +376,39 @@ def next_page(self):
ns_dir = self._project_dir.open(
self._tr, (KindIndex.DIR_NAME, self._namespace))
kinds = ns_dir.list(self._tr)
populated_kinds = [
kind for kind, populated in zip(
kinds, (yield [self._populated(ns_dir, kind) for kind in kinds]))
if populated]

results = [IndexEntry(self._project_dir.get_path()[-1], self._namespace,
(u'__kind__', kind), None, None)
for kind in kinds]
for kind in populated_kinds]

self._done = True
raise gen.Return((results, False))

@gen.coroutine
def _populated(self, ns_dir, kind):
""" Checks if at least one entity exists for a given kind. """
kind_dir = ns_dir.open(self._tr, (kind,))
kind_index = KindIndex(kind_dir)
# TODO: Check if the presence of stat entities should mark a kind as being
# populated.
index_slice = kind_index.get_slice(())
# This query is reversed to increase the likelihood of getting a relevant
# (not marked for GC) entry.
iterator = IndexIterator(self._tr, self._tornado_fdb, kind_index,
index_slice, fetch_limit=1, reverse=True,
snapshot=True)
while True:
results, more_results = yield iterator.next_page()
if results:
raise gen.Return(True)

if not more_results:
raise gen.Return(False)


class MergeJoinIterator(object):
"""
Expand Down Expand Up @@ -1142,7 +1169,8 @@ def get_iterator(self, tr, query, read_versionstamp=None):
raise gen.Return(NamespaceIterator(tr, project_dir))
elif query.has_kind() and query.kind() == u'__kind__':
project_dir = yield self._directory_cache.get(tr, (project_id,))
raise gen.Return(KindIterator(tr, project_dir, namespace))
raise gen.Return(KindIterator(tr, self._tornado_fdb, project_dir,
namespace))

index = yield self._get_perfect_index(tr, query)
reverse = get_scan_direction(query, index) == Query_Order.DESCENDING
Expand Down

0 comments on commit b00cde4

Please sign in to comment.