Commit 9d311f4c authored by Costin Grigoras's avatar Costin Grigoras
Browse files

A few queries that could be easily converted to prepared statements

parent ff0bf574
......@@ -116,7 +116,7 @@ public final class CatalogueUtils {
*/
public static final long CACHE_TIMEOUT = 1000 * 60 * 5;
private static final void updateGuidIndexCache() {
private static void updateGuidIndexCache() {
guidIndexReadLock.lock();
try {
......@@ -465,9 +465,9 @@ public final class CatalogueUtils {
do {
read = 0;
final String q = "select guid,size from G" + idx.tableName + "L LIMIT " + LIMIT + " OFFSET " + offset + ";";
final String q = "select guid,size from G" + idx.tableName + "L LIMIT ? OFFSET ?;";
while (!gdb.query(q))
while (!gdb.query(q, false, Long.valueOf(LIMIT), Long.valueOf(offset)))
System.err.println("Retrying query " + q);
while (gdb.moveNext()) {
......
......@@ -270,8 +270,8 @@ public class GUID implements Comparable<GUID>, CatalogEntity {
}
// only the SE list can change, and the size for a collection, and md5 when it was missing
if (!db.query("UPDATE G" + tableName + "L SET seStringlist=" + setToString(seStringList) + ", size=" + size + ", md5='" + Format.escSQL(md5) + "', owner='" + Format.escSQL(owner)
+ "', gowner='" + Format.escSQL(gowner) + "',perm='" + Format.escSQL(perm) + "' WHERE guidId=" + guidId))
if (!db.query("UPDATE G" + tableName + "L SET seStringlist=?, size=?, md5=?, owner=?, gowner=?,perm=? WHERE guidId=?", false, setToString(seStringList),
Long.valueOf(size), md5, owner, gowner, perm, Integer.valueOf(guidId)))
// wrong table name or what?
return false;
......@@ -537,21 +537,12 @@ public class GUID implements Comparable<GUID>, CatalogEntity {
guidIDs.drainTo(toExecute);
final StringBuilder sb = new StringBuilder(guidIDs.size() * 10);
for (final Integer id : toExecute) {
if (sb.length() > 0)
sb.append(",");
sb.append(id.toString());
}
if (monitor != null)
monitor.incrementCounter("GUID_flush");
if (sb.length() > 0)
if (toExecute.size() > 0)
try (DBFunctions db = host.getDB()) {
db.query("DELETE FROM G" + tableName + "L" + tableSuffix + " WHERE guidId IN (" + sb.toString() + ")");
db.query("DELETE FROM G" + tableName + "L" + tableSuffix + " WHERE guidId IN (" + Format.toCommaList(toExecute) + ")");
}
return true;
......@@ -787,7 +778,7 @@ public class GUID implements Comparable<GUID>, CatalogEntity {
final SE se = SEUtils.getSE(pfn.seNumber);
if (se != null && !(se.getName().equalsIgnoreCase("no_se"))) {
if (se != null && !("no_se".equalsIgnoreCase(se.getName()))) {
final GUID g = pfn.getGuid();
if (g != null && g.guid != null) {
......@@ -874,7 +865,7 @@ public class GUID implements Comparable<GUID>, CatalogEntity {
/**
* This method is _not_ authoritative, if you want to do the actual lookup to see which LFNs point to this GUID then use {@link LFNUtils#getLFN(GUID)}.
* Should only be called when the previous code has filled the cache with known LFN objects.
*
*
* @return the <b>cached</b> LFNs associated to this GUID, from either the internal cache or the G*L_REF tables.
*/
public Set<LFN> getLFNs() {
......@@ -1020,7 +1011,7 @@ public class GUID implements Comparable<GUID>, CatalogEntity {
/**
* Change the access permissions on this LFN
*
*
* @param newPermissions
* @return the previous permissions, if anything changed and the change was successfully propagated to the database, or <code>null</code> if nothing was touched
*/
......@@ -1065,7 +1056,7 @@ public class GUID implements Comparable<GUID>, CatalogEntity {
/**
* Get the two digit hash (first level of folders in a storage)
*
*
* @param guid
* @return chash
*/
......@@ -1152,7 +1143,7 @@ public class GUID implements Comparable<GUID>, CatalogEntity {
/**
* Get the replica from a particular SE
*
*
* @param seNumber
* @return the PFN to the replica on this storage element, if it exists
*/
......
......@@ -133,7 +133,7 @@ public final class TransferUtils {
final String orderClause = (orderAsc ? " ORDER BY transferId ASC" : " ORDER BY transferId DESC");
final String limitClause = (count != null ? " LIMIT " + count.toString() : "");
String query = "SELECT * FROM TRANSFERS_DIRECT WHERE " + "(? IS NULL OR user=?) AND (? IS NULL OR destination=?)" + " AND (? IS NULL OR status=?)";
String query = "SELECT * FROM TRANSFERS_DIRECT WHERE (? IS NULL OR user=?) AND (? IS NULL OR destination=?) AND (? IS NULL OR status=?)";
// db.query("SELECT * FROM TRANSFERS_DIRECT ORDER BY transferId", false);
query += orderClause + limitClause;
db.query(query, false, user, user, targetSE, targetSE, status, status);
......@@ -601,7 +601,7 @@ public final class TransferUtils {
* @param exitCode
* @param failureReason
*/
static final void logAttempt(final Protocol p, final PFN source, final PFN target, final int exitCode, final String failureReason) {
static void logAttempt(final Protocol p, final PFN source, final PFN target, final int exitCode, final String failureReason) {
try (DBFunctions db = getDB()) {
if (db != null)
if (ConfigUtils.getConfig().getb("alien.io.TransferUtils.logReason", false))
......
package alien.quotas;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import lazyj.DBFunctions;
import lazyj.Format;
......@@ -19,7 +18,7 @@ public final class FileQuota implements Serializable, Comparable<FileQuota> {
*/
/**
*
*
*/
private static final long serialVersionUID = 7587668615003121402L;
......@@ -61,7 +60,7 @@ public final class FileQuota implements Serializable, Comparable<FileQuota> {
/**
* Fields allowed to modify via fquota set command
*/
private final static List<String> allowed_to_update = Arrays.asList("maxNbFiles", "maxTotalSize");
private final static Set<String> allowed_to_update = Set.of("maxNbFiles", "maxTotalSize");
/**
* @param db
......
......@@ -4,8 +4,7 @@
package alien.quotas;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.logging.Logger;
import alien.config.ConfigUtils;
......@@ -111,7 +110,7 @@ public class Quota implements Serializable, Comparable<Quota> {
/**
* Fields allowed to modify via jquota set command
*/
private final static ArrayList<String> allowed_to_update = new ArrayList<>(Arrays.asList("maxUnfinishedJobs", "maxTotalCpuCost", "maxTotalRunningTime"));
private final static Set<String> allowed_to_update = Set.of("maxUnfinishedJobs", "maxTotalCpuCost", "maxTotalRunningTime");
/**
* @param db
......
......@@ -178,13 +178,13 @@ public final class QuotaUtilities {
public static boolean saveJobQuota(final String username, final String fld, final String val) {
if (!Quota.canUpdateField(fld))
return false;
try (DBFunctions db = ConfigUtils.getDB("processes")) {
final String query = "UPDATE PRIORITY p LEFT JOIN QUEUE_USER qu " + "ON qu.user='" + Format.escSQL(username) + "' SET p." + Format.escSQL(fld) + "=" + Format.escSQL(val)
+ " WHERE qu.userid=p.userid";
final String query = "UPDATE PRIORITY p LEFT JOIN QUEUE_USER qu ON qu.user=? SET p." + Format.escSQL(fld) + "=? WHERE qu.userid=p.userid";
db.setQueryTimeout(120);
if (db.query(query)) {
if (db.query(query, false, username, val)) {
jobQuotasLastUpdated = 0;
updateJobQuotasCache();
}
......@@ -226,11 +226,11 @@ public final class QuotaUtilities {
return false;
try (DBFunctions db = ConfigUtils.getDB("alice_users")) {
final String query = "UPDATE FQUOTAS SET " + Format.escSQL(fld) + "='" + Format.escSQL(val) + "'" + " WHERE user='" + Format.escSQL(username) + "'";
final String query = "UPDATE FQUOTAS SET " + Format.escSQL(fld) + "=? WHERE user=?";
db.setQueryTimeout(120);
if (db.query(query)) {
if (db.query(query, false, val, username)) {
fileQuotasLastUpdated = 0;
updateFileQuotasCache();
}
......@@ -246,7 +246,7 @@ public final class QuotaUtilities {
*
* @return file quota for all accounts, sorted by username
*/
public static final List<Quota> getJobQuotas() {
public static List<Quota> getJobQuotas() {
updateJobQuotasCache();
if (jobQuotas == null)
......@@ -264,7 +264,7 @@ public final class QuotaUtilities {
*
* @return file quota for all accounts, sorted by username
*/
public static final List<FileQuota> getFileQuotas() {
public static List<FileQuota> getFileQuotas() {
updateFileQuotasCache();
if (fileQuotas == null)
......@@ -279,13 +279,13 @@ public final class QuotaUtilities {
/**
* Check job quota authorization for a user, for n jobs
*
*
* @param account
* @param numberOfJobsToSubmit
* @return allowed or not
*/
public static final Map.Entry<Integer, String> checkJobQuota(String account, int numberOfJobsToSubmit) {
Quota q = getJobQuota(account);
public static Map.Entry<Integer, String> checkJobQuota(final String account, final int numberOfJobsToSubmit) {
final Quota q = getJobQuota(account);
if (q == null)
return new AbstractMap.SimpleEntry<>(Integer.valueOf(1), "Error: couldn't get quotas for user: " + account);
......
......@@ -1120,8 +1120,8 @@ public final class SEUtils {
}
}
else {
gdb.query("select distinct binary2string(guid),size,md5 from G" + idx.tableName + "L INNER JOIN G" + idx.tableName + "L_PFN using(guidId) where seNumber=" + se.seNumber
+ ";");
gdb.query("select distinct binary2string(guid),size,md5 from G" + idx.tableName + "L INNER JOIN G" + idx.tableName + "L_PFN using(guidId) where seNumber=?", true,
Integer.valueOf(se.seNumber));
while (gdb.moveNext()) {
final String guid = gdb.gets(1);
......
......@@ -20,7 +20,7 @@ public class FindOrphanFoldersOutput {
/**
* @param args
*/
public static void main(String[] args) {
public static void main(final String[] args) {
final int nargs = args.length;
if (nargs < 1) {
......@@ -29,27 +29,27 @@ public class FindOrphanFoldersOutput {
}
for (int i = 0; i < nargs; i++) {
long jobid = Long.parseLong(args[i]);
final long jobid = Long.parseLong(args[i]);
JDL jobjdl = null;
try {
jobjdl = new JDL(jobid);
}
catch (IOException e) {
catch (final IOException e) {
System.err.println("Can't get job JDL: " + e);
return;
}
String outputdir = jobjdl.getOutputDir();
final String outputdir = jobjdl.getOutputDir();
IndexTableEntry ite = CatalogueUtils.getClosestMatch(outputdir);
final IndexTableEntry ite = CatalogueUtils.getClosestMatch(outputdir);
if (ite == null) {
System.err.println("Can't find indextable for output: " + outputdir);
return;
}
String dblfn = outputdir.replaceAll(ite.lfn, "");
final String dblfn = outputdir.replaceAll(ite.lfn, "");
System.out.println("db pattern: " + dblfn);
......@@ -58,11 +58,11 @@ public class FindOrphanFoldersOutput {
System.out.println("Going to select distinct dir");
if (!db.query("select distinct dir as dir from L" + ite.tableName + "L where type='d' and lfn like '" + dblfn + "/%/' order by 1 asc")) {
if (!db.query("select distinct dir as dir from L" + ite.tableName + "L where type='d' and lfn like ? order by 1 asc", false, dblfn + "/%/")) {
System.err.println("Can't get distinct dirs");
System.exit(-1);
}
int count = db.count();
final int count = db.count();
if (count <= 1) {
System.out.println("Only 1 dir found");
......@@ -72,11 +72,11 @@ public class FindOrphanFoldersOutput {
System.out.println("Found several folders: " + count);
db.moveNext();
int mindir = db.geti("dir");
final int mindir = db.geti("dir");
System.out.println("select lfn from L" + ite.tableName + "L where type='d' and dir=" + mindir + " order by 1 asc");
if (!db.query("select lfn from L" + ite.tableName + "L where type='d' and dir=" + mindir + " order by 1 asc")) {
if (!db.query("select lfn from L" + ite.tableName + "L where type='d' and dir=? order by 1 asc", false, Integer.valueOf(mindir))) {
System.err.println("Can't get distinct dirs");
System.exit(-1);
}
......@@ -87,7 +87,7 @@ public class FindOrphanFoldersOutput {
outfile.println(ite.lfn + db.gets("lfn"));
}
}
catch (FileNotFoundException e) {
catch (final FileNotFoundException e) {
System.err.println("Could not write to file for: " + jobid + " :" + e);
return;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment