1
1
package com .xxdb .data ;
2
2
3
3
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 .*;
9
5
import com .xxdb .compression .VectorDecompressor ;
10
6
import com .xxdb .io .ExtendedDataInput ;
11
7
import com .xxdb .io .ExtendedDataOutput ;
17
13
*/
18
14
19
15
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 ;
24
20
25
21
public BasicTable (ExtendedDataInput in ) throws IOException {
26
22
int rows = in .readInt ();
@@ -30,8 +26,8 @@ public BasicTable(ExtendedDataInput in) throws IOException{
30
26
//read column names
31
27
for (int i =0 ; i <cols ; ++i ){
32
28
String name = in .readString ();
33
- name2index_ .put (name , name2index_ .size ());
34
- names_ .add (name );
29
+ colNamesIndex .put (name , colNamesIndex .size ());
30
+ colNames .add (name );
35
31
}
36
32
37
33
VectorDecompressor decompressor = null ;
@@ -48,7 +44,7 @@ public BasicTable(ExtendedDataInput in) throws IOException{
48
44
DATA_FORM df = DATA_FORM .values ()[form ];
49
45
DATA_TYPE dt = DATA_TYPE .valueOf (type );
50
46
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 );
52
48
Vector vector ;
53
49
if (dt == DATA_TYPE .DT_SYMBOL && extended ){
54
50
if (collection == null )
@@ -63,8 +59,8 @@ public BasicTable(ExtendedDataInput in) throws IOException{
63
59
vector = (Vector )BasicEntityFactory .instance ().createEntity (df , dt , in , extended );
64
60
}
65
61
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 );
68
64
}
69
65
if (collection != null )
70
66
collection .clear ();
@@ -85,8 +81,8 @@ public BasicTable(final List<String> colNames, final List<Vector> cols) {
85
81
}
86
82
87
83
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 ()+"." );
90
86
}
91
87
if (colCompresses !=null ) {
92
88
for (int i = 0 ; i < colCompresses .length ; i ++) {
@@ -105,27 +101,32 @@ public void setColumnCompressTypes(int[] colCompresses) {
105
101
}
106
102
}
107
103
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 );
110
106
}else
111
- colCompresses_ = null ;
107
+ this . colCompresses = null ;
112
108
}
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 ();
117
117
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 ());
120
120
}
121
+ }
121
122
122
123
}
123
124
124
125
public void setColumns (final List <Vector > cols ) {
125
- columns_ .clear ();
126
+ columns .clear ();
126
127
// this is a shallow copy!
127
128
for (Vector vector : cols )
128
- columns_ .add (vector );
129
+ columns .add (vector );
129
130
130
131
}
131
132
@@ -149,30 +150,30 @@ public int rows() {
149
150
if (columns ()<=0 )
150
151
return 0 ;
151
152
else
152
- return columns_ .get (0 ).rows ();
153
+ return columns .get (0 ).rows ();
153
154
}
154
155
155
156
@ Override
156
157
public int columns () {
157
- return columns_ .size ();
158
+ return columns .size ();
158
159
}
159
160
160
161
@ Override
161
162
public Vector getColumn (int index ) {
162
- return columns_ .get (index );
163
+ return columns .get (index );
163
164
}
164
165
165
166
@ Override
166
167
public Vector getColumn (String name ) {
167
- Integer index = name2index_ .get (name );
168
+ Integer index = colNamesIndex .get (name );
168
169
if (index == null )
169
170
return null ;
170
171
else
171
172
return getColumn (index );
172
173
}
173
174
174
175
public String getColumnName (int index ){
175
- return names_ .get (index );
176
+ return colNames .get (index );
176
177
}
177
178
178
179
public String getString (){
@@ -255,17 +256,17 @@ public String getRowJson(int rowIndex){
255
256
try {
256
257
if (rowIndex <rows ()){
257
258
jsonStr .append ("{" );
258
- for (int i = 0 ; i < names_ .size ();i ++){
259
+ for (int i = 0 ; i < colNames .size (); i ++){
259
260
jsonStr .append ("\" " );
260
- jsonStr .append (names_ .get (i ));
261
+ jsonStr .append (colNames .get (i ));
261
262
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 ));
266
267
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 )
269
270
jsonStr .append ("," );
270
271
}
271
272
jsonStr .delete (jsonStr .length ()-1 ,jsonStr .length ()-1 );
@@ -284,10 +285,10 @@ public void write(ExtendedDataOutput out) throws IOException{
284
285
out .writeInt (rows ());
285
286
out .writeInt (columns ());
286
287
out .writeString ("" ); //table name
287
- for (String colName : names_ )
288
+ for (String colName : colNames )
288
289
out .writeString (colName );
289
290
SymbolBaseCollection collection = null ;
290
- for (Vector vector : columns_ ){
291
+ for (Vector vector : columns ){
291
292
if (vector instanceof BasicSymbolVector ){
292
293
if (collection == null )
293
294
collection = new SymbolBaseCollection ();
@@ -317,8 +318,8 @@ public void writeCompressed(ExtendedDataOutput output) throws IOException {
317
318
if (v .getDataType () == DATA_TYPE .DT_SYMBOL )
318
319
v .write (output );
319
320
else {
320
- if (colCompresses_ !=null ){
321
- v .setCompressedMethod (colCompresses_ [i ]);
321
+ if (colCompresses !=null ){
322
+ v .setCompressedMethod (colCompresses [i ]);
322
323
}
323
324
v .writeCompressed (output );
324
325
}
@@ -331,19 +332,19 @@ public BasicTable combine(BasicTable table){
331
332
for (int i =0 ; i < this .columns ();i ++) {
332
333
newCol .add (this .getColumn (i ).combine (table .getColumn (i )));
333
334
}
334
- return new BasicTable (this .names_ ,newCol );
335
+ return new BasicTable (this .colNames ,newCol );
335
336
}
336
337
337
338
public Table getSubTable (int [] indices ){
338
- int colCount = columns_ .size ();
339
+ int colCount = columns .size ();
339
340
List <Vector > cols = new ArrayList <Vector >(colCount );
340
341
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 );
343
344
}
344
345
345
346
public Table getSubTable (int startRow , int endRow ){
346
- int colCount = columns_ .size ();
347
+ int colCount = columns .size ();
347
348
List <Vector > cols = new ArrayList <>();
348
349
for (int i = 0 ; i < colCount ; i ++){
349
350
int index = startRow ;
@@ -352,29 +353,32 @@ public Table getSubTable(int startRow, int endRow){
352
353
indices [j ] = index ;
353
354
index ++;
354
355
}
355
- cols .add (columns_ .get (i ).getSubVector (indices ));
356
+ cols .add (columns .get (i ).getSubVector (indices ));
356
357
}
357
- return new BasicTable (names_ , cols );
358
+ return new BasicTable (colNames , cols );
358
359
}
359
360
360
361
@ Override
361
362
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 ))
363
367
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 );
367
371
}
368
372
369
373
@ Override
370
374
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 );
374
378
} 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 ());
378
382
}
379
383
}
380
384
}
0 commit comments