def build_indices(stream):
    """ Parses the stream of logs and indexes it.
    Args:
        stream, list of string, format <metric_name>|<value>|<coma_separated_tags>
    Returns:
        pair of hashes,
            first hash has the format {tag -> set([line_indices])}
            second hash has the format {line_index -> set([tags])}
    """
    tag_index = {} # tag -> [line_ids]
    line_index = {} # line_id -> [tag]
    for i in range(len(stream)):
        line = stream[i]
        segments = line.split("|")
        tags = segments[2].split(",")
        line_index[i] = set(tags)
        for tag in tags:
            if tag not in tag_index:
                tag_index[tag] = set([])
            tag_index[tag].add(i)
    return tag_index, line_index