Skip to content

Commit 8e094b9

Browse files
author
chengyitian
committed
AJ-424: code review optimize.
1 parent e9ec975 commit 8e094b9

14 files changed

+114
-90
lines changed

src/com/xxdb/DBConnection.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,17 +435,17 @@ private Entity run(String script, String scriptType, ProgressListener listener,
435435
if (asynTask_)
436436
return null;
437437

438-
in_= remoteLittleEndian_ ? new LittleEndianDataInputStream(new BufferedInputStream(socket_.getInputStream())) :
438+
ExtendedDataInput in = remoteLittleEndian_ ? new LittleEndianDataInputStream(new BufferedInputStream(socket_.getInputStream())) :
439439
new BigEndianDataInputStream(new BufferedInputStream(socket_.getInputStream()));
440440
String header = null;
441441
try {
442-
header = in_.readLine();
442+
header = in.readLine();
443443
while (header.equals("MSG")) {
444444
//read intermediate message to indicate the progress
445-
String msg = in_.readString();
445+
String msg = in.readString();
446446
if (listener != null)
447447
listener.progress(msg);
448-
header = in_.readLine();
448+
header = in.readLine();
449449
}
450450
}catch (IOException ex){
451451
isConnected_ = false;
@@ -464,7 +464,7 @@ private Entity run(String script, String scriptType, ProgressListener listener,
464464
int numObject = Integer.parseInt(headers[1]);
465465

466466
try {
467-
header = in_.readLine();
467+
header = in.readLine();
468468
}catch (IOException ex){
469469
isConnected_ = false;
470470
socket_ = null;
@@ -484,7 +484,7 @@ private Entity run(String script, String scriptType, ProgressListener listener,
484484

485485
short flag;
486486
try {
487-
flag = in_.readShort();
487+
flag = in.readShort();
488488
int form = flag >> 8;
489489
int type = flag & 0xff;
490490
boolean extended = type >= 128;
@@ -501,10 +501,10 @@ private Entity run(String script, String scriptType, ProgressListener listener,
501501

502502

503503
if(fetchSize>0 && df == Entity.DATA_FORM.DF_VECTOR && dt == Entity.DATA_TYPE.DT_ANY){
504-
return new EntityBlockReader(in_);
504+
return new EntityBlockReader(in);
505505
}
506506
EntityFactory factory = BasicEntityFactory.instance();
507-
return factory.createEntity(df, dt, in_, extended);
507+
return factory.createEntity(df, dt, in, extended);
508508
}catch (IOException ex){
509509
isConnected_ = false;
510510
socket_ = null;

src/com/xxdb/comm/ErrorCodeInfo.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
public class ErrorCodeInfo {
44

5-
public String errorCode;
6-
public String errorInfo;
5+
private String errorCode;
6+
private String errorInfo;
77

88
public enum Code {
99
EC_None(0),
@@ -40,6 +40,23 @@ public void set(ErrorCodeInfo errorCodeInfo){
4040
public void set(int code, String info){
4141
set(formatApiCode(code), info);
4242
}
43+
44+
public String getErrorCode() {
45+
return errorCode;
46+
}
47+
48+
public String getErrorInfo() {
49+
return errorInfo;
50+
}
51+
52+
public void setErrorCode(String errorCode) {
53+
this.errorCode = errorCode;
54+
}
55+
56+
public void setErrorInfo(String errorInfo) {
57+
this.errorInfo = errorInfo;
58+
}
59+
4360
@Override
4461
public String toString(){
4562
StringBuilder sb=new StringBuilder();

src/com/xxdb/data/BasicDecimal128.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public BasicDecimal128(String data, int scale) {
2424

2525
public BasicDecimal128(BigInteger unscaledVal, int scale) {
2626
if (scale < 0 || scale > 38) {
27-
throw new RuntimeException("Scale out of bound (valid range: [0, 38], but get: " + scale + ")");
27+
throw new RuntimeException("Scale " + scale + "is out of bounds, it must be in [0,38].");
2828
}
2929

3030
BigDecimal bd = new BigDecimal(unscaledVal);

src/com/xxdb/data/BasicDecimal128Vector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public BasicDecimal128Vector(int size, int scale) {
3737
public BasicDecimal128Vector(String[] dataValue, int scale) {
3838
super(DATA_FORM.DF_VECTOR);
3939
if (scale < 0 || scale > 38) {
40-
throw new RuntimeException("Scale out of bound (valid range: [0, 38], but get: " + scale + ")");
40+
throw new RuntimeException("Scale " + scale + "is out of bounds, it must be in [0,38].");
4141
}
4242
this.scale_ = scale;
4343

@@ -275,8 +275,8 @@ public Entity get(int index) {
275275

276276
@Override
277277
public void set(int index, Entity value) throws Exception {
278-
if (value.getDataType() != DT_DECIMAL128) {
279-
throw new RuntimeException("value type is not BasicDecimal128!");
278+
if (!value.isScalar() || value.getDataType() != DT_DECIMAL128) {
279+
throw new RuntimeException("The value type is not BasicDecimal128!");
280280
}
281281

282282
int newScale = ((Scalar) value).getScale();

src/com/xxdb/data/BasicDecimal32.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public BasicDecimal32(String value, int scale) {
2828
}
2929

3030
if (scale < 0 || scale > 9) {
31-
throw new RuntimeException("Scale out of bound (valid range: [0, 9], but get: " + scale + ")");
31+
throw new RuntimeException("Scale " + scale + "is out of bounds, it must be in [0,9].");
3232
}
3333
scale_ = scale;
3434

src/com/xxdb/data/BasicDecimal32Vector.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public BasicDecimal32Vector(int size, int scale){
4141
public BasicDecimal32Vector(String[] data, int scale) {
4242
super(DATA_FORM.DF_VECTOR);
4343
if (scale < 0 || scale > 9) {
44-
throw new RuntimeException("Scale out of bound (valid range: [0, 9], but get: " + scale + ")");
44+
throw new RuntimeException("Scale " + scale + "is out of bounds, it must be in [0,9].");
4545
}
4646
scale_ = scale;
4747

@@ -50,7 +50,9 @@ public BasicDecimal32Vector(String[] data, int scale) {
5050
BigDecimal pow = BigDecimal.TEN.pow(scale_);
5151
for (int i = 0; i < length; i++) {
5252
BigDecimal bd = new BigDecimal(data[i]);
53-
values[i] = bd.multiply(pow).intValue();
53+
if (bd.multiply(pow).intValue() > Integer.MIN_VALUE && bd.multiply(pow).intValue() < Integer.MAX_VALUE) {
54+
values[i] = bd.multiply(pow).intValue();
55+
}
5456
}
5557

5658
size = length;
@@ -180,7 +182,7 @@ public Entity get(int index) {
180182

181183
@Override
182184
public void set(int index, Entity value) throws Exception {
183-
if (value.getDataType() != DT_DECIMAL32) {
185+
if (!value.isScalar() && value.getDataType() != DT_DECIMAL32) {
184186
throw new RuntimeException("value type is not BasicDecimal32!");
185187
}
186188

@@ -236,21 +238,21 @@ public void add(String value) {
236238
if (scale_ < 0){
237239
throw new RuntimeException("Please set scale first.");
238240
}
239-
if (size + 1 > capacity && values.length > 0){
241+
242+
if (size + 1 > capacity && values.length > 0) {
240243
values = Arrays.copyOf(values, values.length * 2);
241-
}else if (values.length <= 0){
242-
values = Arrays.copyOf(values, values.length + 1);
244+
} else if (values.length <= 0){
245+
values = new int[1];
243246
}
247+
244248
capacity = values.length;
245249
if (value.equals("0.0"))
246250
values[size] = 0;
247251
else {
248-
BigDecimal pow = new BigDecimal(1);
249-
for (long i = 0; i < scale_; i++) {
250-
pow = pow.multiply(new BigDecimal(10));
251-
}
252-
BigDecimal dbvalue = new BigDecimal(value);
253-
values[size] = (dbvalue.multiply(pow)).intValue();
252+
BigDecimal pow = BigDecimal.TEN.pow(scale_);
253+
BigDecimal bd = new BigDecimal(value);
254+
if (checkDecimal32Range(bd.multiply(pow).intValue()))
255+
values[size] = bd.multiply(pow).intValue();
254256
}
255257
size++;
256258
}
@@ -291,13 +293,12 @@ public void addRange(String[] valueList) {
291293
throw new RuntimeException("Please set scale first.");
292294
}
293295
int[] newIntValue = new int[valueList.length];
296+
BigDecimal pow = BigDecimal.TEN.pow(scale_);
294297
for(int i = 0; i < valueList.length; i++){
295-
BigDecimal pow = new BigDecimal(1);
296-
for (long j = 0; j < scale_; j++) {
297-
pow = pow.multiply(new BigDecimal(10));
298-
}
299-
BigDecimal dbvalue = new BigDecimal(valueList[i]);
300-
newIntValue[i] = (dbvalue.multiply(pow)).intValue();
298+
BigDecimal bd = new BigDecimal(valueList[i]);
299+
if (checkDecimal32Range(bd.multiply(pow).intValue()))
300+
newIntValue[i] = bd.multiply(pow).intValue();
301+
301302
}
302303
values = Arrays.copyOf(values, newIntValue.length + values.length);
303304
System.arraycopy(newIntValue, 0, values, size, newIntValue.length);
@@ -398,4 +399,8 @@ public int serialize(int indexStart, int offect, int targetNumElement, NumElemen
398399
numElementAndPartial.partial = 0;
399400
return targetNumElement * 4;
400401
}
402+
403+
private boolean checkDecimal32Range(int value) {
404+
return value > Integer.MIN_VALUE && value < Integer.MAX_VALUE;
405+
}
401406
}

src/com/xxdb/data/BasicDecimal64.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public BasicDecimal64(long value, int scale){
2828
}
2929

3030
public BasicDecimal64(String value, int scale) {
31-
value = value.trim();
3231
if (Utils.isEmpty(value)) {
3332
throw new RuntimeException("value is empty!");
3433
}
@@ -177,4 +176,8 @@ public int compareTo(BasicDecimal64 o) {
177176
public long getLong(){
178177
return value_;
179178
}
179+
180+
private boolean checkDecimal64Range(BigDecimal value) {
181+
return value.compareTo(DECIMAL64_MIN_VALUE) > 0 && value.compareTo(DECIMAL64_MAX_VALUE) < 0;
182+
}
180183
}

src/com/xxdb/data/BasicDecimal64Vector.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class BasicDecimal64Vector extends AbstractVector{
1717
private int size;
1818
private int capaticy;
1919

20+
private static final BigDecimal DECIMAL64_MIN_VALUE = new BigDecimal("-9223372036854775808");
21+
private static final BigDecimal DECIMAL64_MAX_VALUE = new BigDecimal("9223372036854775807");
22+
2023
public BasicDecimal64Vector(int size){
2124
this(DATA_FORM.DF_VECTOR, size);
2225
}
@@ -58,7 +61,8 @@ public BasicDecimal64Vector(String[] data, int scale) {
5861
BigDecimal pow = BigDecimal.TEN.pow(scale_);
5962
for (int i = 0; i < length; i++) {
6063
BigDecimal bd = new BigDecimal(data[i]);
61-
values[i] = bd.multiply(pow).longValue();
64+
if (checkDecimal64Range(bd))
65+
values[i] = bd.multiply(pow).longValue();
6266
}
6367

6468
size = length;
@@ -185,7 +189,7 @@ public Entity get(int index) {
185189

186190
@Override
187191
public void set(int index, Entity value) throws Exception {
188-
if (value.getDataType() != DT_DECIMAL64) {
192+
if (!value.isScalar() && value.getDataType() != DT_DECIMAL64) {
189193
throw new RuntimeException("value type is not BasicDecimal64!");
190194
}
191195

@@ -241,21 +245,20 @@ public void add(String value) {
241245
if (scale_ < 0){
242246
throw new RuntimeException("Please set scale first.");
243247
}
244-
if (size + 1 > capaticy && values.length > 0){
248+
if (size + 1 > capaticy && values.length > 0) {
245249
values = Arrays.copyOf(values, values.length * 2);
246-
}else if (values.length <= 0){
247-
values = Arrays.copyOf(values, values.length + 1);
250+
} else if (values.length <= 0) {
251+
values = new long[1];
248252
}
253+
249254
capaticy = values.length;
250255
if (value.equals("0.0"))
251256
values[size] = 0;
252257
else {
253-
BigDecimal pow = new BigDecimal(1);
254-
for (long i = 0; i < scale_; i++) {
255-
pow = pow.multiply(new BigDecimal(10));
256-
}
257-
BigDecimal dbvalue = new BigDecimal(value);
258-
values[size] = (dbvalue.multiply(pow)).longValue();
258+
BigDecimal pow = BigDecimal.TEN.pow(scale_);
259+
BigDecimal bd = new BigDecimal(value);
260+
if (checkDecimal64Range(bd))
261+
values[size] = bd.multiply(pow).longValue();
259262
}
260263
size++;
261264
}
@@ -297,13 +300,11 @@ public void addRange(String[] valueList) {
297300
throw new RuntimeException("Please set scale first.");
298301
}
299302
long[] newLongValue = new long[valueList.length];
300-
for(int i = 0; i < valueList.length; i++){
301-
BigDecimal pow = new BigDecimal(1);
302-
for (long j = 0; j < scale_; j++) {
303-
pow = pow.multiply(new BigDecimal(10));
304-
}
305-
BigDecimal dbvalue = new BigDecimal(valueList[i]);
306-
newLongValue[i] = (dbvalue.multiply(pow)).longValue();
303+
BigDecimal pow = BigDecimal.TEN.pow(scale_);
304+
for (int i = 0; i < valueList.length; i++) {
305+
BigDecimal bd = new BigDecimal(valueList[i]);
306+
if (checkDecimal64Range(bd))
307+
newLongValue[i] = bd.multiply(pow).longValue();
307308
}
308309
values = Arrays.copyOf(values, newLongValue.length + values.length);
309310
System.arraycopy(newLongValue, 0, values, size, newLongValue.length);
@@ -397,4 +398,8 @@ public int serialize(int indexStart, int offect, int targetNumElement, NumElemen
397398
numElementAndPartial.partial = 0;
398399
return targetNumElement * 8;
399400
}
401+
402+
private boolean checkDecimal64Range(BigDecimal value) {
403+
return value.compareTo(DECIMAL64_MIN_VALUE) > 0 && value.compareTo(DECIMAL64_MAX_VALUE) < 0;
404+
}
400405
}

src/com/xxdb/data/BasicDurationVector.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,4 @@ public int serialize(int indexStart, int offect, int targetNumElement, NumElemen
188188
numElementAndPartial.partial = 0;
189189
return targetNumElement * 4;
190190
}
191-
192-
@JsonIgnore
193-
@Override
194-
public int getExtraParamForType() {
195-
return super.getExtraParamForType();
196-
}
197191
}

src/com/xxdb/multithreadedtablewriter/MultithreadedTableWriter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public static class Status extends ErrorCodeInfo{
5555

5656
public String toString(){
5757
StringBuilder sb = new StringBuilder();
58-
sb.append("errorCode : " + errorCode + "\n");
59-
sb.append("errorInfo : " + errorInfo + "\n");
58+
sb.append("errorCode : " + getErrorCode() + "\n");
59+
sb.append("errorInfo : " + getErrorInfo() + "\n");
6060
sb.append("isExiting : " + isExiting + "\n");
6161
sb.append("sentRows : " + sentRows + "\n");
6262
sb.append("unsentRows : " + unsentRows + "\n");
@@ -569,8 +569,8 @@ public List<List<Entity>> getFailedData() throws InterruptedException{
569569

570570
public Status getStatus(){
571571
Status status = new Status();
572-
status.errorCode = errorCodeInfo_.errorCode;
573-
status.errorInfo = errorCodeInfo_.errorInfo;
572+
status.setErrorCode(errorCodeInfo_.getErrorCode());
573+
status.setErrorInfo(errorCodeInfo_.getErrorInfo());
574574
status.sendFailedRows=status.sentRows=status.unsentRows=0;
575575
status.isExiting=isExiting();
576576
for(WriterThread writeThread : threads_){

0 commit comments

Comments
 (0)