All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gov.nasa.pds.registry.common.connection.aws.SearchRespWrap Maven / Gradle / Ivy

package gov.nasa.pds.registry.common.connection.aws;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.NotImplementedException;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.opensearch._types.aggregations.StringTermsBucket;
import org.opensearch.client.opensearch.core.ScrollRequest;
import org.opensearch.client.opensearch.core.ScrollResponse;
import org.opensearch.client.opensearch.core.SearchResponse;
import org.opensearch.client.opensearch.core.search.Hit;
import gov.nasa.pds.registry.common.Response;
import gov.nasa.pds.registry.common.es.dao.dd.LddInfo;
import gov.nasa.pds.registry.common.es.dao.dd.LddVersions;

@SuppressWarnings("unchecked") // JSON heterogenous structures requires raw casting
class SearchRespWrap implements Response.Search {
  final private OpenSearchClient client;
  final private SearchResponse parent;
  SearchRespWrap(OpenSearchClient client, SearchResponse parent) {
    this.client = client;
    this.parent = parent;
  }
  @Override
  public Map> altIds() throws UnsupportedOperationException, IOException {
    HashMap> results = new HashMap>();
    if (true) throw new NotImplementedException("Need to fill this out when have a return value");
    return results;
  }
  @Override
  public Set fields() throws UnsupportedOperationException, IOException {
    Set results = new HashSet();
    for (Hit hit : this.parent.hits().hits()) {
      for (String value : ((Map)hit.source()).values()) {
        results.add(value);
      }
    }
    return results;
  }
  @Override
  public List lidvids() {
    ArrayList lidvids = new ArrayList();
    String scrollID = this.parent.scrollId();
    for (Hit hit : this.parent.hits().hits()) {
      lidvids.add(((Map)hit.source()).get("lidvid"));
    }
    if (this.parent.scrollId() != null) {
      try {
        ScrollResponse page;
        while (lidvids.size() < this.parent.hits().total().value()) {
          page = this.client.scroll(new ScrollRequest.Builder()
              .scroll(new Time.Builder().time("24m").build()).scrollId(scrollID).build(), Object.class);
          scrollID = page.scrollId(); // docs say this can change
          for (Hit hit : page.hits().hits()) {
            lidvids.add(((Map)hit.source()).get("lidvid"));
          }
        }
      } catch (IOException ioe) {
        throw new RuntimeException("How did we get here???", ioe);
      }
    }
    return lidvids;
  }
  @Override
  public List latestLidvids() {
    ArrayList lidvids = new ArrayList();
    if (true) throw new NotImplementedException("Need to fill this out when have a return value");
    return lidvids;
  }
  @Override
  public LddVersions lddInfo() throws UnsupportedOperationException, IOException {
    LddVersions result = new LddVersions();
    for (Hit hit : this.parent.hits().hits()) {
      Map source = (Map)hit.source();
      if (source.containsKey("attr_name") && source.containsKey("date")) {
        result.addSchemaFile(source.get("attr_name"));
        result.updateDate(source.get("date"));
      } else {
        throw new UnsupportedOperationException("Either date or attr_name or both are missing from hit.");
      }
    }
    return result;
  }
  @Override
  public List ldds() throws UnsupportedOperationException, IOException {
    ArrayList results = new ArrayList();
    if (true) throw new NotImplementedException("Need to fill this out when have a return value");
    return results;
  }
  @Override
  public Set nonExistingIds(Collection from_ids)
      throws UnsupportedOperationException, IOException {
    HashSet results = new HashSet(from_ids);
    for (Hit hit : this.parent.hits().hits()) {
      if (from_ids.contains(hit.id())) from_ids.remove(hit.id());
    }
    return results;
  }
  @Override
  public List batch() throws UnsupportedOperationException, IOException {
    return this.parent.documents();
  }
  @Override
  public String field(String name) throws NoSuchFieldException {
    return ((Map)this.parent.documents().get(0)).get(name).toString();
  }
  @Override
  public List> documents() {
    ArrayList> docs = new ArrayList>();
    for (Object doc : this.parent.documents()) {
      docs.add((Map)doc);
    }
    if (docs.isEmpty()) { // try hits
      for (Hit hit : this.parent.hits().hits()) {
        docs.add((Map)hit.source());
      }
    }
    return docs;
  }
  @Override
  public List bucketValues() {
    ArrayList keys =  new ArrayList();
    for (StringTermsBucket bucket : this.parent.aggregations().get("duplicates").sterms().buckets().array()) {
      keys.add(bucket.key());
    }
    return keys;
  }
}