Skip to content

Commit a776ef7

Browse files
author
lucaijun
committed
Merge remote-tracking branch 'origin/release130' into release130
2 parents 54da2cc + 943b710 commit a776ef7

File tree

3 files changed

+113
-61
lines changed

3 files changed

+113
-61
lines changed

src/com/xxdb/data/BasicTable.java

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package com.xxdb.data;
22

33
import java.io.IOException;
4-
import java.util.ArrayList;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
8-
4+
import java.util.*;
95
import com.xxdb.compression.VectorDecompressor;
106
import com.xxdb.io.ExtendedDataInput;
117
import com.xxdb.io.ExtendedDataOutput;
@@ -17,10 +13,10 @@
1713
*/
1814

1915
public class BasicTable extends AbstractEntity implements Table{
20-
private List<Vector> columns_ = new ArrayList<Vector>();
21-
private List<String> names_ = new ArrayList<String>();
22-
private Map<String, Integer> name2index_ = new HashMap<String, Integer>();
23-
private int[] colCompresses_ = null;
16+
private List<Vector> columns = new ArrayList<Vector>();
17+
private List<String> colNames = new ArrayList<String>();
18+
private Map<String, Integer> colNamesIndex = new HashMap<String, Integer>();
19+
private int[] colCompresses = null;
2420

2521
public BasicTable(ExtendedDataInput in) throws IOException{
2622
int rows = in.readInt();
@@ -30,8 +26,8 @@ public BasicTable(ExtendedDataInput in) throws IOException{
3026
//read column names
3127
for(int i=0; i<cols; ++i){
3228
String name = in.readString();
33-
name2index_.put(name, name2index_.size());
34-
names_.add(name);
29+
colNamesIndex.put(name, colNamesIndex.size());
30+
colNames.add(name);
3531
}
3632

3733
VectorDecompressor decompressor = null;
@@ -48,7 +44,7 @@ public BasicTable(ExtendedDataInput in) throws IOException{
4844
DATA_FORM df = DATA_FORM.values()[form];
4945
DATA_TYPE dt = DATA_TYPE.valueOf(type);
5046
if(df != DATA_FORM.DF_VECTOR)
51-
throw new IOException("Invalid form for column [" + names_.get(i) + "] for table " + tableName);
47+
throw new IOException("Invalid form for column [" + colNames.get(i) + "] for table " + tableName);
5248
Vector vector;
5349
if(dt == DATA_TYPE.DT_SYMBOL && extended){
5450
if(collection == null)
@@ -63,8 +59,8 @@ public BasicTable(ExtendedDataInput in) throws IOException{
6359
vector = (Vector)BasicEntityFactory.instance().createEntity(df, dt, in, extended);
6460
}
6561
if(vector.rows() != rows && vector.rows()!= 1)
66-
throw new IOException("The number of rows for column " +names_.get(i) + " is not consistent with other columns");
67-
columns_.add(vector);
62+
throw new IOException("The number of rows for column " + colNames.get(i) + " is not consistent with other columns");
63+
columns.add(vector);
6864
}
6965
if(collection != null)
7066
collection.clear();
@@ -85,8 +81,8 @@ public BasicTable(final List<String> colNames, final List<Vector> cols) {
8581
}
8682

8783
public void setColumnCompressTypes(int[] colCompresses) {
88-
if (colCompresses!=null && colCompresses.length != columns_.size()) {
89-
throw new RuntimeException("Compress type size must match column size "+columns_.size()+".");
84+
if (colCompresses!=null && colCompresses.length != columns.size()) {
85+
throw new RuntimeException("Compress type size must match column size "+ columns.size()+".");
9086
}
9187
if(colCompresses!=null) {
9288
for (int i = 0; i < colCompresses.length; i++) {
@@ -105,27 +101,32 @@ public void setColumnCompressTypes(int[] colCompresses) {
105101
}
106102
}
107103
if(colCompresses!=null){
108-
colCompresses_=new int[colCompresses.length];
109-
System.arraycopy(colCompresses,0,colCompresses_,0,colCompresses.length);
104+
this.colCompresses =new int[colCompresses.length];
105+
System.arraycopy(colCompresses,0, this.colCompresses,0,colCompresses.length);
110106
}else
111-
colCompresses_ = null;
107+
this.colCompresses = null;
112108
}
113-
114-
public void setColName (final List<String> colNames) {
115-
names_.clear();
116-
name2index_.clear();
109+
110+
/**
111+
* set columns' names
112+
* @param colNames
113+
*/
114+
public void setColName (final List<String> colNames) {
115+
this.colNames.clear();
116+
colNamesIndex.clear();
117117
for (String name : colNames){
118-
names_.add(name);
119-
name2index_.put(name, name2index_.size());
118+
this.colNames.add(name);
119+
colNamesIndex.put(name, colNamesIndex.size());
120120
}
121+
}
121122

122123
}
123124

124125
public void setColumns (final List<Vector> cols) {
125-
columns_.clear();
126+
columns.clear();
126127
// this is a shallow copy!
127128
for (Vector vector : cols)
128-
columns_.add(vector);
129+
columns.add(vector);
129130

130131
}
131132

@@ -149,30 +150,30 @@ public int rows() {
149150
if(columns()<=0)
150151
return 0;
151152
else
152-
return columns_.get(0).rows();
153+
return columns.get(0).rows();
153154
}
154155

155156
@Override
156157
public int columns() {
157-
return columns_.size();
158+
return columns.size();
158159
}
159160

160161
@Override
161162
public Vector getColumn(int index) {
162-
return columns_.get(index);
163+
return columns.get(index);
163164
}
164165

165166
@Override
166167
public Vector getColumn(String name) {
167-
Integer index = name2index_.get(name);
168+
Integer index = colNamesIndex.get(name);
168169
if(index == null)
169170
return null;
170171
else
171172
return getColumn(index);
172173
}
173174

174175
public String getColumnName(int index){
175-
return names_.get(index);
176+
return colNames.get(index);
176177
}
177178

178179
public String getString(){
@@ -255,17 +256,17 @@ public String getRowJson(int rowIndex){
255256
try {
256257
if(rowIndex<rows()){
257258
jsonStr.append("{");
258-
for(int i=0;i<names_.size();i++){
259+
for(int i = 0; i< colNames.size(); i++){
259260
jsonStr.append("\"");
260-
jsonStr.append(names_.get(i));
261+
jsonStr.append(colNames.get(i));
261262
jsonStr.append("\":");
262-
if (columns_.get(i) instanceof BasicDoubleVector || columns_.get(i) instanceof BasicFloatVector)
263-
jsonStr.append(((Scalar)columns_.get(i).get(rowIndex)).isNull() ? "null" : ((Scalar) columns_.get(i).get(rowIndex)).getJsonString());
264-
else if (columns_.get(i).getDataType().getValue() >= 65)
265-
jsonStr.append(columns_.get(i).getJsonString(rowIndex));
263+
if (columns.get(i) instanceof BasicDoubleVector || columns.get(i) instanceof BasicFloatVector)
264+
jsonStr.append(((Scalar) columns.get(i).get(rowIndex)).isNull() ? "null" : ((Scalar) columns.get(i).get(rowIndex)).getJsonString());
265+
else if (columns.get(i).getDataType().getValue() >= 65)
266+
jsonStr.append(columns.get(i).getJsonString(rowIndex));
266267
else
267-
jsonStr.append(((Scalar)columns_.get(i).get(rowIndex)).getJsonString());
268-
if(i<names_.size()-1)
268+
jsonStr.append(((Scalar) columns.get(i).get(rowIndex)).getJsonString());
269+
if(i< colNames.size()-1)
269270
jsonStr.append(",");
270271
}
271272
jsonStr.delete(jsonStr.length()-1,jsonStr.length()-1);
@@ -284,10 +285,10 @@ public void write(ExtendedDataOutput out) throws IOException{
284285
out.writeInt(rows());
285286
out.writeInt(columns());
286287
out.writeString(""); //table name
287-
for(String colName : names_)
288+
for(String colName : colNames)
288289
out.writeString(colName);
289290
SymbolBaseCollection collection = null;
290-
for(Vector vector : columns_){
291+
for(Vector vector : columns){
291292
if(vector instanceof BasicSymbolVector){
292293
if(collection == null)
293294
collection = new SymbolBaseCollection();
@@ -317,8 +318,8 @@ public void writeCompressed(ExtendedDataOutput output) throws IOException {
317318
if(v.getDataType() == DATA_TYPE.DT_SYMBOL)
318319
v.write(output);
319320
else {
320-
if(colCompresses_!=null){
321-
v.setCompressedMethod(colCompresses_[i]);
321+
if(colCompresses !=null){
322+
v.setCompressedMethod(colCompresses[i]);
322323
}
323324
v.writeCompressed(output);
324325
}
@@ -331,19 +332,19 @@ public BasicTable combine(BasicTable table){
331332
for (int i=0; i< this.columns();i++) {
332333
newCol.add(this.getColumn(i).combine(table.getColumn(i)));
333334
}
334-
return new BasicTable(this.names_,newCol);
335+
return new BasicTable(this.colNames,newCol);
335336
}
336337

337338
public Table getSubTable(int[] indices){
338-
int colCount = columns_.size();
339+
int colCount = columns.size();
339340
List<Vector> cols = new ArrayList<Vector>(colCount);
340341
for(int i=0; i<colCount; ++i)
341-
cols.add(columns_.get(i).getSubVector(indices));
342-
return new BasicTable(names_, cols);
342+
cols.add(columns.get(i).getSubVector(indices));
343+
return new BasicTable(colNames, cols);
343344
}
344345

345346
public Table getSubTable(int startRow, int endRow){
346-
int colCount = columns_.size();
347+
int colCount = columns.size();
347348
List<Vector> cols = new ArrayList<>();
348349
for (int i = 0; i < colCount; i++){
349350
int index = startRow;
@@ -352,29 +353,32 @@ public Table getSubTable(int startRow, int endRow){
352353
indices[j] = index;
353354
index++;
354355
}
355-
cols.add(columns_.get(i).getSubVector(indices));
356+
cols.add(columns.get(i).getSubVector(indices));
356357
}
357-
return new BasicTable(names_, cols);
358+
return new BasicTable(colNames, cols);
358359
}
359360

360361
@Override
361362
public void addColumn(String colName, Vector col) {
362-
if (names_.contains(colName))
363+
if (Objects.isNull(colName) || Objects.isNull(col))
364+
throw new RuntimeException("The param 'colName' or 'col' in table cannot be null.");
365+
366+
if (colNames.contains(colName))
363367
throw new RuntimeException("The table already contains column '" + colName + "'.");
364-
names_.add(colName);
365-
name2index_.put(colName, name2index_.size());
366-
columns_.add(col);
368+
colNames.add(colName);
369+
colNamesIndex.put(colName, colNamesIndex.size());
370+
columns.add(col);
367371
}
368372

369373
@Override
370374
public void replaceColumn(String colName, Vector col) {
371-
if (names_.contains(colName)) {
372-
int index = names_.indexOf(colName);
373-
columns_.set(index, col);
375+
if (colNames.contains(colName)) {
376+
int index = colNames.indexOf(colName);
377+
columns.set(index, col);
374378
} else {
375-
names_.add(colName);
376-
columns_.add(col);
377-
name2index_.put(colName, name2index_.size());
379+
colNames.add(colName);
380+
columns.add(col);
381+
colNamesIndex.put(colName, colNamesIndex.size());
378382
}
379383
}
380384
}

test/com/xxdb/data/BasicStringTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,4 +909,28 @@ public void test_BasicStringVector_BLOB_toJSONString() throws Exception {
909909
System.out.println(re);
910910
assertEquals("{\"chart\":false,\"chunk\":false,\"dataByteArray\":[[68,111,108,112,104,105,110,100,98],[77,111,110,103,111,68,66],[71,97,117,115,115,68,66],[71,111,108,100,101,110,68,66]],\"dataCategory\":\"LITERAL\",\"dataForm\":\"DF_VECTOR\",\"dataType\":\"DT_BLOB\",\"dictionary\":false,\"elementClass\":\"com.xxdb.data.BasicString\",\"matrix\":false,\"pair\":false,\"scalar\":false,\"string\":\"[Dolphindb,MongoDB,GaussDB,GoldenDB]\",\"table\":false,\"unitLength\":1,\"vector\":true}", re);
911911
}
912+
@Test
913+
public void test_BasicStringVector_setNull() throws Exception {
914+
BasicStringVector bsv = new BasicStringVector(1);
915+
bsv.setNull(0);
916+
System.out.println(bsv.getString());
917+
assertEquals("[]",bsv.getString());
918+
}
919+
@Test
920+
public void test_BasicStringVector_setNull_tableInsert() throws IOException {
921+
conn.run("t = table(1:0,[`col1],[STRING]);share t as test_null\n");
922+
List<String> colNames = Arrays.asList("col1");
923+
List<Vector> cols = new ArrayList<>();
924+
BasicStringVector stringVector = new BasicStringVector(1);
925+
stringVector.setNull(0);
926+
cols.add(stringVector);
927+
List<String> allColNames = new ArrayList<String>();
928+
allColNames.addAll(colNames);
929+
BasicTable table = new BasicTable(allColNames, cols);
930+
List<Entity> list = new ArrayList<>(1);
931+
list.add(table);
932+
conn.run("tableInsert{test_null}", list);
933+
BasicTable re = (BasicTable)conn.run("select * from test_null");
934+
assertEquals(1,re.rows());
935+
}
912936
}

test/com/xxdb/data/BasicSymbolTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,28 @@ public void test_BasicSymbolVector_toJSONString() throws Exception {
552552
System.out.println(re);
553553
assertEquals("{\"chart\":false,\"chunk\":false,\"dataCategory\":\"LITERAL\",\"dataForm\":\"DF_VECTOR\",\"dataType\":\"DT_SYMBOL\",\"dictionary\":false,\"elementClass\":\"com.xxdb.data.BasicString\",\"matrix\":false,\"pair\":false,\"scalar\":false,\"string\":\"[GaussDB,GoldenDB]\",\"table\":false,\"unitLength\":4,\"vector\":true}", re);
554554
}
555+
@Test
556+
public void test_BasicSymbolVector_setNull() throws Exception {
557+
BasicSymbolVector bsv = new BasicSymbolVector(1);
558+
bsv.setNull(0);
559+
System.out.println(bsv.getString());
560+
assertEquals("[]",bsv.getString());
561+
}
562+
@Test
563+
public void test_BasicSymbolVector_setNull_tableInsert() throws IOException {
564+
conn.run("t = table(1:0,[`col1],[STRING]);share t as test_null\n");
565+
List<String> colNames = Arrays.asList("col1");
566+
List<Vector> cols = new ArrayList<>();
567+
BasicSymbolVector symbolVector = new BasicSymbolVector(1);
568+
symbolVector.setNull(0);
569+
cols.add(symbolVector);
570+
List<String> allColNames = new ArrayList<String>();
571+
allColNames.addAll(colNames);
572+
BasicTable table = new BasicTable(allColNames, cols);
573+
List<Entity> list = new ArrayList<>(1);
574+
list.add(table);
575+
conn.run("tableInsert{test_null}", list);
576+
BasicTable re = (BasicTable)conn.run("select * from test_null");
577+
assertEquals(1,re.rows());
578+
}
555579
}

0 commit comments

Comments
 (0)