Skip to content

Commit c7f80e9

Browse files
committed
optimize code
1 parent 86010cf commit c7f80e9

File tree

5 files changed

+32
-34
lines changed

5 files changed

+32
-34
lines changed

core/src/main/java/com/robin/core/base/dao/CommJdbcUtil.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.robin.core.query.util.QueryString;
2525
import com.robin.core.sql.util.BaseSqlGen;
2626
import com.robin.core.sql.util.FilterCondition;
27+
import lombok.NonNull;
2728
import org.apache.commons.dbutils.DbUtils;
2829
import org.apache.commons.lang3.StringUtils;
2930
import org.apache.commons.lang3.math.NumberUtils;
@@ -46,14 +47,15 @@
4647
import java.time.format.DateTimeFormatter;
4748
import java.util.*;
4849

50+
@SuppressWarnings("unused")
4951
public class CommJdbcUtil {
5052
private static final Logger logger = LoggerFactory.getLogger(CommJdbcUtil.class);
5153
private static final DateTimeFormatter dayFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
5254
private static final DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
5355

5456

5557
@SuppressWarnings({"unchecked", "rawtypes"})
56-
private static List getResultItemsByPrepared(JdbcTemplate jdbcTemplate, final PageQuery<Map<String, Object>> pageQuery, final String pageSQL) {
58+
private static List getResultItemsByPrepared(@NonNull JdbcTemplate jdbcTemplate, final PageQuery<Map<String, Object>> pageQuery, final String pageSQL) {
5759
Object ret = jdbcTemplate.query(conn -> {
5860
PreparedStatement ps = conn.prepareStatement(pageSQL);
5961
int len = pageQuery.getParameters().size();
@@ -72,11 +74,11 @@ private static List getResultItemsByPrepared(JdbcTemplate jdbcTemplate, final Pa
7274
ps.setLong(i, Long.parseLong(value));
7375
break;
7476
case Const.META_TYPE_DATE:
75-
Date date = new Date(Date.from(LocalDateTime.parse(value, dayFormat).atZone(ZoneId.systemDefault()).toInstant()).getTime());
77+
Date date = new Date(LocalDateTime.parse(value, dayFormat).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
7678
ps.setDate(i, date);
7779
break;
7880
case Const.META_TYPE_TIMESTAMP:
79-
Timestamp ts = new Timestamp(Date.from(LocalDateTime.parse(value, timeFormat).atZone(ZoneId.systemDefault()).toInstant()).getTime());
81+
Timestamp ts = new Timestamp(LocalDateTime.parse(value, timeFormat).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
8082
ps.setTimestamp(i, ts);
8183
break;
8284
default:
@@ -96,7 +98,7 @@ private static List getResultItemsByPrepared(JdbcTemplate jdbcTemplate, final Pa
9698
}
9799

98100
@SuppressWarnings({"rawtypes", "unchecked"})
99-
private static List getResultItemsByPreparedSimple(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedParameterJdbcTemplate, LobHandler lobHandler, final BaseSqlGen sqlGen, final QueryString qs, final PageQuery pageQuery, final String pageSQL) {
101+
private static List getResultItemsByPreparedSimple(@NonNull JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedParameterJdbcTemplate, LobHandler lobHandler, final BaseSqlGen sqlGen, final QueryString qs, final PageQuery pageQuery, final String pageSQL) {
100102
final String[] fields = sqlGen.getResultColName(qs);
101103
if (pageQuery.getNamedParameters().isEmpty()) {
102104
//Preparedstatment
@@ -129,7 +131,7 @@ static void queryByReplaceParamter(JdbcTemplate jdbcTemplate, LobHandler lobHand
129131
pageQuery.setRecordSet(list);
130132
}
131133

132-
private static List<?> getResultList(JdbcTemplate jdbcTemplate, LobHandler lobHandler, BaseSqlGen sqlGen, QueryString qs, PageQuery<Map<String, Object>> pageQuery, String querySQL, String sumSQL, int pageSize) throws DAOException {
134+
private static List<?> getResultList(@NonNull JdbcTemplate jdbcTemplate, LobHandler lobHandler, BaseSqlGen sqlGen, QueryString qs, PageQuery<Map<String, Object>> pageQuery, String querySQL, String sumSQL, int pageSize) throws DAOException {
133135
List<?> list;
134136
if (pageSize != 0) {
135137
if (pageSize < Integer.parseInt(Const.MIN_PAGE_SIZE)) {
@@ -220,17 +222,17 @@ private static String getReplacementSql(BaseSqlGen sqlGen, QueryString qs, PageQ
220222
return querySQL;
221223
}
222224

223-
private static List<Map> getResultItems(JdbcTemplate jdbcTemplate, LobHandler lobHandler, BaseSqlGen sqlGen, final PageQuery<Map<String, Object>> query, final QueryString qs, final String pageSQL) {
225+
private static List<Map<String,Object>> getResultItems(JdbcTemplate jdbcTemplate, LobHandler lobHandler, BaseSqlGen sqlGen, final PageQuery<Map<String, Object>> query, final QueryString qs, final String pageSQL) {
224226
//getResultColumn from QueryString
225227
final String[] fields = sqlGen.getResultColName(qs);
226228
return jdbcTemplate.query(pageSQL, getDefaultExtract(fields, lobHandler, query));
227229
}
228230

229231

230232
@SuppressWarnings("rawtypes")
231-
private static ResultSetExtractor<List<Map>> getDefaultExtract(final String[] fields, final LobHandler lobHandler, PageQuery query) {
233+
private static ResultSetExtractor<List<Map<String,Object>>> getDefaultExtract(final String[] fields, final LobHandler lobHandler, PageQuery query) {
232234
return rs -> {
233-
List<Map> list = new ArrayList<>();
235+
List<Map<String,Object>> list = new ArrayList<>();
234236

235237
ResultSetMetaData rsmd = rs.getMetaData();
236238
int count = rsmd.getColumnCount();
@@ -396,7 +398,7 @@ static void queryByPreparedParamter(JdbcTemplate jdbcTemplate, NamedParameterJdb
396398
sumSQL = sqlGen.getCountSqlByConfig(qs, pageQuery);
397399
}
398400

399-
Integer total;
401+
int total;
400402
if (CollectionUtils.isEmpty(pageQuery.getNamedParameters()) && !CollectionUtils.isEmpty(pageQuery.getQueryParameters())) {
401403
total = jdbcTemplate.queryForObject(sumSQL, pageQuery.getQueryParameters().toArray(), Integer.class);
402404
} else {

core/src/main/java/com/robin/core/base/dao/SqlMapperDao.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.jdbc.support.lob.LobHandler;
2020
import org.springframework.lang.NonNull;
2121
import org.springframework.util.Assert;
22+
import org.springframework.util.ObjectUtils;
2223

2324
import javax.sql.DataSource;
2425
import java.lang.reflect.Method;
@@ -86,7 +87,7 @@ public List<?> queryByMapper(String nameSpace, String id, PageQuery<Map<String,O
8687
CommJdbcUtil.setPageQuery(query, total);
8788
selectSql = sqlGen.generatePageSql(selectSql, query);
8889
}
89-
list = template.query(selectSql, paramMap, resultSetExtractor(sqlMapperConfigure, nameSpace, segment, lobHandler, query));
90+
list = template.query(selectSql, paramMap, resultSetExtractor(sqlMapperConfigure, nameSpace, segment, query));
9091

9192
} else {
9293
throw new DAOException("Mapper id" + id + " in namespace " + nameSpace + " is not a select Config!");
@@ -179,7 +180,7 @@ private void setGenerateKey(Object targetObj, String columnName, Number number)
179180
}
180181
}
181182

182-
private static ResultSetExtractor<List<?>> resultSetExtractor(SqlMapperConfigure mapper, String nameSpace, CompositeSegment segment, LobHandler lobHandler, PageQuery<Map<String,Object>> pageQuery) throws DAOException {
183+
private static ResultSetExtractor<List<?>> resultSetExtractor(SqlMapperConfigure mapper, String nameSpace, CompositeSegment segment, PageQuery<Map<String,Object>> pageQuery) throws DAOException {
183184
return resultSet -> {
184185
List retList = new ArrayList<>();
185186
if (resultSet.next()) {
@@ -189,7 +190,7 @@ private static ResultSetExtractor<List<?>> resultSetExtractor(SqlMapperConfigure
189190
String resultMap = segment.getResultMap();
190191
if (resultMap != null) {
191192
ResultMapperSegment segment1 = (ResultMapperSegment) mapper.getSegmentsMap().get(nameSpace).get(resultMap).right.get(0);
192-
if ("HashMap".equalsIgnoreCase(segment1.getClassName())) {
193+
if (ObjectUtils.isEmpty(segment1.getClassName()) || "HashMap".equalsIgnoreCase(segment1.getClassName())) {
193194
if (mapper.getSegmentsMap().get(nameSpace).containsKey(resultMap)) {
194195
Map<String, Object> map = new HashMap<>();
195196
for (int i = 0; i < count; i++) {

core/src/main/java/com/robin/core/base/service/BaseAnnotationJdbcService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public BaseAnnotationJdbcService(){
7373
if(type!=null){
7474
entityContent= AnnotationRetriever.getMappingTableByCache(type);
7575
}
76-
LicenseUtils.getInstance();
76+
//LicenseUtils.getInstance();
7777
}
7878

7979
@Override

hadooptool/src/main/java/com/robin/hadoop/hdfs/HDFSCallUtil.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,11 @@ public static List<Map<String, String>> listFileAndDirectory(final Configuration
248248
Path path = new Path(hdfsUrl);
249249
FileStatus[] status = fs.listStatus(path);
250250
Path[] listPaths = FileUtil.stat2Paths(status);
251-
boolean isDir;
252251
for (Path listPath : listPaths) {
253252
Map<String, String> map = new HashMap<>();
254-
if (isDirectory(config, listPath)) {
255-
isDir = true;
256-
} else {
257-
isDir = false;
258-
}
259253
map.put("name", listPath.getName());
260254
map.put("path", listPath.toString());
261-
map.put("isDir", isDir ? "1" : "0");
255+
map.put("isDir", isDirectory(config, listPath) ? "1" : "0");
262256
hdfsUrlList.add(map);
263257
}
264258
} catch (IOException e) {
@@ -549,8 +543,10 @@ public static boolean mergeToNewFile(final Configuration config, String sourcePa
549543
try (OutputStream outputStream = getHDFSOutputStream(config, newFilePath)) {
550544
for (String file : mergeList) {
551545
InputStream inputStream = getHDFSDataByInputStream(config, file);
552-
IOUtils.copyBytes(inputStream, outputStream, 4096);
553-
inputStream.close();
546+
if(!ObjectUtils.isEmpty(inputStream)) {
547+
IOUtils.copyBytes(inputStream, outputStream, 4096);
548+
inputStream.close();
549+
}
554550
}
555551
return true;
556552
} catch (IOException ex) {

hadooptool/src/main/java/com/robin/hadoop/hdfs/HDFSUtil.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.robin.core.fileaccess.meta.DataCollectionMeta;
66
import org.apache.hadoop.conf.Configuration;
77
import org.apache.hadoop.fs.FSDataOutputStream;
8-
import org.apache.hadoop.fs.FileSystem;
98
import org.apache.hadoop.security.UserGroupInformation;
109
import org.slf4j.Logger;
1110
import org.slf4j.LoggerFactory;
@@ -116,7 +115,7 @@ public boolean uploadByInputStream(@NonNull final InputStream in,@NonNull String
116115
if(!useSecurity) {
117116
return HDFSCallUtil.uploadByInputStream(config, in, toUrl, bufferSize);
118117
} else {
119-
return HDFSSecurityUtil.executeSecurityWithProxy(config, f-> HDFSCallUtil.uploadByInputStream(config,in,toUrl, bufferSize));
118+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.uploadByInputStream(config, in, toUrl, bufferSize)));
120119
}
121120
}
122121
public String uploadByInputStream(@NonNull final InputStream in,@NonNull String toUrl, int bufferSize,@NonNull String fromCharset,@NonNull String toCharset) throws HdfsException{
@@ -132,15 +131,15 @@ public synchronized boolean deleteHdsfUrl(@NonNull String uri,@NonNull String pa
132131
if(!useSecurity) {
133132
return HDFSCallUtil.deleteHdsfUrl(config, uri, path);
134133
} else {
135-
return HDFSSecurityUtil.executeSecurityWithProxy(config, f->HDFSCallUtil.deleteHdsfUrl(config,uri,path));
134+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.deleteHdsfUrl(config, uri, path)));
136135
}
137136
}
138137
public synchronized boolean emptyDirectory(String hdfsUrl) throws HdfsException{
139138
Assert.notNull(config,"configuration is null");
140139
if(!useSecurity) {
141140
return HDFSCallUtil.emptyDirectory(config, hdfsUrl);
142141
} else {
143-
return HDFSSecurityUtil.executeSecurityWithProxy(config, f->HDFSCallUtil.emptyDirectory(config,hdfsUrl));
142+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.emptyDirectory(config, hdfsUrl)));
144143
}
145144
}
146145

@@ -179,39 +178,39 @@ public boolean mkdir(String relativeName) throws HdfsException{
179178
if(!useSecurity) {
180179
return HDFSCallUtil.mkdir(config, relativeName);
181180
} else {
182-
return HDFSSecurityUtil.executeSecurityWithProxy(config, f->HDFSCallUtil.mkdir(config,relativeName));
181+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.mkdir(config, relativeName)));
183182
}
184183
}
185184

186185
public boolean isDirectory(String hdfsUrl) throws HdfsException{
187186
if(!useSecurity) {
188187
return HDFSCallUtil.isDirectory(config, hdfsUrl);
189188
} else {
190-
return HDFSSecurityUtil.executeSecurityWithProxy(config, f->HDFSCallUtil.isDirectory(config,hdfsUrl));
189+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.isDirectory(config, hdfsUrl)));
191190
}
192191
}
193192

194193
public boolean delete(String hdfsUrl) throws HdfsException{
195194
if(!useSecurity) {
196195
return HDFSCallUtil.delete(config, hdfsUrl);
197196
} else {
198-
return HDFSSecurityUtil.executeSecurityWithProxy(config, f->HDFSCallUtil.delete(config,hdfsUrl));
197+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.delete(config, hdfsUrl)));
199198
}
200199
}
201200

202201
public boolean setresp(String hdfsUrl,int resp) throws HdfsException{
203202
if(!useSecurity) {
204203
return HDFSCallUtil.setresp(config, hdfsUrl, resp);
205204
} else {
206-
return HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.setresp(config,hdfsUrl, resp));
205+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.setresp(config, hdfsUrl, resp)));
207206
}
208207
}
209208

210209
public boolean exists(String hdfsUrl) throws HdfsException{
211210
if(!useSecurity) {
212211
return HDFSCallUtil.exists(config, hdfsUrl);
213212
} else {
214-
return HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.exists(config,hdfsUrl));
213+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.exists(config, hdfsUrl)));
215214
}
216215
}
217216

@@ -227,15 +226,15 @@ public boolean copyToLocal(String hdfsUrl,String toUrl) throws HdfsException{
227226
if(!useSecurity) {
228227
return HDFSCallUtil.copyToLocal(config, hdfsUrl,toUrl);
229228
} else {
230-
return HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.copyFromLocal(config,hdfsUrl,toUrl));
229+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.copyFromLocal(config, hdfsUrl, toUrl)));
231230
}
232231
}
233232

234233
public boolean copyFromLocal(String hdfsUrl,String toUrl) throws HdfsException{
235234
if(!useSecurity) {
236235
return HDFSCallUtil.copyFromLocal(config, hdfsUrl,toUrl);
237236
} else {
238-
return HDFSSecurityUtil.executeSecurityWithProxy(config,f -> HDFSCallUtil.copyFromLocal(config,hdfsUrl,toUrl));
237+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.copyFromLocal(config, hdfsUrl, toUrl)));
239238
}
240239
}
241240

@@ -350,7 +349,7 @@ public boolean chmod(String path,short permission) throws HdfsException{
350349
if(!useSecurity){
351350
return HDFSCallUtil.chmod(config,path,permission);
352351
}else{
353-
return HDFSSecurityUtil.executeSecurityWithProxy(config,f->HDFSCallUtil.chmod(config,path,permission));
352+
return Boolean.TRUE.equals(HDFSSecurityUtil.executeSecurityWithProxy(config, f -> HDFSCallUtil.chmod(config, path, permission)));
354353
}
355354
}
356355
public OutputStream getHDFSRawOutputStream(String path) throws HdfsException{

0 commit comments

Comments
 (0)