Skip to content

Commit

Permalink
Added better short circuiting to prevent mass street file lookups. Mi…
Browse files Browse the repository at this point in the history
…nor improvement to geocache city lookup.
  • Loading branch information
ash858 committed Dec 10, 2013
1 parent 6e606e7 commit 69d3ac8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
Expand Up @@ -29,8 +29,7 @@
*/
public class DistrictShapefileDao extends BaseDao
{
private static final String SCHEMA = "districts"; // Move to app.properties
private static final String SRID = "4326";
private static final String SCHEMA = "districts";
private final Logger logger = Logger.getLogger(DistrictShapefileDao.class);
private QueryRunner run = getQueryRunner();

Expand Down
Expand Up @@ -55,7 +55,7 @@ public GeoCacheDao() {
private final static String SQLFRAG_WHERE_CITY_ZIP_MATCH =
"WHERE gc.street = '' \n" +
"AND ((gc.zip5 = ? AND gc.zip5 != '') " +
" OR (? = '' AND gc.location = ? AND gc.location != '' AND gc.state = ?))";
" OR (? = '' AND gc.zip5 = '' AND gc.location = ? AND gc.location != '' AND gc.state = ?))";

private final static String SQL_CACHE_HIT_BUILDING = String.format("%s%s", SQLFRAG_SELECT, SQLFRAG_WHERE_BUILDING_MATCH);
private final static String SQL_CACHE_HIT_CITY_ZIP = String.format("%s%s", SQLFRAG_SELECT, SQLFRAG_WHERE_CITY_ZIP_MATCH);
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/gov/nysenate/sage/dao/provider/StreetFileDao.java
Expand Up @@ -149,13 +149,17 @@ public List<DistrictedStreetRange> getDistrictStreetRangesByZip(String zip5)
*/
public List<DistrictedStreetRange> getDistrictStreetRanges(String street, List<String> zip5List)
{
/** Format the street name to aid in street file match */
street = (street != null) ? getFormattedStreet(street, false) : "";

/** Short circuit the request under conditions where lots of data would be retrieved. */
if (zip5List == null || zip5List.isEmpty()) {
return null;
}
if (street == null) street = "";
if (!street.isEmpty()) {
street = getFormattedStreet(street, false);
else if (zip5List.size() > 1 && street.isEmpty()) {
return null;
}

String sql =
"SELECT * " +
"FROM streetfile " +
Expand Down Expand Up @@ -212,7 +216,9 @@ public Map<DistrictType, Set<String>> getAllStandardDistrictMatches(List<String>
*/
public Map<DistrictType, Set<String>> getAllStandardDistrictMatches(List<String> streetList, List<String> zip5List)
{
if ((zip5List == null || zip5List.isEmpty()) && (streetList == null || streetList.isEmpty())) return null; // Short circuit on missing input
/** Short circuit on missing input */
if ((zip5List == null || zip5List.isEmpty()) && (streetList == null || streetList.isEmpty())) return null;

String sqlTmpl = "SELECT DISTINCT %s::character varying AS code, '%s' AS type\n" +
"FROM streetfile\n" +
"WHERE (%s) AND (%s)";
Expand Down
Expand Up @@ -218,10 +218,9 @@ public DistrictResult getMultiMatchResult(GeocodedAddress geocodedAddress, Boole

Address address = geocodedAddress.getAddress();
GeocodeQuality geocodeQuality = geocodedAddress.getGeocode().getQuality();
Map<DistrictType, Set<String>> matches = new HashMap<>();
Map<DistrictType, Set<String>> matches;
List<String> zip5List = new ArrayList<>();
List<String> streetList = new ArrayList<>();
String streetLineJson = null;

logger.debug("Zip Provided: " + zipProvided);

Expand Down Expand Up @@ -252,7 +251,7 @@ else if (!address.getCity().isEmpty()) {
if (matches != null && !matches.isEmpty()) {
/** Retrieve source map for city and zip match levels */
if (matchLevel.compareTo(DistrictMatchLevel.STREET) < 0) {
DistrictMap sourceMap = districtShapefileDao.getOverlapReferenceBoundary(DistrictType.ZIP, new HashSet<String>(zip5List));
DistrictMap sourceMap = districtShapefileDao.getOverlapReferenceBoundary(DistrictType.ZIP, new HashSet<>(zip5List));
districtInfo.setReferenceMap(sourceMap);
}

Expand Down Expand Up @@ -281,7 +280,7 @@ else if (matchType.equals(DistrictType.SENATE) && overlap != null && overlap.get
resultStatus = ResultStatus.SUCCESS;
districtedAddress.setDistrictInfo(districtInfo);
districtedAddress.setDistrictMatchLevel(matchLevel);
logger.info("Resulting match level: " + matchLevel);
logger.info("District match level: " + matchLevel);
}
}
}
Expand Down
Expand Up @@ -366,9 +366,11 @@ public List<DistrictResult> assignDistricts(final List<GeocodedAddress> geocoded

public DistrictResult assignMultiMatchDistricts(GeocodedAddress geocodedAddress, Boolean zipProvided)
{
Timestamp startTime = TimeUtil.currentTimestamp();
DistrictShapefile districtShapeFile = (DistrictShapefile) this.getInstance("shapefile");
DistrictResult districtResult = districtShapeFile.getMultiMatchResult(geocodedAddress, zipProvided);
districtResult.setResultTime(new Timestamp(new Date().getTime()));
logger.info(String.format("Multi-match district assign in %d ms.", TimeUtil.getElapsedMs(startTime)));
return districtResult;
}

Expand Down

0 comments on commit 69d3ac8

Please sign in to comment.