Author: tv
Date: Fri Apr 6 12:47:41 2018
New Revision: 1828518
URL: http://svn.apache.org/viewvc?rev=1828518&view=rev
Log:
Use ConcurrentMap
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java?rev=1828518&r1=1828517&r2=1828518&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
Fri Apr 6 12:47:41 2018
@@ -26,9 +26,11 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
-import java.util.Hashtable;
import java.util.List;
+import java.util.ListIterator;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;
@@ -103,10 +105,10 @@ public class IDBroker implements Runnabl
public static final String QUANTITY = ID_TABLE + "." + COL_QUANTITY;
/** The backup quantity which is used if an error occurs. */
- private static final double PREFETCH_BACKUP_QUANTITY = 10d;
+ private static final BigDecimal PREFETCH_BACKUP_QUANTITY = BigDecimal.TEN;
/** The default maximum for the quantity determined by cleverquantity. */
- private static final double CLEVERQUANTITY_MAX_DEFAULT = 10000d;
+ private static final BigDecimal CLEVERQUANTITY_MAX_DEFAULT =
BigDecimal.valueOf(10000L);
/** the name of the database in which this IdBroker is running. */
private final String databaseName;
@@ -123,8 +125,8 @@ public class IDBroker implements Runnabl
* Key: String table name.
* Value: List of Integer IDs.
*/
- private final Map<String, List<BigDecimal>> ids
- = new Hashtable<String, List<BigDecimal>>(DEFAULT_SIZE);
+ private final ConcurrentMap<String, List<BigDecimal>> ids
+ = new ConcurrentHashMap<String, List<BigDecimal>>(DEFAULT_SIZE);
/**
* The quantity of ids to grab for each table.
@@ -132,8 +134,8 @@ public class IDBroker implements Runnabl
* Key: String table name.
* Value: Integer quantity.
*/
- private final Map<String, BigDecimal> quantityStore
- = new Hashtable<String, BigDecimal>(DEFAULT_SIZE);
+ private final ConcurrentMap<String, BigDecimal> quantityStore
+ = new ConcurrentHashMap<String, BigDecimal>(DEFAULT_SIZE);
/**
* The last time this IDBroker queried the database for ids.
@@ -141,8 +143,8 @@ public class IDBroker implements Runnabl
* Key: String table name.
* Value: Date of last id request.
*/
- private final Map<String, java.util.Date> lastQueryTime
- = new Hashtable<String, java.util.Date>(DEFAULT_SIZE);
+ private final ConcurrentMap<String, java.util.Date> lastQueryTime
+ = new ConcurrentHashMap<String, java.util.Date>(DEFAULT_SIZE);
/**
* Amount of time for the thread to sleep
@@ -280,6 +282,7 @@ public class IDBroker implements Runnabl
* @return An int with the value for the id.
* @exception Exception Database error.
*/
+ @Override
public int getIdAsInt(Connection connection, Object tableName)
throws TorqueException
{
@@ -299,6 +302,7 @@ public class IDBroker implements Runnabl
* @return A long with the value for the id.
* @exception Exception Database error.
*/
+ @Override
public long getIdAsLong(Connection connection, Object tableName)
throws TorqueException
{
@@ -317,6 +321,7 @@ public class IDBroker implements Runnabl
* @return A BigDecimal id.
* @exception Exception Database error.
*/
+ @Override
public BigDecimal getIdAsBigDecimal(Connection connection,
Object tableName)
throws TorqueException
@@ -337,6 +342,7 @@ public class IDBroker implements Runnabl
* @return A String id
* @exception Exception Database error.
*/
+ @Override
public String getIdAsString(Connection connection, Object tableName)
throws TorqueException
{
@@ -348,6 +354,7 @@ public class IDBroker implements Runnabl
* A flag to determine the timing of the id generation *
* @return a <code>boolean</code> value
*/
+ @Override
public boolean isPriorToInsert()
{
return true;
@@ -358,6 +365,7 @@ public class IDBroker implements Runnabl
*
* @return a <code>boolean</code> value
*/
+ @Override
public boolean isPostInsert()
{
return false;
@@ -369,6 +377,7 @@ public class IDBroker implements Runnabl
*
* @return a <code>boolean</code> value
*/
+ @Override
public boolean isConnectionRequired()
{
return false;
@@ -392,7 +401,7 @@ public class IDBroker implements Runnabl
* @return A BigDecimal.
* @exception Exception Database error.
*/
- public synchronized BigDecimal[] getNextIds(String tableName,
+ public BigDecimal[] getNextIds(String tableName,
int numOfIdsToReturn)
throws Exception
{
@@ -435,23 +444,24 @@ public class IDBroker implements Runnabl
if (availableIds == null || availableIds.size() < numOfIdsToReturn)
{
- if (availableIds == null)
- {
- log.debug("Forced id retrieval - no available list for table "
- + tableName);
- }
- else
+ if (log.isDebugEnabled())
{
- log.debug("Forced id retrieval - " + availableIds.size()
- + " ids still available for table " + tableName);
+ if (availableIds == null)
+ {
+ log.debug("Forced id retrieval - no available list for
table "
+ + tableName);
+ }
+ else
+ {
+ log.debug("Forced id retrieval - " + availableIds.size()
+ + " ids still available for table " + tableName);
+ }
}
storeIDs(tableName, true, connection);
availableIds = ids.get(tableName);
}
- int size = availableIds.size() < numOfIdsToReturn
- ? availableIds.size() : numOfIdsToReturn;
-
+ int size = Math.min(availableIds.size(), numOfIdsToReturn);
BigDecimal[] results = new BigDecimal[size];
// We assume that availableIds will always come from the ids
@@ -459,10 +469,11 @@ public class IDBroker implements Runnabl
// a specific table.
// synchronized (availableIds)
// {
+ ListIterator<BigDecimal> it = availableIds.listIterator(size);
for (int i = size - 1; i >= 0; i--)
{
- results[i] = availableIds.get(i);
- availableIds.remove(i);
+ results[i] = it.previous();
+ it.remove();
}
// }
@@ -513,6 +524,7 @@ public class IDBroker implements Runnabl
* for ids, that there are already some loaded and that the
* database is not accessed.
*/
+ @Override
public void run()
{
log.debug("IDBroker thread was started.");
@@ -532,14 +544,15 @@ public class IDBroker implements Runnabl
}
// logger.info("IDBroker thread checking for more keys.");
- for (String tableName : ids.keySet())
+ for (Map.Entry<String, List<BigDecimal>> id_entry : ids.entrySet())
{
+ String tableName = id_entry.getKey();
if (log.isDebugEnabled())
{
log.debug("IDBroker thread checking for more keys "
+ "on table: " + tableName);
}
- List<BigDecimal> availableIds = ids.get(tableName);
+ List<BigDecimal> availableIds = id_entry.getValue();
int quantity = getQuantity(tableName, null).intValue();
if (quantity > availableIds.size())
{
@@ -607,8 +620,8 @@ public class IDBroker implements Runnabl
}
// Get the last id request for this table.
- java.util.Date lastTime = lastQueryTime.get(tableName);
java.util.Date now = new java.util.Date();
+ java.util.Date lastTime = lastQueryTime.putIfAbsent(tableName, now);
if (lastTime != null)
{
@@ -643,27 +656,27 @@ public class IDBroker implements Runnabl
+ " from double the old quantity (time lapse 0)");
}
- Double maxQuantity = configuration.getDouble(
+ BigDecimal bdNewQuantity = BigDecimal.valueOf(newQuantity);
+ BigDecimal maxQuantity = configuration.getBigDecimal(
DB_IDBROKER_CLEVERQUANTITY_MAX,
- CLEVERQUANTITY_MAX_DEFAULT);
- if (maxQuantity != null && newQuantity > maxQuantity)
+ CLEVERQUANTITY_MAX_DEFAULT);
+ if (maxQuantity != null &&
bdNewQuantity.compareTo(maxQuantity) > 0)
{
- if (quantity.doubleValue() > maxQuantity)
+ if (quantity.compareTo(maxQuantity) > 0)
{
// do not decrease quantity value;
- newQuantity = quantity.doubleValue();
+ bdNewQuantity = quantity;
}
else
{
- newQuantity = maxQuantity;
+ bdNewQuantity = maxQuantity;
}
}
- quantityStore.put(tableName, new BigDecimal(newQuantity));
- log.debug("checkTiming(): new quantity " + newQuantity
+ quantityStore.put(tableName, bdNewQuantity);
+ log.debug("checkTiming(): new quantity " + bdNewQuantity
+ " stored in quantity store (not in db)");
}
}
- lastQueryTime.put(tableName, now);
}
/**
@@ -745,11 +758,11 @@ public class IDBroker implements Runnabl
throw e;
}
- List<BigDecimal> availableIds = ids.get(tableName);
+ List<BigDecimal> newAvailableIds = new ArrayList<BigDecimal>();
+ List<BigDecimal> availableIds = ids.putIfAbsent(tableName,
newAvailableIds);
if (availableIds == null)
{
- availableIds = new ArrayList<BigDecimal>();
- ids.put(tableName, availableIds);
+ availableIds = newAvailableIds;
}
// Create the ids and store them in the list of available ids.
@@ -768,7 +781,7 @@ public class IDBroker implements Runnabl
* read from the db. (ie the value in ID_TABLE.QUANTITY).
*
* Though this method returns a BigDecimal for the quantity, it is
- * unlikey the system could withstand whatever conditions would lead
+ * unlikely the system could withstand whatever conditions would lead
* to really needing a large quantity, it is retrieved as a BigDecimal
* only because it is going to be added to another BigDecimal.
*
@@ -783,7 +796,7 @@ public class IDBroker implements Runnabl
// If prefetch is turned off we simply return 1
if (!configuration.getBoolean(DB_IDBROKER_PREFETCH, true))
{
- quantity = new BigDecimal((double) 1);
+ quantity = BigDecimal.ONE;
}
// Initialize quantity, if necessary.
else if (quantityStore.containsKey(tableName))
@@ -829,7 +842,7 @@ public class IDBroker implements Runnabl
}
catch (Exception e)
{
- quantity = new BigDecimal(PREFETCH_BACKUP_QUANTITY);
+ quantity = PREFETCH_BACKUP_QUANTITY;
}
finally
{
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]