def rebuild_collection_tree(cursor, ident_hash, history_map):
collection_tree_sql = """<STR_LIT>"""<EOL>tree_insert_sql = """<STR_LIT>"""<EOL>def get_tree():<EOL><INDENT>cursor.execute(collection_tree_sql, (ident_hash,))<EOL>for row in cursor.fetchall():<EOL><INDENT>yield row[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>def insert(fields):<EOL><INDENT>cursor.execute(tree_insert_sql, fields)<EOL>results = cursor.fetchone()[<NUM_LIT:0>]<EOL>return results<EOL><DEDENT>tree = {}  <EOL>children = {}  <EOL>for node in get_tree():<EOL><INDENT>tree[node['<STR_LIT>']] = node<EOL>children.setdefault(node['<STR_LIT>'], [])<EOL>children[node['<STR_LIT>']].append(node['<STR_LIT>'])<EOL><DEDENT>def build_tree(nodeid, parent_id):<EOL><INDENT>data = tree[nodeid]<EOL>data['<STR_LIT>'] = parent_id<EOL>if history_map.get(data['<STR_LIT>']) is not Noneand (data['<STR_LIT>'] or parent_id is None):<EOL><INDENT>data['<STR_LIT>'] = history_map[data['<STR_LIT>']]<EOL><DEDENT>new_nodeid = insert(data)<EOL>for child_nodeid in children.get(nodeid, []):<EOL><INDENT>build_tree(child_nodeid, new_nodeid)<EOL><DEDENT><DEDENT>root_node = children[None][<NUM_LIT:0>]<EOL>build_tree(root_node, None)<EOL>
Create a new tree for the collection based on the old tree but with
    new document ids