Frage

I have a bunch of log files consisting of event logs along with their vector clocks logged in them. Now while comparing the vector clocks of any two events, would it be correct to take the root of the sum of the squares of each component of the vector clock and use the result to compare with that of another, and then conclude that the one with the lesser value precedes the other one?

War es hilfreich?

Lösung

No, if there were a way to reduce it down to one value, we would be using that instead of the vector!

To compare vector clocks you need to piece-wise compare the whole vector.

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;
    }
}

You can do a less-accurate pass using just the min and the max:

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;
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top