31
31
import org .apache .tsfile .utils .DateUtils ;
32
32
import org .apache .tsfile .write .UnSupportedDataTypeException ;
33
33
import org .apache .tsfile .write .record .Tablet ;
34
+ import org .apache .tsfile .write .record .Tablet .ColumnCategory ;
34
35
import org .apache .tsfile .write .schema .IMeasurementSchema ;
35
36
36
37
import java .time .LocalDate ;
37
38
import java .util .ArrayList ;
39
+ import java .util .Collections ;
38
40
import java .util .Iterator ;
39
41
import java .util .List ;
40
42
import java .util .Map ;
41
43
import java .util .Objects ;
42
44
import java .util .TreeMap ;
43
45
import java .util .stream .Collectors ;
46
+ import java .util .stream .Stream ;
44
47
45
48
public class SubscriptionSessionDataSet implements ISessionDataSet {
46
49
@@ -52,15 +55,67 @@ public Tablet getTablet() {
52
55
return tablet ;
53
56
}
54
57
58
+ public SubscriptionSessionDataSet (final Tablet tablet , @ Nullable final String databaseName ) {
59
+ this .tablet = tablet ;
60
+ this .databaseName = databaseName ;
61
+ generateRowIterator ();
62
+ }
63
+
64
+ /////////////////////////////// table model ///////////////////////////////
65
+
66
+ @ TableModel private List <ColumnCategory > columnCategoryList ;
67
+
55
68
@ TableModel
56
69
public String getDatabaseName () {
57
70
return databaseName ;
58
71
}
59
72
60
- public SubscriptionSessionDataSet (final Tablet tablet , @ Nullable final String databaseName ) {
61
- this .tablet = tablet ;
62
- this .databaseName = databaseName ;
63
- generateRowIterator ();
73
+ @ TableModel
74
+ public String getTableName () {
75
+ return tablet .getTableName ();
76
+ }
77
+
78
+ @ TableModel
79
+ public List <ColumnCategory > getColumnCategories () {
80
+ if (Objects .nonNull (columnCategoryList )) {
81
+ return columnCategoryList ;
82
+ }
83
+
84
+ if (!isTableData ()) {
85
+ return Collections .emptyList ();
86
+ }
87
+
88
+ return columnCategoryList =
89
+ Stream .concat (
90
+ Stream .of (ColumnCategory .TIME ),
91
+ tablet .getColumnTypes ().stream ()
92
+ .map (
93
+ columnCategory -> {
94
+ switch (columnCategory ) {
95
+ case FIELD :
96
+ return ColumnCategory .FIELD ;
97
+ case TAG :
98
+ return ColumnCategory .TAG ;
99
+ case ATTRIBUTE :
100
+ return ColumnCategory .ATTRIBUTE ;
101
+ default :
102
+ throw new IllegalArgumentException (
103
+ "Unknown column category: " + columnCategory );
104
+ }
105
+ }))
106
+ .collect (Collectors .toList ());
107
+ }
108
+
109
+ @ TableModel
110
+ public enum ColumnCategory {
111
+ TIME ,
112
+ TAG ,
113
+ FIELD ,
114
+ ATTRIBUTE
115
+ }
116
+
117
+ private boolean isTableData () {
118
+ return Objects .nonNull (databaseName );
64
119
}
65
120
66
121
/////////////////////////////// override ///////////////////////////////
@@ -74,16 +129,17 @@ public List<String> getColumnNames() {
74
129
return columnNameList ;
75
130
}
76
131
77
- columnNameList = new ArrayList <>();
78
- columnNameList .add ("Time" );
79
-
80
- String deviceId = tablet .getDeviceId ();
81
132
List <IMeasurementSchema > schemas = tablet .getSchemas ();
82
- columnNameList .addAll (
83
- schemas .stream ()
84
- .map ((schema ) -> deviceId + "." + schema .getMeasurementName ())
85
- .collect (Collectors .toList ()));
86
- return columnNameList ;
133
+ String deviceId = tablet .getDeviceId ();
134
+ return columnNameList =
135
+ isTableData ()
136
+ ? Stream .concat (
137
+ Stream .of ("time" ), schemas .stream ().map (IMeasurementSchema ::getMeasurementName ))
138
+ .collect (Collectors .toList ())
139
+ : Stream .concat (
140
+ Stream .of ("Time" ),
141
+ schemas .stream ().map (schema -> deviceId + "." + schema .getMeasurementName ()))
142
+ .collect (Collectors .toList ());
87
143
}
88
144
89
145
@ Override
@@ -92,13 +148,12 @@ public List<String> getColumnTypes() {
92
148
return columnTypeList ;
93
149
}
94
150
95
- columnTypeList = new ArrayList <>();
96
- columnTypeList .add (TSDataType .INT64 .toString ());
97
-
98
151
List <IMeasurementSchema > schemas = tablet .getSchemas ();
99
- columnTypeList .addAll (
100
- schemas .stream ().map (schema -> schema .getType ().toString ()).collect (Collectors .toList ()));
101
- return columnTypeList ;
152
+ return columnTypeList =
153
+ Stream .concat (
154
+ Stream .of (TSDataType .INT64 .toString ()),
155
+ schemas .stream ().map (schema -> schema .getType ().toString ()))
156
+ .collect (Collectors .toList ());
102
157
}
103
158
104
159
public boolean hasNext () {
0 commit comments