Skip to content

Commit

Permalink
Merge pull request #2965 from alibaba/bugfix
Browse files Browse the repository at this point in the history
Bugfix
  • Loading branch information
zhuangjiaju committed Feb 10, 2023
2 parents 526629f + 4514b55 commit 5fa867b
Show file tree
Hide file tree
Showing 93 changed files with 736 additions and 135 deletions.
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ assignees: zhuangjiaju
这里写代码
```
# 提示的异常或者没有达到的效果
大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ assignees: ''
```
# 异常提示
大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。
# 问题描述
2 changes: 1 addition & 1 deletion .github/workflows/sync2gitee.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 通过 Github action, 在仓库的每一次 commit 后自动同步到 Gitee 上
name: Mirror the Github organization repos to Gitee
on: [push, pull_request]
on: [push]

jobs:
repo-sync:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析
* github地址:[https://github.com/alibaba/easyexcel](https://github.com/alibaba/easyexcel)
* gitee地址:[https://gitee.com/easyexcel/easyexcel](https://gitee.com/easyexcel/easyexcel)

# 64M内存20秒读取75M(46W行25列)的Excel(3.0.2+版本)
# 16M内存23秒读取75M(46W行25列)的Excel(3.2.1+版本)

当然还有[极速模式](https://easyexcel.opensource.alibaba.com/qa/read#%E5%BC%80%E5%90%AF%E6%80%A5%E9%80%9F%E6%A8%A1%E5%BC%8F)
能更快,但是内存占用会在100M多一点
Expand All @@ -28,7 +28,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion easyexcel-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel-parent</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<url>https://github.com/alibaba/easyexcel</url>
<packaging>jar</packaging>
<artifactId>easyexcel-core</artifactId>
<name>easyexcel-core</name>
Expand Down
16 changes: 16 additions & 0 deletions easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook;

import lombok.extern.slf4j.Slf4j;

/**
* Excel readers are all read in event mode.
*
* @author jipengfei
*/
@Slf4j
public class ExcelReader implements Closeable {

/**
Expand Down Expand Up @@ -104,4 +107,17 @@ public void finish() {
public void close() {
finish();
}

/**
* Prevents calls to {@link #finish} from freeing the cache
*
*/
@Override
protected void finalize() {
try {
finish();
} catch (Throwable e) {
log.warn("Destroy object failed", e);
}
}
}
15 changes: 15 additions & 0 deletions easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.alibaba.excel.write.metadata.WriteWorkbook;
import com.alibaba.excel.write.metadata.fill.FillConfig;

import lombok.extern.slf4j.Slf4j;

/**
* Excel Writer This tool is used to write value out to Excel via POI. This object can perform the following two
* functions.
Expand All @@ -23,6 +25,7 @@
*
* @author jipengfei
*/
@Slf4j
public class ExcelWriter implements Closeable {

private final ExcelBuilder excelBuilder;
Expand Down Expand Up @@ -154,4 +157,16 @@ public WriteContext writeContext() {
public void close() {
finish();
}

/**
* Prevents calls to {@link #finish} from freeing the cache
*/
@Override
protected void finalize() {
try {
finish();
} catch (Throwable e) {
log.warn("Destroy object failed", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class BoundSheetRecordHandler extends AbstractXlsRecordHandler implements
@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
BoundSheetRecord bsr = (BoundSheetRecord)record;
xlsReadContext.xlsReadWorkbookHolder().getBoundSheetRecordList().add((BoundSheetRecord)record);
xlsReadContext.xlsReadWorkbookHolder().getBoundSheetRecordList().add(bsr);
}
}
60 changes: 48 additions & 12 deletions easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Optional;
import java.util.UUID;

import com.alibaba.excel.context.AnalysisContext;
Expand All @@ -15,6 +16,7 @@
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;

/**
Expand All @@ -35,6 +37,8 @@ public class Ehcache implements ReadCache {
private static final CacheManager FILE_CACHE_MANAGER;
private static final CacheConfiguration<Integer, ArrayList> FILE_CACHE_CONFIGURATION;
private static final CacheManager ACTIVE_CACHE_MANAGER;
private static final File CACHE_PATH_FILE;

private final CacheConfiguration<Integer, ArrayList> activeCacheConfiguration;
/**
* Bulk storage data
Expand All @@ -50,29 +54,61 @@ public class Ehcache implements ReadCache {
*/
private int cacheMiss = 0;

public Ehcache(int maxCacheActivateSize) {
activeCacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, ArrayList.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(maxCacheActivateSize, MemoryUnit.MB))
.withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(maxCacheActivateSize, MemoryUnit.MB)
.build();
@Deprecated
public Ehcache(Integer maxCacheActivateSize) {
this(maxCacheActivateSize, null);
}

public Ehcache(Integer maxCacheActivateSize, Integer maxCacheActivateBatchCount) {
// In order to be compatible with the code
// If the user set up `maxCacheActivateSize`, then continue using it
if (maxCacheActivateSize != null) {
this.activeCacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, ArrayList.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(maxCacheActivateSize, MemoryUnit.MB))
.build();
} else {
this.activeCacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, ArrayList.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(maxCacheActivateBatchCount, EntryUnit.ENTRIES))
.build();
}
}

static {
File cacheFile = FileUtils.createCacheTmpFile();
CACHE_PATH_FILE = FileUtils.createCacheTmpFile();
FILE_CACHE_MANAGER =
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(cacheFile)).build(true);
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(CACHE_PATH_FILE)).build(
true);
ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true);
FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, ArrayList.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB))
.withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(10, MemoryUnit.GB).build();
.newCacheConfigurationBuilder(Integer.class, ArrayList.class, ResourcePoolsBuilder.newResourcePoolsBuilder()
.disk(20, MemoryUnit.GB)).build();
}

@Override
public void init(AnalysisContext analysisContext) {
cacheAlias = UUID.randomUUID().toString();
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION);
try {
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION);
} catch (IllegalStateException e) {
//fix Issue #2693,Temporary files may be deleted if there is no operation for a long time, so they need
// to be recreated.
if (CACHE_PATH_FILE.exists()) {
throw e;
}
synchronized (Ehcache.class) {
if (!CACHE_PATH_FILE.exists()) {
if (log.isDebugEnabled()) {
log.debug("cache file dir is not exist retry create");
}
FileUtils.createDirectory(CACHE_PATH_FILE);
}
}
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION);
}
activeCache = ACTIVE_CACHE_MANAGER.createCache(cacheAlias, activeCacheConfiguration);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.io.IOException;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -15,6 +19,9 @@
*
* @author Jiaju Zhuang
**/
@Getter
@Setter
@EqualsAndHashCode
public class SimpleReadCacheSelector implements ReadCacheSelector {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleReadCacheSelector.class);
/**
Expand All @@ -24,37 +31,46 @@ public class SimpleReadCacheSelector implements ReadCacheSelector {
/**
* If it's less than 5M, use map cache, or use ehcache.unit MB.
*/
private static final int DEFAULT_MAX_USE_MAP_CACHE_SIZE = 5;
private static final long DEFAULT_MAX_USE_MAP_CACHE_SIZE = 5;

/**
* Maximum size of cache activation.unit MB.
* Maximum batch of `SharedStrings` stored in memory.
* The batch size is 100.{@link Ehcache#BATCH_COUNT}
*/
private static final int DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE = 20;
private static final int DEFAULT_MAX_EHCACHE_ACTIVATE_BATCH_COUNT = 20;

/**
* Shared strings exceeding this value will use {@link Ehcache},or use {@link MapCache}.unit MB.
*/
private final long maxUseMapCacheSize;
private Long maxUseMapCacheSize;

/**
* Maximum size of cache activation.unit MB.
*
* @deprecated Please use maxCacheActivateBatchCount to control the size of the occupied memory
*/
@Deprecated
private Integer maxCacheActivateSize;

/**
* Maximum batch of `SharedStrings` stored in memory.
* The batch size is 100.{@link Ehcache#BATCH_COUNT}
*/
private final int maxCacheActivateSize;
private Integer maxCacheActivateBatchCount;

public SimpleReadCacheSelector() {
this(DEFAULT_MAX_USE_MAP_CACHE_SIZE, DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE);
}

public SimpleReadCacheSelector(long maxUseMapCacheSize, int maxCacheActivateSize) {
if (maxUseMapCacheSize <= 0) {
this.maxUseMapCacheSize = DEFAULT_MAX_USE_MAP_CACHE_SIZE;
} else {
this.maxUseMapCacheSize = maxUseMapCacheSize;
}
if (maxCacheActivateSize <= 0) {
this.maxCacheActivateSize = DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE;
} else {
this.maxCacheActivateSize = maxCacheActivateSize;
}
/**
* Parameter maxCacheActivateSize has already been abandoned
*
* @param maxUseMapCacheSize
* @param maxCacheActivateSize
*/
@Deprecated
public SimpleReadCacheSelector(Long maxUseMapCacheSize, Integer maxCacheActivateSize) {
this.maxUseMapCacheSize = maxUseMapCacheSize;
this.maxCacheActivateSize = maxCacheActivateSize;
}

@Override
Expand All @@ -68,6 +84,9 @@ public ReadCache readCache(PackagePart sharedStringsTablePackagePart) {
return new MapCache();
}
}
if (maxUseMapCacheSize == null) {
maxUseMapCacheSize = DEFAULT_MAX_USE_MAP_CACHE_SIZE;
}
if (size < maxUseMapCacheSize * B2M) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Use map cache.size:{}", size);
Expand All @@ -77,6 +96,17 @@ public ReadCache readCache(PackagePart sharedStringsTablePackagePart) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Use ehcache.size:{}", size);
}
return new Ehcache(maxCacheActivateSize);

// In order to be compatible with the code
// If the user set up `maxCacheActivateSize`, then continue using it
if (maxCacheActivateSize != null) {
return new Ehcache(maxCacheActivateSize, maxCacheActivateBatchCount);
} else {
if (maxCacheActivateBatchCount == null) {
maxCacheActivateBatchCount = DEFAULT_MAX_EHCACHE_ACTIVATE_BATCH_COUNT;
}
return new Ehcache(maxCacheActivateSize, maxCacheActivateBatchCount);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
import com.alibaba.excel.converters.integer.IntegerBooleanConverter;
import com.alibaba.excel.converters.integer.IntegerNumberConverter;
import com.alibaba.excel.converters.integer.IntegerStringConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateNumberConverter;
import com.alibaba.excel.converters.localdate.LocalDateDateConverter;
import com.alibaba.excel.converters.localdate.LocalDateNumberConverter;
import com.alibaba.excel.converters.localdate.LocalDateStringConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateTimeNumberConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateTimeDateConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateTimeStringConverter;
import com.alibaba.excel.converters.longconverter.LongBooleanConverter;
Expand Down Expand Up @@ -83,6 +86,9 @@ private static void initAllConverter() {
putAllConverter(new DateStringConverter());

putAllConverter(new LocalDateNumberConverter());
putAllConverter(new LocalDateStringConverter());

putAllConverter(new LocalDateTimeNumberConverter());
putAllConverter(new LocalDateTimeStringConverter());

putAllConverter(new DoubleBooleanConverter());
Expand Down Expand Up @@ -121,6 +127,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new ByteNumberConverter());
putWriteConverter(new DateDateConverter());
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter());
Expand All @@ -139,6 +146,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new BooleanStringConverter());
putWriteStringConverter(new ByteStringConverter());
putWriteStringConverter(new DateStringConverter());
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter());
Expand Down

0 comments on commit 5fa867b

Please sign in to comment.