|
12 | 12 | import static java.util.concurrent.TimeUnit.MINUTES; |
13 | 13 |
|
14 | 14 | import java.util.Collections; |
| 15 | +import java.util.LinkedHashMap; |
15 | 16 | import java.util.List; |
| 17 | +import java.util.Map; |
16 | 18 | import java.util.Optional; |
17 | 19 | import java.util.Set; |
18 | 20 | import java.util.stream.Collectors; |
19 | 21 |
|
20 | 22 | import com.google.common.cache.CacheBuilder; |
21 | 23 | import com.google.common.cache.CacheLoader; |
22 | 24 | import com.google.common.cache.LoadingCache; |
23 | | -import com.google.common.collect.ImmutableList; |
24 | 25 | import com.google.common.collect.ImmutableSet; |
25 | 26 | import com.google.common.util.concurrent.UncheckedExecutionException; |
26 | 27 | import com.redis.lettucemod.RedisModulesUtils; |
|
29 | 30 | import com.redis.lettucemod.search.AggregateWithCursorResults; |
30 | 31 | import com.redis.lettucemod.search.CreateOptions; |
31 | 32 | import com.redis.lettucemod.search.CursorOptions; |
| 33 | +import com.redis.lettucemod.search.Document; |
32 | 34 | import com.redis.lettucemod.search.Field; |
33 | 35 | import com.redis.lettucemod.search.Group; |
34 | 36 | import com.redis.lettucemod.search.IndexInfo; |
@@ -158,26 +160,40 @@ public void dropColumn(SchemaTableName schemaTableName, String columnName) { |
158 | 160 | } |
159 | 161 |
|
160 | 162 | private RediSearchTable loadTableSchema(SchemaTableName schemaTableName) { |
161 | | - IndexInfo indexInfo = getIndexInfo(schemaTableName.getSchemaName(), schemaTableName.getTableName()); |
162 | | - |
163 | | - ImmutableList.Builder<RediSearchColumnHandle> columnHandles = ImmutableList.builder(); |
164 | | - |
165 | | - for (Field columnMetadata : indexInfo.getFields()) { |
| 163 | + String index = schemaTableName.getTableName(); |
| 164 | + Optional<IndexInfo> indexInfo = indexInfo(index); |
| 165 | + if (indexInfo.isEmpty()) { |
| 166 | + throw new TableNotFoundException(schemaTableName, format("Index '%s' not found", index), null); |
| 167 | + } |
| 168 | + Map<String, RediSearchColumnHandle> columns = new LinkedHashMap<>(); |
| 169 | + for (Field columnMetadata : indexInfo.get().getFields()) { |
166 | 170 | RediSearchColumnHandle columnHandle = buildColumnHandle(columnMetadata); |
167 | | - columnHandles.add(columnHandle); |
| 171 | + columns.put(columnHandle.getName(), columnHandle); |
168 | 172 | } |
169 | 173 |
|
| 174 | + SearchResults<String, String> results = connection.sync().search(index, "*"); |
| 175 | + for (Document<String, String> doc : results) { |
| 176 | + Set<String> fields = doc.keySet(); |
| 177 | + fields.removeAll(columns.keySet()); |
| 178 | + for (String field : fields) { |
| 179 | + columns.put(field, new RediSearchColumnHandle(field, VarcharType.VARCHAR, false)); |
| 180 | + } |
| 181 | + } |
170 | 182 | RediSearchTableHandle tableHandle = new RediSearchTableHandle(RediSearchTableHandle.Type.SEARCH, |
171 | 183 | schemaTableName); |
172 | | - return new RediSearchTable(tableHandle, columnHandles.build()); |
| 184 | + return new RediSearchTable(tableHandle, columns.values()); |
173 | 185 | } |
174 | 186 |
|
175 | | - private IndexInfo getIndexInfo(String schemaName, String tableName) throws TableNotFoundException { |
176 | | - if (connection.sync().list().contains(tableName)) { |
177 | | - return RedisModulesUtils.indexInfo(connection.sync().indexInfo(tableName)); |
| 187 | + private Optional<IndexInfo> indexInfo(String index) { |
| 188 | + try { |
| 189 | + List<Object> indexInfoList = connection.sync().indexInfo(index); |
| 190 | + if (indexInfoList != null) { |
| 191 | + return Optional.of(RedisModulesUtils.indexInfo(indexInfoList)); |
| 192 | + } |
| 193 | + } catch (Exception e) { |
| 194 | + // Ignore as index might not exist |
178 | 195 | } |
179 | | - throw new TableNotFoundException(new SchemaTableName(schemaName, tableName), |
180 | | - format("Index '%s' not found", tableName), null); |
| 196 | + return Optional.empty(); |
181 | 197 | } |
182 | 198 |
|
183 | 199 | private RediSearchColumnHandle buildColumnHandle(Field field) { |
|
0 commit comments