Commit 6bbd8d5b authored by Costin Grigoras's avatar Costin Grigoras
Browse files

Implement QUEUE_* dictionary caches

parent 6e3b034d
......@@ -27,6 +27,7 @@ import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -2703,61 +2704,73 @@ public class TaskQueueUtils {
}
/**
* @param key
* @param value
* @return value for this key
* @author costing
* @since May 5, 2021
*/
public static int getOrInsertFromLookupTable(final String key, final String value) {
// FIXME: these values can also be cached
try (DBFunctions db = getQueueDB()) {
if (db == null)
return 0;
db.setQueryTimeout(60);
private static final class LookupTable extends GenericLastValuesCache<String, Integer> {
private static final long serialVersionUID = 1L;
final String table = "QUEUE_" + key.toUpperCase();
final String id = key + "id";
final String q = "select " + id + " from " + table + " where " + key + "=?";
private final String query;
private final String insert;
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "Going to get hostId, query: " + q);
/**
* @param key
*
*/
public LookupTable(final String key) {
query = "SELECT " + key.toLowerCase() + "id FROM QUEUE_" + key + " WHERE " + key.toLowerCase() + "=?";
insert = "INSERT INTO QUEUE_" + key.toUpperCase() + " (" + key.toLowerCase() + ") VALUES (?)";
}
db.setReadOnly(true);
db.query(q, false, value);
@Override
protected int getMaximumSize() {
return 100000;
}
// the host exists
if (db.moveNext()) {
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "The host exists: " + db.geti(1));
@Override
protected Integer resolve(String key) {
try (DBFunctions db = getQueueDB()) {
if (db == null)
return null;
return db.geti(1);
}
// host doesn't exist, we insert it
if (logger.isLoggable(Level.FINE))
logger.log(Level.FINE, "The host doesn't exist. Inserting...");
db.setReadOnly(true);
db.setQueryTimeout(60);
db.setLastGeneratedKey(true);
if (!db.query(query, false, key))
return null;
final String qi = "insert into " + table + " (" + key + ") values (?);";
db.setReadOnly(false);
final boolean ret = db.query(qi, false, value);
if (db.moveNext())
return Integer.valueOf(db.geti(1));
if (logger.isLoggable(Level.FINE))
logger.log(Level.FINE, qi + " with ?=" + value + ": " + ret);
db.setReadOnly(false);
db.setLastGeneratedKey(true);
if (ret) {
final int val = db.getLastGeneratedKey().intValue();
if (logger.isLoggable(Level.FINE))
logger.log(Level.FINE, "Returning: " + val);
if (!db.query(insert, false, key))
return null;
return val;
return db.getLastGeneratedKey();
}
// something went wrong ? :-(
return 0;
}
}
private static ConcurrentHashMap<String, LookupTable> lookupTable = new ConcurrentHashMap<>();
/**
* @param key
* @param value
* @return value for this key
*/
public static int getOrInsertFromLookupTable(final String key, final String value) {
LookupTable cache = lookupTable.computeIfAbsent(key.toUpperCase(), (k) -> new LookupTable(k));
Integer i = cache.get(value);
if (i != null)
return i.intValue();
return 0;
}
/**
* @param host
* @param port
......
Markdown is supported
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