'Given a state group, count how many hops there are in the tree.
This is used to ensure the delta chains don\'t get too long.'

def _count_state_group_hops_txn(self, txn, state_group):

    if isinstance(self.database_engine, PostgresEngine):
        sql = '\n                                                                WITH    RECURSIVE    state(state_group)    AS    (\n                                                                                VALUES(?::bigint)\n                                                                                UNION    ALL\n                                                                                SELECT    prev_state_group    FROM    state_group_edges    e,    state    s\n                                                                                WHERE    s.state_group    =    e.state_group\n                                                                )\n                                                                SELECT    count(*)    FROM    state;\n                                                '
        txn.execute(sql, (state_group,))
        row = txn.fetchone()
        if (row and row[0]):
            return row[0]
        else:
            return 0
    else:
        next_group = state_group
        count = 0
        while next_group:
            next_group = self._simple_select_one_onecol_txn(txn, table='state_group_edges', keyvalues={'state_group': next_group}, retcol='prev_state_group', allow_none=True)
            if next_group:
                count += 1
        return count
