Skip to content

Commit 4dabc02

Browse files
committed
Added table caching
1 parent 8467d63 commit 4dabc02

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

subprojects/trino-redisearch/src/main/java/com/redis/trino/RediSearchPageSink.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Map;
2323
import java.util.Random;
2424
import java.util.concurrent.CompletableFuture;
25-
import java.util.concurrent.TimeUnit;
2625

2726
import com.github.f4b6a3.ulid.UlidFactory;
2827
import com.google.common.collect.ImmutableList;
@@ -90,6 +89,7 @@ public CompletableFuture<?> appendPage(Page page) {
9089
return NOT_BLOCKED;
9190
}
9291

92+
@SuppressWarnings("deprecation")
9393
private String getValue(Type type, Block block, int position) {
9494
if (block.isNull(position)) {
9595
return null;

subprojects/trino-redisearch/src/main/java/com/redis/trino/RediSearchPageSource.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
2727
import java.math.BigDecimal;
2828
import java.time.LocalDate;
2929
import java.time.format.DateTimeFormatter;
30-
import java.time.temporal.TemporalAccessor;
3130
import java.util.Iterator;
3231
import java.util.List;
33-
import java.util.concurrent.TimeUnit;
3432

3533
import com.fasterxml.jackson.core.JsonFactory;
3634
import com.fasterxml.jackson.core.JsonGenerator;

subprojects/trino-redisearch/src/main/java/com/redis/trino/RediSearchSession.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.redis.trino;
22

33
import static com.google.common.base.Preconditions.checkArgument;
4+
import static com.google.common.base.Throwables.throwIfInstanceOf;
45
import static com.google.common.base.Verify.verify;
56
import static io.trino.spi.type.BigintType.BIGINT;
67
import static io.trino.spi.type.DoubleType.DOUBLE;
@@ -10,6 +11,8 @@
1011
import static java.lang.Math.toIntExact;
1112
import static java.lang.String.format;
1213
import static java.util.Objects.requireNonNull;
14+
import static java.util.concurrent.TimeUnit.HOURS;
15+
import static java.util.concurrent.TimeUnit.MINUTES;
1316

1417
import java.util.ArrayList;
1518
import java.util.Collections;
@@ -20,11 +23,15 @@
2023
import java.util.stream.Collectors;
2124

2225
import com.google.common.annotations.VisibleForTesting;
26+
import com.google.common.cache.CacheBuilder;
27+
import com.google.common.cache.CacheLoader;
28+
import com.google.common.cache.LoadingCache;
2329
import com.google.common.collect.ImmutableList;
2430
import com.google.common.collect.ImmutableSet;
2531
import com.google.common.primitives.Primitives;
2632
import com.google.common.primitives.Shorts;
2733
import com.google.common.primitives.SignedBytes;
34+
import com.google.common.util.concurrent.UncheckedExecutionException;
2835
import com.redis.lettucemod.RedisModulesUtils;
2936
import com.redis.lettucemod.api.StatefulRedisModulesConnection;
3037
import com.redis.lettucemod.search.CreateOptions;
@@ -43,6 +50,7 @@
4350
import io.redisearch.querybuilder.Value;
4451
import io.redisearch.querybuilder.Values;
4552
import io.trino.spi.HostAddress;
53+
import io.trino.spi.TrinoException;
4654
import io.trino.spi.connector.ColumnHandle;
4755
import io.trino.spi.connector.ColumnMetadata;
4856
import io.trino.spi.connector.SchemaNotFoundException;
@@ -72,12 +80,16 @@ public class RediSearchSession {
7280
private final TypeManager typeManager;
7381
private final StatefulRedisModulesConnection<String, String> connection;
7482
private final RediSearchConfig config;
83+
private final LoadingCache<SchemaTableName, RediSearchTable> tableCache;
7584

7685
public RediSearchSession(TypeManager typeManager, StatefulRedisModulesConnection<String, String> connection,
7786
RediSearchConfig config) {
7887
this.typeManager = requireNonNull(typeManager, "typeManager is null");
7988
this.connection = requireNonNull(connection, "connection is null");
8089
this.config = requireNonNull(config, "config is null");
90+
// TODO make table cache expiration configurable
91+
this.tableCache = CacheBuilder.newBuilder().expireAfterWrite(1, HOURS).refreshAfterWrite(1, MINUTES)
92+
.build(CacheLoader.from(this::loadTableSchema));
8193
}
8294

8395
public StatefulRedisModulesConnection<String, String> getConnection() {
@@ -107,7 +119,12 @@ public Set<String> getAllTables() throws SchemaNotFoundException {
107119
}
108120

109121
public RediSearchTable getTable(SchemaTableName tableName) throws TableNotFoundException {
110-
return loadTableSchema(tableName);
122+
try {
123+
return tableCache.getUnchecked(tableName);
124+
} catch (UncheckedExecutionException e) {
125+
throwIfInstanceOf(e.getCause(), TrinoException.class);
126+
throw e;
127+
}
111128
}
112129

113130
public void createTable(SchemaTableName name, List<RediSearchColumnHandle> columns) {
@@ -167,7 +184,7 @@ public SearchResults<String, String> execute(RediSearchTableHandle tableHandle,
167184
String index = tableHandle.getSchemaTableName().getTableName();
168185
String query = buildQuery(tableHandle.getConstraint());
169186
Builder<String, String> options = SearchOptions.<String, String>builder();
170-
tableHandle.getLimit().ifPresent(num -> options.limit(Limit.of(0, num)));
187+
options.limit(Limit.of(0, tableHandle.getLimit().isPresent() ? tableHandle.getLimit().getAsInt() : 1000));
171188
log.debug("Find documents: index: %s, query: %s", index, query);
172189
return connection.sync().search(index, query, options.build());
173190
}
@@ -227,7 +244,7 @@ private static Optional<Node> buildPredicate(RediSearchColumnHandle column, Doma
227244

228245
// Add back all of the possible single values either as an equality or an IN
229246
// predicate
230-
247+
231248
if (singleValues.size() == 1) {
232249
disjuncts.add(QueryBuilder.intersect(name, value(singleValues.get(0), type)));
233250
} else {
@@ -263,9 +280,7 @@ private static Value value(Object trinoNativeValue, Type type) {
263280
return Values.eq((Long) trinoNativeValue);
264281
}
265282
if (type instanceof VarcharType) {
266-
// TODO introduce RediSearch Field type to know which to use (tag, text,
267-
// numeric)
268-
return Values.value((String) trinoNativeValue);
283+
return Values.tags((String) trinoNativeValue);
269284

270285
}
271286
throw new UnsupportedOperationException("Type " + type + " not supported");

0 commit comments

Comments
 (0)