dcapwell commented on code in PR #3785:
URL: https://github.com/apache/cassandra/pull/3785#discussion_r1927760848


##########
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)

Review Comment:
   thats planned work but not covered in this patch
   
   `IF NOT EXISTS` is paxos, which means the model needs to handle conditions.  
 I plan to have the model handle conditions (and `Txn`) but i wanted to do that 
as its own thing and focus on basic read/write for now.
   
   > I see conditions being handled for update 
   
   Where?  `IF` isn't currently handled by the model.  Only `WHERE`.
   
   The random generation of mutations supports `IF`, but we disable in the 
existing tests as the model doesn't support yet.
   



-- 
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]

Reply via email to