douwei3172 2018-07-03 13:13
浏览 183
已采纳

Kadmelia K-Bucket算法中的路由表,而无需遍历节点ID中的每个位

Context

I'm trying to implement Kadmelia's K-Bucket algorithm to keep track of closer nodes. I understand in theory how the algorithm works

  1. When a new node is added
  2. If the bucket size hasn't exceeded k (bucket size) we add it to the current bucket
  3. else we split the bucket and divide the contacts in the parent bucket by looping over each bit and split them into two buckets.
  4. This also implies that for a given node there would be k * 8 buckets (or lists)

Question

The question is in reference to the approach adopted in this example http://blog.notdot.net/2009/11/Implementing-a-DHT-in-Go-part-1

Given that we have defined a Node as a byte array of length 20



const IdLength = 20
type NodeID [IdLength]byte

I'm trying to understand what the PrefixLen function does to actually compute the prefix and populate the routing table. I understand what each component of the method does. By that I mean, I understand what the bit shift operators do and AND with 1 to check if the bit is set.

What I fail to understand is the return values and why they're set in that way.



func (node NodeID) PrefixLen() (ret int) {
  for i := 0; i < IdLength; i++ {
    for j := 0; j < 8; j++ {
      if (node[i] >> uint8(7 - j)) & 0x1 != 0 {
        return i * 8 + j;
      }
    }
  }
  return IdLength * 8 - 1;
}

How is the return value suitable to be used as an index for the routing table?


  ...
  prefix_length := contact.id.Xor(table.node.id).PrefixLen();
  bucket := table.buckets[prefix_length];
  ...

How is this approach identical to looping over each bit? How does the author achieve the same result using the PrefixLen method.

Could you help me understand this with examples. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dqqpf32897 2018-07-03 18:34
    关注

    How is this approach identical to looping over each bit?

    The loop simply iterates over bytes and then for each byte over the bits doing shifts and masks. So in effect it does iterate over all bits, the bits just happen to be packed into bytes since the smallest addressable unit of memory generally is one byte.

    What I fail to understand is the return values and why they're set in that way.

    It simply calculates the bit position in terms of i complete bytes and j bits in the last partial byte.

    Context

    The context is actually misguided here since you're trying to explain the splittable routing table design while looking at a code sample with a fixed array layout. That is a common source of confusion.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程