Skip to content

Commit

Permalink
Rename the byte-comparable translation version to OSS50
Browse files Browse the repository at this point in the history
Also fix some minor issues in ByteComparable.md

patch by Branimir Lambov and Jacek Lewandowski; reviewed by Caleb Rackliffe and Maxwell Guo for CASSANDRA-18398
  • Loading branch information
blambov committed May 29, 2023
1 parent f16fb67 commit fad1f74
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 224 deletions.
6 changes: 3 additions & 3 deletions src/java/org/apache/cassandra/db/ClusteringComparator.java
Expand Up @@ -351,7 +351,7 @@ public String toString()
*/
public <V> Clustering<V> clusteringFromByteComparable(ValueAccessor<V> accessor, ByteComparable comparable)
{
ByteComparable.Version version = ByteComparable.Version.OSS42;
ByteComparable.Version version = ByteComparable.Version.OSS50;
ByteSource.Peekable orderedBytes = ByteSource.peekable(comparable.asComparableBytes(version));

// First check for special cases (partition key only, static clustering) that can do without buffers.
Expand Down Expand Up @@ -415,7 +415,7 @@ public <V> ClusteringBound<V> boundFromByteComparable(ValueAccessor<V> accessor,
ByteComparable comparable,
boolean isEnd)
{
ByteComparable.Version version = ByteComparable.Version.OSS42;
ByteComparable.Version version = ByteComparable.Version.OSS50;
ByteSource.Peekable orderedBytes = ByteSource.peekable(comparable.asComparableBytes(version));

int sep = orderedBytes.next();
Expand Down Expand Up @@ -472,7 +472,7 @@ public <V> ClusteringBound<V> boundFromByteComparable(ValueAccessor<V> accessor,
*/
public <V> ClusteringBoundary<V> boundaryFromByteComparable(ValueAccessor<V> accessor, ByteComparable comparable)
{
ByteComparable.Version version = ByteComparable.Version.OSS42;
ByteComparable.Version version = ByteComparable.Version.OSS50;
ByteSource.Peekable orderedBytes = ByteSource.peekable(comparable.asComparableBytes(version));

int sep = orderedBytes.next();
Expand Down
4 changes: 2 additions & 2 deletions src/java/org/apache/cassandra/db/marshal/TupleType.java
Expand Up @@ -209,7 +209,7 @@ public <V> ByteSource asComparableBytes(ValueAccessor<V> accessor, V data, ByteC
{
case LEGACY:
return asComparableBytesLegacy(accessor, data);
case OSS42:
case OSS50:
return asComparableBytesNew(accessor, data, version);
default:
throw new AssertionError();
Expand Down Expand Up @@ -255,7 +255,7 @@ private <V> ByteSource asComparableBytesNew(ValueAccessor<V> accessor, V data, B
@Override
public <V> V fromComparableBytes(ValueAccessor<V> accessor, ByteSource.Peekable comparableBytes, ByteComparable.Version version)
{
assert version == ByteComparable.Version.OSS42; // Reverse translation is not supported for the legacy version.
assert version == ByteComparable.Version.OSS50; // Reverse translation is not supported for the legacy version.
if (comparableBytes == null)
return accessor.empty();

Expand Down
Expand Up @@ -115,7 +115,7 @@ public class TrieMemtable extends AbstractShardedMemtable
public static final int MAX_RECURSIVE_KEY_LENGTH = 128;

/** The byte-ordering conversion version to use for memtables. */
public static final ByteComparable.Version BYTE_COMPARABLE_VERSION = ByteComparable.Version.OSS42;
public static final ByteComparable.Version BYTE_COMPARABLE_VERSION = ByteComparable.Version.OSS50;

// Set to true when the memtable requests a switch (e.g. for trie size limit being reached) to ensure only one
// thread calls cfs.switchMemtableIfCurrent.
Expand Down
2 changes: 1 addition & 1 deletion src/java/org/apache/cassandra/db/tries/Trie.java
Expand Up @@ -270,7 +270,7 @@ protected interface Walker<T, R> extends ResettingTransitionsReceiver
}

// Version of the byte comparable conversion to use for all operations
protected static final ByteComparable.Version BYTE_COMPARABLE_VERSION = ByteComparable.Version.OSS42;
protected static final ByteComparable.Version BYTE_COMPARABLE_VERSION = ByteComparable.Version.OSS50;

/**
* Adapter interface providing the methods a {@link Walker} to a {@link Consumer}, so that the latter can be used
Expand Down
2 changes: 1 addition & 1 deletion src/java/org/apache/cassandra/io/tries/Walker.java
Expand Up @@ -58,7 +58,7 @@ public class Walker<CONCRETE extends Walker<CONCRETE>> implements AutoCloseable
protected long lesserBranch;

// Version of the byte comparable conversion to use
public static final ByteComparable.Version BYTE_COMPARABLE_VERSION = ByteComparable.Version.OSS42;
public static final ByteComparable.Version BYTE_COMPARABLE_VERSION = ByteComparable.Version.OSS50;

/**
* Creates a walker. Rebufferer must be aligned and with a buffer size that is at least 4k.
Expand Down
Expand Up @@ -37,7 +37,7 @@ public interface ByteComparable
enum Version
{
LEGACY, // Encoding used in legacy sstable format; forward (value to byte-comparable) translation only
OSS42, // CASSANDRA 4.2 encoding
OSS50, // CASSANDRA 5.0 encoding
}

ByteComparable EMPTY = (Version version) -> ByteSource.EMPTY;
Expand Down
370 changes: 194 additions & 176 deletions src/java/org/apache/cassandra/utils/bytecomparable/ByteComparable.md

Large diffs are not rendered by default.

Expand Up @@ -61,7 +61,7 @@
public class AbstractTypeByteSourceDecodingBench
{

private static final ByteComparable.Version LATEST = ByteComparable.Version.OSS42;
private static final ByteComparable.Version LATEST = ByteComparable.Version.OSS50;

private static final Map<AbstractType, BiFunction<Random, Integer, ByteSource.Peekable>> PEEKABLE_GENERATOR_BY_TYPE = new HashMap<>();
static
Expand Down Expand Up @@ -135,6 +135,6 @@ public int baseline()
public ByteBuffer fromComparableBytes()
{
ByteSource.Peekable peekableBytes = randomPeekableBytes();
return abstractType.fromComparableBytes(peekableBytes, ByteComparable.Version.OSS42);
return abstractType.fromComparableBytes(peekableBytes, ByteComparable.Version.OSS50);
}
}
Expand Up @@ -153,7 +153,7 @@ public Long fromLong(long l)

public Long fromByteComparable(ByteComparable bc)
{
return ByteSourceInverse.getSignedLong(bc.asComparableBytes(ByteComparable.Version.OSS42));
return ByteSourceInverse.getSignedLong(bc.asComparableBytes(ByteComparable.Version.OSS50));
}

public ByteComparable longToByteComparable(long l)
Expand All @@ -176,8 +176,8 @@ public BigInteger fromLong(long l)

public BigInteger fromByteComparable(ByteComparable bc)
{
return IntegerType.instance.compose(IntegerType.instance.fromComparableBytes(ByteSource.peekable(bc.asComparableBytes(ByteComparable.Version.OSS42)),
ByteComparable.Version.OSS42));
return IntegerType.instance.compose(IntegerType.instance.fromComparableBytes(ByteSource.peekable(bc.asComparableBytes(ByteComparable.Version.OSS50)),
ByteComparable.Version.OSS50));
}

public ByteComparable longToByteComparable(long l)
Expand All @@ -200,8 +200,8 @@ public BigDecimal fromLong(long l)

public BigDecimal fromByteComparable(ByteComparable bc)
{
return DecimalType.instance.compose(DecimalType.instance.fromComparableBytes(ByteSource.peekable(bc.asComparableBytes(ByteComparable.Version.OSS42)),
ByteComparable.Version.OSS42));
return DecimalType.instance.compose(DecimalType.instance.fromComparableBytes(ByteSource.peekable(bc.asComparableBytes(ByteComparable.Version.OSS50)),
ByteComparable.Version.OSS50));
}

public ByteComparable longToByteComparable(long l)
Expand Down Expand Up @@ -231,7 +231,7 @@ public String fromLong(long l)

public String fromByteComparable(ByteComparable bc)
{
return new String(ByteSourceInverse.readBytes(bc.asComparableBytes(ByteComparable.Version.OSS42)), StandardCharsets.UTF_8);
return new String(ByteSourceInverse.readBytes(bc.asComparableBytes(ByteComparable.Version.OSS50)), StandardCharsets.UTF_8);
}

public ByteComparable longToByteComparable(long l)
Expand Down Expand Up @@ -267,7 +267,7 @@ public byte[] fromLong(long l)

public byte[] fromByteComparable(ByteComparable bc)
{
return ByteSourceInverse.readBytes(bc.asComparableBytes(ByteComparable.Version.OSS42));
return ByteSourceInverse.readBytes(bc.asComparableBytes(ByteComparable.Version.OSS50));
}

public ByteComparable longToByteComparable(long l)
Expand Down
12 changes: 6 additions & 6 deletions test/unit/org/apache/cassandra/db/tries/SlicedTrieTest.java
Expand Up @@ -132,7 +132,7 @@ private static ByteComparable[] toByteComparable(String[] keys)
@Test
public void testSingletonSubtrie()
{
Arrays.sort(BOUNDARIES, (a, b) -> ByteComparable.compare(a, b, ByteComparable.Version.OSS42));
Arrays.sort(BOUNDARIES, (a, b) -> ByteComparable.compare(a, b, ByteComparable.Version.OSS50));
for (int li = -1; li < BOUNDARIES.length; ++li)
{
ByteComparable l = li < 0 ? null : BOUNDARIES[li];
Expand All @@ -147,8 +147,8 @@ public void testSingletonSubtrie()

for (ByteComparable key : KEYS)
{
int cmp1 = l != null ? ByteComparable.compare(key, l, ByteComparable.Version.OSS42) : 1;
int cmp2 = r != null ? ByteComparable.compare(r, key, ByteComparable.Version.OSS42) : 1;
int cmp1 = l != null ? ByteComparable.compare(key, l, ByteComparable.Version.OSS50) : 1;
int cmp2 = r != null ? ByteComparable.compare(r, key, ByteComparable.Version.OSS50) : 1;
Trie<Boolean> ix = new SlicedTrie<>(Trie.singleton(key, true), l, includeLeft, r, includeRight);
boolean expected = true;
if (cmp1 < 0 || cmp1 == 0 && !includeLeft)
Expand All @@ -162,10 +162,10 @@ public void testSingletonSubtrie()
System.err.println(ix.dump());
Assert.fail(String.format("Failed on range %s%s,%s%s key %s expected %s got %s\n",
includeLeft ? "[" : "(",
l != null ? l.byteComparableAsString(ByteComparable.Version.OSS42) : null,
r != null ? r.byteComparableAsString(ByteComparable.Version.OSS42) : null,
l != null ? l.byteComparableAsString(ByteComparable.Version.OSS50) : null,
r != null ? r.byteComparableAsString(ByteComparable.Version.OSS50) : null,
includeRight ? "]" : ")",
key.byteComparableAsString(ByteComparable.Version.OSS42),
key.byteComparableAsString(ByteComparable.Version.OSS50),
expected,
actual));
}
Expand Down
Expand Up @@ -68,7 +68,7 @@ public class AbstractTypeByteSourceTest
@Parameterized.Parameters(name = "version={0}")
public static Iterable<ByteComparable.Version> versions()
{
return ImmutableList.of(ByteComparable.Version.OSS42);
return ImmutableList.of(ByteComparable.Version.OSS50);
}

private final ByteComparable.Version version;
Expand Down Expand Up @@ -709,7 +709,7 @@ public void testReversedType()
// Test how ReversedType handles null ByteSource.Peekable - here the choice of base type is important, as
// the base type should also be able to handle null ByteSource.Peekable.
ReversedType<BigInteger> reversedVarintType = ReversedType.getInstance(IntegerType.instance);
ByteBuffer decodedNull = reversedVarintType.fromComparableBytes(null, ByteComparable.Version.OSS42);
ByteBuffer decodedNull = reversedVarintType.fromComparableBytes(null, ByteComparable.Version.OSS50);
Assert.assertEquals(ByteBufferUtil.EMPTY_BYTE_BUFFER, decodedNull);

// Test how ReversedType handles random data with some common and important base types.
Expand Down Expand Up @@ -751,8 +751,8 @@ public void testReversedType()
for (int i = 0; i < 100; ++i)
{
ByteBuffer initial = entry.getValue().apply(prng, length);
ByteSource.Peekable reversedPeekable = ByteSource.peekable(reversedType.asComparableBytes(initial, ByteComparable.Version.OSS42));
ByteBuffer decoded = reversedType.fromComparableBytes(reversedPeekable, ByteComparable.Version.OSS42);
ByteSource.Peekable reversedPeekable = ByteSource.peekable(reversedType.asComparableBytes(initial, ByteComparable.Version.OSS50));
ByteBuffer decoded = reversedType.fromComparableBytes(reversedPeekable, ByteComparable.Version.OSS50);
Assert.assertEquals(initial, decoded);
}
}
Expand Down
Expand Up @@ -76,7 +76,7 @@ public void testStringsAscii()
public void testStringsUTF8()
{
testType(UTF8Type.instance, testStrings);
testDirect(x -> ByteSource.of(x, Version.OSS42), Ordering.<String>natural()::compare, testStrings);
testDirect(x -> ByteSource.of(x, Version.OSS50), Ordering.<String>natural()::compare, testStrings);
}

@Test
Expand Down Expand Up @@ -378,10 +378,10 @@ public void testNullsInClusteringLegacy()
@Test
public void testEmptyClustering()
{
assertEmptyComparedToStatic(1, ClusteringPrefix.Kind.CLUSTERING, Version.OSS42);
assertEmptyComparedToStatic(0, ClusteringPrefix.Kind.STATIC_CLUSTERING, Version.OSS42);
assertEmptyComparedToStatic(1, ClusteringPrefix.Kind.INCL_START_BOUND, Version.OSS42);
assertEmptyComparedToStatic(1, ClusteringPrefix.Kind.INCL_END_BOUND, Version.OSS42);
assertEmptyComparedToStatic(1, ClusteringPrefix.Kind.CLUSTERING, Version.OSS50);
assertEmptyComparedToStatic(0, ClusteringPrefix.Kind.STATIC_CLUSTERING, Version.OSS50);
assertEmptyComparedToStatic(1, ClusteringPrefix.Kind.INCL_START_BOUND, Version.OSS50);
assertEmptyComparedToStatic(1, ClusteringPrefix.Kind.INCL_END_BOUND, Version.OSS50);

assertEmptyComparedToStatic(1, ClusteringPrefix.Kind.CLUSTERING, Version.LEGACY);
assertEmptyComparedToStatic(0, ClusteringPrefix.Kind.STATIC_CLUSTERING, Version.LEGACY);
Expand Down Expand Up @@ -526,11 +526,11 @@ public void testTupleNewField()
ByteComparable bOneAndNull2 = typeToComparable(t2, vOneAndNull);

assertEquals("The byte-comparable version of a one-field tuple must be the same as a two-field tuple with non-present second component.",
bOne1.byteComparableAsString(Version.OSS42),
bOne2.byteComparableAsString(Version.OSS42));
bOne1.byteComparableAsString(Version.OSS50),
bOne2.byteComparableAsString(Version.OSS50));
assertEquals("The byte-comparable version of a one-field tuple must be the same as a two-field tuple with null as second component.",
bOne1.byteComparableAsString(Version.OSS42),
bOneAndNull2.byteComparableAsString(Version.OSS42));
bOne1.byteComparableAsString(Version.OSS50),
bOneAndNull2.byteComparableAsString(Version.OSS50));
}


Expand Down Expand Up @@ -659,7 +659,7 @@ public void testMapTypeStringLongTree()
public void testDecoratedKeyPrefixesVOSS50()
{
// This should pass with the OSS 4.1 encoding
testDecoratedKeyPrefixes(Version.OSS42);
testDecoratedKeyPrefixes(Version.OSS50);
}

@Test
Expand Down Expand Up @@ -878,13 +878,13 @@ private int compare(ByteSource s1, ByteSource s2)

private void maybeAssertNotPrefix(ByteComparable s1, ByteComparable s2, Version version)
{
if (version == Version.OSS42)
if (version == Version.OSS50)
assertNotPrefix(s1.asComparableBytes(version), s2.asComparableBytes(version));
}

private void maybeCheck41Properties(int expectedComparison, ByteComparable s1, ByteComparable s2, Version version)
{
if (version != Version.OSS42)
if (version != Version.OSS50)
return;

if (s1 == null || s2 == null || 0 == expectedComparison)
Expand Down Expand Up @@ -957,7 +957,7 @@ public <T> void testType(AbstractType<? super T> type, Iterable<T> values)
safeStr(i),
safeStr(type.getSerializer().toCQLLiteral(b)),
safeStr(ByteBufferUtil.bytesToHex(b)),
typeToComparable(type, b).byteComparableAsString(Version.OSS42));
typeToComparable(type, b).byteComparableAsString(Version.OSS50));
}
for (T i : values)
for (T j : values)
Expand All @@ -974,7 +974,7 @@ public void testBuffers(AbstractType<?> type, List<ByteBuffer> values)
logger.info("Value {} bytes {} ByteSource {}",
safeStr(type.getSerializer().toCQLLiteral(b)),
safeStr(ByteBufferUtil.bytesToHex(b)),
typeToComparable(type, b).byteComparableAsString(Version.OSS42));
typeToComparable(type, b).byteComparableAsString(Version.OSS50));
}
}
catch (UnsupportedOperationException e)
Expand Down Expand Up @@ -1038,7 +1038,7 @@ void assertComparesSameDecoratedKeys(IPartitioner type, ByteBuffer b1, ByteBuffe

void assertDecoratedKeyBounds(IPartitioner type, ByteBuffer b)
{
Version version = Version.OSS42;
Version version = Version.OSS50;
DecoratedKey k = type.decorateKey(b);
final ByteComparable after = k.asComparableBound(false);
final ByteComparable before = k.asComparableBound(true);
Expand Down
Expand Up @@ -58,7 +58,7 @@
public class ByteSourceConversionTest extends ByteSourceTestBase
{
private final static Logger logger = LoggerFactory.getLogger(ByteSourceConversionTest.class);
public static final Version VERSION = Version.OSS42;
public static final Version VERSION = Version.OSS50;

@Rule
public final ExpectedException expectedException = ExpectedException.none();
Expand Down
Expand Up @@ -47,7 +47,7 @@ public class ByteSourceInverseTest
@Parameterized.Parameters(name = "version={0}")
public static Iterable<ByteComparable.Version> versions()
{
return ImmutableList.of(ByteComparable.Version.OSS42);
return ImmutableList.of(ByteComparable.Version.OSS50);
}

private final ByteComparable.Version version;
Expand Down Expand Up @@ -218,7 +218,7 @@ public void testBadByteSourceForVariableLengthNumbers()
ArrayList<ByteSource> sources = new ArrayList<>();
sources.add(null);
sources.add(ByteSource.EMPTY);
int length = ByteComparable.length(version -> ByteSource.variableLengthInteger(value), ByteComparable.Version.OSS42);
int length = ByteComparable.length(version -> ByteSource.variableLengthInteger(value), ByteComparable.Version.OSS50);
for (int i = 0; i < length; ++i)
sources.add(ByteSource.cut(ByteSource.variableLengthInteger(value), i));

Expand Down
Expand Up @@ -56,7 +56,7 @@ public class ByteSourceSequenceTest
@Parameterized.Parameters(name = "version={0}")
public static Iterable<ByteComparable.Version> versions()
{
return ImmutableList.of(ByteComparable.Version.OSS42);
return ImmutableList.of(ByteComparable.Version.OSS50);
}

private final ByteComparable.Version version;
Expand Down
Expand Up @@ -39,7 +39,7 @@ public class DecoratedKeyByteSourceTest
@Parameterized.Parameters(name = "version={0}")
public static Iterable<ByteComparable.Version> versions()
{
return ImmutableList.of(ByteComparable.Version.OSS42);
return ImmutableList.of(ByteComparable.Version.OSS50);
}

private final ByteComparable.Version version;
Expand Down

0 comments on commit fad1f74

Please sign in to comment.