|
1 | 1 | package org.apache.calcite.adapter.redisearch; |
2 | 2 |
|
3 | | -import com.google.common.collect.ImmutableMap; |
4 | | -import com.redis.lettucemod.RedisModulesClient; |
5 | | -import com.redis.lettucemod.Utils; |
6 | | -import com.redis.lettucemod.api.StatefulRedisModulesConnection; |
7 | | -import com.redis.lettucemod.api.search.Field; |
8 | | -import com.redis.lettucemod.api.search.IndexInfo; |
9 | | -import io.lettuce.core.RedisURI; |
10 | | -import org.apache.calcite.rel.type.RelDataTypeFactory; |
11 | | -import org.apache.calcite.rel.type.RelDataTypeImpl; |
12 | | -import org.apache.calcite.rel.type.RelDataTypeSystem; |
13 | | -import org.apache.calcite.rel.type.RelProtoDataType; |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
14 | 7 | import org.apache.calcite.schema.Table; |
15 | 8 | import org.apache.calcite.schema.impl.AbstractSchema; |
16 | | -import org.apache.calcite.sql.type.SqlTypeFactoryImpl; |
17 | | -import org.apache.calcite.sql.type.SqlTypeName; |
18 | 9 |
|
19 | | -import java.util.Map; |
| 10 | +import com.google.common.collect.ImmutableMap; |
| 11 | +import com.redis.lettucemod.RedisModulesUtils; |
| 12 | +import com.redis.lettucemod.api.StatefulRedisModulesConnection; |
| 13 | +import com.redis.lettucemod.search.IndexInfo; |
20 | 14 |
|
21 | 15 | /** |
22 | 16 | * Schema mapped onto a RediSearch index. |
23 | 17 | */ |
24 | 18 | public class RediSearchSchema extends AbstractSchema { |
25 | 19 |
|
26 | | - private final String index; |
27 | 20 | private final StatefulRedisModulesConnection<String, String> connection; |
28 | | - |
29 | | - public RediSearchSchema(String uri, String index, String username, String password) { |
30 | | - this.index = index; |
31 | | - RedisURI redisURI = RedisURI.create(uri); |
32 | | - if (username != null && password != null) { |
33 | | - redisURI.setUsername(username); |
34 | | - redisURI.setPassword(password.toCharArray()); |
| 21 | + private final Map<String, Table> tableMap; |
| 22 | + |
| 23 | + public RediSearchSchema(StatefulRedisModulesConnection<String, String> connection, String index) { |
| 24 | + this.connection = connection; |
| 25 | + if (index == null) { |
| 26 | + this.tableMap = this.createTables(this.indicesFromRediSearch()); |
| 27 | + } else { |
| 28 | + this.tableMap = this.createTables(Collections.singletonList(index)); |
35 | 29 | } |
36 | | - RedisModulesClient client = RedisModulesClient.create(redisURI); |
37 | | - this.connection = client.connect(); |
38 | 30 | } |
39 | 31 |
|
40 | | - @Override |
41 | | - protected Map<String, Table> getTableMap() { |
42 | | - final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder(); |
43 | | - builder.put(index, new RediSearchTable(this, index)); |
44 | | - return builder.build(); |
| 32 | + private List<String> indicesFromRediSearch() { |
| 33 | + return connection.sync().list(); |
45 | 34 | } |
46 | 35 |
|
47 | | - public StatefulRedisModulesConnection<String, String> getConnection() { |
48 | | - return connection; |
49 | | - } |
| 36 | + private Map<String, Table> createTables(List<String> indices) { |
| 37 | + ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder(); |
50 | 38 |
|
51 | | - public IndexInfo getIndexInfo() { |
52 | | - return Utils.indexInfo(connection.sync().indexInfo(index)); |
| 39 | + for (String index : indices) { |
| 40 | + builder.put(index, new RediSearchTable(connection, indexInfo(index))); |
| 41 | + } |
| 42 | + |
| 43 | + return builder.build(); |
53 | 44 | } |
54 | 45 |
|
55 | | - private SqlTypeName sqlTypeName(Field field) { |
56 | | - if (field.getType() == Field.Type.NUMERIC) { |
57 | | - return SqlTypeName.DOUBLE; |
58 | | - } |
59 | | - return SqlTypeName.VARCHAR; |
| 46 | + @Override |
| 47 | + protected Map<String, Table> getTableMap() { |
| 48 | + return this.tableMap; |
60 | 49 | } |
61 | 50 |
|
62 | | - public RelProtoDataType getRelDataType() { |
63 | | - final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT); |
64 | | - final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder(); |
65 | | - IndexInfo indexInfo = getIndexInfo(); |
66 | | - for (Field field : indexInfo.getFields()) { |
67 | | - fieldInfo.add(field.getName(), typeFactory.createSqlType(sqlTypeName(field))).nullable(true); |
68 | | - } |
69 | | - return RelDataTypeImpl.proto(fieldInfo.build()); |
| 51 | + private IndexInfo indexInfo(String index) { |
| 52 | + return RedisModulesUtils.indexInfo(connection.sync().indexInfo(index)); |
70 | 53 | } |
| 54 | + |
71 | 55 | } |
0 commit comments