Lock against multiple threads trying to update
@@ -12,6 +12,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.locks.ReentrantLock;
/**
* Nominatim update logic
@@ -34,11 +35,18 @@ public class NominatimUpdater {
private Updater updater;
+ /**
+ * when updating lockout other threads
+ */
+ private ReentrantLock updateLock = new ReentrantLock();
+
public void setUpdater(Updater updater) {
this.updater = updater;
}
public void update() {
+ if (updateLock.tryLock()) {
+ try {
int updatedPlaces = 0;
int deletedPlaces = 0;
for (int rank = MIN_RANK; rank <= MAX_RANK; rank++) {
@@ -121,22 +129,28 @@ public class NominatimUpdater {
}
}
}
- LOGGER.info(String.format("%d interpolations created or updated, %d deleted, %d documents added or updated",
- updatedInterpolations, deletedInterpolations, interpolationDocuments));
+ LOGGER.info(String.format("%d interpolations created or updated, %d deleted, %d documents added or updated", updatedInterpolations,
+ deletedInterpolations, interpolationDocuments));
updater.finish();
template.update("update import_status set indexed=true;"); // indicate that we are finished
LOGGER.info("Finished updating");
+ } finally {
+ updateLock.unlock();
+ }
+ } else {
+ LOGGER.info("Update already in progress");
+ }
}
private List<Map<String, Object>> getIndexSectors(Integer rank) {
- return template.queryForList("select geometry_sector,count(*) from placex where rank_search = ? " +
- "and indexed_status > 0 group by geometry_sector order by geometry_sector;", rank);
+ return template.queryForList("select geometry_sector,count(*) from placex where rank_search = ? "
+ + "and indexed_status > 0 group by geometry_sector order by geometry_sector;", rank);
}
private List<UpdateRow> getIndexSectorPlaces(Integer rank, Integer geometrySector) {
- return template.query("select place_id, indexed_status from placex where rank_search = ?" +
- " and geometry_sector = ? and indexed_status > 0;", new Object[]{rank, geometrySector}, new RowMapper<UpdateRow>() {
+ return template.query("select place_id, indexed_status from placex where rank_search = ?" + " and geometry_sector = ? and indexed_status > 0;",
+ new Object[] { rank, geometrySector }, new RowMapper<UpdateRow>() {
@Override
public UpdateRow mapRow(ResultSet rs, int rowNum) throws SQLException {
UpdateRow updateRow = new UpdateRow();
