向量时钟的比较,用于事件关联

4

我有一堆包含事件日志和它们的向量时钟的日志文件。现在,当比较任意两个事件的向量时钟时,将每个向量时钟的每个组件的平方和的根相加是否正确,并使用结果与另一个进行比较,然后得出值更小的事件先于另一个事件的结论?

1个回答

3
不,如果有一种方法可以将其缩减为一个值,那么我们会使用该值而不是向量!要比较向量时,您需要逐个比较整个向量。
class VectorClock {
    private long[] clocks;
    ...
    /**
     * This is before other iff both conditions are met:
     * - each process's clock is less-than-or-equal-to its own clock in other; and
     * - there is at least one process's clock which is strictly less-than its
     *   own clock in other
     */
    public boolean isBefore(VectorClock other) {
        boolean isBefore = false;
        for (int i = 0; i < clocks.length; i++) {
            int cmp = Long.compare(clocks[i], other.clocks[i]);
            if (cmp > 0)
              return false; // note, could return false even if isBefore is true
            else if (cmp < 0)
              isBefore = true;
        }
        return isBefore;
    }
}

您可以只使用最小值和最大值进行不太精确的处理:

class VectorClockSummary {
    private long min, max;
    ...
    public tribool isBefore(VectorClockSummary other) {
        if (max < other.min)
            return true;
        else if (min > other.max)
            return false;
        else
            return maybe;
    }
}

谢谢Michael,我最终采用了你上面建议的方法。 - HungryTux

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接