andresgomezfrr commented on a change in pull request #5789: Add stringLast and 
stringFirst aggregators extension
URL: https://github.com/apache/incubator-druid/pull/5789#discussion_r205683872
 
 

 ##########
 File path: 
processing/src/main/java/io/druid/query/aggregation/SerializablePairLongStringSerde.java
 ##########
 @@ -0,0 +1,180 @@
+/*
+ * Licensed to Metamarkets Group Inc. (Metamarkets) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. Metamarkets 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 io.druid.query.aggregation;
+
+import io.druid.data.input.InputRow;
+import io.druid.java.util.common.StringUtils;
+import io.druid.segment.GenericColumnSerializer;
+import io.druid.segment.column.ColumnBuilder;
+import io.druid.segment.data.GenericIndexed;
+import io.druid.segment.data.ObjectStrategy;
+import io.druid.segment.serde.ComplexColumnPartSupplier;
+import io.druid.segment.serde.ComplexMetricExtractor;
+import io.druid.segment.serde.ComplexMetricSerde;
+import io.druid.segment.serde.LargeColumnSupportedComplexColumnSerializer;
+import io.druid.segment.writeout.SegmentWriteOutMedium;
+
+import java.nio.ByteBuffer;
+
+/**
+ * The SerializablePairLongStringSerde serializes a Long-String pair 
(SerializablePairLongString).
+ * The serialization structure is: Long:Integer:String
+ * <p>
+ * The class is used on first/last String aggregators to store the time and 
the first/last string.
+ * Long:Integer:String -> Timestamp:StringSize:StringData
+ */
+public class SerializablePairLongStringSerde extends ComplexMetricSerde
+{
+
+  private static final String TYPE_NAME = "serializablePairLongString";
+
+  @Override
+  public String getTypeName()
+  {
+    return TYPE_NAME;
+  }
+
+  @Override
+  public ComplexMetricExtractor getExtractor()
+  {
+    return new ComplexMetricExtractor()
+    {
+      @Override
+      public Class<SerializablePairLongString> extractedClass()
+      {
+        return SerializablePairLongString.class;
+      }
+
+      @Override
+      public Object extractValue(InputRow inputRow, String metricName)
+      {
+        return inputRow.getRaw(metricName);
+      }
+    };
+  }
+
+  @Override
+  public void deserializeColumn(ByteBuffer buffer, ColumnBuilder columnBuilder)
+  {
+    final GenericIndexed column = GenericIndexed.read(buffer, 
getObjectStrategy(), columnBuilder.getFileMapper());
+    columnBuilder.setComplexColumn(new 
ComplexColumnPartSupplier(getTypeName(), column));
+  }
+
+  @Override
+  public ObjectStrategy getObjectStrategy()
+  {
+    return new ObjectStrategy<SerializablePairLongString>()
+    {
+      @Override
+      public int compare(SerializablePairLongString o1, 
SerializablePairLongString o2)
+      {
+        int comparation;
+
+        // First we check if the objects are null
+        if (o1 == null && o2 == null) {
+          comparation = 0;
+        } else if (o1 == null) {
+          comparation = -1;
+        } else if (o2 == null) {
+          comparation = 1;
+        } else {
+
+          // If the objects are not null, we will try to compare using 
timestamp
+          comparation = o1.lhs.compareTo(o2.lhs);
+
+          // If both timestamp are the same, we try to compare the Strings
+          if (comparation == 0) {
+
+            // First we check if the strings are null
+            if (o1.rhs == null && o2.rhs == null) {
+              comparation = 0;
+            } else if (o1.rhs == null) {
+              comparation = -1;
+            } else if (o2.rhs == null) {
+              comparation = 1;
+            } else {
+
+              // If the strings are not null, we will compare them
+              // Note: This comparison maybe doesn't make sense to first/last 
aggregators,
+              // because compare both SerializablePairLongString based on 
String maybe doesn't matter
+              // to define each is first or last.
+              comparation = o1.rhs.compareTo(o2.rhs);
+            }
+          }
+        }
+
+        return comparation;
+      }
+
+      @Override
+      public Class<? extends SerializablePairLongString> getClazz()
+      {
+        return SerializablePairLongString.class;
+      }
+
+      @Override
+      public SerializablePairLongString fromByteBuffer(ByteBuffer buffer, int 
numBytes)
+      {
+        final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
+
+        long lhs = readOnlyBuffer.getLong();
+        int stringSize = readOnlyBuffer.getInt();
+
+        String lastString = null;
+        if (stringSize > 0) {
+          byte[] stringBytes = new byte[stringSize];
+          readOnlyBuffer.get(stringBytes, 0, stringSize);
+          lastString = StringUtils.fromUtf8(stringBytes);
+        }
+
+        return new SerializablePairLongString(lhs, lastString);
+      }
+
+      @Override
+      public byte[] toBytes(SerializablePairLongString val)
+      {
+        String rhsString = val.rhs;
+        ByteBuffer bbuf;
+
+
 
 Review comment:
   :+1:

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org
For additional commands, e-mail: commits-h...@druid.apache.org

Reply via email to