dcapwell commented on code in PR #3785: URL: https://github.com/apache/cassandra/pull/3785#discussion_r1927762467
########## test/harry/main/org/apache/cassandra/harry/model/ASTSingleTableModel.java: ########## @@ -0,0 +1,1289 @@ +/* + * 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.model; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.NavigableSet; +import java.util.Objects; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.function.IntFunction; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import javax.annotation.Nullable; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +import accord.utils.Invariants; +import org.apache.cassandra.cql3.ast.Conditional; +import org.apache.cassandra.cql3.ast.Element; +import org.apache.cassandra.cql3.ast.Expression; +import org.apache.cassandra.cql3.ast.ExpressionEvaluator; +import org.apache.cassandra.cql3.ast.FunctionCall; +import org.apache.cassandra.cql3.ast.Mutation; +import org.apache.cassandra.cql3.ast.Select; +import org.apache.cassandra.cql3.ast.Symbol; +import org.apache.cassandra.db.BufferClustering; +import org.apache.cassandra.db.Clustering; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.dht.Murmur3Partitioner; +import org.apache.cassandra.dht.Token; +import org.apache.cassandra.harry.util.StringUtils; +import org.apache.cassandra.schema.TableMetadata; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.ImmutableUniqueList; +import org.apache.cassandra.utils.Pair; +import org.apache.cassandra.utils.TableUtil; + +import static org.apache.cassandra.harry.model.BytesPartitionState.asCQL; + +public class ASTSingleTableModel +{ + public final BytesPartitionState.Factory factory; + private final TreeMap<BytesPartitionState.Ref, BytesPartitionState> partitions = new TreeMap<>(); + + public ASTSingleTableModel(TableMetadata metadata) + { + this.factory = new BytesPartitionState.Factory(metadata); + } + + public NavigableSet<BytesPartitionState.Ref> partitionKeys() + { + return partitions.navigableKeySet(); + } + + public int size() + { + return partitions.size(); + } + + public boolean isEmpty() + { + return partitions.isEmpty(); + } + + public TreeMap<ByteBuffer, List<BytesPartitionState.PrimaryKey>> index(Symbol symbol) + { + if (factory.pkPositions.contains(symbol)) + return indexPartitionColumn(symbol); + if (factory.staticPositions.contains(symbol)) + return indexStaticColumn(symbol); + return indexRowColumn(symbol); + } + + private TreeMap<ByteBuffer, List<BytesPartitionState.PrimaryKey>> indexPartitionColumn(Symbol symbol) + { + int offset = factory.pkPositions.indexOf(symbol); + TreeMap<ByteBuffer, List<BytesPartitionState.PrimaryKey>> index = new TreeMap<>(symbol.type()::compare); + for (BytesPartitionState partition : partitions.values()) + { + if (partition.isEmpty()) continue; + ByteBuffer bb = partition.key.bufferAt(offset); + List<BytesPartitionState.PrimaryKey> list = index.computeIfAbsent(bb, i -> new ArrayList<>()); + for (BytesPartitionState.Row row : partition.rows()) + list.add(row.ref()); + } + return index; + } + + private TreeMap<ByteBuffer, List<BytesPartitionState.PrimaryKey>> indexStaticColumn(Symbol symbol) + { + TreeMap<ByteBuffer, List<BytesPartitionState.PrimaryKey>> index = new TreeMap<>(symbol.type()::compare); + for (BytesPartitionState partition : partitions.values()) + { + if (partition.isEmpty()) continue; + ByteBuffer bb = partition.staticRow().get(symbol); + if (bb == null) + continue; + List<BytesPartitionState.PrimaryKey> list = index.computeIfAbsent(bb, i -> new ArrayList<>()); + for (BytesPartitionState.Row row : partition.rows()) + list.add(row.ref()); + } + return index; + } + + private TreeMap<ByteBuffer, List<BytesPartitionState.PrimaryKey>> indexRowColumn(Symbol symbol) + { + boolean clustering = factory.ckPositions.contains(symbol); + int offset = clustering ? factory.ckPositions.indexOf(symbol) : factory.regularPositions.indexOf(symbol); + TreeMap<ByteBuffer, List<BytesPartitionState.PrimaryKey>> index = new TreeMap<>(symbol.type()::compare); + for (BytesPartitionState partition : partitions.values()) + { + if (partition.isEmpty()) continue; + for (BytesPartitionState.Row row : partition.rows()) + { + ByteBuffer bb = clustering ? row.clustering.bufferAt(offset) : row.get(offset); + if (bb == null) + continue; + index.computeIfAbsent(bb, i -> new ArrayList<>()).add(row.ref()); + } + } + return index; + } + + public void update(Mutation mutation) + { + switch (mutation.kind) + { + case INSERT: + update((Mutation.Insert) mutation); + break; + case UPDATE: + update((Mutation.Update) mutation); + break; + case DELETE: + update((Mutation.Delete) mutation); + break; + default: + throw new UnsupportedOperationException(mutation.kind.name()); + } + } + + public void update(Mutation.Insert insert) + { + Clustering<ByteBuffer> pd = pd(insert); + BytesPartitionState partition = partitions.get(factory.createRef(pd)); + if (partition == null) + { + partition = factory.create(pd); + partitions.put(partition.ref(), partition); + } + Map<Symbol, Expression> values = insert.values; + if (!factory.staticPositions.isEmpty() && !Sets.intersection(factory.staticPositions.asSet(), values.keySet()).isEmpty()) + { + // static columns to add in. If we are doing something like += to a row that doesn't exist, we still update statics... + Map<Symbol, ByteBuffer> write = new HashMap<>(); + for (Symbol col : Sets.intersection(factory.staticPositions.asSet(), values.keySet())) + write.put(col, eval(values.get(col))); + partition.setStaticColumns(write); + } + Map<Symbol, ByteBuffer> write = new HashMap<>(); + for (Symbol col : Sets.intersection(factory.regularPositions.asSet(), values.keySet())) + write.put(col, eval(values.get(col))); + partition.setColumns(key(insert.values, factory.ckPositions), + write, + true); + } + + public void update(Mutation.Update update) + { + var split = splitOnPartition(update.where.simplify()); Review Comment: `BATCH` isn't supported yet in this patch. I know one user that relies on `BATCH` with conditional writes... This patch doesn't handle paxos conditions yet, thats planned as follow up work. -- 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]

