Skip to content
Permalink
Browse files
Improve count lines in FileUtil (#236)
  • Loading branch information
Linary committed Jul 15, 2020
1 parent 2107de9 commit 5c038a02c9ee2c7f220f170e13b555c70d01e730
Showing 6 changed files with 61 additions and 20 deletions.
@@ -191,22 +191,12 @@ public int getLoadProgress() {
if (this.fileTotalLines == null || this.fileReadLines == null ||
this.fileTotalLines == 0) {
return 0;
} else {
long totalLines = this.fileTotalLines;
if (this.fileTotalLines < this.fileReadLines) {
/*
* The line counted is one less than actually read,
* it caused by FileUtil.countLines
*/
Ex.check(this.fileTotalLines + 1 == this.fileReadLines,
"The file total lines must be >= read lines or " +
"one less than read lines, but got total lines %s, " +
"read lines %s",
this.fileTotalLines, this.fileReadLines);
totalLines = this.fileTotalLines + 1;
}
return (int) ((double) this.fileReadLines / totalLines * 100);
}
Ex.check(this.fileTotalLines >= this.fileReadLines,
"The file total lines must be >= read lines, " +
"but got total lines %s, read lines %s",
this.fileTotalLines, this.fileReadLines);
return (int) ((double) this.fileReadLines / this.fileTotalLines * 100);
}

@JsonProperty("load_rate")
@@ -30,26 +30,33 @@

public final class FileUtil {

public static long countLines(String path) {
public static int countLines(String path) {
return countLines(new File(path));
}

/**
* NOTE: If there is no blank line at the end of the file,
* one line will be missing
*/
public static long countLines(File file) {
public static int countLines(File file) {
if (!file.exists()) {
throw new IllegalArgumentException(String.format(
"The file %s doesn't exist", file));
}
long fileLength = file.length();
assert fileLength > 0;
LineNumberReader lineReader = null;
try {
FileReader fileReader = new FileReader(file);
lineReader = new LineNumberReader(fileReader);
lineReader.skip(fileLength);
return lineReader.getLineNumber();
/*
* The last character may be an EOL or a non-EOL character.
* If it is the EOL, need to add 1 line; if it is the non-EOL,
* also need to add 1 line, because the next character means the EOF
* and should also be counted as a line.
*/
lineReader.skip(fileLength - 1);
return lineReader.getLineNumber() + 1;
} catch (IOException e) {
throw new InternalException("Failed to count lines of file %s",
file);
@@ -0,0 +1,37 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.unit;

import org.junit.Test;

import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.util.FileUtil;

public class FileUtilTest {

@Test
public void testCountLines() {
int lines = FileUtil.countLines("target/test-classes/files/file_with_empty_line");
Assert.assertEquals(3, lines);

lines = FileUtil.countLines("target/test-classes/files/file_without_empty_line");
Assert.assertEquals(3, lines);
}
}
@@ -24,7 +24,8 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
EntityUtilTest.class
EntityUtilTest.class,
FileUtilTest.class
})
public class UnitTestSuite {
}
@@ -0,0 +1,3 @@
1
2
3
@@ -0,0 +1,3 @@
1
2
3

0 comments on commit 5c038a0

Please sign in to comment.