Skip to content

Commit

Permalink
updates to match registry-common refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Al Niessner authored and Al Niessner committed Mar 31, 2024
1 parent 4a37b49 commit 1995445
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 97 deletions.
5 changes: 2 additions & 3 deletions src/main/java/gov/nasa/pds/registry/mgr/dao/IndexDao.java
Expand Up @@ -32,8 +32,7 @@ public IndexDao(RestClient client)
*/
public boolean indexExists(String indexName) throws Exception
{
Request req = client.createRequest(Request.Method.HEAD, "/" + indexName);
Response resp = client.performRequest(req);
Response resp = client.exists (indexName);
return resp.getStatusLine().getStatusCode() == 200;
}

Expand All @@ -46,7 +45,7 @@ public boolean indexExists(String indexName) throws Exception
*/
public IndexSettings getIndexSettings(String indexName) throws Exception
{
Request req = client.createRequest(Request.Method.GET, "/" + indexName + "/_settings");
Request.Setting req = client.createSettingRequest().setIndex(indexName);
Response resp = client.performRequest(req);

SettingsResponseParser parser = new SettingsResponseParser();
Expand Down
40 changes: 15 additions & 25 deletions src/main/java/gov/nasa/pds/registry/mgr/dao/RegistryDao.java
Expand Up @@ -6,24 +6,21 @@
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import gov.nasa.pds.registry.common.Request;
import gov.nasa.pds.registry.common.Response;
import gov.nasa.pds.registry.common.RestClient;
import gov.nasa.pds.registry.common.es.dao.BulkResponseParser;
import gov.nasa.pds.registry.common.util.CloseUtils;
import gov.nasa.pds.registry.common.util.SearchResponseParser;
import gov.nasa.pds.registry.common.util.Tuple;
import gov.nasa.pds.registry.mgr.dao.resp.GetAltIdsParser;

/**
* Data access object
* @author karpenko
*/
public class RegistryDao
{
private Logger log;

{
private RestClient client;
private String indexName;

Expand All @@ -35,9 +32,7 @@ public class RegistryDao
* @param indexName Elasticsearch index
*/
public RegistryDao(RestClient client, String indexName)
{
log = LogManager.getLogger(this.getClass());

{
this.client = client;
this.indexName = indexName;
}
Expand All @@ -62,15 +57,11 @@ public void setPretty(boolean b)
public Map<String, Set<String>> getAlternateIds(Collection<String> ids) throws Exception
{
if(ids == null || ids.isEmpty()) return null;

RegistryRequestBuilder bld = new RegistryRequestBuilder();
String jsonReq = bld.createGetAlternateIdsRequest(ids);

String reqUrl = "/" + indexName + "/_search";
if(pretty) reqUrl += "?pretty";

Request req = client.createRequest(Request.Method.GET, reqUrl);
req.setJsonEntity(jsonReq);

Request.Search req = client.createSearchRequest()
.buildAlternativeIds(ids)
.setIndex(this.indexName)
.setPretty(pretty);
Response resp = client.performRequest(req);

//DebugUtils.dumpResponseBody(resp);
Expand All @@ -93,14 +84,13 @@ public void updateAlternateIds(Map<String, Set<String>> newIds) throws Exception
{
if(newIds == null || newIds.isEmpty()) return;

RegistryRequestBuilder bld = new RegistryRequestBuilder();
String json = bld.createUpdateAltIdsRequest(newIds);
log.debug("Request:\n" + json);

String reqUrl = "/" + indexName + "/_bulk"; //?refresh=wait_for";
Request req = client.createRequest(Request.Method.POST, reqUrl);
req.setJsonEntity(json);

RegistryRequestBuilder bld = new RegistryRequestBuilder();
Request.Bulk req = client.createBulkRequest()
.setIndex(this.indexName)
.setRefresh("wair_for");
for (Tuple t : bld.createUpdateAltIdsRequest(newIds)) {
req.add(t.item1, t.item2);
}
Response resp = client.performRequest(req);

// Check for Elasticsearch errors.
Expand Down
Expand Up @@ -5,7 +5,9 @@
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
Expand All @@ -14,6 +16,7 @@
import com.google.gson.stream.JsonWriter;

import gov.nasa.pds.registry.common.util.CloseUtils;
import gov.nasa.pds.registry.common.util.Tuple;
import gov.nasa.pds.registry.mgr.Constants;
import gov.nasa.pds.registry.mgr.util.es.EsQueryUtils;

Expand Down Expand Up @@ -268,70 +271,21 @@ public String createMatchAllQuery() throws IOException
writer.close();
return out.toString();
}


/**
* Build a query to select alternate ids by document primary key
* @param ids list of primary keys (lidvids right now)
* @return JSON
* @throws Exception an exception
*/
public String createGetAlternateIdsRequest(Collection<String> ids) throws Exception
{
if(ids == null || ids.isEmpty()) throw new Exception("Missing ids");

StringWriter out = new StringWriter();
JsonWriter writer = createJsonWriter(out);

// Create ids query
writer.beginObject();

// Exclude source from response
writer.name("_source").value("alternate_ids");
writer.name("size").value(ids.size());

writer.name("query");
writer.beginObject();
writer.name("ids");
writer.beginObject();

writer.name("values");
writer.beginArray();
for(String id: ids)
{
writer.value(id);
}
writer.endArray();

writer.endObject();
writer.endObject();
writer.endObject();

writer.close();
return out.toString();
}


public String createUpdateAltIdsRequest(Map<String, Set<String>> newIds) throws Exception
public List<Tuple> createUpdateAltIdsRequest(Map<String, Set<String>> newIds) throws Exception
{
if(newIds == null || newIds.isEmpty()) throw new IllegalArgumentException("Missing ids");

StringBuilder bld = new StringBuilder();

ArrayList<Tuple> updates = new ArrayList<Tuple>();
// Build NJSON (new-line delimited JSON)
for(Map.Entry<String, Set<String>> entry: newIds.entrySet())
{
// Line 1: Elasticsearch document ID
bld.append("{ \"update\" : {\"_id\" : \"" + entry.getKey() + "\" } }\n");

// Line 2: Data
String dataJson = buildUpdateDocJson("alternate_ids", entry.getValue());
bld.append(dataJson);
bld.append("\n");
Tuple statement = new Tuple();
// Line 1: Elasticsearch document ID
statement.item1 = "{ \"update\" : {\"_id\" : \"" + entry.getKey() + "\" } }";
// Line 2: Data
statement.item2 = buildUpdateDocJson("alternate_ids", entry.getValue());
updates.add(statement);
}

return bld.toString();

return updates;
}


Expand Down
13 changes: 2 additions & 11 deletions src/main/java/gov/nasa/pds/registry/mgr/srv/IndexService.java
Expand Up @@ -4,7 +4,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import gov.nasa.pds.registry.common.Request;
import gov.nasa.pds.registry.common.Response;
import gov.nasa.pds.registry.common.ResponseException;
import gov.nasa.pds.registry.common.RestClient;
Expand Down Expand Up @@ -60,13 +59,9 @@ public void createIndex(String relativeSchemaPath, String indexName, int shards,
log.info("Replicas: " + replicas);

// Create request
Request req = client.createRequest(Request.Method.PUT, "/" + indexName);
RegistryRequestBuilder bld = new RegistryRequestBuilder();
String jsonReq = bld.createCreateIndexRequest(schemaFile, shards, replicas);
req.setJsonEntity(jsonReq);

// Execute request
Response resp = client.performRequest(req);
Response resp = client.create(indexName, bld.createCreateIndexRequest(schemaFile, shards, replicas));
resp.printWarnings();
}
catch(ResponseException ex)
Expand Down Expand Up @@ -163,12 +158,8 @@ public void deleteIndex(String indexName) throws Exception
{
return;
}

// Create request
Request req = client.createRequest(Request.Method.DELETE, "/" + indexName);

// Execute request
Response resp = client.performRequest(req);
Response resp = client.delete(indexName);
resp.printWarnings();
}
catch(ResponseException ex)
Expand Down

0 comments on commit 1995445

Please sign in to comment.