Skip to content

Commit

Permalink
fix: consume cat api in JSON format vs List (to avoid manipulating va…
Browse files Browse the repository at this point in the history
…lues)
  • Loading branch information
evans-g-crsj committed Nov 23, 2020
1 parent 07d1af2 commit 91a987a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
31 changes: 21 additions & 10 deletions src/main/java/bio/overture/rollcall/repository/IndexRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package bio.overture.rollcall.repository;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.val;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
Expand All @@ -31,6 +33,7 @@
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions.add;
import static org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions.remove;
Expand Down Expand Up @@ -59,19 +62,27 @@ public String[] getIndices() {

@SneakyThrows
public Map<String, Set<AliasMetaData>> getAliasState() {
InputStream inputStream = this.client.getLowLevelClient()
.performRequest("GET", "/_cat/aliases?h=i,a")
InputStream inputStreamFromCatApi = this.client.getLowLevelClient()
.performRequest("GET", "/_cat/aliases?format=json")
.getEntity()
.getContent();

return new BufferedReader(new InputStreamReader(inputStream))
.lines()
.map(s -> s.split("\\s+"))
.collect(
Collectors.groupingBy(a -> a[0],
Collectors.mapping(a-> AliasMetaData.builder(a[1]).build(), Collectors.toSet())
)
);
TypeReference<List<Map<String, String>>> typeRef = new TypeReference<>() {
};
ObjectMapper mapper = new ObjectMapper();
List<Map<String, String>> rawAliasesMetaData = mapper.readValue(inputStreamFromCatApi, typeRef);

Map<String, Set<AliasMetaData>> indexNameToAliasesMetaData = new HashMap<>();
for (Map<String, String> rawAliasMetaData : rawAliasesMetaData) {
String keyIndexName = rawAliasMetaData.get("index");
AliasMetaData aliasMetaData = AliasMetaData
.builder(rawAliasMetaData.get("alias"))
.build();
Set<AliasMetaData> newSetOfAliasesMetaData = Stream.of(aliasMetaData)
.collect(Collectors.toCollection(HashSet::new));
indexNameToAliasesMetaData.put(keyIndexName,newSetOfAliasesMetaData);
}
return indexNameToAliasesMetaData;
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.junit.Test;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down

0 comments on commit 91a987a

Please sign in to comment.