code refactoring

This commit is contained in:
Ivan Nikolaev-Axenov 2026-05-27 13:21:13 +03:00
parent 4d9cdf9c9f
commit b73ecd8635
9 changed files with 73 additions and 77 deletions

View file

@ -16,7 +16,7 @@ public class Settings {
public Settings() { public Settings() {
} }
public Settings(Long id, Long version, String name, String value) { public Settings(Long id, @Nullable Long version, @Nullable String name, @Nullable String value) {
this.id = id; this.id = id;
this.version = version; this.version = version;
this.name = name; this.name = name;
@ -31,27 +31,28 @@ public class Settings {
this.id = id; this.id = id;
} }
@Nullable
public Long getVersion() { public Long getVersion() {
return version; return version;
} }
public void setVersion(Long version) { public void setVersion(@Nullable Long version) {
this.version = version; this.version = version;
} }
public String getName() { public @Nullable String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(@Nullable String name) {
this.name = name; this.name = name;
} }
public String getValue() { public @Nullable String getValue() {
return value; return value;
} }
public void setValue(String value) { public void setValue(@Nullable String value) {
this.value = value; this.value = value;
} }

View file

@ -7,11 +7,7 @@ import ru.nbch.indicsearchservice.model.LocalizationObject;
@Mapper @Mapper
public interface LocalizationObjectMapper { public interface LocalizationObjectMapper {
List<LocalizationObject> findByCode(String code);
List<LocalizationObject> findAllByCodes(@Param("codes") List<String> codes); List<LocalizationObject> findAllByCodes(@Param("codes") List<String> codes);
void insert(LocalizationObject localization);
void insertBatch(@Param("list") List<LocalizationObject> list); void insertBatch(@Param("list") List<LocalizationObject> list);
} }

View file

@ -1,6 +1,5 @@
package ru.nbch.indicsearchservice.repository.bank; package ru.nbch.indicsearchservice.repository.bank;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
@ -13,9 +12,5 @@ public interface SettingsMapper {
List<Settings> findAllByNames(@Param("names") List<String> names); List<Settings> findAllByNames(@Param("names") List<String> names);
Collection<Settings> findAll();
void insert(@Param("settings") Settings settings);
void insertBatch(@Param("list") List<Settings> list); void insertBatch(@Param("list") List<Settings> list);
} }

View file

@ -199,7 +199,7 @@ public class IndicSearchService {
for (String key : columnKeys) { for (String key : columnKeys) {
String table = keyToTable.get(key); String table = keyToTable.get(key);
Optional<String> valueOpt = settingsValues.getOrDefault(key, Optional.empty()); Optional<String> valueOpt = settingsValues.get(key);
if (valueOpt.isPresent() && StringUtils.isNotBlank(valueOpt.get())) { if (valueOpt.isPresent() && StringUtils.isNotBlank(valueOpt.get())) {
String[] fields = valueOpt.get().split(";"); String[] fields = valueOpt.get().split(";");
@ -272,7 +272,7 @@ public class IndicSearchService {
String tableKey = "db.viewer.table." + tableUpper; String tableKey = "db.viewer.table." + tableUpper;
// Локализация для таблиц // Локализация для таблиц
Optional<String> tableLoc = localizationValues.getOrDefault(tableKey, Optional.empty()); Optional<String> tableLoc = localizationValues.get(tableKey);
if (tableLoc.isPresent() && StringUtils.isNotBlank(tableLoc.get())) { if (tableLoc.isPresent() && StringUtils.isNotBlank(tableLoc.get())) {
metaData.setTableNameLocalization(tableLoc.get()); metaData.setTableNameLocalization(tableLoc.get());
} else { } else {
@ -291,12 +291,12 @@ public class IndicSearchService {
String primaryKey = "db.viewer." + tableUpper + ".field." + fieldUpper; String primaryKey = "db.viewer." + tableUpper + ".field." + fieldUpper;
String fallbackKey = "db.viewer.field." + fieldUpper; String fallbackKey = "db.viewer.field." + fieldUpper;
Optional<String> primaryLoc = localizationValues.getOrDefault(primaryKey, Optional.empty()); Optional<String> primaryLoc = localizationValues.get(primaryKey);
if (primaryLoc.isPresent() && StringUtils.isNotBlank(primaryLoc.get())) { if (primaryLoc.isPresent() && StringUtils.isNotBlank(primaryLoc.get())) {
metaData.addLocalization(field, primaryLoc.get()); metaData.addLocalization(field, primaryLoc.get());
} else { } else {
Optional<String> fallbackLoc = localizationValues.getOrDefault(fallbackKey, Optional.empty()); Optional<String> fallbackLoc = localizationValues.get(fallbackKey);
if (fallbackLoc.isPresent() && StringUtils.isNotBlank(fallbackLoc.get())) { if (fallbackLoc.isPresent() && StringUtils.isNotBlank(fallbackLoc.get())) {
metaData.addLocalization(field, fallbackLoc.get()); metaData.addLocalization(field, fallbackLoc.get());
} else { } else {

View file

@ -4,10 +4,10 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.nbch.indicsearchservice.config.settings.ApplicationSettings; import ru.nbch.indicsearchservice.config.settings.ApplicationSettings;
@ -41,12 +41,25 @@ public class SettingsService {
return Map.of(); return Map.of();
} }
List<Settings> foundValues = settingsMapper.findAllByNames(names); Map<String, Optional<String>> resultMap = new HashMap<>();
if (foundValues == null || foundValues.isEmpty()) { for (String name : names) {
return Map.of(); if (name != null) {
resultMap.put(name, Optional.empty());
}
} }
return foundValues.stream().collect(Collectors.toMap(Settings::getName, s -> Optional.ofNullable(s.getValue()))); List<Settings> foundValues = settingsMapper.findAllByNames(names);
if (foundValues == null || foundValues.isEmpty()) {
return resultMap;
}
for (Settings setting : foundValues) {
if (setting != null && setting.getName() != null) {
resultMap.put(setting.getName(), Optional.ofNullable(setting.getValue()));
}
}
return resultMap;
} }
public Optional<String> getOptionalSettingsValueByName(String name) { public Optional<String> getOptionalSettingsValueByName(String name) {

View file

@ -4,8 +4,10 @@ import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -201,18 +203,31 @@ public class DataSourceUpdater {
case INDIC -> "indic"; case INDIC -> "indic";
}; };
DataSourceSettings dataSourceSettings = new DataSourceSettings(); Map<String, Optional<String>> fetchedSettings = settingsService.getSettingsValuesByNames(List.of(
dataSourceSettings.setDriver(settingsService.getSettingsValueByName("datasource." + key + ".driver")); "datasource." + key + ".driver",
dataSourceSettings.setUrl(settingsService.getSettingsValueByName("datasource." + key + ".url")); "datasource." + key + ".url",
dataSourceSettings.setUsername(settingsService.getSettingsValueByName("datasource." + key + ".username")); "datasource." + key + ".username",
dataSourceSettings.setPassword(settingsService.getSettingsValueByName("datasource." + key + ".password")); "datasource." + key + ".password",
"datasource." + key + ".connection-timeout",
"datasource." + key + ".validation-timeout",
"datasource." + key + ".idle-timeout",
"datasource." + key + ".max-pool-size",
"datasource." + key + ".max-lifetime",
"datasource." + key + ".keep-alive-time"
));
dataSourceSettings.setConnectionTimeout(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".connection-timeout").orElse("30000"))); DataSourceSettings dataSourceSettings = new DataSourceSettings();
dataSourceSettings.setValidationTimeout(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".validation-timeout").orElse("5000"))); dataSourceSettings.setDriver(fetchedSettings.get("datasource." + key + ".driver").orElseThrow(() -> new RuntimeException("Required SETTINGS not found!")));
dataSourceSettings.setIdleTimeout(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".idle-timeout").orElse("600000"))); dataSourceSettings.setUrl(fetchedSettings.get("datasource." + key + ".url").orElseThrow(() -> new RuntimeException("Required SETTINGS not found!")));
dataSourceSettings.setMaxPoolSize(Integer.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".max-pool-size").orElse("100"))); dataSourceSettings.setUsername(fetchedSettings.get("datasource." + key + ".username").orElseThrow(() -> new RuntimeException("Required SETTINGS not found!")));
dataSourceSettings.setMaxLifetime(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".max-lifetime").orElse("1800000"))); dataSourceSettings.setPassword(fetchedSettings.get("datasource." + key + ".password").orElseThrow(() -> new RuntimeException("Required SETTINGS not found!")));
dataSourceSettings.setKeepAliveTime(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".keep-alive-time").orElse("120000")));
dataSourceSettings.setConnectionTimeout(Long.valueOf(fetchedSettings.get("datasource." + key + ".connection-timeout").orElse("30000")));
dataSourceSettings.setValidationTimeout(Long.valueOf(fetchedSettings.get("datasource." + key + ".validation-timeout").orElse("5000")));
dataSourceSettings.setIdleTimeout(Long.valueOf(fetchedSettings.get("datasource." + key + ".idle-timeout").orElse("600000")));
dataSourceSettings.setMaxPoolSize(Integer.valueOf(fetchedSettings.get("datasource." + key + ".max-pool-size").orElse("100")));
dataSourceSettings.setMaxLifetime(Long.valueOf(fetchedSettings.get("datasource." + key + ".max-lifetime").orElse("1800000")));
dataSourceSettings.setKeepAliveTime(Long.valueOf(fetchedSettings.get("datasource." + key + ".keep-alive-time").orElse("120000")));
settingsConsumer.accept(dataSourceSettings); settingsConsumer.accept(dataSourceSettings);
} }

View file

@ -5,6 +5,7 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -12,7 +13,6 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.ibatis.cursor.Cursor; import org.apache.ibatis.cursor.Cursor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ru.nbch.indicsearchservice.config.settings.ApplicationSettings; import ru.nbch.indicsearchservice.config.settings.ApplicationSettings;
@ -105,11 +105,24 @@ public class IndicSearchUtils {
return Map.of(); return Map.of();
} }
List<LocalizationObject> foundValues = localizationObjectMapper.findAllByCodes(localizationCodes); Map<String, Optional<String>> resultMap = new HashMap<>();
if (foundValues == null || foundValues.isEmpty()) { for (String code : localizationCodes) {
return Map.of(); if (code != null) {
resultMap.put(code, Optional.empty());
}
} }
return foundValues.stream().collect(Collectors.toMap(LocalizationObject::getCode, s -> Optional.ofNullable(s.getText()))); List<LocalizationObject> foundValues = localizationObjectMapper.findAllByCodes(localizationCodes);
if (foundValues == null || foundValues.isEmpty()) {
return resultMap;
}
for (LocalizationObject localization : foundValues) {
if (localization != null && localization.getCode() != null) {
resultMap.put(localization.getCode(), Optional.ofNullable(localization.getText()));
}
}
return resultMap;
} }
} }

View file

@ -28,12 +28,6 @@
FROM LOCALIZATION_OBJECT FROM LOCALIZATION_OBJECT
</sql> </sql>
<select id="findByCode" resultMap="localizationObjectResultMap">
<include refid="baseSelect"/>
WHERE CODE = #{code}
WITH UR
</select>
<select id="findAllByCodes" resultMap="localizationObjectResultMap"> <select id="findAllByCodes" resultMap="localizationObjectResultMap">
<include refid="baseSelect"/> <include refid="baseSelect"/>
WHERE CODE IN WHERE CODE IN
@ -41,23 +35,6 @@
WITH UR WITH UR
</select> </select>
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
INSERT INTO LOCALIZATION_OBJECT (VERSION,
CODE,
DATE_CREATED,
LAST_UPDATED,
LOC,
RELEVANCE,
TEXT)
VALUES (0,
#{code},
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
#{loc},
#{relevance},
#{text})
</insert>
<insert id="insertBatch" useGeneratedKeys="true" keyProperty="id"> <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
INSERT INTO LOCALIZATION_OBJECT (VERSION, INSERT INTO LOCALIZATION_OBJECT (VERSION,
CODE, CODE,

View file

@ -30,20 +30,6 @@
WITH UR WITH UR
</select> </select>
<select id="findAll" resultMap="settingsResultMap">
<include refid="baseSelect"/>
WITH UR
</select>
<insert id="insert" useGeneratedKeys="true" keyProperty="settings.id" keyColumn="ID">
insert into SETTINGS (VERSION,
NAME,
"VALUE")
values (0,
#{settings.name},
#{settings.value})
</insert>
<insert id="insertBatch"> <insert id="insertBatch">
insert into SETTINGS (VERSION, NAME, "VALUE") insert into SETTINGS (VERSION, NAME, "VALUE")
values values