You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by GitBox <gi...@apache.org> on 2018/11/05 07:49:45 UTC

[GitHub] paul-rogers commented on a change in pull request #1500: DRILL-6820: Msgpack format reader

paul-rogers commented on a change in pull request #1500: DRILL-6820: Msgpack format reader
URL: https://github.com/apache/drill/pull/1500#discussion_r230652873
 
 

 ##########
 File path: contrib/format-msgpack/src/main/java/org/apache/drill/exec/store/msgpack/MsgpackReader.java
 ##########
 @@ -0,0 +1,192 @@
+/*
+ * 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.drill.exec.store.msgpack;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.BitSet;
+import java.util.EnumMap;
+import java.util.List;
+
+import org.apache.drill.common.expression.PathSegment;
+import org.apache.drill.common.expression.SchemaPath;
+import org.apache.drill.exec.record.MaterializedField;
+import org.apache.drill.exec.store.msgpack.valuewriter.AbstractValueWriter;
+import org.apache.drill.exec.store.msgpack.valuewriter.ArrayValueWriter;
+import org.apache.drill.exec.store.msgpack.valuewriter.BinaryValueWriter;
+import org.apache.drill.exec.store.msgpack.valuewriter.BooleanValueWriter;
+import org.apache.drill.exec.store.msgpack.valuewriter.ExtensionValueWriter;
+import org.apache.drill.exec.store.msgpack.valuewriter.FloatValueWriter;
+import org.apache.drill.exec.store.msgpack.valuewriter.IntegerValueWriter;
+import org.apache.drill.exec.store.msgpack.valuewriter.MapValueWriter;
+import org.apache.drill.exec.store.msgpack.valuewriter.StringValueWriter;
+import org.apache.drill.exec.vector.complex.fn.FieldSelection;
+import org.apache.drill.exec.vector.complex.writer.BaseWriter;
+import org.apache.drill.exec.vector.complex.writer.BaseWriter.ComplexWriter;
+import org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter;
+import org.apache.drill.shaded.guava.com.google.common.collect.Lists;
+import org.msgpack.core.MessageInsufficientBufferException;
+import org.msgpack.core.MessagePack;
+import org.msgpack.core.MessageUnpacker;
+import org.msgpack.value.MapValue;
+import org.msgpack.value.Value;
+import org.msgpack.value.ValueType;
+
+import io.netty.buffer.DrillBuf;
+
+public class MsgpackReader {
+
+  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MsgpackReader.class);
+  private final List<SchemaPath> columns;
+  private final FieldSelection rootSelection;
+  protected MessageUnpacker unpacker;
+  protected MsgpackReaderContext context;
+  private final boolean skipQuery;
+
+  /**
+   * Collection for tracking empty array writers during reading and storing them
+   * for initializing empty arrays
+   */
+  private final List<ListWriter> emptyArrayWriters = Lists.newArrayList();
+  private boolean allTextMode = false;
+  private MapValueWriter mapValueWriter;
+
+  public MsgpackReader(InputStream stream, MsgpackReaderContext context, DrillBuf managedBuf, List<SchemaPath> columns,
+      boolean skipQuery) {
+
+    this.context = context;
+    this.context.workBuf = managedBuf;
+    this.unpacker = MessagePack.newDefaultUnpacker(stream);
+    this.columns = columns;
+    this.skipQuery = skipQuery;
+    rootSelection = FieldSelection.getFieldSelection(columns);
+    EnumMap<ValueType, AbstractValueWriter> valueWriterMap = new EnumMap<>(ValueType.class);
+    valueWriterMap.put(ValueType.ARRAY, new ArrayValueWriter(valueWriterMap, emptyArrayWriters));
+    valueWriterMap.put(ValueType.FLOAT, new FloatValueWriter());
+    valueWriterMap.put(ValueType.INTEGER, new IntegerValueWriter());
+    valueWriterMap.put(ValueType.BOOLEAN, new BooleanValueWriter());
+    valueWriterMap.put(ValueType.STRING, new StringValueWriter());
+    valueWriterMap.put(ValueType.BINARY, new BinaryValueWriter());
+    valueWriterMap.put(ValueType.EXTENSION, new ExtensionValueWriter());
+    mapValueWriter = new MapValueWriter(valueWriterMap);
+    valueWriterMap.put(ValueType.MAP, mapValueWriter);
+
+    for (AbstractValueWriter w : valueWriterMap.values()) {
+      w.setup(this.context);
+    }
+  }
+
+  public boolean write(ComplexWriter writer, MaterializedField schema)
+      throws IOException, MessageInsufficientBufferException {
+    if (!unpacker.hasNext()) {
+      return false;
+    }
+
+    context.useSchema = schema != null;
+
+    try {
+      Value v = unpacker.unpackValue();
+      ValueType type = v.getValueType();
+      if (type == ValueType.MAP) {
+        writeRecord(v.asMapValue(), writer, schema);
+      } else {
+        throw new MsgpackParsingException(
+            "Value in root of message pack file is not of type MAP. Skipping type found: " + type);
+      }
+    } catch (MessageInsufficientBufferException e) {
+      throw new MsgpackParsingException("Failed to unpack MAP, possibly because key/value tuples do not match.");
+    }
+    return true;
+  }
+
+  protected void writeRecord(MapValue value, ComplexWriter writer, MaterializedField schema) throws IOException {
+    if (skipQuery) {
+      writer.rootAsMap().bit("count").writeBit(1);
+    } else {
+      mapValueWriter.writeToMap(value, writer.rootAsMap(), this.rootSelection, schema);
+    }
+  }
+
+//
+//
+//  private void ensure(final int length) {
+//    workBuf = workBuf.reallocIfNeeded(length);
+//  }
+
+  @SuppressWarnings("resource")
+  public void ensureAtLeastOneField(ComplexWriter writer) {
 
 Review comment:
   Looks like you happened to look at one of the more complex readers for an example. The example cited above shows a much easier way to do this.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services