Skip to content

Commit

Permalink
Add Slice.mismatch method
Browse files Browse the repository at this point in the history
There are couple of places in Trino that are looking for longest prefix between two byte[] arrays
  • Loading branch information
wendigo committed Jul 2, 2024
1 parent 34e6421 commit 85d3d9d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/io/airlift/slice/Slice.java
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ public int compareTo(int offset, int length, Slice that, int otherOffset, int ot
}

// Find index of the first mismatched byte
long mismatch = MemorySegment.mismatch(segment, offset, offset + length, that.segment, otherOffset, otherOffset + otherLength);
long mismatch = mismatch(offset, length, that, otherOffset, otherLength);
if (mismatch == -1) {
return 0;
}
Expand All @@ -1088,6 +1088,16 @@ public int compareTo(int offset, int length, Slice that, int otherOffset, int ot
return Byte.compareUnsigned(segment.get(BYTE, offset + mismatch), that.segment.get(BYTE, otherOffset + mismatch));
}

public int mismatch(int offset, int length, Slice that, int otherOffset, int otherLength)
{
return toIntExact(MemorySegment.mismatch(segment, offset, offset + length, that.segment, otherOffset, otherOffset + otherLength));
}

public int mismatch(Slice that)
{
return mismatch(0, length(), that, 0, that.length());
}

/**
* Compares the specified object with this slice for equality. Equality is
* solely based on the contents of the slice.
Expand Down Expand Up @@ -1237,7 +1247,7 @@ public ByteBuffer toByteBuffer(int offset, int length)
return EMPTY_BYTE_BUFFER;
}

return segment.asByteBuffer().slice(offset, length);
return segment.asSlice(offset, length).asByteBuffer();
}

public ByteBuffer toByteBuffer(int offset)
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/io/airlift/slice/TestSlice.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,41 @@ public void testInt()
}
}

@Test
public void testMismatch()
{
Slice first = Slices.utf8Slice("ala ma kota");
Slice second = Slices.utf8Slice("ala ma psa");

int mismatch = first.mismatch(second);
assertThat(mismatch).isEqualTo(7);

assertThat(first.toStringUtf8().substring(0, mismatch))
.isEqualTo(second.toStringUtf8().substring(0, mismatch));

assertThat(first.toStringUtf8().substring(0, mismatch + 1))
.isNotEqualTo(second.toStringUtf8().substring(0, mismatch + 1));

// Different slices
assertThat(first.mismatch(utf8Slice("pies i kot")))
.isEqualTo(0);

assertThat(first.mismatch(0, first.length(), second, 5, second.length() - 5))
.isEqualTo(1);

assertThat(first.mismatch(first))
.isEqualTo(-1);

assertThat(second.mismatch(second))
.isEqualTo(-1);

assertThat(first.slice(0, mismatch).mismatch(second.slice(0, mismatch)))
.isEqualTo(-1);

assertThat(first.mismatch(0, mismatch, second, 0, mismatch))
.isEqualTo(-1);
}

private static void assertInt(Slice slice, int index)
{
// fill slice with FF
Expand Down

0 comments on commit 85d3d9d

Please sign in to comment.