AnyValue优化

This commit is contained in:
redkale
2023-06-30 13:20:22 +08:00
parent 7c66446459
commit 6bb91552f7
7 changed files with 63 additions and 26 deletions

View File

@@ -30,7 +30,7 @@ import org.redkale.util.*;
*/ */
public class CacheClusterAgent extends ClusterAgent implements Resourcable { public class CacheClusterAgent extends ClusterAgent implements Resourcable {
@Resource(name = "$") @Resource(name = Resource.PARENT_NAME)
private CacheSource source; private CacheSource source;
private String sourceName; private String sourceName;

View File

@@ -7,7 +7,7 @@ import java.util.function.Function;
/** /**
* *
* 关系型数据库的数据源, 接口与DataSource基本一致。 <br> * 关系型sql数据库的数据源, DataSource多了操作sql语句的接口。 <br>
* *
* <p> * <p>
* 详情见: https://redkale.org * 详情见: https://redkale.org

View File

@@ -5,11 +5,9 @@
*/ */
package org.redkale.source; package org.redkale.source;
import org.redkale.source.DistributeTableStrategy; import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.*; import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/** /**
* Entity分库分表的注解需要结合DistributeTableStrategy使用 <br> * Entity分库分表的注解需要结合DistributeTableStrategy使用 <br>

View File

@@ -42,12 +42,26 @@ public interface DistributeTableStrategy<T> {
*/ */
public String getTable(String table, T bean); public String getTable(String table, T bean);
/**
* 获取对象的表名 <br>
* 查询、修改、删除对象DataSource.find、DataSource.query、DataSource.delete、DataSource.update时调用本方法获取表名 <br>
*
* @param table 模板表的表名
* @param node 过滤条件
*
* @return 带库名的全表名
*
* @since 2.8.0
*/
public String[] getTables(String table, FilterNode node);
/** /**
* 获取对象的表名 <br> * 获取对象的表名 <br>
* 查询、修改、删除对象DataSource.find、DataSource.query、DataSource.delete、DataSource.update时调用本方法获取表名 <br> * 查询、修改、删除对象DataSource.find、DataSource.query、DataSource.delete、DataSource.update时调用本方法获取表名 <br>
* 注意: 需保证FilterNode过滤的结果集合必须在一个数据库表中 <br> * 注意: 需保证FilterNode过滤的结果集合必须在一个数据库表中 <br>
* *
* @deprecated 2.8.0 replaced by getTables(String table, FilterNode node) * @deprecated 2.8.0 replaced by getTables(String table, FilterNode node)
* @see #getTables(java.lang.String, org.redkale.source.FilterNode)
* *
* @param table 模板表的表名 * @param table 模板表的表名
* @param node 过滤条件 * @param node 过滤条件
@@ -59,19 +73,4 @@ public interface DistributeTableStrategy<T> {
return getTables(table, node)[0]; return getTables(table, node)[0];
} }
/**
* 获取对象的表名 <br>
* 查询、修改、删除对象DataSource.find、DataSource.query、DataSource.delete、DataSource.update时调用本方法获取表名 <br>
*
* @param table 模板表的表名
* @param node 过滤条件
*
* @return 带库名的全表名
*
* @since 2.8.0
*/
default String[] getTables(String table, FilterNode node) {
return new String[]{getTable(table, node)};
}
} }

View File

@@ -41,8 +41,6 @@ public final class EntityCache<T> {
//continuousid=true此字段值才有效 //continuousid=true此字段值才有效
private T[] array; private T[] array;
private final IntFunction<T> mapFunc = c -> array[c];
//Flipper.sort转换成Comparator的缓存 //Flipper.sort转换成Comparator的缓存
private final Map<String, Comparator<T>> sortComparators = new ConcurrentHashMap<>(); private final Map<String, Comparator<T>> sortComparators = new ConcurrentHashMap<>();

View File

@@ -1673,8 +1673,8 @@ public abstract class AnyValue {
* *
* @return String * @return String
*/ */
public String toXML(String rootName) { public String toXml(String rootName) {
return toXMLString(new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n"), rootName, this, 0).toString(); return toXmlString(new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n"), rootName, this, 0).toString();
} }
/** /**
@@ -1687,7 +1687,7 @@ public abstract class AnyValue {
* *
* @return StringBuilder * @return StringBuilder
*/ */
protected static StringBuilder toXMLString(StringBuilder sb, String nodeName, AnyValue conf, int indent) { //indent: 缩进长度 protected static StringBuilder toXmlString(StringBuilder sb, String nodeName, AnyValue conf, int indent) { //indent: 缩进长度
if (indent < 0) { if (indent < 0) {
indent = 0; indent = 0;
} }
@@ -1702,9 +1702,40 @@ public abstract class AnyValue {
} }
sb.append(">\r\n\r\n"); sb.append(">\r\n\r\n");
for (Entry<AnyValue> en : conf.getAnyEntrys()) { for (Entry<AnyValue> en : conf.getAnyEntrys()) {
toXMLString(sb, en.name, en.getValue(), indent + 4); toXmlString(sb, en.name, en.getValue(), indent + 4);
} }
return sb.append(space).append("</").append(nodeName).append(">\r\n\r\n"); return sb.append(space).append("</").append(nodeName).append(">\r\n\r\n");
} }
public String toJsonString() {
Entry<String>[] stringArray = getStringEntrys();
Entry<AnyValue>[] anyArray = getAnyEntrys();
final StringBuilder sb = new StringBuilder();
sb.append('{');
int size = (stringArray == null ? 0 : stringArray.length) + (anyArray == null ? 0 : anyArray.length);
int index = 0;
if (stringArray != null) {
for (Entry<String> en : stringArray) {
if (en.value == null) {
sb.append('"').append(en.name.replace("\"", "\\\"")).append("\":null");
} else {
sb.append('"').append(en.name.replace("\"", "\\\"")).append("\":\"").append(en.value.replace("\"", "\\\"")).append('"');
}
if (++index < size) {
sb.append(',');
}
}
}
if (anyArray != null) {
for (Entry<AnyValue> en : anyArray) {
sb.append('"').append(en.name.replace("\"", "\\\"")).append("\":").append(en.value.toJsonString());
if (++index < size) {
sb.append(',');
}
}
}
sb.append('}');
return sb.toString();
}
} }

View File

@@ -5,6 +5,7 @@ package org.redkale.test.util;
import java.util.Properties; import java.util.Properties;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.redkale.util.AnyValue; import org.redkale.util.AnyValue;
import org.redkale.util.AnyValue.DefaultAnyValue;
/** /**
* *
@@ -106,4 +107,14 @@ public class AnyValuePropertiesTest {
//System.out.println(conf.copy().merge(conf2)); //System.out.println(conf.copy().merge(conf2));
//System.out.println(conf); //System.out.println(conf);
} }
@Test
public void run3() {
DefaultAnyValue conf = AnyValue.create();
conf.addValue("name", "haha");
conf.addValue("value", AnyValue.create().addValue("id", 1234).addValue("key", (String) null).addValue("desc", "nothing !!!"));
String json = "{\"name\":\"haha\",\"value\":{\"id\":\"1234\",\"key\":null,\"desc\":\"nothing !!!\"}}";
Assertions.assertEquals(json, conf.toJsonString());
}
} }