Skip to content

Commit

Permalink
* 默认对象反射缓存改成ThreadLocal,并支持设置 [Issue #2792](#2792)
Browse files Browse the repository at this point in the history
* 支持根据`includeColumnIndexes`和`includeColumnFieldNames`排序 [Issue #2697](#2697)
  • Loading branch information
zhuangjiaju committed Apr 28, 2023
1 parent e6f7078 commit e9856e5
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.NumberDataFormatterUtils;
Expand Down Expand Up @@ -212,6 +213,7 @@ public void finish() {
private void removeThreadLocalCache() {
NumberDataFormatterUtils.removeThreadLocalCache();
DateUtils.removeThreadLocalCache();
ClassUtils.removeThreadLocalCache();
}

private void clearEncrypt03() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private void addOneRowOfHeadDataToExcel(Row row, Integer rowIndex, Map<Integer,
Head head = entry.getValue();
int columnIndex = entry.getKey();
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null,
currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName());
currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName(), currentWriteHolder);

CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(this, row,
rowIndex, head, columnIndex, relativeRowIndex, Boolean.TRUE, excelContentProperty);
Expand Down Expand Up @@ -427,6 +427,7 @@ public void finish(boolean onException) {
private void removeThreadLocalCache() {
NumberDataFormatterUtils.removeThreadLocalCache();
DateUtils.removeThreadLocalCache();
ClassUtils.removeThreadLocalCache();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alibaba.excel.enums;

/**
* cache locaciton
*
* @author Jiaju Zhuang
**/
public enum CacheLocationEnum {
/**
* The cache will be stored in {@code ThreadLocal}, and will be cleared when the excel read and write is completed.
*/
THREAD_LOCAL,

/**
* The cache will not be cleared unless the app is stopped.
*/
MEMORY,

/**
* No caching.It may lose some of performance.
*/
NONE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstrac
globalConfiguration.setLocale(basicParameter.getLocale());
}

if (basicParameter.getFiledCacheLocation() == null) {
if (prentAbstractHolder != null) {
globalConfiguration.setFiledCacheLocation(
prentAbstractHolder.getGlobalConfiguration().getFiledCacheLocation());
}
} else {
globalConfiguration.setFiledCacheLocation(basicParameter.getFiledCacheLocation());
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Locale;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CacheLocationEnum;

import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -54,4 +55,11 @@ public class BasicParameter {
* default is false
*/
private Boolean useScientificFormat;

/**
* The cache used when parsing fields such as head.
*
* default is THREAD_LOCAL.
*/
private CacheLocationEnum filedCacheLocation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.alibaba.excel.metadata;

import java.lang.reflect.Field;
import java.util.Map;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

/**
* filed cache
*
* @author Jiaju Zhuang
*/
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
public class FieldCache {

/**
* A field cache that has been sorted by a class.
* It will exclude fields that are not needed.
*/
private Map<Integer, Field> sortedFieldMap;

/**
* Fields using the index attribute
*/
private Map<Integer, Field> indexFieldMap;

/**
* Fields to ignore
*/
private Map<String, Field> ignoreMap;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Locale;

import com.alibaba.excel.enums.CacheLocationEnum;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -32,17 +34,26 @@ public class GlobalConfiguration {
* used when formatting dates and numbers.
*/
private Locale locale;

/**
* Whether to use scientific Format.
*
* default is false
*/
private Boolean useScientificFormat;

/**
* The cache used when parsing fields such as head.
*
* default is THREAD_LOCAL.
*/
private CacheLocationEnum filedCacheLocation;

public GlobalConfiguration() {
this.autoTrim = Boolean.TRUE;
this.use1904windowing = Boolean.FALSE;
this.locale = Locale.getDefault();
this.useScientificFormat = Boolean.FALSE;
this.filedCacheLocation = CacheLocationEnum.MEMORY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.metadata.ConfigurationHolder;
import com.alibaba.excel.metadata.FieldCache;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.util.ClassUtils;
Expand Down Expand Up @@ -51,22 +53,17 @@ public class ExcelHeadProperty {
* Configuration header information
*/
private Map<Integer, Head> headMap;
/**
* Fields ignored
*/
private Map<String, Field> ignoreMap;

public ExcelHeadProperty(Holder holder, Class<?> headClazz, List<List<String>> head) {
public ExcelHeadProperty(ConfigurationHolder configurationHolder, Class<?> headClazz, List<List<String>> head) {
this.headClazz = headClazz;
headMap = new TreeMap<>();
ignoreMap = MapUtils.newHashMap();
headKind = HeadKindEnum.NONE;
headRowNumber = 0;
if (head != null && !head.isEmpty()) {
int headIndex = 0;
for (int i = 0; i < head.size(); i++) {
if (holder instanceof AbstractWriteHolder) {
if (((AbstractWriteHolder)holder).ignore(null, i)) {
if (configurationHolder instanceof AbstractWriteHolder) {
if (((AbstractWriteHolder)configurationHolder).ignore(null, i)) {
continue;
}
}
Expand All @@ -76,7 +73,7 @@ public ExcelHeadProperty(Holder holder, Class<?> headClazz, List<List<String>> h
headKind = HeadKindEnum.STRING;
}
// convert headClazz to head
initColumnProperties(holder);
initColumnProperties(configurationHolder);

initHeadRowNumber();
if (LOGGER.isDebugEnabled()) {
Expand Down Expand Up @@ -104,24 +101,15 @@ private void initHeadRowNumber() {
}
}

private void initColumnProperties(Holder holder) {
private void initColumnProperties(ConfigurationHolder configurationHolder) {
if (headClazz == null) {
return;
}
// Declared fields
Map<Integer, Field> sortedAllFieldMap = MapUtils.newTreeMap();
Map<Integer, Field> indexFieldMap = MapUtils.newTreeMap();

boolean needIgnore = (holder instanceof AbstractWriteHolder) && (
!CollectionUtils.isEmpty(((AbstractWriteHolder)holder).getExcludeColumnFieldNames()) || !CollectionUtils
.isEmpty(((AbstractWriteHolder)holder).getExcludeColumnIndexes()) || !CollectionUtils
.isEmpty(((AbstractWriteHolder)holder).getIncludeColumnFieldNames()) || !CollectionUtils
.isEmpty(((AbstractWriteHolder)holder).getIncludeColumnIndexes()));

ClassUtils.declaredFields(headClazz, sortedAllFieldMap, indexFieldMap, ignoreMap, needIgnore, holder);
FieldCache fieldCache = ClassUtils.declaredFields(headClazz, configurationHolder);

for (Map.Entry<Integer, Field> entry : sortedAllFieldMap.entrySet()) {
initOneColumnProperty(entry.getKey(), entry.getValue(), indexFieldMap.containsKey(entry.getKey()));
for (Map.Entry<Integer, Field> entry : fieldCache.getSortedFieldMap().entrySet()) {
initOneColumnProperty(entry.getKey(), entry.getValue(),
fieldCache.getIndexFieldMap().containsKey(entry.getKey()));
}
headKind = HeadKindEnum.CLASS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ private Object buildUserModel(Map<Integer, ReadCellData<?>> cellDataMap, ReadShe
ReadCellData<?> cellData = cellDataMap.get(index);
Object value = ConverterUtils.convertToJavaObject(cellData, head.getField(),
ClassUtils.declaredExcelContentProperty(dataMap, readSheetHolder.excelReadHeadProperty().getHeadClazz(),
fieldName), readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(), index);
fieldName, readSheetHolder), readSheetHolder.converterMap(), context,
context.readRowHolder().getRowIndex(), index);
if (value != null) {
dataMap.put(fieldName, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.alibaba.excel.metadata.ConfigurationHolder;
import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.metadata.property.ExcelHeadProperty;

Expand All @@ -12,7 +13,7 @@
*/
public class ExcelReadHeadProperty extends ExcelHeadProperty {

public ExcelReadHeadProperty(Holder holder, Class headClazz, List<List<String>> head) {
super(holder, headClazz, head);
public ExcelReadHeadProperty(ConfigurationHolder configurationHolder, Class headClazz, List<List<String>> head) {
super(configurationHolder, headClazz, head);
}
}

0 comments on commit e9856e5

Please sign in to comment.