11package com .xxdb .data ;
22
33import 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 .*;
95import com .xxdb .compression .VectorDecompressor ;
106import com .xxdb .io .ExtendedDataInput ;
117import com .xxdb .io .ExtendedDataOutput ;
1713 */
1814
1915public 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}
0 commit comments