Skip to content

Commit

Permalink
Add cache for field data types.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
  • Loading branch information
Yury-Fridlyand committed Feb 9, 2023
1 parent a6cf875 commit d47c624
Showing 1 changed file with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ public class OpenSearchIndex implements Table {
/**
* The cached mapping of field and type in index.
*/
private Map<String, OpenSearchDataType> cachedFieldTypes = null;
private Map<String, OpenSearchDataType> cachedFieldOpenSearchTypes = null;

/**
* The cached ExprType of fields.
*/
private Map<String, ExprType> cachedFieldTypes = null;

/**
* The cached max result window setting of index.
Expand Down Expand Up @@ -75,7 +80,6 @@ public void create(Map<String, ExprType> schema) {
mappings.put("properties", properties);

for (Map.Entry<String, ExprType> colType : schema.entrySet()) {
//properties.put(colType.getKey(), OpenSearchDataType.getOpenSearchType(colType.getValue()));
properties.put(colType.getKey(), colType.getValue().legacyTypeName().toLowerCase());
}
client.createIndex(indexName.toString(), mappings);
Expand All @@ -93,25 +97,30 @@ public void create(Map<String, ExprType> schema) {
*/
@Override
public Map<String, ExprType> getFieldTypes() {
if (cachedFieldOpenSearchTypes == null) {
cachedFieldOpenSearchTypes = new OpenSearchDescribeIndexRequest(client, indexName)
.getFieldTypes();
}
if (cachedFieldTypes == null) {
cachedFieldTypes = new OpenSearchDescribeIndexRequest(client, indexName).getFieldTypes();
cachedFieldTypes = OpenSearchDataType.traverseAndFlatten(cachedFieldOpenSearchTypes)
.entrySet().stream().collect(
LinkedHashMap::new,
(map, item) -> map.put(item.getKey(), item.getValue().getExprType()),
Map::putAll);
}
return OpenSearchDataType.traverseAndFlatten(cachedFieldTypes).entrySet().stream()
.collect(
LinkedHashMap::new,
(map, item) -> map.put(item.getKey(), item.getValue().getExprType()),
Map::putAll);
return cachedFieldTypes;
}

/**
* Get parsed mapping info.
* @return A complete map between field names and their types.
*/
public Map<String, OpenSearchDataType> getFieldOpenSearchTypes() {
if (cachedFieldTypes == null) {
cachedFieldTypes = new OpenSearchDescribeIndexRequest(client, indexName).getFieldTypes();
if (cachedFieldOpenSearchTypes == null) {
cachedFieldOpenSearchTypes = new OpenSearchDescribeIndexRequest(client, indexName)
.getFieldTypes();
}
return cachedFieldTypes;
return cachedFieldOpenSearchTypes;
}

/**
Expand Down

0 comments on commit d47c624

Please sign in to comment.