Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Bug fixes: #69, #53, #62 #71

Merged
merged 5 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,64 +90,53 @@ public ResponseEntity<Products> collectionsOfABundle(@ApiParam(value = "lidvid (
,@ApiParam(value = "only return the summary, useful to get the list of available properties", defaultValue = "false") @Valid @RequestParam(value = "only-summary", required = false, defaultValue="false") Boolean onlySummary
)
{
return this.getBundlesCollections(lidvid, start, limit, fields, sort, onlySummary);
return this.getBundlesCollectionsEntity(lidvid, start, limit, fields, sort, onlySummary);
}

private List<String> getRefLidCollection (String lidvid) throws IOException,LidVidNotFoundException
{
List<String> reflids = new ArrayList<String>();

for (final Map<String, Object> bundle : new ElasticSearchHitIterator(this.esRegistryConnection.getRestHighLevelClient(),
ElasticSearchRegistrySearchRequestBuilder.getQueryFieldFromLidvid(lidvid, "ref_lid_collection",
this.esRegistryConnection.getRegistryIndex())))
{
if (bundle.get("ref_lid_collection") instanceof String)
{ reflids.add(this.productBO.getLatestLidVidFromLid(bundle.get("ref_lid_collection").toString())); }
else
{
@SuppressWarnings("unchecked")
List<String> clids = (List<String>)bundle.get("ref_lid_collection");
for (String clid : clids)
{ reflids.add(this.productBO.getLatestLidVidFromLid(clid)); }
}
}
return reflids;
}

private Products getCollectionChildren(String lidvid, int start, int limit, List<String> fields, List<String> sort, boolean onlySummary) throws IOException,LidVidNotFoundException
private Products getBundleCollections(String lidvid, int start, int limit, List<String> fields,
List<String> sort, boolean onlySummary) throws IOException, LidVidNotFoundException
{
long begin = System.currentTimeMillis();
lidvid = this.productBO.getLatestLidVidFromLid(lidvid);
MyBundlesApiController.log.info("request bundle lidvid, collections children: " + lidvid);

lidvid = productBO.getLidVidDao().getLatestLidVidFromLid(lidvid);
MyBundlesApiController.log.info("Get bundle's collections. Bundle LIDVID = " + lidvid);

List<String> clidvids = productBO.getBundleDao().getBundleCollectionLidVids(lidvid);

HashSet<String> uniqueProperties = new HashSet<String>();
List<String> clidvids = this.getRefLidCollection(lidvid);
Products products = new Products();
Summary summary = new Summary();

if (sort == null) { sort = Arrays.asList(); }

summary.setHits(clidvids.size());
summary.setStart(start);
summary.setLimit(limit);
summary.setSort(sort);
summary.setStart(start);
summary.setTook(-1);
products.setSummary(summary);

if (0 < clidvids.size())
int size = clidvids.size();
if (size > 0 && start < size && limit > 0)
{
this.fillProductsFromLidvids(products, uniqueProperties,
clidvids.subList(start, clidvids.size() < start+limit ? clidvids.size(): start+limit),
fields, onlySummary);
int end = start + limit;
if(end > size) end = size;
List<String> ids = clidvids.subList(start, end);
fillProductsFromLidvids(products, uniqueProperties, ids, fields, onlySummary);
}
else
{
MyBundlesApiController.log.warn ("Did not find any collections for bundle lidvid: " + lidvid);
}
else MyBundlesApiController.log.warn ("Did not find any collections for bundle lidvid: " + lidvid);

summary.setProperties(new ArrayList<String>(uniqueProperties));
summary.setTook((int)(System.currentTimeMillis() - begin));
return products;
}

private ResponseEntity<Products> getBundlesCollections(String lidvid, int start, int limit, List<String> fields, List<String> sort, boolean onlySummary)

private ResponseEntity<Products> getBundlesCollectionsEntity(String lidvid, int start, int limit,
List<String> fields, List<String> sort, boolean onlySummary)
{
String accept = this.request.getHeader("Accept");
MyBundlesApiController.log.info("accept value is " + accept);
Expand All @@ -160,7 +149,7 @@ private ResponseEntity<Products> getBundlesCollections(String lidvid, int start,
{
try
{
Products products = this.getCollectionChildren(lidvid, start, limit, fields, sort, onlySummary);
Products products = getBundleCollections(lidvid, start, limit, fields, sort, onlySummary);
return new ResponseEntity<Products>(products, HttpStatus.OK);
}
catch (IOException e)
Expand All @@ -177,6 +166,7 @@ private ResponseEntity<Products> getBundlesCollections(String lidvid, int start,
else return new ResponseEntity<Products>(HttpStatus.NOT_IMPLEMENTED);
}


@Override
public ResponseEntity<Products> productsOfABundle(String lidvid, @Valid Integer start, @Valid Integer limit,
@Valid List<String> fields, @Valid List<String> sort, @Valid Boolean onlySummary)
Expand Down Expand Up @@ -217,7 +207,7 @@ private Products getProductChildren(String lidvid, int start, int limit, List<St

int iteration=0,wsize=0;
HashSet<String> uniqueProperties = new HashSet<String>();
List<String> clidvids = this.getRefLidCollection(lidvid);
List<String> clidvids = productBO.getBundleDao().getBundleCollectionLidVids(lidvid);
List<String> plidvids = new ArrayList<String>();
List<String> wlidvids = new ArrayList<String>();
Products products = new Products();
Expand Down Expand Up @@ -264,7 +254,7 @@ private Products getProductChildren(String lidvid, int start, int limit, List<St

MyBundlesApiController.log.info("found " + Integer.toString(plidvids.size()) + " products in this bundle");

if (0 < plidvids.size())
if (plidvids.size() > 0 && limit > 0)
{
this.fillProductsFromLidvids(products, uniqueProperties,
plidvids.subList(0, plidvids.size() < limit ? plidvids.size() : limit), fields, onlySummary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private Products getProductChildren(String lidvid, int start, int limit, List<St
iteration = iteration + pageOfLidvids.size() + wsize;
}

if (0 < productLidvids.size())
if (productLidvids.size() > 0 && limit > 0)
{
this.fillProductsFromLidvids(products, uniqueProperties,
productLidvids.subList(0, productLidvids.size() < limit ? productLidvids.size() : limit), fields, onlySummary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private List<String> getCollectionLidvids (String lidvid, boolean noVer) throws
private Products getContainingCollection(String lidvid, @Valid Integer start, @Valid Integer limit,
@Valid List<String> fields, @Valid List<String> sort, @Valid Boolean summaryOnly) throws IOException,LidVidNotFoundException
{
long begin = System.currentTimeMillis();
long begin = System.currentTimeMillis();
if (!lidvid.contains("::")) lidvid = this.productBO.getLatestLidVidFromLid(lidvid);

MyProductsApiController.log.info("find all bundles containing the product lidvid: " + lidvid);
Expand All @@ -236,12 +236,19 @@ private Products getContainingCollection(String lidvid, @Valid Integer start, @V
summary.setTook(-1);
products.setSummary(summary);

if (0 < collectionLidvids.size())
{
this.fillProductsFromLidvids(products, uniqueProperties,
collectionLidvids.subList(start, collectionLidvids.size() < start+limit ? collectionLidvids.size() : +limit), fields,
summaryOnly); }
else MyProductsApiController.log.warn("Did not find a product with lidvid: " + lidvid);
int size = collectionLidvids.size();
if (size > 0 && limit > 0 && start < size)
{
int end = start + limit;
if(end > size) end = size;
List<String> ids = collectionLidvids.subList(start, end);

this.fillProductsFromLidvids(products, uniqueProperties, ids, fields, summaryOnly);
}
else
{
MyProductsApiController.log.warn("Did not find a product with lidvid: " + lidvid);
}

summary.setHits(collectionLidvids.size());
summary.setProperties(new ArrayList<String>(uniqueProperties));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import org.elasticsearch.client.RestHighLevelClient;

public interface ElasticSearchRegistryConnection {
public interface ElasticSearchRegistryConnection
{
public RestHighLevelClient getRestHighLevelClient();
public String getRegistryIndex();
public String getRegistryRefIndex();
public int getTimeOutSeconds();


public void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.util.Set;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
Expand Down Expand Up @@ -40,6 +42,12 @@ public class ElasticSearchRegistryConnectionImpl implements ElasticSearchRegistr
private int timeOutSeconds;
private ArrayList<String> crossClusterNodes;


public ElasticSearchRegistryConnectionImpl()
{
this(Arrays.asList("localhost:9200"), "registry", "registry-refs", 5, null, null, false);
}

public ElasticSearchRegistryConnectionImpl(List<String> hosts,
String registryIndex,
String registryRefIndex,
Expand Down Expand Up @@ -163,7 +171,7 @@ private ArrayList<String> checkCCSConfig() {
}
}
catch(Exception ex) {
throw new RuntimeException(ex);
log.warn("Could not get cluster information. Cross cluster search is inactive. " + ex.getMessage());
}
return result;
}
Expand All @@ -183,4 +191,17 @@ private String createCCSIndexString(String indexName) {

return result;
}


public void close()
{
try
{
restHighLevelClient.close();
}
catch(Exception ex)
{
// Ignore
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ static private Product addPropertiesFromESEntity(
meta.setVersion(ep.getVersion());
}

String creationDateTime = ep.getCreationDate();
if (creationDateTime != null) {
meta.setCreationDateTime(ep.getCreationDate());
List<String> creationDateTime = ep.getCreationDate();
if (creationDateTime != null && !creationDateTime.isEmpty()) {
meta.setCreationDateTime(creationDateTime.get(0));
}

List<String> updateDateTime = ep.getModificationDate();
if (updateDateTime != null) {
meta.setUpdateDateTime(updateDateTime.get(0)); // TODO check which modification time to use when there are more than one
if (updateDateTime != null && !updateDateTime.isEmpty()) {
// TODO check which modification time to use when there are more than one
meta.setUpdateDateTime(updateDateTime.get(0));
}



String labelUrl = ep.getPDS4FileRef();
if (labelUrl != null) {
meta.setLabelUrl(labelUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class Pds4JsonSearchRequestBuilder
};

private String registryIndex;
private String registryRefIndex;
private int timeOutSeconds;

/**
Expand All @@ -40,10 +39,9 @@ public class Pds4JsonSearchRequestBuilder
* @param registryRefindex Elasticsearch registry refs index
* @param timeOutSeconds Elasticsearch request timeout
*/
public Pds4JsonSearchRequestBuilder(String registryIndex, String registryRefindex, int timeOutSeconds)
public Pds4JsonSearchRequestBuilder(String registryIndex, int timeOutSeconds)
{
this.registryIndex = registryIndex;
this.registryRefIndex = registryRefindex;
this.timeOutSeconds = timeOutSeconds;
}

Expand All @@ -53,7 +51,7 @@ public Pds4JsonSearchRequestBuilder(String registryIndex, String registryRefinde
*/
public Pds4JsonSearchRequestBuilder()
{
this("registry", "registry-refs", 60);
this("registry", 10);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package gov.nasa.pds.api.engineering.elasticsearch.business;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;

import gov.nasa.pds.api.engineering.elasticsearch.ElasticSearchRegistryConnection;
import gov.nasa.pds.model.Products;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;


/**
* Bundle Data Access Object (DAO).
* Provides methods to get bundle information from Elasticsearch.
*
* @author karpenko
*/
public class BundleDAO
{
private ElasticSearchRegistryConnection esConnection;

/**
* Constructor
* @param esConnection Elasticsearch connection
*/
public BundleDAO(ElasticSearchRegistryConnection esConnection)
{
this.esConnection = esConnection;
}


public Products getBundleCollections(String lidvid, int start, int limit, List<String> fields,
List<String> sort, boolean onlySummary) throws IOException, LidVidNotFoundException
{
// TODO: Move code from the bundle controller here.
throw new NotImplementedException();
}


public List<String> getBundleCollectionLidVids(String bundleLidVid) throws IOException, LidVidNotFoundException
{
// Get bundle by lidvid.
GetRequest esRequest = new GetRequest(esConnection.getRegistryIndex(), bundleLidVid);

// Fetch collection references only.
String[] includes = new String[] { "ref_lidvid_collection", "ref_lid_collection" };
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, null);
esRequest.fetchSourceContext(fetchSourceContext);

// Call Elasticsearch
RestHighLevelClient client = esConnection.getRestHighLevelClient();
GetResponse esResponse = client.get(esRequest, RequestOptions.DEFAULT);
if(!esResponse.isExists()) throw new LidVidNotFoundException(bundleLidVid);

Map<String, Object> fieldMap = esResponse.getSourceAsMap();

// LidVid references (e.g., OREX bundle)
List<String> ids = ESResponseUtils.getFieldValues(fieldMap, "ref_lidvid_collection");
if(ids != null) return ids;

// Lid references (e.g., Kaguya bundle)
ids = ESResponseUtils.getFieldValues(fieldMap, "ref_lid_collection");
if(ids != null)
{
// Get the latest versions of LIDs (Return LIDVIDs)
ids = LidVidUtils.getLatestLids(esConnection, ids);
return ids;
}

return new ArrayList<String>(0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gov.nasa.pds.api.engineering.elasticsearch.business;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


public class ESResponseUtils
{
@SuppressWarnings({"unchecked", "rawtypes"})
public static List<String> getFieldValues(Map<String, Object> fieldMap, String fieldName)
{
if(fieldMap == null || fieldName == null) return null;

Object obj = fieldMap.get(fieldName);
if(obj == null) return null;

if(obj instanceof String)
{
return Arrays.asList((String)obj);
}
else if(obj instanceof List)
{
return (List)obj;
}

return null;
}
}
Loading