Commit f947c04fd326b1fae6831666077545cc5fc672f8

Authored by Igor Kulikov
Committed by GitHub
2 parents 75b1f848 178db50d

Merge pull request #1458 from vparomskiy/master

skip JPA TsKVEntry initialization if no data found
... ... @@ -62,49 +62,53 @@ public final class TsKvEntity implements ToData<TsKvEntry> {
62 62 }
63 63
64 64 public TsKvEntity(Long longValue, Double doubleValue, Long longCountValue, Long doubleCountValue, String aggType) {
65   - switch (aggType) {
66   - case AVG:
67   - double sum = 0.0;
68   - if (longValue != null) {
69   - sum += longValue;
70   - }
71   - if (doubleValue != null) {
72   - sum += doubleValue;
73   - }
74   - long totalCount = longCountValue + doubleCountValue;
75   - if (totalCount > 0) {
76   - this.doubleValue = sum / (longCountValue + doubleCountValue);
77   - } else {
78   - this.doubleValue = 0.0;
79   - }
80   - break;
81   - case SUM:
82   - if (doubleCountValue > 0) {
83   - this.doubleValue = doubleValue + (longValue != null ? longValue.doubleValue() : 0.0);
84   - } else {
85   - this.longValue = longValue;
86   - }
87   - break;
88   - case MIN:
89   - case MAX:
90   - if (longCountValue > 0 && doubleCountValue > 0) {
91   - this.doubleValue = MAX.equals(aggType) ? Math.max(doubleValue, longValue.doubleValue()) : Math.min(doubleValue, longValue.doubleValue());
92   - } else if (doubleCountValue > 0) {
93   - this.doubleValue = doubleValue;
94   - } else if (longCountValue > 0) {
95   - this.longValue = longValue;
96   - }
97   - break;
  65 + if(!isAllNull(longValue, doubleValue, longCountValue, doubleCountValue)) {
  66 + switch (aggType) {
  67 + case AVG:
  68 + double sum = 0.0;
  69 + if (longValue != null) {
  70 + sum += longValue;
  71 + }
  72 + if (doubleValue != null) {
  73 + sum += doubleValue;
  74 + }
  75 + long totalCount = longCountValue + doubleCountValue;
  76 + if (totalCount > 0) {
  77 + this.doubleValue = sum / (longCountValue + doubleCountValue);
  78 + } else {
  79 + this.doubleValue = 0.0;
  80 + }
  81 + break;
  82 + case SUM:
  83 + if (doubleCountValue > 0) {
  84 + this.doubleValue = doubleValue + (longValue != null ? longValue.doubleValue() : 0.0);
  85 + } else {
  86 + this.longValue = longValue;
  87 + }
  88 + break;
  89 + case MIN:
  90 + case MAX:
  91 + if (longCountValue > 0 && doubleCountValue > 0) {
  92 + this.doubleValue = MAX.equals(aggType) ? Math.max(doubleValue, longValue.doubleValue()) : Math.min(doubleValue, longValue.doubleValue());
  93 + } else if (doubleCountValue > 0) {
  94 + this.doubleValue = doubleValue;
  95 + } else if (longCountValue > 0) {
  96 + this.longValue = longValue;
  97 + }
  98 + break;
  99 + }
98 100 }
99 101 }
100 102
101 103 public TsKvEntity(Long booleanValueCount, Long strValueCount, Long longValueCount, Long doubleValueCount) {
102   - if (booleanValueCount != 0) {
103   - this.longValue = booleanValueCount;
104   - } else if (strValueCount != 0) {
105   - this.longValue = strValueCount;
106   - } else {
107   - this.longValue = longValueCount + doubleValueCount;
  104 + if(!isAllNull(booleanValueCount, strValueCount, longValueCount, doubleValueCount)) {
  105 + if (booleanValueCount != 0) {
  106 + this.longValue = booleanValueCount;
  107 + } else if (strValueCount != 0) {
  108 + this.longValue = strValueCount;
  109 + } else {
  110 + this.longValue = longValueCount + doubleValueCount;
  111 + }
108 112 }
109 113 }
110 114
... ... @@ -155,4 +159,13 @@ public final class TsKvEntity implements ToData<TsKvEntry> {
155 159 public boolean isNotEmpty() {
156 160 return strValue != null || longValue != null || doubleValue != null || booleanValue != null;
157 161 }
  162 +
  163 + private static boolean isAllNull(Object... args) {
  164 + for (Object arg : args) {
  165 + if(arg != null) {
  166 + return false;
  167 + }
  168 + }
  169 + return true;
  170 + }
158 171 }
... ...