'Generate the subset of accessible nodes which match the filter'

def __subset(self):

    theset = self.theset
    roots = set()
    subset = {}
    resource = current.s3db.resource(self.tablename, filter=self.filter)
    pkey = self.pkey
    rows = resource.select([pkey.name], as_rows=True)
    if rows:
        key = str(pkey)
        if self.leafonly:
            ids = set()
            for row in rows:
                node_id = row[key]
                if ((node_id in theset) and (not theset[node_id]['s'])):
                    ids.add(node_id)
        else:
            ids = set((row[key] for row in rows))
        while ids:
            node_id = ids.pop()
            if (node_id in subset):
                continue
            node = theset.get(node_id)
            if (not node):
                continue
            parent_id = node['p']
            if (parent_id and (parent_id not in subset)):
                ids.add(parent_id)
            elif (not parent_id):
                roots.add(node_id)
            subset[node_id] = dict(node)
        for node in subset.values():
            node['s'] = set((node_id for node_id in node['s'] if (node_id in subset)))
    self.__roots = roots
    self.__nodes = subset
    return
