vimt/MaxMind-DB-Writer-python

Subnet inserts

Closed this issue · 1 comments

Hey, thanks for this great package. I made an adaption to add subnet inserts, instead of passing them. Inserts always overwrite the previous data record. The guts is a change on gts_merger/writer.py:458

            previous_node = None
            supernet_leaf = None  # Tracks whether we are inserting into a subnet
            for (index, ip_bit) in enumerate(bits[:-1]):
                previous_node = current_node
                current_node = previous_node.get_or_create(ip_bit)
                next_bit = bits[index + 1]

                if isinstance(current_node, SearchTreeLeaf):
                    logger.info(f"Inserting {cidr} into subnet of {current_node.value['network']}")
                    supernet_leaf = current_node
                    current_node = SearchTreeNode()
                    previous_node[ip_bit] = current_node

                if supernet_leaf:
                    # Insert supernet information on each inverse bit of the current subnet
                    current_node[1 - next_bit] = supernet_leaf

Are you interested to add this to your package? Then I will test it with your examples. I also added sibling-merging, so I'd need to clean my code first.

vimt commented

Hi, thanks for your suggestion. It's a good idea to handle subnet duplication.
I will replace the code in new branch.