clintropolis commented on a change in pull request #11888:
URL: https://github.com/apache/druid/pull/11888#discussion_r779376508



##########
File path: 
core/src/main/java/org/apache/druid/segment/column/TypeStrategies.java
##########
@@ -0,0 +1,502 @@
+/*
+ * 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.druid.segment.column;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Ordering;
+import com.google.common.primitives.Floats;
+import com.google.common.primitives.Longs;
+import org.apache.druid.common.config.NullHandling;
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.StringUtils;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class TypeStrategies
+{
+  public static final int VALUE_OFFSET = Byte.BYTES;
+  public static final int NULLABLE_LONG_SIZE = Byte.BYTES + Long.BYTES;
+  public static final int NULLABLE_DOUBLE_SIZE = Byte.BYTES + Double.BYTES;
+  public static final int NULLABLE_FLOAT_SIZE = Byte.BYTES + Float.BYTES;
+
+  public static final LongTypeStrategy LONG = new LongTypeStrategy();
+  public static final FloatTypeStrategy FLOAT = new FloatTypeStrategy();
+  public static final DoubleTypeStrategy DOUBLE = new DoubleTypeStrategy();
+  public static final StringTypeStrategy STRING = new StringTypeStrategy();
+  public static final ConcurrentHashMap<String, TypeStrategy<?>> 
COMPLEX_STRATEGIES = new ConcurrentHashMap<>();
+
+  /**
+   * Get an {@link TypeStrategy} registered to some {@link 
TypeSignature#getComplexTypeName()}.
+   */
+  @Nullable
+  public static TypeStrategy<?> getComplex(String typeName)
+  {
+    return COMPLEX_STRATEGIES.get(typeName);
+  }
+
+  /**
+   * hmm... this might look familiar... (see ComplexMetrics)
+   *
+   * Register a complex type name -> {@link TypeStrategy} mapping.
+   *
+   * If the specified type name is already used and the supplied {@link 
TypeStrategy} is not of the
+   * same type as the existing value in the map for said key, an {@link ISE} 
is thrown.
+   *
+   * @param strategy The {@link TypeStrategy} object to be associated with the 
'type' in the map.
+   */
+  public static void registerComplex(String typeName, TypeStrategy<?> strategy)
+  {
+    Preconditions.checkNotNull(typeName);
+    COMPLEX_STRATEGIES.compute(typeName, (key, value) -> {
+      if (value == null) {
+        return strategy;
+      } else {
+        if (!value.getClass().getName().equals(strategy.getClass().getName())) 
{
+          throw new ISE(
+              "Incompatible strategy for type[%s] already exists. Expected 
[%s], found [%s].",
+              key,
+              strategy.getClass().getName(),
+              value.getClass().getName()
+          );
+        } else {
+          return value;
+        }
+      }
+    });
+  }
+
+  /**
+   * Clear and set the 'null' byte of a nullable value to {@link 
NullHandling#IS_NULL_BYTE} to a {@link ByteBuffer} at
+   * the supplied position. This method does not change the buffer position, 
limit, or mark, because it does not expect
+   * to own the buffer given to it (i.e. buffer aggs)
+   *
+   * Nullable types are stored with a leading byte to indicate if the value is 
null, followed by the value bytes
+   * (if not null)
+   *
+   * layout: | null (byte) | value |
+   *
+   * @return number of bytes written (always 1)
+   */
+  public static int writeNull(ByteBuffer buffer, int offset)
+  {
+    buffer.put(offset, NullHandling.IS_NULL_BYTE);
+    return 1;
+  }
+
+  /**
+   * Checks if a 'nullable' value's null byte is set to {@link 
NullHandling#IS_NULL_BYTE}. This method will mask the
+   * value of the null byte to only check if the null bit is set, meaning that 
the higher bits can be utilized for
+   * flags as necessary (e.g. using high bits to indicate if the value has 
been set or not for aggregators).
+   *
+   * Note that writing nullable values with the methods of {@link Types} will 
always clear and set the null byte to
+   * either {@link NullHandling#IS_NULL_BYTE} or {@link 
NullHandling#IS_NOT_NULL_BYTE}, losing any flag bits.
+   *
+   * layout: | null (byte) | value |
+   */
+  public static boolean isNullableNull(ByteBuffer buffer, int offset)
+  {
+    // use & so that callers can use the high bits of the null byte to pack 
additional information if necessary
+    return (buffer.get(offset) & NullHandling.IS_NULL_BYTE) == 
NullHandling.IS_NULL_BYTE;
+  }
+
+  /**
+   * Write a non-null long value to a {@link ByteBuffer} at the supplied 
offset. The first byte is always cleared and
+   * set to {@link NullHandling#IS_NOT_NULL_BYTE}, the long value is written 
in the next 8 bytes.
+   *
+   * layout: | null (byte) | long |
+   *
+   * This method does not change the buffer position, limit, or mark, because 
it does not expect to own the buffer
+   * given to it (i.e. buffer aggs)
+   *
+   * @return number of bytes written (always 9)
+   */
+  public static int writeNullableLong(ByteBuffer buffer, int offset, long 
value)

Review comment:
       changed to `readNotNullNullable...`/`writeNotNullNullable...` to reduce 
confusion




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