Skip to content

Commit

Permalink
#891 appending list kvp to downloads stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Collins committed Mar 25, 2024
1 parent 91bd4e0 commit 9b0f8f1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/main/java/au/org/ala/biocache/dao/SolrIndexDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@ private TupleStream openStream(SolrParams params) throws IOException {
@Override
public int streamingQuery(SolrQuery query, ProcessInterface procSearch, ProcessInterface procFacet, SolrQuery endemicFacetSuperset) throws SolrServerException {
int tupleCount = 0;
boolean finished = false;
try {
if (logger.isDebugEnabled()) {
logger.debug("SOLR query:" + query.toString());
Expand All @@ -1125,9 +1126,9 @@ public int streamingQuery(SolrQuery query, ProcessInterface procSearch, ProcessI
if (procSearch != null && query.getRows() != 0) {
try (TupleStream solrStream = openStream(buildSearchExpr(query));) {
Tuple tuple;
while (!(tuple = solrStream.read()).EOF && (tupleCount < query.getRows() || query.getRows() < 0)) {
while (!(tuple = solrStream.read()).EOF && (tupleCount < query.getRows() || query.getRows() < 0) && !finished) {
tupleCount++;
procSearch.process(tuple);
finished = procSearch.process(tuple);
}
procSearch.flush();
}
Expand Down
38 changes: 33 additions & 5 deletions src/main/java/au/org/ala/biocache/service/ListsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import au.org.ala.biocache.service.ListsService.SpeciesListSearchDTO.SpeciesListDTO;
import au.org.ala.biocache.util.SearchUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import groovyjarjarantlr4.v4.misc.OrderedHashMap;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -176,6 +177,8 @@ public List<Kvp> getKvp(String dataResourceUid) {
int max = 400; // response size can be limited by api gateway
int offset = 0;

Map<String, Integer> kvpKeys = new OrderedHashMap<>();

try {
while (hasAnotherPage) {
SpeciesListItemsDTO speciesListItems = restTemplate.getForObject(new URI(speciesListUrl + "/ws/speciesListItems/" + dataResourceUid + "?includeKVP=true&max=" + max + "&offset=" + offset), SpeciesListItemsDTO.class);
Expand All @@ -188,12 +191,23 @@ public List<Kvp> getKvp(String dataResourceUid) {
// ignore species list item when there are no lft rgt values for the LSID
String fq = searchUtils.getTaxonSearch(item.lsid)[0];
if (fq.startsWith("lft:[")) {
List<String> keys = new ArrayList<>();
List<String> values = new ArrayList<>();
List<String> keys = new ArrayList<>(kvpKeys.size());
List<String> values = new ArrayList<>(kvpKeys.size());

for (KvpDTO kvp : item.kvpValues) {
keys.add(kvp.key);
values.add(kvp.value);
int pos = kvpKeys.getOrDefault(kvp.key, kvpKeys.size());
if (pos == kvpKeys.size()) {
kvpKeys.put(kvp.key, pos);
}

// padding. keys and length is updated before return
while (keys.size() <= pos) {
keys.add("");
values.add("");
}

keys.set(pos, kvp.key);
values.set(pos, kvp.value);
}

long lft = Long.parseLong(fq.replaceAll("(.*\\[| TO.*)", ""));
Expand All @@ -212,6 +226,20 @@ public List<Kvp> getKvp(String dataResourceUid) {

if (list.size() > 0) {
list.sort(Kvp.KvpComparator);

// kvp array padding and key standardisation
for (Kvp kvp : list) {
// padding
while (kvp.keys.size() < kvpKeys.size()) {
kvp.keys.add("");
kvp.values.add("");
}

// standardise key
for (Map.Entry<String, Integer> entity : kvpKeys.entrySet()) {
kvp.keys.set(entity.getValue(), entity.getKey());
}
}
return list;
} else {
return null;
Expand Down Expand Up @@ -307,7 +335,7 @@ public String getKvpValue(int idx, List<Kvp> kvps, Kvp lftrgt) {

if (kvps != null && kvps.size() > idx) {
Kvp kvp = find(kvps, lftrgt);
if (kvp != null) {
if (kvp != null && idx < kvp.values.size()) {
value = kvp.values.get(idx);
}
}
Expand Down

0 comments on commit 9b0f8f1

Please sign in to comment.