1
- package com .deathrayresearch .outlier ;
2
-
3
- import com .deathrayresearch .outlier .columns .CategoryColumn ;
4
- import com .deathrayresearch .outlier .columns .Column ;
5
- import com .deathrayresearch .outlier .columns .IntColumn ;
6
- import com .deathrayresearch .outlier .filter .Filter ;
7
- import com .deathrayresearch .outlier .sorting .Sort ;
8
- import com .deathrayresearch .outlier .store .TableMetadata ;
9
- import com .deathrayresearch .outlier .util .IntComparatorChain ;
10
- import com .deathrayresearch .outlier .util .ReverseIntComparator ;
1
+ package com .github .lwhite1 .outlier ;
2
+
3
+ import com .github .lwhite1 .outlier .columns .IntColumn ;
4
+ import com .github .lwhite1 .outlier .columns .CategoryColumn ;
5
+ import com .github .lwhite1 .outlier .columns .Column ;
6
+ import com .github .lwhite1 .outlier .filter .Filter ;
7
+ import com .github .lwhite1 .outlier .sorting .Sort ;
8
+ import com .github .lwhite1 .outlier .store .TableMetadata ;
9
+ import com .github .lwhite1 .outlier .util .IntComparatorChain ;
10
+ import com .github .lwhite1 .outlier .util .ReverseIntComparator ;
11
+ import com .google .common .annotations .VisibleForTesting ;
11
12
import com .google .common .base .Preconditions ;
12
13
import it .unimi .dsi .fastutil .ints .IntArrayList ;
13
14
import it .unimi .dsi .fastutil .ints .IntArrays ;
16
17
17
18
import java .util .*;
18
19
19
- import static com .deathrayresearch .outlier .sorting .Sort .Order ;
20
+ import static com .github . lwhite1 .outlier .sorting .Sort .Order ;
20
21
21
22
/**
22
- *
23
+ * A table of data, consisting of some number of columns, each of which has the same number of rows.
24
+ * All the data in a column has the same type: integer, float, category, etc.
23
25
*/
24
26
public class Table implements Relation {
25
27
28
+ /**
29
+ * A unique identifier for the table.
30
+ * TODO(lwhite): consider removing
31
+ */
26
32
private final String id ;
27
33
34
+ /**
35
+ * The name of the table
36
+ */
28
37
private String name ;
29
38
39
+ /**
40
+ * The columns that hold the data in this table
41
+ */
30
42
private final List <Column > columnList = new ArrayList <>();
31
43
44
+ /**
45
+ * Returns a new table initialized with the given name
46
+ */
32
47
public Table (String name ) {
33
48
this .name = name ;
34
49
this .id = UUID .randomUUID ().toString ();
35
50
}
36
51
52
+ /**
53
+ * Returns a new table initialized with data from the given TableMetadata object
54
+ * <p/>
55
+ * The metadata is used by the storage module to save tables and read their data from disk
56
+ */
37
57
public Table (TableMetadata metadata ) {
38
58
this .name = metadata .getName ();
39
59
this .id = metadata .getId ();
@@ -45,48 +65,72 @@ public Table(TableMetadata metadata) {
45
65
* @param name The name of the table
46
66
* @param columns One or more columns, all of which must have either the same length or size 0
47
67
*/
48
- public Table (String name , Column ... columns ) {
68
+ public Table (String name , Column ... columns ) {
49
69
this (name );
50
70
for (Column column : columns ) {
51
71
this .addColumn (column );
52
72
}
53
73
}
54
74
75
+ /**
76
+ * Adds the given column to this table
77
+ */
55
78
@ Override
56
79
public void addColumn (Column column ) {
57
80
columnList .add (column );
58
81
}
59
82
83
+ /**
84
+ * Sets the name of the table
85
+ */
60
86
@ Override
61
87
public void setName (String name ) {
62
88
this .name = name ;
63
89
}
64
90
91
+ /**
92
+ * Returns the column at the given index in the column list
93
+ *
94
+ * @param columnIndex an integer >= 0 and < number of columns in the relation
95
+ */
65
96
@ Override
66
97
public Column column (int columnIndex ) {
67
98
return columnList .get (columnIndex );
68
99
}
69
100
101
+ /**
102
+ * Returns the number of columns in the table
103
+ */
70
104
@ Override
71
105
public int columnCount () {
72
106
return columnList .size ();
73
107
}
74
108
109
+ /**
110
+ * Returns the number of rows in the table
111
+ */
75
112
@ Override
76
113
public int rowCount () {
77
114
int result = 0 ;
78
115
if (!columnList .isEmpty ()) {
116
+ // all the columns have the same number of elements, so we can check any of them
79
117
result = columnList .get (0 ).size ();
80
118
}
81
119
return result ;
82
120
}
83
121
122
+ /**
123
+ * Returns the list of columns
124
+ */
84
125
@ Override
85
126
public List <Column > columns () {
86
127
return columnList ;
87
128
}
88
129
89
- // TODO(lwhite): Implement for views and add to relation api
130
+
131
+ /**
132
+ * Returns only the columns whose names are given in the input array
133
+ */
90
134
public List <Column > columns (String [] columnNames ) {
91
135
List <Column > columns = new ArrayList <>();
92
136
for (String columnName : columnNames ) {
@@ -95,6 +139,11 @@ public List<Column> columns(String[] columnNames) {
95
139
return columns ;
96
140
}
97
141
142
+ /**
143
+ * Returns the index of the column with the given name
144
+ *
145
+ * @throws IllegalArgumentException if the input string is not the name of any column in the table
146
+ */
98
147
public int columnIndex (String columnName ) {
99
148
int columnIndex = -1 ;
100
149
for (int i = 0 ; i < columnList .size (); i ++) {
@@ -109,6 +158,12 @@ public int columnIndex(String columnName) {
109
158
return columnIndex ;
110
159
}
111
160
161
+ /**
162
+ * Returns the index of the given column (its position in the list of columns)
163
+ * <p/>
164
+ *
165
+ * @throws IllegalArgumentException if the column is not present in this table
166
+ */
112
167
public int columnIndex (Column column ) {
113
168
int columnIndex = -1 ;
114
169
for (int i = 0 ; i < columnList .size (); i ++) {
@@ -123,6 +178,9 @@ public int columnIndex(Column column) {
123
178
return columnIndex ;
124
179
}
125
180
181
+ /**
182
+ * Returns the name of the table
183
+ */
126
184
@ Override
127
185
public String name () {
128
186
return name ;
@@ -156,6 +214,12 @@ public int row(int r) {
156
214
return r ;
157
215
}
158
216
217
+ /**
218
+ * Returns a string representation of the value at the given row and column indexes
219
+ *
220
+ * @param c the column index, 0 based
221
+ * @param r the row index, 0 based
222
+ */
159
223
@ Override
160
224
public String get (int c , int r ) {
161
225
Column column = column (c );
@@ -173,13 +237,19 @@ public Table emptyCopy() {
173
237
return copy ;
174
238
}
175
239
240
+ /**
241
+ * Clears all the data from this table
242
+ */
176
243
@ Override
177
244
public void clear () {
178
245
for (Column column : columnList ) {
179
246
column .clear ();
180
247
}
181
248
}
182
249
250
+ /**
251
+ * Returns the unique id of this table
252
+ */
183
253
public String id () {
184
254
return id ;
185
255
}
@@ -218,6 +288,9 @@ public Table sortOn(String... columnNames) {
218
288
return sortOn (key );
219
289
}
220
290
291
+ /**
292
+ * Returns a copy of this table sorted in the order of the given column names, in ascending order
293
+ */
221
294
public Table sortAscendingOn (String ... columnNames ) {
222
295
return this .sortOn (columnNames );
223
296
}
@@ -230,7 +303,11 @@ public Table sortDescendingOn(String... columnNames) {
230
303
return sortOn (key );
231
304
}
232
305
233
- public static Sort getSort (String ... columnNames ) {
306
+ /**
307
+ * Returns an object that can be used to sort this table in the order specified for by the given column names
308
+ */
309
+ @ VisibleForTesting
310
+ public static Sort getSort (String ... columnNames ) {
234
311
Sort key = null ;
235
312
for (String s : columnNames ) {
236
313
if (key == null ) {
@@ -257,6 +334,9 @@ public Table sortOn(Sort key) {
257
334
return sortOn (chain );
258
335
}
259
336
337
+ /**
338
+ * Returns a comparator that can be used to sort the records in this table according to the given sort key
339
+ */
260
340
public IntComparator getComparator (Sort key ) {
261
341
Iterator <Map .Entry <String , Sort .Order >> entries = key .iterator ();
262
342
Map .Entry <String , Sort .Order > sort = entries .next ();
@@ -269,6 +349,9 @@ public IntComparator getComparator(Sort key) {
269
349
return comparator ;
270
350
}
271
351
352
+ /**
353
+ * Returns a comparator chain for sorting according to the given key
354
+ */
272
355
private IntComparatorChain getChain (Sort key ) {
273
356
Iterator <Map .Entry <String , Sort .Order >> entries = key .iterator ();
274
357
Map .Entry <String , Sort .Order > sort = entries .next ();
@@ -305,7 +388,11 @@ public Table sortOn(IntComparator rowComparator) {
305
388
return newTable ;
306
389
}
307
390
308
- public int [] rows () {
391
+ /**
392
+ * Returns an array of ints of the same number of rows as the table
393
+ */
394
+ @ VisibleForTesting
395
+ int [] rows () {
309
396
int [] rowIndexes = new int [rowCount ()];
310
397
for (int i = 0 ; i < rowCount (); i ++) {
311
398
rowIndexes [i ] = i ;
@@ -344,7 +431,7 @@ public Table selectIf(Filter filter) {
344
431
return newTable ;
345
432
}
346
433
347
- public Query select (String ... columnName ) {
434
+ public Query select (String ... columnName ) {
348
435
return new Query (this , columnName );
349
436
}
350
437
@@ -354,7 +441,7 @@ public Query select(String ... columnName) {
354
441
@ Override
355
442
public void removeColumns (Column ... columns ) {
356
443
for (Column c : columns )
357
- columnList .remove (c );
444
+ columnList .remove (c );
358
445
}
359
446
360
447
public Table countBy (String byColumnName ) {
@@ -379,24 +466,24 @@ private SubTable splitGroupingColumn(SubTable subTable, List<Column> columnNames
379
466
Iterator row = columnNames .iterator ();
380
467
381
468
Column c ;
382
- while (row .hasNext ()) {
383
- c = (Column )row .next ();
469
+ while (row .hasNext ()) {
470
+ c = (Column ) row .next ();
384
471
Column col = c .emptyCopy ();
385
472
newColumns .add (col );
386
473
}
387
474
388
- for (int var7 = 0 ; var7 < subTable .rowCount (); ++var7 ) {
475
+ for (int var7 = 0 ; var7 < subTable .rowCount (); ++var7 ) {
389
476
String [] var8 = subTable .name ().split ("|||" );
390
477
391
- for (int var9 = 0 ; var9 < newColumns .size (); ++var9 ) {
392
- ((Column )newColumns .get (var9 )).addCell (var8 [var9 ]);
478
+ for (int var9 = 0 ; var9 < newColumns .size (); ++var9 ) {
479
+ ((Column ) newColumns .get (var9 )).addCell (var8 [var9 ]);
393
480
}
394
481
}
395
482
396
483
row = newColumns .iterator ();
397
484
398
- while (row .hasNext ()) {
399
- c = (Column )row .next ();
485
+ while (row .hasNext ()) {
486
+ c = (Column ) row .next ();
400
487
subTable .addColumn (c );
401
488
}
402
489
0 commit comments