@@ -92,8 +92,8 @@ private int priorityOf(@NotNull final ColumnSource<?> keyColumn) {
92
92
@ NotNull
93
93
private static Collection <ColumnSource <?>> getColumnSources (
94
94
@ NotNull final Table table ,
95
- @ NotNull final String ... keyColumnNames ) {
96
- return Arrays .stream (keyColumnNames )
95
+ @ NotNull final Collection < String > keyColumnNames ) {
96
+ return keyColumnNames .stream ()
97
97
.map (table ::getColumnSource )
98
98
.collect (Collectors .toList ());
99
99
}
@@ -107,16 +107,29 @@ private static Collection<ColumnSource<?>> getColumnSources(
107
107
* @param keyColumnNames The key column names to check
108
108
* @return Whether {@code table} has a DataIndexer with a {@link DataIndex} for the given key columns
109
109
*/
110
- public static boolean hasDataIndex (@ NotNull Table table , @ NotNull final String ... keyColumnNames ) {
111
- if (keyColumnNames .length == 0 ) {
110
+ public static boolean hasDataIndex (@ NotNull final Table table , @ NotNull final String ... keyColumnNames ) {
111
+ return hasDataIndex (table , Arrays .asList (keyColumnNames ));
112
+ }
113
+
114
+ /**
115
+ * Test whether {@code table} has a DataIndexer with a usable {@link DataIndex} for the given key columns. Note that
116
+ * a result from this method is a snapshot of current state, and does not guarantee anything about future calls to
117
+ * {@link #hasDataIndex}, {@link #getDataIndex}, or {@link #getOrCreateDataIndex(Table, String...)}.
118
+ *
119
+ * @param table The {@link Table} to check
120
+ * @param keyColumnNames The key column names to check
121
+ * @return Whether {@code table} has a DataIndexer with a {@link DataIndex} for the given key columns
122
+ */
123
+ public static boolean hasDataIndex (@ NotNull final Table table , @ NotNull final Collection <String > keyColumnNames ) {
124
+ if (keyColumnNames .isEmpty ()) {
112
125
return false ;
113
126
}
114
- table = table .coalesce ();
115
- final DataIndexer indexer = DataIndexer .existingOf (table .getRowSet ());
127
+ final Table tableToUse = table .coalesce ();
128
+ final DataIndexer indexer = DataIndexer .existingOf (tableToUse .getRowSet ());
116
129
if (indexer == null ) {
117
130
return false ;
118
131
}
119
- return indexer .hasDataIndex (getColumnSources (table , keyColumnNames ));
132
+ return indexer .hasDataIndex (getColumnSources (tableToUse , keyColumnNames ));
120
133
}
121
134
122
135
/**
@@ -152,19 +165,34 @@ public boolean hasDataIndex(@NotNull final Collection<ColumnSource<?>> keyColumn
152
165
* index is no longer live.
153
166
*
154
167
* @param table The {@link Table} to check
155
- * @param keyColumnNames The key column for which to retrieve a DataIndex
168
+ * @param keyColumnNames The key columns for which to retrieve a DataIndex
156
169
* @return The {@link DataIndex}, or {@code null} if one does not exist
157
170
*/
158
- public static DataIndex getDataIndex (@ NotNull Table table , final String ... keyColumnNames ) {
159
- if (keyColumnNames .length == 0 ) {
171
+ @ Nullable
172
+ public static DataIndex getDataIndex (@ NotNull final Table table , final String ... keyColumnNames ) {
173
+ return getDataIndex (table , Arrays .asList (keyColumnNames ));
174
+ }
175
+
176
+ /**
177
+ * If {@code table} has a DataIndexer, return a {@link DataIndex} for the given key columns, or {@code null} if no
178
+ * such index exists, if the cached index is invalid, or if the {@link DataIndex#isRefreshing() refreshing} cached
179
+ * index is no longer live.
180
+ *
181
+ * @param table The {@link Table} to check
182
+ * @param keyColumnNames The key columns for which to retrieve a DataIndex
183
+ * @return The {@link DataIndex}, or {@code null} if one does not exist
184
+ */
185
+ @ Nullable
186
+ public static DataIndex getDataIndex (@ NotNull final Table table , final Collection <String > keyColumnNames ) {
187
+ if (keyColumnNames .isEmpty ()) {
160
188
return null ;
161
189
}
162
- table = table .coalesce ();
163
- final DataIndexer indexer = DataIndexer .existingOf (table .getRowSet ());
190
+ final Table tableToUse = table .coalesce ();
191
+ final DataIndexer indexer = DataIndexer .existingOf (tableToUse .getRowSet ());
164
192
if (indexer == null ) {
165
193
return null ;
166
194
}
167
- return indexer .getDataIndex (getColumnSources (table , keyColumnNames ));
195
+ return indexer .getDataIndex (getColumnSources (tableToUse , keyColumnNames ));
168
196
}
169
197
170
198
/**
@@ -239,13 +267,28 @@ public static DataIndex getOptimalPartialIndex(Table table, final String... keyC
239
267
public static DataIndex getOrCreateDataIndex (
240
268
@ NotNull final Table table ,
241
269
@ NotNull final String ... keyColumnNames ) {
242
- if (keyColumnNames .length == 0 ) {
270
+ return getOrCreateDataIndex (table , Arrays .asList (keyColumnNames ));
271
+ }
272
+
273
+ /**
274
+ * Create a {@link DataIndex} for {@code table} indexing {@code keyColumns}, if no valid, live data index already
275
+ * exists for these inputs.
276
+ *
277
+ * @param table The {@link Table} to index
278
+ * @param keyColumnNames The key column names to include
279
+ * @return The existing or newly created {@link DataIndex}
280
+ * @apiNote This method causes the returned {@link DataIndex} to be managed by the enclosing liveness manager.
281
+ */
282
+ public static DataIndex getOrCreateDataIndex (
283
+ @ NotNull final Table table ,
284
+ @ NotNull final Collection <String > keyColumnNames ) {
285
+ if (keyColumnNames .isEmpty ()) {
243
286
throw new IllegalArgumentException ("Cannot create a DataIndex without any key columns" );
244
287
}
245
288
final QueryTable tableToUse = (QueryTable ) table .coalesce ();
246
289
final DataIndexer dataIndexer = DataIndexer .of (tableToUse .getRowSet ());
247
290
return dataIndexer .rootCache .computeIfAbsent (dataIndexer .pathFor (getColumnSources (tableToUse , keyColumnNames )),
248
- () -> new TableBackedDataIndex (tableToUse , keyColumnNames ));
291
+ () -> new TableBackedDataIndex (tableToUse , keyColumnNames . toArray ( String []:: new ) ));
249
292
}
250
293
251
294
/**
0 commit comments