我有一个使用 AVL 树快速搜索 IP 地址的大型系统:
struct avl_node
{
struct avl_node *left;
struct avl_node *right;
...
void *info; /* point to nhlfe_entry describing nexthop */
}
struct nhlfe_entry
{
u_int32_t nhlfe_ix;
u_char opcode;
...
struct nhlfe_key key;
}
/* defines a search key. */
struct nhlfe_key
{
struct in_addr nh_addr;
u_int32_t oif_ix;
u_int32_t out_label;
}
所以搜索基于'struct nhlfe_key',即AVL树中的比较器函数如下所示:
static int
mpls_cmp_nhlfe_ipv4_key (void *data1, void* data2)
{
struct nhlfe_entry *nh1, *nh2;
struct nhlfe_key *key1, *key2;
int ret;
nh1 = (struct nhlfe_entry *) data1;
nh2 = (struct nhlfe_entry *) data2;
key1 = (struct nhlfe_key *) nh1->nkey;
key2 = (struct nhlfe_key *) nh2->nkey;
ret = memcmp (&key1->nh_addr, &key2->nh_addr, sizeof (struct in_addr));
if (ret != 0)
return ret;
if (key1->oif_ix > key2->oif_ix)
return 1;
else if (key1->oif_ix < key2->oif_ix)
return -1;
if (key1->out_label > key2->out_label)
return 1;
else if (key1->out_label < key2->out_label)
return -1;
return 0;
}
现在,我要做的是添加对多个下一跳的支持,即我在 nhlfe_entry 中添加一个链表:
struct nhlfe_entry
{
u_int32_t nhlfe_ix;
u_char opcode;
...
struct list *nhkey_list;
}
每个'struct list'都是struct listnode,它嵌入'void *data'指针到调用者的私有数据,这就是'struct nhlfe_key'。
所以我的问题是——如何基于列表中的多个元素生成密钥以存储/搜索树中的节点(因为否则现在在引入列表之后,将不可能仅基于一个 下一跳地址)。此外,他们同样的问题适用于搜索。
另外,在列表中添加新节点后,是否需要重新构建树,因为我认为此操作会更改键,因此树可能会变得不平衡?(或者正确实现的AVL树自然不需要重建?)
我正在考虑在每个列表节点上生成 CRC,然后进行总结。这可以保证密钥的唯一性吗?(缺点是每当我添加/删除列表节点时,我必须重新生成密钥,从树中删除节点并使用新密钥重新添加)。
谢谢!