Added concurrent hashmap for user's api key to speed up/avoid its retrieval

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@133219 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-10-14 19:23:47 +00:00
parent 6c4140aff7
commit b2f63cf36b
2 changed files with 37 additions and 24 deletions

View File

@ -16,6 +16,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import net.htmlparser.jericho.Renderer;
import net.htmlparser.jericho.Segment;
@ -73,6 +74,9 @@ public class DataCatalogueImpl implements DataCatalogue{
// ckan client
private CkanClient client;
// hashmap for ckan api keys
private ConcurrentHashMap<String, String> apiKeysMap;
/**
* The ckan catalogue url and database will be discovered in this scope
* @param scope
@ -98,6 +102,9 @@ public class DataCatalogueImpl implements DataCatalogue{
// build the client
client = new CkanClient(CKAN_CATALOGUE_URL);
// init map
apiKeysMap = new ConcurrentHashMap<String, String>();
}
/**
@ -147,33 +154,39 @@ public class DataCatalogueImpl implements DataCatalogue{
String apiToReturn = null;
// the connection
Connection connection = null;
// check in the hashmap first
if(apiKeysMap.containsKey(ckanUsername))
apiToReturn = apiKeysMap.get(ckanUsername);
else{
try{
// the connection
Connection connection = null;
connection = getConnection();
try{
String query = "SELECT \"apikey\" FROM \"user\" WHERE \"name\"=? and \"state\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, ckanUsername);
preparedStatement.setString(2, State.ACTIVE.toString().toLowerCase());
connection = getConnection();
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
apiToReturn = rs.getString("apikey");
logger.debug("Api key retrieved for user " + ckanUsername);
break;
String query = "SELECT \"apikey\" FROM \"user\" WHERE \"name\"=? and \"state\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, ckanUsername);
preparedStatement.setString(2, State.ACTIVE.toString().toLowerCase());
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
apiToReturn = rs.getString("apikey");
logger.debug("Api key retrieved for user " + ckanUsername);
break;
}
// save into the hashmap
apiKeysMap.put(ckanUsername, apiToReturn);
}catch(Exception e){
logger.error("Unable to retrieve key for user " + ckanUsername, e);
}finally{
closeConnection(connection);
}
}catch(Exception e){
logger.error("Unable to retrieve key for user " + ckanUsername, e);
}finally{
closeConnection(connection);
}
return apiToReturn;
}
@ -1446,7 +1459,7 @@ public class DataCatalogueImpl implements DataCatalogue{
logger.info("Dataset with id " + datasetId + " delete and purged!");
return true;
}
return false;
}

View File

@ -52,12 +52,12 @@ public class UtilMethods {
String regexGroupNameTransform = "[^A-Za-z0-9-]";
String modified = groupName.trim().replaceAll(regexGroupNameTransform, "-").replaceAll("-+", "-").toLowerCase();
if(modified.startsWith("-"))
modified = modified.substring(1);
if(modified.endsWith("-"))
modified = modified.substring(0, modified.length() -1);
return modified;
}