This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit 7998ef3b135456261f734f0b934c335522da2ca1 Author: Thomas Vandahl <[email protected]> AuthorDate: Thu Feb 12 17:12:46 2026 +0100 More records, less code --- .../jcs4/auxiliary/disk/AbstractDiskCache.java | 2 +- .../disk/block/BlockDiskElementDescriptor.java | 114 +++------------------ .../auxiliary/disk/block/BlockDiskKeyStore.java | 4 +- .../jcs4/auxiliary/disk/indexed/IndexedDisk.java | 33 +++--- .../auxiliary/disk/indexed/IndexedDiskCache.java | 50 +++++---- .../disk/indexed/IndexedDiskElementDescriptor.java | 58 ++--------- .../jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java | 25 +---- .../jcs4/auxiliary/disk/jdbc/TableState.java | 52 ++++------ .../auxiliary/disk/jdbc/mysql/MySQLDiskCache.java | 9 +- .../disk/jdbc/mysql/MySQLTableOptimizer.java | 7 +- .../lateral/LateralElementDescriptor.java | 97 ++++-------------- .../lateral/socket/tcp/LateralTCPCache.java | 8 +- .../lateral/socket/tcp/LateralTCPCacheNoWait.java | 4 +- .../lateral/socket/tcp/LateralTCPListener.java | 20 ++-- .../lateral/socket/tcp/LateralTCPService.java | 12 +-- .../remote/CommonRemoteCacheAttributes.java | 4 +- .../jcs4/auxiliary/remote/RemoteLocation.java | 78 ++------------ .../commons/jcs4/auxiliary/remote/RemoteUtils.java | 2 +- .../org/apache/commons/jcs4/engine/CacheInfo.java | 13 +-- .../jcs4/utils/discovery/UDPDiscoveryReceiver.java | 2 +- .../jcs4/utils/discovery/UDPDiscoverySender.java | 6 +- .../disk/indexed/IndexDiskCacheCountUnitTest.java | 13 --- .../disk/jdbc/mysql/MySQLDiskCacheUnitTest.java | 3 +- .../jcs4/auxiliary/remote/RemoteUtilsUnitTest.java | 8 +- .../BasicRemoteCacheClientServerUnitTest.java | 2 +- 25 files changed, 167 insertions(+), 459 deletions(-) diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java index 6b8c950a..8165f561 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java @@ -264,7 +264,7 @@ public abstract class AbstractDiskCache<K, V> // create queue final CacheEventQueueFactory<K, V> fact = new CacheEventQueueFactory<>(); this.cacheEventQueue = fact.createCacheEventQueue( - new MyCacheListener(), CacheInfo.listenerId, cacheName, + new MyCacheListener(), CacheInfo.INSTANCE.listenerId(), cacheName, diskCacheAttributes.getEventQueuePoolName(), diskCacheAttributes.getEventQueueType() ); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskElementDescriptor.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskElementDescriptor.java index 9fec545d..9a1ddb50 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskElementDescriptor.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskElementDescriptor.java @@ -19,10 +19,6 @@ package org.apache.commons.jcs4.auxiliary.disk.block; * under the License. */ -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; import java.io.Serializable; import java.util.Arrays; @@ -31,87 +27,17 @@ import java.util.Arrays; * block addresses in memory. We don't need the length here, since all the blocks are the same size * recycle bin. */ -public class BlockDiskElementDescriptor<K> - implements Serializable, Externalizable +public record BlockDiskElementDescriptor<K>( + /** The key */ + K key, + + /** The array of block numbers */ + int[] blocks +) implements Serializable { /** Don't change */ private static final long serialVersionUID = -1400659301208101411L; - /** The key */ - private K key; - - /** The array of block numbers */ - private int[] blocks; - - /** - * Default constructor - */ - public BlockDiskElementDescriptor() - { - } - - /** - * Constructs a new instance. - * - * @param key the key - * @param blocks the data - * @since 3.1 - */ - public BlockDiskElementDescriptor(final K key, final int[] blocks) - { - this.key = key; - this.blocks = blocks; - } - - /** - * This holds the block numbers. An item my be dispersed between multiple blocks. - * - * @return the blocks. - */ - public int[] getBlocks() - { - return blocks; - } - - /** - * @return the key. - */ - public K getKey() - { - return key; - } - - /** - * Saves on reflection. - * <p> - * (non-Javadoc) - * @see java.io.Externalizable#readExternal(java.io.ObjectInput) - */ - @Override - @SuppressWarnings("unchecked") // Need cast to K - public void readExternal( final ObjectInput input ) - throws IOException, ClassNotFoundException - { - this.key = (K) input.readObject(); - this.blocks = (int[]) input.readObject(); - } - - /** - * @param blocks The blocks to set. - */ - public void setBlocks( final int[] blocks ) - { - this.blocks = blocks; - } - - /** - * @param key The key to set. - */ - public void setKey( final K key ) - { - this.key = key; - } - /** * For debugging. * @@ -121,28 +47,14 @@ public class BlockDiskElementDescriptor<K> public String toString() { final StringBuilder buf = new StringBuilder(); - buf.append( "\nBlockDiskElementDescriptor" ); - buf.append( "\n key [" + this.getKey() + "]" ); - buf.append( "\n blocks [" ); - if ( this.getBlocks() != null ) + buf.append("\nBlockDiskElementDescriptor" ); + buf.append("\n key [").append(this.key()).append("]"); + buf.append("\n blocks [" ); + if ( this.blocks() != null ) { - Arrays.stream(this.getBlocks()).forEach(buf::append); + Arrays.stream(this.blocks()).forEach(buf::append); } - buf.append( "]" ); + buf.append("]"); return buf.toString(); } - - /** - * Saves on reflection. - * <p> - * (non-Javadoc) - * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput) - */ - @Override - public void writeExternal( final ObjectOutput output ) - throws IOException - { - output.writeObject( this.key ); - output.writeObject( this.blocks ); - } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskKeyStore.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskKeyStore.java index ee397c03..f3a54f74 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskKeyStore.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskKeyStore.java @@ -415,7 +415,7 @@ public class BlockDiskKeyStore<K> serializer.deSerializeFrom(bc, null); if (descriptor != null) { - keys.put(descriptor.getKey(), descriptor.getBlocks()); + keys.put(descriptor.key(), descriptor.blocks()); } } catch (final EOFException e) @@ -443,7 +443,7 @@ public class BlockDiskKeyStore<K> BlockDiskElementDescriptor<K> descriptor = (BlockDiskElementDescriptor<K>) ois.readObject(); if (descriptor != null) { - keys.put(descriptor.getKey(), descriptor.getBlocks()); + keys.put(descriptor.key(), descriptor.blocks()); } } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDisk.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDisk.java index 47426ffd..955d5bc5 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDisk.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDisk.java @@ -114,24 +114,24 @@ public class IndexedDisk implements AutoCloseable * * @param ded * @param newPosition + * @return new IndexedDiskElementDescriptor with updated position * @throws IOException */ - protected void move(final IndexedDiskElementDescriptor ded, final long newPosition) + protected IndexedDiskElementDescriptor move(final IndexedDiskElementDescriptor ded, final long newPosition) throws IOException { final ByteBuffer datalength = ByteBuffer.allocate(HEADER_SIZE_BYTES); - fc.read(datalength, ded.pos); + fc.read(datalength, ded.pos()); datalength.flip(); final int length = datalength.getInt(); - if (length != ded.len) + if (length != ded.len()) { throw new IOException("Mismatched memory and disk length (" + length + ") for " + ded); } // TODO: more checks? - - long readPos = ded.pos; + long readPos = ded.pos(); long writePos = newPosition; // header len + data len @@ -153,7 +153,7 @@ public class IndexedDisk implements AutoCloseable remaining -= chunkSize; } - ded.pos = newPosition; + return new IndexedDiskElementDescriptor(newPosition, ded.len()); } /** @@ -173,7 +173,7 @@ public class IndexedDisk implements AutoCloseable String message = null; boolean corrupted = false; final long fileLength = fc.size(); - if (ded.pos > fileLength) + if (ded.pos() > fileLength) { corrupted = true; message = "Record " + ded + " starts past EOF."; @@ -181,15 +181,15 @@ public class IndexedDisk implements AutoCloseable else { final ByteBuffer datalength = ByteBuffer.allocate(HEADER_SIZE_BYTES); - fc.read(datalength, ded.pos); + fc.read(datalength, ded.pos()); datalength.flip(); final int datalen = datalength.getInt(); - if (ded.len != datalen) + if (ded.len() != datalen) { corrupted = true; message = "Record " + ded + " does not match data length on disk (" + datalen + ")"; } - else if (ded.pos + ded.len > fileLength) + else if (ded.pos() + ded.len() > fileLength) { corrupted = true; message = "Record " + ded + " exceeds file length."; @@ -202,8 +202,8 @@ public class IndexedDisk implements AutoCloseable throw new IOException("The File Is Corrupt, need to reset"); } - final ByteBuffer data = ByteBuffer.allocate(ded.len); - fc.read(data, ded.pos + HEADER_SIZE_BYTES); + final ByteBuffer data = ByteBuffer.allocate(ded.len()); + fc.read(data, ded.pos() + HEADER_SIZE_BYTES); data.flip(); return elementSerializer.deSerialize(data.array(), null); @@ -246,14 +246,13 @@ public class IndexedDisk implements AutoCloseable protected boolean write(final IndexedDiskElementDescriptor ded, final byte[] data) throws IOException { - final long pos = ded.pos; if (log.isTraceEnabled()) { - log.trace("write> pos={0}", pos); + log.trace("write> pos={0}", ded.pos()); log.trace("{0} -- data.length = {1}", fc, data.length); } - if (data.length != ded.len) + if (data.length != ded.len()) { throw new IOException("Mismatched descriptor and data lengths"); } @@ -262,12 +261,12 @@ public class IndexedDisk implements AutoCloseable headerBuffer.putInt(data.length); // write the header headerBuffer.flip(); - int written = fc.write(headerBuffer, pos); + int written = fc.write(headerBuffer, ded.pos()); assert written == HEADER_SIZE_BYTES; //write the data final ByteBuffer dataBuffer = ByteBuffer.wrap(data); - written = fc.write(dataBuffer, pos + HEADER_SIZE_BYTES); + written = fc.write(dataBuffer, ded.pos() + HEADER_SIZE_BYTES); return written == data.length; } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCache.java index 4c3c0421..00df55a8 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCache.java @@ -126,7 +126,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> // keep the content size in kB, so 2^31 kB is reasonable value private void addLengthToCacheSize(final IndexedDiskElementDescriptor value) { - contentSize.addAndGet((value.len + IndexedDisk.HEADER_SIZE_BYTES) / 1024 + 1); + contentSize.addAndGet((value.len() + IndexedDisk.HEADER_SIZE_BYTES) / 1024 + 1); } /** @@ -206,7 +206,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> // keep the content size in kB, so 2^31 kB is reasonable value private void subLengthFromCacheSize(final IndexedDiskElementDescriptor value) { - contentSize.addAndGet((value.len + IndexedDisk.HEADER_SIZE_BYTES) / -1024 - 1); + contentSize.addAndGet((value.len() + IndexedDisk.HEADER_SIZE_BYTES) / -1024 - 1); } } @@ -321,7 +321,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> // Make a clean file name this.fileName = getCacheName().replaceAll("[^a-zA-Z0-9-_\\.]", "_"); this.keyHash = createInitialKeyMap(); - this.queuedPutList = new ConcurrentSkipListSet<>(Comparator.comparing(ded1 -> ded1.pos)); + this.queuedPutList = new ConcurrentSkipListSet<>(Comparator.comparing(ded1 -> ded1.pos())); this.recycle = new ConcurrentSkipListSet<>(); try @@ -392,7 +392,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> { if (ded != null) { - final int amount = ded.len + IndexedDisk.HEADER_SIZE_BYTES; + final int amount = ded.len() + IndexedDisk.HEADER_SIZE_BYTES; if (add) { @@ -420,13 +420,13 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> boolean isOk = true; long expectedNextPos = 0; for (final IndexedDiskElementDescriptor ded : sortedDescriptors) { - if (expectedNextPos > ded.pos) + if (expectedNextPos > ded.pos()) { log.error("{0}: Corrupt file: overlapping deds {1}", logCacheName, ded); isOk = false; break; } - expectedNextPos = ded.pos + IndexedDisk.HEADER_SIZE_BYTES + ded.len; + expectedNextPos = ded.pos() + IndexedDisk.HEADER_SIZE_BYTES + ded.len(); } log.debug("{0}: Check for DED overlaps took {1} ms.", () -> logCacheName, timer::getElapsedTime); @@ -456,7 +456,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> final long fileLength = dataFile.length(); final IndexedDiskElementDescriptor corruptDed = keyHash.values().stream() - .filter(ded -> ded.pos + IndexedDisk.HEADER_SIZE_BYTES + ded.len > fileLength) + .filter(ded -> ded.pos() + IndexedDisk.HEADER_SIZE_BYTES + ded.len() > fileLength) .findFirst() .orElse(null); @@ -464,7 +464,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> { isOk = false; log.warn("{0}: The dataFile is corrupted!\n raf.length() = {1}\n ded.pos = {2}", - logCacheName, fileLength, corruptDed.pos); + logCacheName, fileLength, corruptDed.pos()); } else if (checkForDedOverlaps) { @@ -525,7 +525,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> private Collection<IndexedDiskElementDescriptor> createPositionSortedDescriptorList() { final List<IndexedDiskElementDescriptor> defragList = new ArrayList<>(keyHash.values()); - defragList.sort(Comparator.comparing(ded1 -> ded1.pos)); + defragList.sort(Comparator.comparing(ded1 -> ded1.pos())); return defragList; } @@ -558,11 +558,12 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> storageLock.writeLock().lock(); try { - if (expectedNextPos != element.pos) + IndexedDiskElementDescriptor newDed = element; + if (expectedNextPos != element.pos()) { - dataFile.move(element, expectedNextPos); + newDed = dataFile.move(element, expectedNextPos); } - expectedNextPos = element.pos + IndexedDisk.HEADER_SIZE_BYTES + element.len; + expectedNextPos = newDed.pos() + IndexedDisk.HEADER_SIZE_BYTES + newDed.len(); } finally { @@ -688,7 +689,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> /** * For debugging. This dumps the values by default. */ - public void dump() + protected void dump() { dump(true); } @@ -700,7 +701,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> * @param dumpValues * A boolean indicating if values should be dumped. */ - public void dump(final boolean dumpValues) + protected void dump(final boolean dumpValues) { if (log.isTraceEnabled()) { @@ -713,7 +714,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> log.trace("{0}: [dump] Disk element, key: {1}, pos: {2}, len: {3}" + (dumpValues ? ", val: " + get(key) : ""), - logCacheName, key, ded.pos, ded.len); + logCacheName, key, ded.pos(), ded.len()); } } } @@ -1434,12 +1435,9 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> // Item with the same key already exists in file. // Try to reuse the location if possible. - if (old != null && data.length <= old.len) + if (old != null && data.length <= old.len()) { - // Reuse the old ded. The defrag relies on ded updates by reference, not - // replacement. - ded = old; - ded.len = data.length; + ded = new IndexedDiskElementDescriptor(old.pos(), data.length); } else { @@ -1453,18 +1451,14 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> { // remove element from recycle bin recycle.remove(rep); - ded = rep; - ded.len = data.length; + ded = new IndexedDiskElementDescriptor(rep.pos(), data.length); recycleCnt++; this.adjustBytesFree(ded, false); log.debug("{0}: using recycled ded {1} rep.len = {2} ded.len = {3}", - logCacheName, ded.pos, rep.len, ded.len); + logCacheName, ded.pos(), rep.len(), ded.len()); } } - // Put it in the map - keyHash.put(ce.getKey(), ded); - if (queueInput) { queuedPutList.add(ded); @@ -1479,6 +1473,8 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> } } + // Put it in the map + keyHash.put(ce.getKey(), ded); dataFile.write(ded, data); } finally @@ -1487,7 +1483,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V> } log.debug("{0}: Put to file: {1}, key: {2}, position: {3}, size: {4}", - logCacheName, fileName, ce.getKey(), ded.pos, ded.len); + logCacheName, fileName, ce.getKey(), ded.pos(), ded.len()); } catch (final IOException e) { diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskElementDescriptor.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskElementDescriptor.java index 34a5bcfc..7c08ccfb 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskElementDescriptor.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskElementDescriptor.java @@ -25,30 +25,17 @@ import java.io.Serializable; * Disk objects are located by descriptor entries. These are saved on shutdown and loaded into * memory on startup. */ -public class IndexedDiskElementDescriptor - implements Serializable, Comparable<IndexedDiskElementDescriptor> +public record IndexedDiskElementDescriptor( + /** Position of the cache data entry on disk. */ + long pos, + + /** Number of bytes the serialized form of the cache data takes. */ + int len +) implements Serializable, Comparable<IndexedDiskElementDescriptor> { /** Don't change */ private static final long serialVersionUID = -3029163572847659450L; - /** Position of the cache data entry on disk. */ - long pos; - - /** Number of bytes the serialized form of the cache data takes. */ - int len; - - /** - * Constructs a usable disk element descriptor. - * - * @param pos - * @param len - */ - public IndexedDiskElementDescriptor( final long pos, final int len ) - { - this.pos = pos; - this.len = len; - } - /** * Compares based on length, then on pos descending. * @@ -72,33 +59,6 @@ public class IndexedDiskElementDescriptor return lenCompare; } - /** - * @see Object#equals(Object) - */ - @Override - public boolean equals(final Object o) - { - if (o == null) - { - return false; - } - if (o instanceof IndexedDiskElementDescriptor ided) - { - return pos == ided.pos && len == ided.len; - } - - return false; - } - - /** - * @see Object#hashCode() - */ - @Override - public int hashCode() - { - return Long.valueOf(this.pos).hashCode() ^ Integer.valueOf(len).hashCode(); - } - /** * @return debug string */ @@ -107,8 +67,8 @@ public class IndexedDiskElementDescriptor { final StringBuilder buf = new StringBuilder(); buf.append( "[DED: " ); - buf.append( " pos = " + pos ); - buf.append( " len = " + len ); + buf.append( " pos = ").append(pos); + buf.append( " len = ").append(len); buf.append( "]" ); return buf.toString(); } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java index fd35af49..3e6e3a90 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java @@ -34,6 +34,7 @@ import javax.sql.DataSource; import org.apache.commons.jcs4.auxiliary.AuxiliaryCacheAttributes; import org.apache.commons.jcs4.auxiliary.disk.AbstractDiskCache; +import org.apache.commons.jcs4.auxiliary.disk.jdbc.TableState.TableStateType; import org.apache.commons.jcs4.auxiliary.disk.jdbc.dsfactory.DataSourceFactory; import org.apache.commons.jcs4.engine.behavior.ICache; import org.apache.commons.jcs4.engine.behavior.ICacheElement; @@ -129,8 +130,8 @@ public class JDBCDiskCache<K, V> { super( cattr ); - setTableState( tableState ); - setJdbcDiskCacheAttributes( cattr ); + this.tableState = tableState; + this.jdbcDiskCacheAttributes = cattr; log.info( "jdbcDiskCacheAttributes = {0}", this::getJdbcDiskCacheAttributes); @@ -184,7 +185,7 @@ public class JDBCDiskCache<K, V> if (result.next()) { - getTableState().setState( TableState.DELETE_RUNNING ); + getTableState().setState( TableStateType.DELETE_RUNNING ); final long now = System.currentTimeMillis() / 1000; final String sql = String.format(SQL_DELETE_EXPIRED, getTableName()); @@ -222,7 +223,7 @@ public class JDBCDiskCache<K, V> } finally { - getTableState().setState( TableState.FREE ); + getTableState().setState( TableStateType.FREE ); } return deleted; @@ -299,14 +300,6 @@ public class JDBCDiskCache<K, V> return jdbcDiskCacheAttributes; } - /** - * @param jdbcDiskCacheAttributes The jdbcDiskCacheAttributes to set. - */ - protected void setJdbcDiskCacheAttributes( final JDBCDiskCacheAttributes jdbcDiskCacheAttributes ) - { - this.jdbcDiskCacheAttributes = jdbcDiskCacheAttributes; - } - /** * Return the keys in this cache. * @@ -393,14 +386,6 @@ public class JDBCDiskCache<K, V> return tableState; } - /** - * @param tableState The tableState to set. - */ - public void setTableState( final TableState tableState ) - { - this.tableState = tableState; - } - /** * If test before insert it true, we check to see if the element exists. If the element exists * we will update. Otherwise, we try inserting. If this fails because the item exists, we will diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/TableState.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/TableState.java index 46057af8..f1dc91bc 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/TableState.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/TableState.java @@ -33,23 +33,26 @@ public class TableState /** Don't change. */ private static final long serialVersionUID = -6625081552084964885L; - /** - * The table is free. It can be accessed and no potentially table locking - * jobs are running. - */ - public static final int FREE = 0; + public enum TableStateType + { + /** + * The table is free. It can be accessed and no potentially table locking + * jobs are running. + */ + FREE, - /** A potentially table locking deletion is running */ - public static final int DELETE_RUNNING = 1; + /** A potentially table locking deletion is running */ + DELETE_RUNNING, - /** A table locking optimization is running. */ - public static final int OPTIMIZATION_RUNNING = 2; + /** A table locking optimization is running. */ + OPTIMIZATION_RUNNING + } /** Name of the table whose state this reflects. */ private String tableName; /** We might want to add error */ - private int state = FREE; + private TableStateType state = TableStateType.FREE; /** * Constructs a usable table state. @@ -58,43 +61,26 @@ public class TableState */ public TableState( final String tableName ) { - setTableName( tableName ); + this.tableName = tableName; } /** * @return the state. */ - public int getState() + public TableStateType getState() { return state; } - /** - * @return the tableName. - */ - public String getTableName() - { - return tableName; - } - /** * @param state * The state to set. */ - public void setState( final int state ) + public void setState(TableStateType state) { this.state = state; } - /** - * @param tableName - * The tableName to set. - */ - public void setTableName( final String tableName ) - { - this.tableName = tableName; - } - /** * Converts this instance to a String for debugging purposes. * @@ -104,9 +90,9 @@ public class TableState public String toString() { final StringBuilder str = new StringBuilder(); - str.append( "TableState " ); - str.append( "\n TableName = " + getTableName() ); - str.append( "\n State = " + getState() ); + str.append("TableState " ); + str.append("\n TableName = ").append(tableName); + str.append("\n State = ").append(getState()); return str.toString(); } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLDiskCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLDiskCache.java index 1fc3ad1b..723ce0b3 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLDiskCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLDiskCache.java @@ -24,6 +24,7 @@ import java.util.Map; import org.apache.commons.jcs4.auxiliary.disk.jdbc.JDBCDiskCache; import org.apache.commons.jcs4.auxiliary.disk.jdbc.TableState; +import org.apache.commons.jcs4.auxiliary.disk.jdbc.TableState.TableStateType; import org.apache.commons.jcs4.auxiliary.disk.jdbc.dsfactory.DataSourceFactory; import org.apache.commons.jcs4.engine.behavior.ICacheElement; import org.apache.commons.jcs4.log.Log; @@ -75,7 +76,7 @@ public class MySQLDiskCache<K, V> @Override protected int deleteExpired() { - if (getTableState().getState() == TableState.OPTIMIZATION_RUNNING && + if (getTableState().getState() == TableStateType.OPTIMIZATION_RUNNING && this.mySQLDiskCacheAttributes.isBalkDuringOptimization()) { return -1; @@ -93,7 +94,7 @@ public class MySQLDiskCache<K, V> @Override protected ICacheElement<K, V> processGet( final K key ) { - if (getTableState().getState() == TableState.OPTIMIZATION_RUNNING && + if (getTableState().getState() == TableStateType.OPTIMIZATION_RUNNING && this.mySQLDiskCacheAttributes.isBalkDuringOptimization()) { return null; @@ -111,7 +112,7 @@ public class MySQLDiskCache<K, V> @Override protected Map<K, ICacheElement<K, V>> processGetMatching( final String pattern ) { - if (getTableState().getState() == TableState.OPTIMIZATION_RUNNING && + if (getTableState().getState() == TableStateType.OPTIMIZATION_RUNNING && this.mySQLDiskCacheAttributes.isBalkDuringOptimization()) { return null; @@ -128,7 +129,7 @@ public class MySQLDiskCache<K, V> @Override protected void processUpdate( final ICacheElement<K, V> element ) { - if (getTableState().getState() == TableState.OPTIMIZATION_RUNNING && + if (getTableState().getState() == TableStateType.OPTIMIZATION_RUNNING && this.mySQLDiskCacheAttributes.isBalkDuringOptimization()) { return; diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java index 73fff5e9..760c238a 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java @@ -27,6 +27,7 @@ import java.sql.Statement; import javax.sql.DataSource; import org.apache.commons.jcs4.auxiliary.disk.jdbc.TableState; +import org.apache.commons.jcs4.auxiliary.disk.jdbc.TableState.TableStateType; import org.apache.commons.jcs4.log.Log; import org.apache.commons.jcs4.utils.timing.ElapsedTimer; @@ -154,7 +155,7 @@ public class MySQLTableOptimizer final ElapsedTimer timer = new ElapsedTimer(); boolean success = false; - if ( tableState.getState() == TableState.OPTIMIZATION_RUNNING ) + if ( tableState.getState() == TableStateType.OPTIMIZATION_RUNNING ) { log.warn( "Skipping optimization. Optimize was called, but the " + "table state indicates that an optimization is currently running." ); @@ -163,7 +164,7 @@ public class MySQLTableOptimizer try { - tableState.setState( TableState.OPTIMIZATION_RUNNING ); + tableState.setState( TableStateType.OPTIMIZATION_RUNNING ); log.info( "Optimizing table [{0}]", getTableName()); try (Connection con = dataSource.getConnection()) @@ -219,7 +220,7 @@ public class MySQLTableOptimizer } finally { - tableState.setState( TableState.FREE ); + tableState.setState( TableStateType.FREE ); log.info( "Optimization of table [{0}] took {1} ms.", this::getTableName, timer::getElapsedTime); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/LateralElementDescriptor.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/LateralElementDescriptor.java index 4cc9e124..e77ccac8 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/LateralElementDescriptor.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/LateralElementDescriptor.java @@ -28,39 +28,28 @@ import org.apache.commons.jcs4.engine.behavior.ICacheElement; * JCS-TCP-Lateral packet. The headers specify the action the receiver should * take. */ -public class LateralElementDescriptor<K, V> - implements Serializable +public record LateralElementDescriptor<K, V>( + /** The Cache Element that we are distributing. */ + ICacheElement<K, V> payload, + + /** The operation has been requested by the client. */ + LateralCommand command, + + /** + * The id of the source of the request. This is used to prevent infinite + * loops. + */ + long requesterId, + + /** + * The hash code value for this element. + */ + int valHashCode +) implements Serializable { /** Don't change */ private static final long serialVersionUID = 5268222498076063575L; - /** The Cache Element that we are distributing. */ - public ICacheElement<K, V> ce; - - /** - * The id of the source of the request. This is used to prevent infinite - * loops. - */ - public long requesterId; - - /** The operation has been requested by the client. */ - public LateralCommand command = LateralCommand.UPDATE; - - /** - * The hash code value for this element. - */ - public int valHashCode = -1; - - /** - * Constructor for the LateralElementDescriptor object - * - * @param ce ICacheElement<K, V> payload - */ - public LateralElementDescriptor( final ICacheElement<K, V> ce ) - { - this.ce = ce; - } - /** * Constructor for the LateralElementDescriptor object * @@ -70,8 +59,7 @@ public class LateralElementDescriptor<K, V> */ public LateralElementDescriptor( final ICacheElement<K, V> ce, final LateralCommand command) { - this(ce); - this.command = command; + this(ce, command, 0, -1); } /** @@ -84,50 +72,7 @@ public class LateralElementDescriptor<K, V> */ public LateralElementDescriptor( final ICacheElement<K, V> ce, final LateralCommand command, final long requesterId) { - this(ce, command); - this.requesterId = requesterId; - } - - /** - * Return operation requested by the client - * - * @return the command - * @since 3.1 - */ - public LateralCommand getCommand() - { - return command; - } - - /** - * Return payload - * - * @return the ce - * @since 3.1 - */ - public ICacheElement<K, V> getPayload() - { - return ce; - } - - /** - * Return id of the source of the request - * - * @return the requesterId - * @since 3.1 - */ - public long getRequesterId() - { - return requesterId; - } - - /** - * @return the valHashCode - * @since 3.1 - */ - public int getValHashCode() - { - return valHashCode; + this(ce, command, requesterId, -1); } /** @@ -140,7 +85,7 @@ public class LateralElementDescriptor<K, V> buf.append( "\n LateralElementDescriptor " ); buf.append( "\n command = [" + this.command + "]" ); buf.append( "\n valHashCode = [" + this.valHashCode + "]" ); - buf.append( "\n ICacheElement = [" + this.ce + "]" ); + buf.append( "\n ICacheElement = [" + this.payload + "]" ); return buf.toString(); } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCache.java index 6ac44da0..be1c7852 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCache.java @@ -320,7 +320,7 @@ public class LateralTCPCache<K, V> try { - lateralCacheService.remove( cacheName, key, CacheInfo.listenerId ); + lateralCacheService.remove( cacheName, key, CacheInfo.INSTANCE.listenerId()); } catch ( final IOException ex ) { @@ -341,7 +341,7 @@ public class LateralTCPCache<K, V> { try { - lateralCacheService.removeAll( cacheName, CacheInfo.listenerId ); + lateralCacheService.removeAll( cacheName, CacheInfo.INSTANCE.listenerId()); } catch ( final IOException ex ) { @@ -364,8 +364,8 @@ public class LateralTCPCache<K, V> if (ce != null) { log.debug( "update: lateral = [{0}], CacheInfo.listenerId = {1}", - lateralCacheService, CacheInfo.listenerId ); - lateralCacheService.update( ce, CacheInfo.listenerId ); + lateralCacheService, CacheInfo.INSTANCE.listenerId()); + lateralCacheService.update( ce, CacheInfo.INSTANCE.listenerId()); } } catch ( final IOException ex ) diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCacheNoWait.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCacheNoWait.java index 02a99d57..e8f58788 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCacheNoWait.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCacheNoWait.java @@ -86,7 +86,7 @@ public class LateralTCPCacheNoWait<K, V> final CacheEventQueueFactory<K, V> fact = new CacheEventQueueFactory<>(); this.eventQueue = fact.createCacheEventQueue( new CacheAdaptor<>( cache ), - CacheInfo.listenerId, cache.getCacheName(), + CacheInfo.INSTANCE.listenerId(), cache.getCacheName(), getAuxiliaryCacheAttributes().getEventQueuePoolName(), getAuxiliaryCacheAttributes().getEventQueueType() ); @@ -392,7 +392,7 @@ public class LateralTCPCacheNoWait<K, V> } final CacheEventQueueFactory<K, V> fact = new CacheEventQueueFactory<>(); this.eventQueue = fact.createCacheEventQueue( new CacheAdaptor<>( cache ), - CacheInfo.listenerId, cache.getCacheName(), + CacheInfo.INSTANCE.listenerId(), cache.getCacheName(), getAuxiliaryCacheAttributes().getEventQueuePoolName(), getAuxiliaryCacheAttributes().getEventQueueType() ); } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPListener.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPListener.java index 29804712..f8750128 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPListener.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPListener.java @@ -114,7 +114,7 @@ public class LateralTCPListener<K, V> * Use the vmid by default. This can be set for testing. If we ever need to run more than one * per vm, then we need a new technique. */ - private long listenerId = CacheInfo.listenerId; + private long listenerId = CacheInfo.INSTANCE.listenerId(); /** Is this shut down? */ private final AtomicBoolean shutdown = new AtomicBoolean(); @@ -232,7 +232,7 @@ public class LateralTCPListener<K, V> return; } - if ( led.getRequesterId() == getListenerId() ) + if ( led.requesterId() == getListenerId() ) { log.debug( "from self" ); } @@ -292,35 +292,35 @@ public class LateralTCPListener<K, V> */ private Object handleElement(final LateralElementDescriptor<K, V> led) throws IOException { - final String cacheName = led.getPayload().getCacheName(); - final K key = led.getPayload().getKey(); + final String cacheName = led.payload().getCacheName(); + final K key = led.payload().getKey(); Object obj = null; - switch (led.getCommand()) + switch (led.command()) { case UPDATE: - handlePut(led.getPayload()); + handlePut(led.payload()); break; case REMOVE: // if a hash code was given and filtering is on // check to see if they are the same // if so, then don't remove, otherwise issue a remove - if (led.getValHashCode() != -1 && + if (led.valHashCode() != -1 && getTcpLateralCacheAttributes().isFilterRemoveByHashCode()) { final ICacheElement<K, V> test = getCache( cacheName ).localGet( key ); if ( test != null ) { - if ( test.getVal().hashCode() == led.getValHashCode() ) + if ( test.getVal().hashCode() == led.valHashCode() ) { log.debug( "Filtering detected identical hashCode [{0}], " + "not issuing a remove for led {1}", - led.getValHashCode(), led ); + led.valHashCode(), led ); return null; } log.debug( "Different hash codes, in cache [{0}] sent [{1}]", - test.getVal()::hashCode, led::getValHashCode ); + test.getVal()::hashCode, led::valHashCode ); } } handleRemove( cacheName, key ); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPService.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPService.java index 6af8fb8c..2f8ab57f 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPService.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPService.java @@ -54,7 +54,7 @@ public class LateralTCPService<K, V> private final LateralTCPSender sender; /** Use the vmid by default */ - private long listenerId = CacheInfo.listenerId; + private long listenerId = CacheInfo.INSTANCE.listenerId(); /** * Constructor for the LateralTCPService object @@ -131,8 +131,7 @@ public class LateralTCPService<K, V> { final CacheElement<K, V> ce = new CacheElement<>( cacheName, key, null ); final LateralElementDescriptor<K, V> led = - new LateralElementDescriptor<>(ce, LateralCommand.GET); - // led.requesterId = requesterId; // later + new LateralElementDescriptor<>(ce, LateralCommand.GET, requesterId); @SuppressWarnings("unchecked") // Need to cast from Object final ICacheElement<K, V> response = (ICacheElement<K, V>)sender.sendAndReceive( led ); @@ -211,8 +210,7 @@ public class LateralTCPService<K, V> } final CacheElement<String, String> ce = new CacheElement<>( cacheName, pattern, null ); final LateralElementDescriptor<String, String> led = - new LateralElementDescriptor<>(ce, LateralCommand.GET_MATCHING); - // led.requesterId = requesterId; // later + new LateralElementDescriptor<>(ce, LateralCommand.GET_MATCHING, requesterId); final Object response = sender.sendAndReceive( led ); if ( response != null ) @@ -391,8 +389,8 @@ public class LateralTCPService<K, V> // set the value to null so we don't send the item final CacheElement<K, V> ce = new CacheElement<>( item.getCacheName(), item.getKey(), null ); final LateralElementDescriptor<K, V> led = - new LateralElementDescriptor<>(ce, LateralCommand.REMOVE, requesterId); - led.valHashCode = item.getVal().hashCode(); + new LateralElementDescriptor<>(ce, LateralCommand.REMOVE, requesterId, + item.getVal().hashCode()); sender.send( led ); } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/CommonRemoteCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/CommonRemoteCacheAttributes.java index 9668a9e0..4a801fcf 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/CommonRemoteCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/CommonRemoteCacheAttributes.java @@ -265,8 +265,8 @@ public abstract class CommonRemoteCacheAttributes buf.append( "\n RemoteCacheAttributes "); if (this.location != null) { - buf.append( "\n remoteHost = [").append(this.location.getHost()).append("]"); - buf.append( "\n remotePort = [").append(this.location.getPort()).append("]"); + buf.append( "\n remoteHost = [").append(this.location.host()).append("]"); + buf.append( "\n remotePort = [").append(this.location.port()).append("]"); } buf.append( "\n cacheName = [").append(getCacheName()).append("]"); buf.append( "\n remoteType = [").append(remoteType).append("]"); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteLocation.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteLocation.java index 6df2938d..9bd45d72 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteLocation.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteLocation.java @@ -27,7 +27,13 @@ import org.apache.commons.jcs4.log.Log; /** * Location of the RMI registry. */ -public final class RemoteLocation +public record RemoteLocation( + /** Host name */ + String host, + + /** Port */ + int port +) { /** The logger. */ private static final Log log = Log.getLog( RemoteLocation.class ); @@ -48,79 +54,13 @@ public final class RemoteLocation if (match.find() && match.groupCount() == 2) { - return new RemoteLocation( match.group(1), Integer.parseInt( match.group(2) ) ); + return new RemoteLocation(match.group(1), Integer.parseInt( match.group(2))); } - log.error("Invalid server descriptor: {0}", server); + log.error("Invalid server descriptor: {0}", server); return null; } - /** Host name */ - private final String host; - - /** Port */ - private final int port; - - /** - * Constructor for the Location object - * - * @param host - * @param port - */ - public RemoteLocation( final String host, final int port ) - { - this.host = host; - this.port = port; - } - - /** - * @param obj - * @return true if the host and port are equal - */ - @Override - public boolean equals( final Object obj ) - { - if ( obj == this ) - { - return true; - } - if (obj instanceof RemoteLocation l) - { - if ( this.host == null ) - { - return l.host == null && port == l.port; - } - return host.equals( l.host ) && port == l.port; - } - - return false; - } - - /** - * @return the host - */ - public String getHost() - { - return host; - } - - /** - * @return the port - */ - public int getPort() - { - return port; - } - - /** - * @return int - */ - @Override - public int hashCode() - { - return host == null ? port : host.hashCode() ^ port; - } - /** * @see Object#toString() */ diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtils.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtils.java index acff24c2..53f89ea9 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtils.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtils.java @@ -165,7 +165,7 @@ public class RemoteUtils */ public static String getNamingURL(final RemoteLocation location, final String serviceName) { - return getNamingURL(location.getHost(), location.getPort(), serviceName); + return getNamingURL(location.host(), location.port(), serviceName); } /** diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/CacheInfo.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/CacheInfo.java index 0482f6c9..d703571a 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/CacheInfo.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/CacheInfo.java @@ -24,23 +24,20 @@ import java.rmi.dgc.VMID; /** * This is a static variable holder for the distribution auxiliaries that need something like a vmid. */ -public final class CacheInfo +public record CacheInfo( + long listenerId +) { /** * Used to identify a client, so we can run multiple clients off one host. * Need since there is no way to identify a client other than by host in * rmi. - * <p> - * TODO: may have some trouble in failover mode if the cache keeps its old - * id. We may need to reset this when moving into failover. */ - private static final VMID vmid = new VMID(); - - /** By default this is the hash code of the VMID */ - public static final long listenerId = vmid.hashCode(); + public static final CacheInfo INSTANCE = new CacheInfo(); /** Shouldn't be instantiated */ private CacheInfo() { + this(new VMID().hashCode()); } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/discovery/UDPDiscoveryReceiver.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/discovery/UDPDiscoveryReceiver.java index fdbe9069..2ec7c033 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/discovery/UDPDiscoveryReceiver.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/discovery/UDPDiscoveryReceiver.java @@ -200,7 +200,7 @@ public class UDPDiscoveryReceiver private void handleMessage(final UDPDiscoveryMessage message) { // consider comparing ports here instead. - if ( message.getRequesterId() == CacheInfo.listenerId ) + if ( message.getRequesterId() == CacheInfo.INSTANCE.listenerId()) { log.debug( "Ignoring message sent from self" ); } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/discovery/UDPDiscoverySender.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/discovery/UDPDiscoverySender.java index 0b2324b8..8f47e853 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/discovery/UDPDiscoverySender.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/discovery/UDPDiscoverySender.java @@ -158,7 +158,7 @@ public class UDPDiscoverySender implements AutoCloseable public void passiveBroadcast( final String host, final int port, final ArrayList<String> cacheNames ) throws IOException { - passiveBroadcast( host, port, cacheNames, CacheInfo.listenerId ); + passiveBroadcast( host, port, cacheNames, CacheInfo.INSTANCE.listenerId()); } /** @@ -197,7 +197,7 @@ public class UDPDiscoverySender implements AutoCloseable public void removeBroadcast( final String host, final int port, final ArrayList<String> cacheNames ) throws IOException { - removeBroadcast( host, port, cacheNames, CacheInfo.listenerId ); + removeBroadcast( host, port, cacheNames, CacheInfo.INSTANCE.listenerId()); } /** @@ -232,7 +232,7 @@ public class UDPDiscoverySender implements AutoCloseable public void requestBroadcast() throws IOException { - requestBroadcast(CacheInfo.listenerId); + requestBroadcast(CacheInfo.INSTANCE.listenerId()); } /** diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexDiskCacheCountUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexDiskCacheCountUnitTest.java index ee34273a..760aaa8d 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexDiskCacheCountUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexDiskCacheCountUnitTest.java @@ -57,18 +57,14 @@ public class IndexDiskCacheCountUnitTest extends AbstractIndexDiskCacheUnitTest final String[] test = { "a", "bb", "ccc", "dddd", "eeeee", "ffffff", "ggggggg", "hhhhhhhhh", "iiiiiiiiii" }; final String[] expect = { null, "bb", "ccc", null, null, "ffffff", null, "hhhhhhhhh", "iiiiiiiiii" }; - //System.out.println( "------------------------- testRecycleBin " ); - for ( int i = 0; i < 6; i++ ) { final ICacheElement<String, String> element = new CacheElement<>( "testRecycleBin", "key:" + test[i], test[i]); - //System.out.println( "About to add key:" + test[i] + " i = " + i ); disk.processUpdate( element ); } for ( int i = 3; i < 5; i++ ) { - //System.out.println( "About to remove key:" + test[i] + " i = " + i ); disk.remove( "key:" + test[i]); } @@ -77,7 +73,6 @@ public class IndexDiskCacheCountUnitTest extends AbstractIndexDiskCacheUnitTest for ( int i = 7; i < 9; i++ ) { final ICacheElement<String, String> element = new CacheElement<>( "testRecycleBin", "key:" + test[i], test[i]); - //System.out.println( "About to add key:" + test[i] + " i = " + i ); disk.processUpdate( element ); } @@ -86,14 +81,6 @@ public class IndexDiskCacheCountUnitTest extends AbstractIndexDiskCacheUnitTest for ( int i = 0; i < 9; i++ ) { final ICacheElement<String, String> element = disk.get( "key:" + test[i]); - if ( element != null ) - { - //System.out.println( "element = " + element.getVal() ); - } - else - { - //System.out.println( "null --key:" + test[i]); - } final String expectedValue = expect[i]; if ( expectedValue == null ) diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLDiskCacheUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLDiskCacheUnitTest.java index c8c5d9ab..291a955c 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLDiskCacheUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/mysql/MySQLDiskCacheUnitTest.java @@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import java.sql.SQLException; import org.apache.commons.jcs4.auxiliary.disk.jdbc.TableState; +import org.apache.commons.jcs4.auxiliary.disk.jdbc.TableState.TableStateType; import org.apache.commons.jcs4.auxiliary.disk.jdbc.dsfactory.SharedPoolDataSourceFactory; import org.junit.jupiter.api.Test; @@ -59,7 +60,7 @@ class MySQLDiskCacheUnitTest dsFactory.initialize(attributes); final TableState tableState = new TableState( tableName ); - tableState.setState( TableState.OPTIMIZATION_RUNNING ); + tableState.setState( TableStateType.OPTIMIZATION_RUNNING ); final MySQLDiskCache<String, String> cache = new MySQLDiskCache<>(attributes, dsFactory, tableState); diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtilsUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtilsUnitTest.java index 873ee10a..d78a17c7 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtilsUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtilsUnitTest.java @@ -57,12 +57,12 @@ class RemoteUtilsUnitTest void testParseServerAndPort() { RemoteLocation loc = RemoteLocation.parseServerAndPort("server1:1234"); - assertEquals("server1", loc.getHost()); - assertEquals(1234, loc.getPort()); + assertEquals("server1", loc.host()); + assertEquals(1234, loc.port()); loc = RemoteLocation.parseServerAndPort(" server2 : 4567 "); - assertEquals("server2", loc.getHost()); - assertEquals(4567, loc.getPort()); + assertEquals("server2", loc.host()); + assertEquals(4567, loc.port()); loc = RemoteLocation.parseServerAndPort("server2 : port"); assertNull(loc); diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/server/BasicRemoteCacheClientServerUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/server/BasicRemoteCacheClientServerUnitTest.java index 594a31b0..b033554a 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/server/BasicRemoteCacheClientServerUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/server/BasicRemoteCacheClientServerUnitTest.java @@ -112,7 +112,7 @@ class BasicRemoteCacheClientServerUnitTest server = RemoteCacheServerStartupUtil.startServerUsingProperties(configFile); factory = new RemoteCacheFactory(); factory.initialize(); - remotePort = server.remoteCacheServerAttributes.getRemoteLocation().getPort(); + remotePort = server.remoteCacheServerAttributes.getRemoteLocation().port(); } @AfterAll
