001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.store;
032:
033: import java.math.BigDecimal;
034: import java.sql.Date;
035:
036: /**
037: * Supports pooling of Integer, Long, Double, BigDecimal, String and Date
038: * Java Objects. Leads to reduction in memory use when an Object is used more
039: * then twice in the database.
040: *
041: * getXXX methods are used for retrival of values. If a value is not in
042: * the pool, it is added to the pool and returned. When the pool gets
043: * full, half the contents that have been accessed less recently are purged.
044: *
045: * @author fredt@users
046: * @version 1.8.0
047: * @since 1.7.2
048: */
049: public class ValuePool {
050:
051: //
052: static ValuePoolHashMap intPool;
053: static ValuePoolHashMap longPool;
054: static ValuePoolHashMap doublePool;
055: static ValuePoolHashMap bigdecimalPool;
056: static ValuePoolHashMap stringPool;
057: static ValuePoolHashMap datePool;
058: static final int DEFAULT_VALUE_POOL_SIZE = 10000;
059: static final int[] defaultPoolLookupSize = new int[] {
060: DEFAULT_VALUE_POOL_SIZE, DEFAULT_VALUE_POOL_SIZE,
061: DEFAULT_VALUE_POOL_SIZE, DEFAULT_VALUE_POOL_SIZE,
062: DEFAULT_VALUE_POOL_SIZE, DEFAULT_VALUE_POOL_SIZE };
063: static final int POOLS_COUNT = defaultPoolLookupSize.length;
064: static final int defaultSizeFactor = 2;
065: static final int defaultMaxStringLength = 16;
066:
067: //
068: static ValuePoolHashMap[] poolList;
069:
070: //
071: static int maxStringLength;
072:
073: //
074: static {
075: initPool();
076: }
077:
078: private static void initPool() {
079:
080: int[] sizeArray = defaultPoolLookupSize;
081: int sizeFactor = defaultSizeFactor;
082:
083: synchronized (ValuePool.class) {
084: maxStringLength = defaultMaxStringLength;
085: poolList = new ValuePoolHashMap[POOLS_COUNT];
086:
087: for (int i = 0; i < POOLS_COUNT; i++) {
088: int size = sizeArray[i];
089:
090: poolList[i] = new ValuePoolHashMap(size, size
091: * sizeFactor, BaseHashMap.PURGE_HALF);
092: }
093:
094: intPool = poolList[0];
095: longPool = poolList[1];
096: doublePool = poolList[2];
097: bigdecimalPool = poolList[3];
098: stringPool = poolList[4];
099: datePool = poolList[5];
100: }
101: }
102:
103: public static void resetPool(int[] sizeArray, int sizeFactor) {
104:
105: synchronized (ValuePool.class) {
106: for (int i = 0; i < POOLS_COUNT; i++) {
107: poolList[i].resetCapacity(sizeArray[i] * sizeFactor,
108: BaseHashMap.PURGE_HALF);
109: }
110: }
111: }
112:
113: public static void resetPool() {
114:
115: synchronized (ValuePool.class) {
116: resetPool(defaultPoolLookupSize, defaultSizeFactor);
117: }
118: }
119:
120: public static void clearPool() {
121:
122: synchronized (ValuePool.class) {
123: for (int i = 0; i < POOLS_COUNT; i++) {
124: poolList[i].clear();
125: }
126: }
127: }
128:
129: public static Integer getInt(int val) {
130:
131: synchronized (intPool) {
132: return intPool.getOrAddInteger(val);
133: }
134: }
135:
136: public static Long getLong(long val) {
137:
138: synchronized (longPool) {
139: return longPool.getOrAddLong(val);
140: }
141: }
142:
143: public static Double getDouble(long val) {
144:
145: synchronized (doublePool) {
146: return doublePool.getOrAddDouble(val);
147: }
148: }
149:
150: public static String getString(String val) {
151:
152: if (val == null || val.length() > maxStringLength) {
153: return val;
154: }
155:
156: synchronized (stringPool) {
157: return stringPool.getOrAddString(val);
158: }
159: }
160:
161: public static Date getDate(long val) {
162:
163: synchronized (datePool) {
164: return datePool.getOrAddDate(val);
165: }
166: }
167:
168: public static BigDecimal getBigDecimal(BigDecimal val) {
169:
170: if (val == null) {
171: return val;
172: }
173:
174: synchronized (bigdecimalPool) {
175: return (BigDecimal) bigdecimalPool.getOrAddObject(val);
176: }
177: }
178:
179: public static Boolean getBoolean(boolean b) {
180: return b ? Boolean.TRUE : Boolean.FALSE;
181: }
182: }
|