Skip to content

Commit

Permalink
修改目录状态,优化增加的问题.
Browse files Browse the repository at this point in the history
  • Loading branch information
vnobo committed Nov 6, 2023
1 parent ddf216d commit 217d046
Show file tree
Hide file tree
Showing 24 changed files with 13 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.time.LocalDateTime;

/**
* Error response wrapper class
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
public record ErrorResponse(String requestId, String path, Integer code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.io.Serializable;

/**
* Snowflake 算法生成分布式唯一 ID
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
public class Snowflake implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,13 @@
import java.util.List;

/**
* This class provides global exception handling for the application.
* It handles various types of exceptions and returns an appropriate error response.
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
@ControllerAdvice
public class GlobalExceptionHandler {

private static final Log log = LogFactory.getLog(GlobalExceptionHandler.class);

/**
* Handles exceptions thrown when there is an error in the input provided by the client.
*
* @param exchange the server web exchange
* @param ex the exception thrown
* @return a response entity with an error response
*/
@ExceptionHandler(ServerWebInputException.class)
public ResponseEntity<ErrorResponse> handleBindException(ServerWebExchange exchange, ServerWebInputException ex) {
List<String> errors = Lists.newArrayList(ex.getLocalizedMessage());
Expand All @@ -62,14 +52,6 @@ public ResponseEntity<ErrorResponse> handleBindException(ServerWebExchange excha
4170, "请求参数验证失败!", errors));
}


/**
* Handles exceptions thrown when there is an error in the database operation.
*
* @param exchange the server web exchange
* @param ex the exception thrown
* @return a response entity with an error response
*/
@ExceptionHandler({DataAccessException.class, R2dbcException.class})
public ResponseEntity<ErrorResponse> handleFailureException(ServerWebExchange exchange, RuntimeException ex) {
List<String> errors = Lists.newArrayList(ex.getLocalizedMessage());
Expand All @@ -92,13 +74,6 @@ public ResponseEntity<ErrorResponse> handleFailureException(ServerWebExchange ex
5070, "数据库操作错误!", errors));
}

/**
* Handles exceptions thrown when there is an error in the client request.
*
* @param exchange the server web exchange
* @param ex the exception thrown
* @return a response entity with an error response
*/
@ExceptionHandler(ClientException.class)
public ResponseEntity<ErrorResponse> handleClientException(ServerWebExchange exchange, ClientException ex) {
log.error("%s内部服务访问错误! 信息: %s".formatted(exchange.getLogPrefix(), ex.getMessage()));
Expand All @@ -110,13 +85,6 @@ public ResponseEntity<ErrorResponse> handleClientException(ServerWebExchange exc
ex.getCode(), ex.getServiceId() + "内部服务访问错误!", ex.getMsg()));
}

/**
* Handles exceptions thrown when there is an error in the server.
*
* @param exchange the server web exchange
* @param ex the exception thrown
* @return a response entity with an error response
*/
@ExceptionHandler(RestServerException.class)
public ResponseEntity<ErrorResponse> handleRestServerException(ServerWebExchange exchange, RestServerException ex) {
log.error("%s服务器自定义错误! 信息: %s".formatted(exchange.getLogPrefix(), ex.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import lombok.Getter;

/**
* This class represents an exception that occurs when a client request is invalid.
* It extends the RestServerException class.
* It contains a serviceId field and methods to set and get it.
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import java.io.IOException;

/**
* The JsonException class is used to handle JSON exceptions.
* It inherits from the RestServerException class.
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
* @see RestServerException
* @since 1.0
*/
public class JsonException extends RestServerException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
import java.io.Serializable;

/**
* This class represents a custom exception for the system.
* It extends the RuntimeException class and implements Serializable.
* It contains a message and a code to identify the error.
* The class also provides static methods to create instances of the exception.
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
import java.util.Map;

/**
* This is an abstract class that provides a base implementation for database services.
* It sets up the R2dbcEntityTemplate, DatabaseClient, and R2dbcConverter for use in the service.
* Extend this class to create a new database service.
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
public abstract class AbstractDatabase extends AbstractService {
Expand All @@ -31,99 +27,53 @@ public abstract class AbstractDatabase extends AbstractService {
protected DatabaseClient databaseClient;
protected R2dbcConverter r2dbcConverter;

/**
* Query entities that meet the conditions with cache.
*
* @param key cache key
* @param entityClass entity class
* @param query query condition
* @param <T> generic type, representing entity class
* @return a Flux containing entity class objects
*/
protected <T> Flux<T> queryWithCache(Object key, Query query, Class<T> entityClass) {
// Construct the query request and sort the results
Flux<T> source = this.entityTemplate.select(query, entityClass);
// If there is no data in the cache, return the query result; otherwise, return the cache data
return queryWithCache(key, source).cache();
}

/**
* Query entities that meet the conditions with cache.
*
* @param key cache key
* @param entityClass entity class
* @param query query condition
* @param <T> generic type, representing entity class
* @return a Flux containing entity class objects
*/
protected <T> Flux<T> queryWithCache(Object key, String query,
Map<String, Object> bindParams, Class<T> entityClass) {
// Create a GenericExecuteSpec object from the given query
DatabaseClient.GenericExecuteSpec executeSpec = this.databaseClient.sql(() -> query);
// Bind the given parameters to the query
for (Map.Entry<String, Object> e : bindParams.entrySet()) {
executeSpec = executeSpec.bind(e.getKey(), e.getValue());
}
// Read the results of the query into an entity class
Flux<T> source = executeSpec
.map((row, rowMetadata) -> this.r2dbcConverter.read(entityClass, row, rowMetadata)).all();
// If there is no data in the cache, return the query result; otherwise, return the cache data
return queryWithCache(key, source);
}

/**
* Query data from cache.
*
* @param key cache key
* @param sourceMono data source
* @return query result
*/
protected <T> Flux<T> queryWithCache(Object key, Flux<T> sourceMono) {
String cacheKey = key + ":data";
// Get data from cache
Collection<T> cacheData = this.cache.get(cacheKey, ArrayList::new);
// Note: the returned list will not be null
assert cacheData != null;

// Construct the query request and sort the results
Flux<T> source = sourceMono
// Add the query result to the cache
.doOnNext(cacheData::add)
// When the query is complete, store the data in the cache
.doAfterTerminate(() -> this.cachePut(cacheKey, cacheData));
// If there is no data in the cache, return the query result; otherwise, return the cache data
return Flux.fromIterable(ObjectUtils.isEmpty(cacheData) ? Collections.emptyList() : cacheData)
.switchIfEmpty(source);
}

protected <T> Mono<Long> countWithCache(Object key, Query query, Class<T> entityClass) {
// 将查询结果添加到缓存中
Mono<Long> source = this.entityTemplate.count(query, entityClass);
// 如果缓存中没有数据,返回查询结果;否则返回缓存数据
return countWithCache(key, source);
}

protected Mono<Long> countWithCache(Object key, String query, Map<String, Object> bindParams) {
// Create a GenericExecuteSpec object from the given query
DatabaseClient.GenericExecuteSpec executeSpec = this.databaseClient.sql(() -> query);
// Bind the given parameters to the query
for (Map.Entry<String, Object> e : bindParams.entrySet()) {
executeSpec = executeSpec.bind(e.getKey(), e.getValue());
}
// Read the results of the query into an entity class
Mono<Long> source = executeSpec
.map(readable -> readable.get(0, Long.class)).one();
// 如果缓存中没有数据,返回查询结果;否则返回缓存数据
return countWithCache(key, source);
}

protected Mono<Long> countWithCache(Object key, Mono<Long> sourceMono) {
String cacheKey = key + ":count";
// 从缓存中获取数据
Long cacheCount = this.cache.get(cacheKey, () -> null);
// 将查询结果添加到缓存中
Mono<Long> source = sourceMono.doOnNext(count -> this.cachePut(cacheKey, count));
// 如果缓存中没有数据,返回查询结果;否则返回缓存数据
return Mono.justOrEmpty(cacheCount).switchIfEmpty(source);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}

/**
* Initializes the cache using the cacheManager and cacheName provided.
* If the cacheManager is null, a new ConcurrentMapCache is created.
*
* @param cacheName the name of the cache to be initialized
*/
protected void initializingCache(String cacheName) {
this.cache = Optional.ofNullable(this.cacheManager).map(manager -> manager.getCache(cacheName))
.orElse(new ConcurrentMapCache(cacheName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import java.util.StringJoiner;

/**
* ParamSql represents a SQL query with parameters.
* It includes a SQL string and a map of parameter values.
* The whereSql() method returns the WHERE clause of the SQL query.
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
public record ParamSql(StringJoiner sql, Map<String, Object> params) implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public static String getClientIpAddress(ServerHttpRequest httpRequest) {
}

public static StringJoiner cacheKey(Object... objects) {
// Convert objects to a map using ObjectMapper
StringJoiner keyBuilder = new StringJoiner("&");
for (Object object : objects) {
if (object instanceof Pageable pageable) {
Expand All @@ -83,19 +82,14 @@ public static StringJoiner cacheKey(Object... objects) {
keyBuilder.add("offset=" + pageable.getOffset());
continue;
}
// Convert object to a map using ObjectMapper
Map<String, Object> objectMap = com.platform.boot.commons.utils.BeanUtils
.beanToMap(object, true);
// Check if the object map is empty
if (ObjectUtils.isEmpty(objectMap)) {
// Append the class name of the object to the key builder
keyBuilder.add(object.getClass().getName());
continue;
}
// Append each key-value pair from the object map to the key builder
objectMap.forEach((k, v) -> keyBuilder.add(k + "=" + v));
}
// Return the final cache key as a string
return keyBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.base.CaseFormat;
import com.google.common.collect.Maps;
import com.platform.boot.commons.query.ParamSql;
import lombok.Data;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.relational.core.query.Criteria;
Expand Down Expand Up @@ -165,10 +164,4 @@ public static Criteria build(Map<String, Object> objectMap) {
}).collect(Collectors.toList());
return Criteria.from(criteriaList);
}

@Data(staticConstructor = "of")
public static class Parameter {
private final String sql;
private final Map<String, Object> params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
import java.util.List;

/**
* Configuration class for R2DBC autoconfiguration.
* This class extends AbstractR2dbcConfiguration and enables R2DBC auditing and transaction management.
* It also provides a custom ConnectionFactory and custom converters for JSON nodes and user auditing.
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* Configuration class for Redis.
* Contains a bean for ReactiveRedisTemplate.
* Uses StringRedisSerializer for keys and GenericJackson2JsonRedisSerializer for values.
* Enables caching.
*
* @author Alex Bob (<a href="https://github.com/vnobo">Alex Bob</a>)
*/
@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
import java.util.Set;

/**
* This class contains the security configuration for the application.
* It sets up the security filters and rules for the application.
* It also defines the beans required for the security configuration.
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@
import java.util.List;

/**
* This configuration class is used to configure the session for the application. It is using
* HeaderWebSessionIdResolver and CookieWebSessionIdResolver to resolve the session id. The setHeaderName
* method of HeaderWebSessionIdResolver is used to set the name of the header which contains the session
* id while resolving. It is also overriding setSessionId, resolveSessionIds and expireSession methods
* of its parent class to add custom functionality.
* Configuration class for session management.
*
* @author billb
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
@Configuration(proxyBeanMethods = false)
public class SessionConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;

/**
* This configuration class sets up a Snowflake ID generator using Redis, and also configures a custom argument resolver for Reactive Spring WebFlux.
* The Snowflake ID generator is used to generate unique IDs for entities in the application, and the custom argument resolver allows for easier pagination of large data sets.
*
* @author billb
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
@Configuration(proxyBeanMethods = false)
public class WebConfiguration implements WebFluxConfigurer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import java.io.IOException;

/**
* This class contains the converters for JsonNode
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.springframework.stereotype.Component;

/**
* This class contains converters for UserAuditor objects.
*
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@

/**
* @author <a href="https://github.com/vnobo">Alex bob</a>
* This class represents a logger entity with various properties stored in a database table
* named "se_loggers". It implements the BaseEntity interface with a type parameter of Long.
*/
@Data
@Table("se_loggers")
public class Logger implements BaseEntity<Long> {

/**
* The unique identifier of the logger entity that serves as the primary key of the database table.
*/
@Id
private Long id;

Expand Down
Loading

0 comments on commit 217d046

Please sign in to comment.