Skip to content
Permalink
Browse files

Make LogSequenceNumber implement Comparable<LogSequenceNumber> (#1494)

* LogSequenceNumber now implements Comparable<LogSequenceNumber>

* LogSequenceNumber compareTo tests

* LogSequenceNumber and LogSequenceNumberTest codestyle fixes
  • Loading branch information...
maltalex authored and davecramer committed Jun 6, 2019
1 parent 12ef697 commit 1970c4a3fb8ebf4cc52f5d8b0d4977388ee713e7
@@ -10,7 +10,7 @@
/**
* LSN (Log Sequence Number) data which is a pointer to a location in the XLOG.
*/
public final class LogSequenceNumber {
public final class LogSequenceNumber implements Comparable<LogSequenceNumber> {
/**
* Zero is used indicate an invalid pointer. Bootstrap skips the first possible WAL segment,
* initializing the first WAL page at XLOG_SEG_SIZE, so no XLOG record can begin at zero.
@@ -105,4 +105,14 @@ public int hashCode() {
public String toString() {
return "LSN{" + asString() + '}';
}

@Override
public int compareTo(LogSequenceNumber o) {
if (value == o.value) {
return 0;
}
//Unsigned comparison
return value + Long.MIN_VALUE < o.value + Long.MIN_VALUE ? -1 : 1;
}

}
@@ -96,4 +96,49 @@ public void testSameLSNHaveSameHash() throws Exception {

assertThat(first.hashCode(), equalTo(second.hashCode()));
}

@Test
public void testCompareToSameValue() throws Exception {
LogSequenceNumber first = LogSequenceNumber.valueOf("0/15D690F8");
LogSequenceNumber second = LogSequenceNumber.valueOf("0/15D690F8");

assertThat(first.compareTo(second), equalTo(0));
assertThat(second.compareTo(first), equalTo(0));
}

@Test
public void testCompareToPositiveValues() throws Exception {
LogSequenceNumber first = LogSequenceNumber.valueOf(1234);
LogSequenceNumber second = LogSequenceNumber.valueOf(4321);

assertThat(first.compareTo(second), equalTo(-1));
assertThat(second.compareTo(first), equalTo(1));
}

@Test
public void testCompareToNegativeValues() throws Exception {
LogSequenceNumber first = LogSequenceNumber.valueOf(0x8000000000000000L);
LogSequenceNumber second = LogSequenceNumber.valueOf(0x8000000000000001L);

assertThat(first.compareTo(second), equalTo(-1));
assertThat(second.compareTo(first), equalTo(1));
}

@Test
public void testCompareToMixedSign() throws Exception {
LogSequenceNumber first = LogSequenceNumber.valueOf(1);
LogSequenceNumber second = LogSequenceNumber.valueOf(0x8000000000000001L);

assertThat(first.compareTo(second), equalTo(-1));
assertThat(second.compareTo(first), equalTo(1));
}

@Test
public void testCompareToWithInvalid() throws Exception {
LogSequenceNumber first = LogSequenceNumber.INVALID_LSN;
LogSequenceNumber second = LogSequenceNumber.valueOf(1);

assertThat(first.compareTo(second), equalTo(-1));
assertThat(second.compareTo(first), equalTo(1));
}
}

0 comments on commit 1970c4a

Please sign in to comment.
You can’t perform that action at this time.