Skip to content

Commit

Permalink
Add support for matching names from external appliances..
Browse files Browse the repository at this point in the history
This only applies to the retrieval name matching calls. Refactored all
variations to use the trie.
  • Loading branch information
slacmshankar committed Jun 14, 2016
1 parent a0bd4a0 commit 4f19823
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,15 @@ public void addAlias(String aliasName, String realName) {
} catch(IOException ex) {
logger.error("Exception adding alias name to persistence " + aliasName, ex);
}

// Add aliases into the trie
String[] parts = this.pvName2KeyConverter.breakIntoParts(aliasName);
for(String part : parts) {
if(!parts2PVNamesForThisAppliance.containsKey(part)) {
parts2PVNamesForThisAppliance.put(part, new ConcurrentSkipListSet<String>());
}
parts2PVNamesForThisAppliance.get(part).add(aliasName);
}
}


Expand All @@ -1282,6 +1291,12 @@ public void removeAlias(String aliasName, String realName) {
} catch(IOException ex) {
logger.error("Exception removing alias name from persistence " + aliasName, ex);
}

// Remove the aliasname from the trie
String[] parts = this.pvName2KeyConverter.breakIntoParts(aliasName);
for(String part : parts) {
parts2PVNamesForThisAppliance.get(part).remove(aliasName);
}
}


Expand Down Expand Up @@ -1746,6 +1761,14 @@ private void loadAliasesFromPersistence() {
String realName = persistanceLayer.getAliasNamesToRealName(pvNameFromPersistence);
if(this.pvsForThisAppliance.contains(realName)) {
newAliases.put(pvNameFromPersistence, realName);
// Add the alias into the trie
String[] parts = this.pvName2KeyConverter.breakIntoParts(pvNameFromPersistence);
for(String part : parts) {
if(!parts2PVNamesForThisAppliance.containsKey(part)) {
parts2PVNamesForThisAppliance.put(part, new ConcurrentSkipListSet<String>());
}
parts2PVNamesForThisAppliance.get(part).add(pvNameFromPersistence);
}
}
// Add in batch sizes of 1000 or so...
if(objectCount > 1000) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -64,6 +65,25 @@ public void execute(HttpServletRequest req, HttpServletResponse resp, ConfigServ
}

try (PrintWriter out = resp.getWriter()) {
JSONArray matchingNames = getMatchingPVsInCluster(configService, limit, nameToMatch);
out.println(JSONValue.toJSONString(matchingNames));
} catch(Exception ex) {
logger.error("Exception getting all pvs on appliance " + configService.getMyApplianceInfo().getIdentity(), ex);
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}


/**
* Get a list of PV's being archived in this cluster
* @param configService
* @param limit - The numbers of PV's you want to limit the response to;
* @param nameToMatch - A regex specifying the PV name pattern; globs should be converted to regex's
* @return
* @throws IOException
*/
public static JSONArray getMatchingPVsInCluster(ConfigService configService, int limit, String nameToMatch) throws IOException {
try {
LinkedList<String> mgmtURLs = getMgmtURLsInCluster(configService);

List<String> pvNamesURLs = new LinkedList<String>();
Expand All @@ -74,18 +94,27 @@ public void execute(HttpServletRequest req, HttpServletResponse resp, ConfigServ
pvNamesURLs.add(pvNamesURL);
logger.debug("Getting matching PV names using " + pvNamesURL);
}


Map<String, String> externalServers = configService.getExternalArchiverDataServers();
if(externalServers != null) {
for(String serverUrl : externalServers.keySet()) {
String index = externalServers.get(serverUrl);
if(index.equals("pbraw")) {
logger.debug("Asking external EPICS Archiver Appliance " + serverUrl + " for PV's matching " + nameToMatch);
pvNamesURLs.add(serverUrl + "/bpl/getMatchingPVs?regex=" + URLEncoder.encode(nameToMatch, "UTF-8"));
}
}
}

JSONArray matchingNames = GetUrlContent.combineJSONArrays(pvNamesURLs);
out.println(JSONValue.toJSONString(matchingNames));
} catch(Exception ex) {
logger.error("Exception getting all pvs on appliance " + configService.getMyApplianceInfo().getIdentity(), ex);
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return matchingNames;
} catch(Exception ex) {
throw new IOException(ex);
}
}


private LinkedList<String> getMgmtURLsInCluster(ConfigService configService) {
private static LinkedList<String> getMgmtURLsInCluster(ConfigService configService) {
LinkedList<String> mgmtURLs = new LinkedList<String>();
try {
JSONArray appliancesInCluster = GetUrlContent.getURLContentAsJSONArray(configService.getMyApplianceInfo().getMgmtURL() + "/getAppliancesInCluster");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -17,31 +15,19 @@ public class SearchForPVsRegex implements BPLAction {
private static Logger logger = Logger.getLogger(SearchForPVsRegex.class.getName());
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp, ConfigService configService) throws IOException {
String regex = req.getParameter("regex");
if(regex == null || regex.equals("")) {
String nameToMatch = req.getParameter("regex");
if(nameToMatch == null || nameToMatch.equals("")) {
logger.error("This search needs to be called with a regex argument.");
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}

logger.debug("Regex for searching for pvnames is " + regex);
logger.debug("Regex for searching for pvnames is " + nameToMatch);

Pattern pattern = Pattern.compile(regex);
LinkedList<String> matchingPVNames = new LinkedList<String>();
for(String pvName : configService.getAllPVs()) {
Matcher matcher = pattern.matcher(pvName);
if(matcher.matches()) {
matchingPVNames.add(pvName);
}
}
for(String pvName : configService.getAllAliases()) {
Matcher matcher = pattern.matcher(pvName);
if(matcher.matches()) {
matchingPVNames.add(pvName);
}
}
resp.setContentType("text/plain");
try(PrintWriter out = resp.getWriter()) {
@SuppressWarnings("unchecked")
List<String> matchingPVNames = (List<String>) GetMatchingPVs.getMatchingPVsInCluster(configService, -1, nameToMatch);
for(String pvName : matchingPVNames) {
out.println(pvName);
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/org/epics/archiverappliance/utils/ui/GetUrlContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,27 @@ public static String getURLContent(String urlStr) {
return null;
}

public static JSONArray getURLContentAsJSONArray(String urlStr) {
return getURLContentAsJSONArray(urlStr, true);
}

/**
* Given a URL, get the contents as a JSON Array
* @param urlStr
* @param logErrors - if false, do not log any exceptions (they are expected)
* @return
*/
public static JSONArray getURLContentAsJSONArray(String urlStr) {
public static JSONArray getURLContentAsJSONArray(String urlStr, boolean logErrors) {
try {
logger.debug("Getting the contents of " + urlStr + " as a JSON array.");
JSONParser parser=new JSONParser();
try (InputStream is = getURLContentAsStream(urlStr)) {
return (JSONArray) parser.parse(new InputStreamReader(is));
}
} catch (IOException ex) {
logger.error("Exception getting contents of internal URL " + urlStr, ex);
if (logErrors) { logger.error("Exception getting contents of internal URL " + urlStr, ex); }
} catch (ParseException pex) {
logger.error("Parse exception getting contents of internal URL " + urlStr + " at " + pex.getPosition(), pex);
if (logErrors) { logger.error("Parse exception getting contents of internal URL " + urlStr + " at " + pex.getPosition(), pex); }
}
return null;
}
Expand Down

0 comments on commit 4f19823

Please sign in to comment.