88import static java .lang .String .format ;
99import static java .util .Locale .ENGLISH ;
1010import static java .util .Objects .requireNonNull ;
11- import static java .util .concurrent .TimeUnit .HOURS ;
12- import static java .util .concurrent .TimeUnit .MINUTES ;
1311
1412import java .util .Collections ;
1513import java .util .HashSet ;
1614import java .util .List ;
1715import java .util .Optional ;
1816import java .util .Set ;
17+ import java .util .concurrent .TimeUnit ;
1918import java .util .stream .Collectors ;
2019
2120import com .google .common .cache .CacheBuilder ;
@@ -71,24 +70,24 @@ public class RediSearchSession {
7170
7271 private final TypeManager typeManager ;
7372 private final StatefulRedisModulesConnection <String , String > connection ;
74- private final RediSearchClientConfig config ;
73+ private final RediSearchConfig config ;
7574 private final LoadingCache <SchemaTableName , RediSearchTable > tableCache ;
7675
7776 public RediSearchSession (TypeManager typeManager , StatefulRedisModulesConnection <String , String > connection ,
78- RediSearchClientConfig config ) {
77+ RediSearchConfig config ) {
7978 this .typeManager = requireNonNull (typeManager , "typeManager is null" );
8079 this .connection = requireNonNull (connection , "connection is null" );
8180 this .config = requireNonNull (config , "config is null" );
82- // TODO make table cache expiration configurable
83- this . tableCache = CacheBuilder . newBuilder (). expireAfterWrite ( 1 , HOURS ). refreshAfterWrite ( 1 , MINUTES )
81+ this . tableCache = CacheBuilder . newBuilder (). expireAfterWrite ( config . getTableCacheExpiration (), TimeUnit . SECONDS )
82+ . refreshAfterWrite ( config . getTableCacheRefresh (), TimeUnit . SECONDS )
8483 .build (CacheLoader .from (this ::loadTableSchema ));
8584 }
8685
8786 public StatefulRedisModulesConnection <String , String > getConnection () {
8887 return connection ;
8988 }
9089
91- public RediSearchClientConfig getConfig () {
90+ public RediSearchConfig getConfig () {
9291 return config ;
9392 }
9493
@@ -107,7 +106,7 @@ public List<HostAddress> getAddresses() {
107106
108107 public Set <String > getAllTables () throws SchemaNotFoundException {
109108 ImmutableSet .Builder <String > builder = ImmutableSet .builder ();
110- builder .addAll (connection .sync ().list ());
109+ builder .addAll (connection .sync ().ftList ());
111110 return builder .build ();
112111 }
113112
@@ -120,25 +119,26 @@ public RediSearchTable getTable(SchemaTableName tableName) throws TableNotFoundE
120119 }
121120 }
122121
122+ @ SuppressWarnings ("unchecked" )
123123 public void createTable (SchemaTableName schemaTableName , List <RediSearchColumnHandle > columns ) {
124124 String tableName = schemaTableName .getTableName ();
125- if (!connection .sync ().list ().contains (tableName )) {
126- List <Field > fields = columns .stream ().filter (c -> !c .getName ().equals ("_id" ))
125+ if (!connection .sync ().ftList ().contains (tableName )) {
126+ List <Field < String > > fields = columns .stream ().filter (c -> !c .getName ().equals ("_id" ))
127127 .map (c -> buildField (c .getName (), c .getType ())).collect (Collectors .toList ());
128128 CreateOptions .Builder <String , String > options = CreateOptions .<String , String >builder ();
129129 options .prefix (tableName + ":" );
130- connection .sync ().create (tableName , options .build (), fields .toArray (new Field [0 ] ));
130+ connection .sync ().ftCreate (tableName , options .build (), fields .toArray (Field []:: new ));
131131 }
132132 }
133133
134134 public void dropTable (SchemaTableName tableName ) {
135- connection .sync ().dropindexDeleteDocs (toRemoteTableName (tableName .getTableName ()));
135+ connection .sync ().ftDropindexDeleteDocs (toRemoteTableName (tableName .getTableName ()));
136136 tableCache .invalidate (tableName );
137137 }
138138
139139 public void addColumn (SchemaTableName schemaTableName , ColumnMetadata columnMetadata ) {
140140 String tableName = toRemoteTableName (schemaTableName .getTableName ());
141- connection .sync ().alter (tableName , buildField (columnMetadata .getName (), columnMetadata .getType ()));
141+ connection .sync ().ftAlter (tableName , buildField (columnMetadata .getName (), columnMetadata .getType ()));
142142 tableCache .invalidate (schemaTableName );
143143 }
144144
@@ -167,12 +167,12 @@ private RediSearchTable loadTableSchema(SchemaTableName schemaTableName) {
167167 }
168168 Set <String > fields = new HashSet <>();
169169 ImmutableList .Builder <RediSearchColumnHandle > columnHandles = ImmutableList .builder ();
170- for (Field columnMetadata : indexInfo .get ().getFields ()) {
170+ for (Field < String > columnMetadata : indexInfo .get ().getFields ()) {
171171 RediSearchColumnHandle column = buildColumnHandle (columnMetadata );
172172 fields .add (column .getName ());
173173 columnHandles .add (column );
174174 }
175- SearchResults <String , String > results = connection .sync ().search (index , "*" );
175+ SearchResults <String , String > results = connection .sync ().ftSearch (index , "*" );
176176 for (Document <String , String > doc : results ) {
177177 for (String field : doc .keySet ()) {
178178 if (fields .contains (field )) {
@@ -188,7 +188,7 @@ private RediSearchTable loadTableSchema(SchemaTableName schemaTableName) {
188188
189189 private Optional <IndexInfo > indexInfo (String index ) {
190190 try {
191- List <Object > indexInfoList = connection .sync ().indexInfo (index );
191+ List <Object > indexInfoList = connection .sync ().ftInfo (index );
192192 if (indexInfoList != null ) {
193193 return Optional .of (RedisModulesUtils .indexInfo (indexInfoList ));
194194 }
@@ -198,7 +198,7 @@ private Optional<IndexInfo> indexInfo(String index) {
198198 return Optional .empty ();
199199 }
200200
201- private RediSearchColumnHandle buildColumnHandle (Field field ) {
201+ private RediSearchColumnHandle buildColumnHandle (Field < String > field ) {
202202 return buildColumnHandle (field .getName (), field .getType (), false );
203203 }
204204
@@ -222,32 +222,31 @@ public SearchResults<String, String> search(RediSearchTableHandle tableHandle,
222222 options .limit (Limit .offset (0 ).num (limit (tableHandle )));
223223 options .returnFields (columns .stream ().map (RediSearchColumnHandle ::getName ).toArray (String []::new ));
224224 log .info ("Running search on index %s with query '%s'" , index , query );
225- return connection .sync ().search (index , query , options .build ());
225+ return connection .sync ().ftSearch (index , query , options .build ());
226226 }
227227
228228 public AggregateWithCursorResults <String > aggregate (RediSearchTableHandle table ) {
229229 String index = index (table );
230230 String query = RediSearchQueryBuilder .buildQuery (table .getConstraint ());
231- AggregateOptions .Builder <String , String > optionsBuilder = AggregateOptions .builder ();
232- optionsBuilder . limit (Limit .offset (0 ).num (limit (table )));
231+ AggregateOptions .Builder <String , String > options = AggregateOptions .builder ();
232+ options . operation (Limit .offset (0 ).num (limit (table )));
233233 Optional <Group > group = RediSearchQueryBuilder .group (table .getTermAggregations (),
234234 table .getMetricAggregations ());
235- group .ifPresent (optionsBuilder ::group );
236- AggregateOptions <String , String > options = optionsBuilder .build ();
237- log .info ("Running aggregation on index %s with query '%s' and %s" , index , query , options );
235+ group .ifPresent (options ::operation );
236+ log .info ("Running aggregation on index %s with query '%s' and %s" , index , query , options .build ());
238237 CursorOptions .Builder cursorOptions = CursorOptions .builder ();
239238 if (config .getCursorCount () > 0 ) {
240239 cursorOptions .count (config .getCursorCount ());
241240 }
242- return connection .sync ().aggregate (index , query , cursorOptions .build (), options );
241+ return connection .sync ().ftAggregate (index , query , cursorOptions .build (), options . build () );
243242 }
244243
245244 public AggregateWithCursorResults <String > cursorRead (RediSearchTableHandle tableHandle , long cursor ) {
246245 String index = index (tableHandle );
247246 if (config .getCursorCount () > 0 ) {
248- return connection .sync ().cursorRead (index , cursor , config .getCursorCount ());
247+ return connection .sync ().ftCursorRead (index , cursor , config .getCursorCount ());
249248 }
250- return connection .sync ().cursorRead (index , cursor );
249+ return connection .sync ().ftCursorRead (index , cursor );
251250 }
252251
253252 private String index (RediSearchTableHandle tableHandle ) {
@@ -261,7 +260,7 @@ private long limit(RediSearchTableHandle tableHandle) {
261260 return config .getDefaultLimit ();
262261 }
263262
264- private Field buildField (String columnName , Type columnType ) {
263+ private Field < String > buildField (String columnName , Type columnType ) {
265264 Field .Type fieldType = toFieldType (columnType );
266265 switch (fieldType ) {
267266 case GEO :
@@ -338,7 +337,7 @@ private TypeSignature varcharType() {
338337 }
339338
340339 public void cursorDelete (RediSearchTableHandle tableHandle , long cursor ) {
341- connection .sync ().cursorDelete (index (tableHandle ), cursor );
340+ connection .sync ().ftCursorDelete (index (tableHandle ), cursor );
342341 }
343342
344343}
0 commit comments