diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java index 65818dd..ffdc2bb 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -94,6 +94,7 @@ import org.apache.hive.beeline.hs2connection.BeelineHS2ConnectionFileParseException; import org.apache.hive.beeline.hs2connection.HS2ConnectionFileUtils; import org.apache.hive.beeline.hs2connection.UserHS2ConnectionFileParser; +import org.apache.hive.beeline.util.FileHistoryWithLimit; import org.apache.hive.beeline.hs2connection.HS2ConnectionFileParser; import org.apache.hive.beeline.hs2connection.HiveSiteHS2ConnectionFileParser; import org.apache.thrift.transport.TTransportException; @@ -1182,7 +1183,7 @@ private void setupHistory() throws IOException { return; } - this.history = new FileHistory(new File(getOpts().getHistoryFile())); + this.history = new FileHistoryWithLimit(new File(getOpts().getHistoryFile())); // add shutdown hook to flush the history to history file ShutdownHookManager.addShutdownHook(new Runnable() { @Override diff --git a/beeline/src/java/org/apache/hive/beeline/util/FileHistoryWithLimit.java b/beeline/src/java/org/apache/hive/beeline/util/FileHistoryWithLimit.java new file mode 100644 index 0000000..69188b3 --- /dev/null +++ b/beeline/src/java/org/apache/hive/beeline/util/FileHistoryWithLimit.java @@ -0,0 +1,56 @@ +/** + * 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.hive.beeline.util; + +import java.io.File; +import java.io.IOException; + +import jline.console.history.FileHistory; + +/** + * A class of FileHistory with file size limit. + * + */ +public class FileHistoryWithLimit extends FileHistory { + public static final int DEFAULT_MAX_FILESIZE = 100; // The default file size in MB + private int maxFileSize; + public FileHistoryWithLimit(File file) throws IOException { + this(file, DEFAULT_MAX_FILESIZE * 1024 * 1024); + } + + public FileHistoryWithLimit(File file, double fileSizeInMB) throws IOException { + super(file); + this.maxFileSize = (int)(fileSizeInMB * 1024 * 1024); + } + + @Override + public void flush() throws IOException { + // Control the size to flush + int totalSize = 0; + for (Entry entry : this) { + totalSize += entry.value().length(); + } + + while (!this.isEmpty() && totalSize > maxFileSize) { + totalSize -= this.removeFirst().length(); + } + + super.flush(); + } +} diff --git a/beeline/src/test/org/apache/hive/beeline/TestBeeLineHistory.java b/beeline/src/test/org/apache/hive/beeline/TestBeeLineHistory.java index 623e667..2e34e86 100644 --- a/beeline/src/test/org/apache/hive/beeline/TestBeeLineHistory.java +++ b/beeline/src/test/org/apache/hive/beeline/TestBeeLineHistory.java @@ -24,6 +24,7 @@ import java.io.PrintWriter; import java.lang.reflect.Method; +import org.apache.hive.beeline.util.FileHistoryWithLimit; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; @@ -89,6 +90,29 @@ public void testHistory() throws Exception { beeline.close(); } + @Test + public void testHistoryWithFileSizeLimit() throws Exception { + String fileName = System.getProperty("test.tmp.dir") + "/sizelimitedhistory"; + PrintWriter writer = new PrintWriter(fileName); + writer.println("select 1;"); + writer.println("select 2;"); + writer.println("select 3;"); + writer.println("select 4;"); + writer.println("select 5;"); + writer.println("select 6;"); + writer.println("select 7;"); + writer.println("select 8;"); + writer.println("select 9;"); + writer.println("select 10;"); + writer.close(); + FileHistoryWithLimit fileHistory = new FileHistoryWithLimit(new File(fileName), 1.0/102400.0); + fileHistory.flush(); + + // Read back and check the size + FileHistoryWithLimit fileHistory2 = new FileHistoryWithLimit(new File(fileName), 1.0/102400.0); + Assert.assertEquals(1, fileHistory2.size()); + } + @AfterClass public static void afterTests() throws Exception { File file = new File(fileName);