34
34
35
35
import java .io .File ;
36
36
import java .util .Collections ;
37
+ import java .util .HashMap ;
37
38
import java .util .HashSet ;
38
39
import java .util .List ;
40
+ import java .util .Map ;
39
41
import java .util .Optional ;
40
42
import java .util .Set ;
41
43
import java .util .concurrent .TimeUnit ;
48
50
import com .google .common .collect .ImmutableSet ;
49
51
import com .google .common .util .concurrent .UncheckedExecutionException ;
50
52
import com .redis .lettucemod .api .StatefulRedisModulesConnection ;
53
+ import com .redis .lettucemod .search .AggregateOperation ;
51
54
import com .redis .lettucemod .search .AggregateWithCursorResults ;
52
55
import com .redis .lettucemod .search .CreateOptions ;
53
56
import com .redis .lettucemod .search .Document ;
54
57
import com .redis .lettucemod .search .Field ;
58
+ import com .redis .lettucemod .search .Group ;
55
59
import com .redis .lettucemod .search .IndexInfo ;
56
60
import com .redis .lettucemod .search .SearchResults ;
57
61
import com .redis .lettucemod .util .ClientBuilder ;
@@ -161,6 +165,12 @@ public Set<String> getAllTables() throws SchemaNotFoundException {
161
165
return builder .build ();
162
166
}
163
167
168
+ /**
169
+ *
170
+ * @param schemaTableName SchemaTableName to load
171
+ * @return RediSearchTable describing the RediSearch index
172
+ * @throws TableNotFoundException if no index by that name was found
173
+ */
164
174
public RediSearchTable getTable (SchemaTableName tableName ) throws TableNotFoundException {
165
175
try {
166
176
return tableCache .getUnchecked (tableName );
@@ -172,13 +182,13 @@ public RediSearchTable getTable(SchemaTableName tableName) throws TableNotFoundE
172
182
173
183
@ SuppressWarnings ("unchecked" )
174
184
public void createTable (SchemaTableName schemaTableName , List <RediSearchColumnHandle > columns ) {
175
- String tableName = schemaTableName . getTableName ( );
176
- if (!connection .sync ().ftList ().contains (tableName )) {
185
+ String index = index ( schemaTableName );
186
+ if (!connection .sync ().ftList ().contains (index )) {
177
187
List <Field <String >> fields = columns .stream ().filter (c -> !c .getName ().equals ("_id" ))
178
- .map (c -> buildField (c .getName (), c .getType ())).collect (Collectors .toList ());
188
+ .map (c -> buildField (c .getName (), c .getType ())).collect (Collectors .toUnmodifiableList ());
179
189
CreateOptions .Builder <String , String > options = CreateOptions .<String , String >builder ();
180
- options .prefix (tableName + ":" );
181
- connection .sync ().ftCreate (tableName , options .build (), fields .toArray (Field []::new ));
190
+ options .prefix (index + ":" );
191
+ connection .sync ().ftCreate (index , options .build (), fields .toArray (Field []::new ));
182
192
}
183
193
}
184
194
@@ -210,19 +220,26 @@ public void dropColumn(SchemaTableName schemaTableName, String columnName) {
210
220
throw new TrinoException (NOT_SUPPORTED , "This connector does not support dropping columns" );
211
221
}
212
222
213
- private RediSearchTable loadTableSchema (SchemaTableName schemaTableName ) {
223
+ /**
224
+ *
225
+ * @param schemaTableName SchemaTableName to load
226
+ * @return RediSearchTable describing the RediSearch index
227
+ * @throws TableNotFoundException if no index by that name was found
228
+ */
229
+ private RediSearchTable loadTableSchema (SchemaTableName schemaTableName ) throws TableNotFoundException {
214
230
String index = schemaTableName .getTableName ();
215
- Optional <IndexInfo > indexInfo = indexInfo (index );
216
- if (indexInfo .isEmpty ()) {
231
+ Optional <IndexInfo > indexInfoOptional = indexInfo (index );
232
+ if (indexInfoOptional .isEmpty ()) {
217
233
throw new TableNotFoundException (schemaTableName , format ("Index '%s' not found" , index ), null );
218
234
}
235
+ IndexInfo indexInfo = indexInfoOptional .get ();
219
236
Set <String > fields = new HashSet <>();
220
237
ImmutableList .Builder <RediSearchColumnHandle > columns = ImmutableList .builder ();
221
238
for (RediSearchBuiltinField builtinfield : RediSearchBuiltinField .values ()) {
222
239
fields .add (builtinfield .getName ());
223
240
columns .add (builtinfield .getColumnHandle ());
224
241
}
225
- for (Field <String > indexedField : indexInfo .get (). getFields ()) {
242
+ for (Field <String > indexedField : indexInfo .getFields ()) {
226
243
RediSearchColumnHandle column = buildColumnHandle (indexedField );
227
244
fields .add (column .getName ());
228
245
columns .add (column );
@@ -237,8 +254,9 @@ private RediSearchTable loadTableSchema(SchemaTableName schemaTableName) {
237
254
fields .add (docField );
238
255
}
239
256
}
240
- return new RediSearchTable (new RediSearchTableHandle (RediSearchTableHandle .Type .SEARCH , schemaTableName ),
241
- columns .build ());
257
+ RediSearchTableHandle tableHandle = new RediSearchTableHandle (RediSearchTableHandle .Type .SEARCH ,
258
+ schemaTableName );
259
+ return new RediSearchTable (tableHandle , columns .build (), indexInfo );
242
260
}
243
261
244
262
private Optional <IndexInfo > indexInfo (String index ) {
@@ -278,8 +296,7 @@ private Type columnType(TypeSignature typeSignature) {
278
296
return typeManager .fromSqlType (typeSignature .toString ());
279
297
}
280
298
281
- public SearchResults <String , String > search (RediSearchTableHandle tableHandle ,
282
- List <RediSearchColumnHandle > columns ) {
299
+ public SearchResults <String , String > search (RediSearchTableHandle tableHandle , String [] columns ) {
283
300
Search search = translator .search (tableHandle , columns );
284
301
log .info ("Running %s" , search );
285
302
return connection .sync ().ftSearch (search .getIndex (), search .getQuery (), search .getOptions ());
@@ -288,20 +305,32 @@ public SearchResults<String, String> search(RediSearchTableHandle tableHandle,
288
305
public AggregateWithCursorResults <String > aggregate (RediSearchTableHandle table ) {
289
306
Aggregation aggregation = translator .aggregate (table );
290
307
log .info ("Running %s" , aggregation );
291
- return connection .sync ().ftAggregate (aggregation .getIndex (), aggregation .getQuery (),
292
- aggregation .getCursorOptions (), aggregation .getOptions ());
308
+ AggregateWithCursorResults <String > results = connection .sync ().ftAggregate (aggregation .getIndex (),
309
+ aggregation .getQuery (), aggregation .getCursorOptions (), aggregation .getOptions ());
310
+ List <AggregateOperation <?, ?>> groupBys = aggregation .getOptions ().getOperations ().stream ()
311
+ .filter (o -> o .getType () == AggregateOperation .Type .GROUP ).collect (Collectors .toUnmodifiableList ());
312
+ if (results .isEmpty () && !groupBys .isEmpty ()) {
313
+ Group groupBy = (Group ) groupBys .get (0 );
314
+ Optional <String > as = groupBy .getReducers ()[0 ].getAs ();
315
+ if (as .isPresent ()) {
316
+ Map <String , Object > doc = new HashMap <>();
317
+ doc .put (as .get (), 0 );
318
+ results .add (doc );
319
+ }
320
+ }
321
+ return results ;
293
322
}
294
323
295
324
public AggregateWithCursorResults <String > cursorRead (RediSearchTableHandle tableHandle , long cursor ) {
296
- String index = index (tableHandle );
325
+ String index = index (tableHandle . getSchemaTableName () );
297
326
if (config .getCursorCount () > 0 ) {
298
327
return connection .sync ().ftCursorRead (index , cursor , config .getCursorCount ());
299
328
}
300
329
return connection .sync ().ftCursorRead (index , cursor );
301
330
}
302
331
303
- private String index (RediSearchTableHandle tableHandle ) {
304
- return tableHandle . getSchemaTableName () .getTableName ();
332
+ private String index (SchemaTableName schemaTableName ) {
333
+ return schemaTableName .getTableName ();
305
334
}
306
335
307
336
private Field <String > buildField (String columnName , Type columnType ) {
@@ -381,7 +410,11 @@ private TypeSignature varcharType() {
381
410
}
382
411
383
412
public void cursorDelete (RediSearchTableHandle tableHandle , long cursor ) {
384
- connection .sync ().ftCursorDelete (index (tableHandle ), cursor );
413
+ connection .sync ().ftCursorDelete (index (tableHandle .getSchemaTableName ()), cursor );
414
+ }
415
+
416
+ public Long deleteDocs (List <String > docIds ) {
417
+ return connection .sync ().del (docIds .toArray (String []::new ));
385
418
}
386
419
387
420
}
0 commit comments