jasonk000 commented on code in PR #13545:
URL: https://github.com/apache/druid/pull/13545#discussion_r1051082187


##########
core/src/main/java/org/apache/druid/data/input/impl/FastLineIterator.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.data.input.impl;
+
+import com.google.common.base.Preconditions;
+import it.unimi.dsi.fastutil.bytes.ByteArrayList;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.parsers.CloseableIterator;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.NoSuchElementException;
+
+/**
+ * Like the Apache Commons LineIterator, but faster.
+ */
+public class FastLineIterator implements CloseableIterator<String>
+{
+  static final int BUFFER_SIZE = 512;
+  private static final ThreadLocal<byte[]> BUFFER_LOCAL = 
ThreadLocal.withInitial(() -> new byte[BUFFER_SIZE]);
+
+  static final byte CR = (byte) '\r';
+  static final byte LF = (byte) '\n';
+
+  final InputStream source;
+  final ByteArrayList buffer;
+
+  boolean finished;
+  String nextLine;
+
+  /**
+   * Constructor; a local buffer will be created
+   * @param source
+   */
+  public FastLineIterator(InputStream source)
+  {
+    this(source, new ByteArrayList());
+  }
+
+  /**
+   * Constructor; BYO buffer.
+   *
+   * Existing contents of the buffer will be destroyed.
+   * @param source
+   * @param buffer
+   */
+  public FastLineIterator(InputStream source, ByteArrayList buffer)
+  {
+    Preconditions.checkNotNull(source);
+    this.source = source;
+    this.finished = false;
+    this.nextLine = null;
+    this.buffer = buffer;
+    this.buffer.size(0);
+  }
+
+  @Override
+  public void close() throws IOException
+  {
+    nextLine = null;
+    finished = true;
+    source.close();
+    // Note: do not remove the thread local buffer; retain it for reuse later

Review Comment:
   No need to, the buffer contents are overwritten when used.



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