dcapwell commented on code in PR #3689: URL: https://github.com/apache/cassandra/pull/3689#discussion_r1848856232
########## test/harry/main/org/apache/cassandra/harry/ColumnSpec.java: ########## @@ -0,0 +1,605 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.harry; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.InetAddress; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; + +import accord.utils.Invariants; +import org.apache.cassandra.cql3.ast.Symbol; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.AsciiType; +import org.apache.cassandra.db.marshal.BooleanType; +import org.apache.cassandra.db.marshal.ByteType; +import org.apache.cassandra.db.marshal.BytesType; +import org.apache.cassandra.db.marshal.DecimalType; +import org.apache.cassandra.db.marshal.DoubleType; +import org.apache.cassandra.db.marshal.FloatType; +import org.apache.cassandra.db.marshal.InetAddressType; +import org.apache.cassandra.db.marshal.Int32Type; +import org.apache.cassandra.db.marshal.IntegerType; +import org.apache.cassandra.db.marshal.LongType; +import org.apache.cassandra.db.marshal.ShortType; +import org.apache.cassandra.db.marshal.TimeType; +import org.apache.cassandra.db.marshal.TimeUUIDType; +import org.apache.cassandra.db.marshal.TimestampType; +import org.apache.cassandra.db.marshal.UTF8Type; +import org.apache.cassandra.db.marshal.UUIDType; +import org.apache.cassandra.harry.gen.Generator; +import org.apache.cassandra.harry.gen.Generators; +import org.apache.cassandra.harry.gen.TypeAdapters; +import org.apache.cassandra.utils.ByteArrayUtil; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.TimeUUID; + +// TODO: counters +// TODO: UDTs +// TODO: collections: frozen/unfrozen +// TODO: empty / 0 / min / max values if present +public class ColumnSpec<T> +{ + public final String name; + public final DataType<T> type; + public final Generator<T> gen; + public final Kind kind; + + public ColumnSpec(String name, + DataType<T> type, + Generator<T> gen, + Kind kind) + { + + this.name = name; + this.type = Invariants.nonNull(type); + this.gen = Invariants.nonNull(gen); + this.kind = kind; + } + + public String toCQL() + { + return String.format("%s %s%s", + Symbol.maybeQuote(name), + type, + kind == Kind.STATIC ? " static" : ""); + } + + @Override + public boolean equals(Object o) + { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ColumnSpec<?> that = (ColumnSpec<?>) o; + return Objects.equals(name, that.name) && + Objects.equals(type.cqlName, that.type.cqlName) && + kind == that.kind; + } + + @Override + public int hashCode() + { + return Objects.hash(name, type.cqlName, kind); + } + + public String name() + { + return name; + } + + public boolean isReversed() + { + return type.isReversed(); + } + + public String toString() + { + return name + '(' + type.toString() + ")"; + } + + public Generator<T> gen() + { + return gen; + } + + public static <T> ColumnSpec<T> pk(String name, DataType<T> type, Generator<T> gen) + { + return new ColumnSpec<>(name, type, gen, Kind.PARTITION_KEY); + } + + public static <T> ColumnSpec<T> pk(String name, DataType<T> type) + { + return new ColumnSpec<>(name, type, (Generator<T>) TypeAdapters.forValues(type.asServerType()), Kind.PARTITION_KEY); + } + + @SuppressWarnings("unchecked") + public static <T> ColumnSpec<T> ck(String name, DataType<T> type, Generator<T> gen, boolean isReversed) + { + return new ColumnSpec(name, isReversed ? ReversedType.getInstance(type) : type, gen, Kind.CLUSTERING); + } + + public static <T> ColumnSpec<T> ck(String name, DataType<T> type, boolean isReversed) + { + return new ColumnSpec(name, isReversed ? ReversedType.getInstance(type) : type, + TypeAdapters.forValues(type.asServerType()), + Kind.CLUSTERING); + } + + + public static <T> ColumnSpec<T> regularColumn(String name, DataType<T> type, Generator<T> gen) + { + return new ColumnSpec<>(name, type, gen, Kind.REGULAR); + } + + public static <T> ColumnSpec<T> regularColumn(String name, DataType<T> type) + { + return new ColumnSpec(name, type, TypeAdapters.forValues(type.asServerType()), Kind.REGULAR); + } + + public static <T> ColumnSpec<T> staticColumn(String name, DataType<T> type, Generator<T> gen) + { + return new ColumnSpec<>(name, type, gen, Kind.STATIC); + } + + public static <T> ColumnSpec<T> staticColumn(String name, DataType<T> type) + { + return new ColumnSpec(name, type, TypeAdapters.forValues(type.asServerType()), Kind.STATIC); + } + + public enum Kind + { + CLUSTERING, REGULAR, STATIC, PARTITION_KEY + } + + public static abstract class DataType<T> + { + protected final String cqlName; + + protected DataType(String cqlName) + { + this.cqlName = cqlName; + } + + public abstract /* unsigned */ long typeEntropy(); + + public boolean isReversed() + { + return false; + } + + public abstract AbstractType<?> asServerType(); + + public final String toString() + { + return cqlName; + } + + public final boolean equals(Object o) + { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DataType<?> dataType = (DataType<?>) o; + return Objects.equals(cqlName, dataType.cqlName); + } + + public final int hashCode() + { + return Objects.hash(cqlName); + } + + public Comparator<T> comparator() + { + return (o1, o2) -> ((Comparable) o1).compareTo(o2); Review Comment: I have a recency bias here, but there were 2-3 bugs found in `AbstractType` due to this pattern... when someone extended the type they didn't know that they *should* override random methods, which then caused the type to be corrupted or buggy... In `TypeSupport` its explicit for similar reasons, when you are defining the type you are the only one that knows what java type is used (client/server have different types) so you *must* think about ordering at that moment... extension allows you to avoid thinking about this which leads to bugs. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

