maedhroz commented on code in PR #2409:
URL: https://github.com/apache/cassandra/pull/2409#discussion_r1232864091


##########
src/java/org/apache/cassandra/index/sai/disk/v1/bbtree/BlockBalancedTreeQueries.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.index.sai.disk.v1.bbtree;
+
+import java.nio.ByteBuffer;
+
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.index.sai.plan.Expression;
+import org.apache.cassandra.index.sai.utils.TypeUtil;
+import org.apache.lucene.index.PointValues.Relation;
+import org.apache.lucene.util.FutureArrays;
+
+public class BlockBalancedTreeQueries
+{
+    private static final BlockBalancedTreeReader.IntersectVisitor MATCH_ALL = 
new BlockBalancedTreeReader.IntersectVisitor()
+    {
+        @Override
+        public boolean visit(byte[] packedValue)
+        {
+            return true;
+        }
+
+        @Override
+        public Relation compare(byte[] minPackedValue, byte[] maxPackedValue)
+        {
+            return Relation.CELL_INSIDE_QUERY;
+        }
+    };
+
+    public static BlockBalancedTreeReader.IntersectVisitor 
balancedTreeQueryFrom(Expression expression, int bytesPerValue)
+    {
+        if (expression.lower == null && expression.upper == null)
+        {
+            return MATCH_ALL;
+        }
+
+        Bound lower = null ;
+        if (expression.lower != null)
+        {
+            final byte[] lowerBound = toComparableBytes(bytesPerValue, 
expression.lower.value.encoded, expression.validator);
+            lower = new Bound(lowerBound, !expression.lower.inclusive);
+        }
+
+        Bound upper = null;
+        if (expression.upper != null)
+        {
+            final byte[] upperBound = toComparableBytes(bytesPerValue, 
expression.upper.value.encoded, expression.validator);
+            upper = new Bound(upperBound, !expression.upper.inclusive);
+        }
+
+        return new RangeQueryVisitor(bytesPerValue, lower, upper);
+    }
+
+    private static byte[] toComparableBytes(int bytesPerDim, ByteBuffer value, 
AbstractType<?> type)
+    {
+        byte[] buffer = new byte[TypeUtil.fixedSizeOf(type)];
+        assert buffer.length == bytesPerDim;
+        TypeUtil.toComparableBytes(value, type, buffer);
+        return buffer;
+    }
+
+    private static abstract class RangeQuery implements 
BlockBalancedTreeReader.IntersectVisitor
+    {
+        final int bytesPerValue;
+
+        RangeQuery(int bytesPerValue)
+        {
+            this.bytesPerValue = bytesPerValue;
+        }
+
+        int compareUnsigned(byte[] packedValue, Bound bound)
+        {
+            return FutureArrays.compareUnsigned(packedValue, 0, bytesPerValue, 
bound.bound, 0, bytesPerValue);
+        }
+    }
+
+    private static class Bound
+    {
+        private final byte[] bound;
+        private final boolean exclusive;
+
+        Bound(byte[] bound, boolean exclusive)
+        {
+            this.bound = bound;
+            this.exclusive = exclusive;
+        }
+
+        boolean smallerThan(int cmp)
+        {
+            return cmp > 0 || (cmp == 0 && exclusive);
+        }
+
+        boolean greaterThan(int cmp)
+        {
+            return cmp < 0 || (cmp == 0 && exclusive);
+        }
+    }
+
+    private static class RangeQueryVisitor extends RangeQuery
+    {
+        private final Bound lower;
+        private final Bound upper;
+
+        private RangeQueryVisitor(int bytesPerValue, Bound lower, Bound upper)
+        {
+            super(bytesPerValue);
+            this.lower = lower;
+            this.upper = upper;
+        }
+
+        @Override
+        public boolean visit(byte[] packedValue)

Review Comment:
   nit: Part of me feels like this method would be better named `within()` or 
`contains()` or something less generic :/



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