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.

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

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭