Commit 708d0c684810ef78864a568868e2a5460c4eb7a6

Authored by Sergey Matvienko
Committed by Andrew Shvayka
1 parent ab356a4b

cpu usage reduced on TbNodeUtils

... ... @@ -39,12 +39,8 @@ public class TbNodeUtils {
39 39
40 40 private static final ObjectMapper mapper = new ObjectMapper();
41 41
42   - private static final String METADATA_VARIABLE_TEMPLATE = "${%s}";
43   -
44 42 private static final Pattern DATA_PATTERN = Pattern.compile("(\\$\\[)(.*?)(])");
45 43
46   - private static final String DATA_VARIABLE_TEMPLATE = "$[%s]";
47   -
48 44 public static <T> T convert(TbNodeConfiguration configuration, Class<T> clazz) throws TbNodeException {
49 45 try {
50 46 return mapper.treeToValue(configuration.getData(), clazz);
... ... @@ -80,7 +76,7 @@ public class TbNodeUtils {
80 76 }
81 77
82 78 if (jsonNode != null && jsonNode.isValueNode()) {
83   - result = result.replace(String.format(DATA_VARIABLE_TEMPLATE, group), jsonNode.asText());
  79 + result = result.replace(formatDataVarTemplate(group), jsonNode.asText());
84 80 }
85 81 }
86 82 }
... ... @@ -106,8 +102,14 @@ public class TbNodeUtils {
106 102 }
107 103
108 104 private static String processVar(String pattern, String key, String val) {
109   - String varPattern = String.format(METADATA_VARIABLE_TEMPLATE, key);
110   - return pattern.replace(varPattern, val);
  105 + return pattern.replace(formatMetadataVarTemplate(key), val);
111 106 }
112 107
  108 + static String formatDataVarTemplate(String key) {
  109 + return "$[" + key + ']';
  110 + }
  111 +
  112 + static String formatMetadataVarTemplate(String key) {
  113 + return "${" + key + '}';
  114 + }
113 115 }
... ...
... ... @@ -16,6 +16,7 @@
16 16 package org.thingsboard.rule.engine.api.util;
17 17
18 18 import com.fasterxml.jackson.databind.node.ObjectNode;
  19 +import org.hamcrest.CoreMatchers;
19 20 import org.junit.Assert;
20 21 import org.junit.Test;
21 22 import org.junit.runner.RunWith;
... ... @@ -25,9 +26,15 @@ import org.thingsboard.server.common.msg.TbMsg;
25 26 import org.thingsboard.server.common.msg.TbMsgMetaData;
26 27 import org.thingsboard.common.util.JacksonUtil;
27 28
  29 +import static org.hamcrest.CoreMatchers.is;
  30 +import static org.hamcrest.MatcherAssert.assertThat;
  31 +
28 32 @RunWith(MockitoJUnitRunner.class)
29 33 public class TbNodeUtilsTest {
30 34
  35 + private static final String DATA_VARIABLE_TEMPLATE = "$[%s]";
  36 + private static final String METADATA_VARIABLE_TEMPLATE = "${%s}";
  37 +
31 38 @Test
32 39 public void testSimpleReplacement() {
33 40 String pattern = "ABC ${metadata_key} $[data_key]";
... ... @@ -112,4 +119,27 @@ public class TbNodeUtilsTest {
112 119 Assert.assertEquals("ABC metadata_value $[key1.key2[0].key3]", result);
113 120 }
114 121
  122 + @Test
  123 + public void givenKey_whenFormatDataVarTemplate_thenReturnTheSameStringAsFormat() {
  124 + assertThat(TbNodeUtils.formatDataVarTemplate("key"), is("$[key]"));
  125 + assertThat(TbNodeUtils.formatDataVarTemplate("key"), is(String.format(DATA_VARIABLE_TEMPLATE, "key")));
  126 +
  127 + assertThat(TbNodeUtils.formatDataVarTemplate(""), is("$[]"));
  128 + assertThat(TbNodeUtils.formatDataVarTemplate(""), is(String.format(DATA_VARIABLE_TEMPLATE, "")));
  129 +
  130 + assertThat(TbNodeUtils.formatDataVarTemplate(null), is("$[null]"));
  131 + assertThat(TbNodeUtils.formatDataVarTemplate(null), is(String.format(DATA_VARIABLE_TEMPLATE, (String) null)));
  132 + }
  133 +
  134 + @Test
  135 + public void givenKey_whenFormatMetadataVarTemplate_thenReturnTheSameStringAsFormat() {
  136 + assertThat(TbNodeUtils.formatMetadataVarTemplate("key"), is("${key}"));
  137 + assertThat(TbNodeUtils.formatMetadataVarTemplate("key"), is(String.format(METADATA_VARIABLE_TEMPLATE, "key")));
  138 +
  139 + assertThat(TbNodeUtils.formatMetadataVarTemplate(""), is("${}"));
  140 + assertThat(TbNodeUtils.formatMetadataVarTemplate(""), is(String.format(METADATA_VARIABLE_TEMPLATE, "")));
  141 +
  142 + assertThat(TbNodeUtils.formatMetadataVarTemplate(null), is("${null}"));
  143 + assertThat(TbNodeUtils.formatMetadataVarTemplate(null), is(String.format(METADATA_VARIABLE_TEMPLATE, (String) null)));
  144 + }
115 145 }
... ...