From 21ca69330f6a82705fb082fb0188b15e497e8146 Mon Sep 17 00:00:00 2001 From: Thomas Vandahl Date: Tue, 22 Aug 2023 17:23:43 +0200 Subject: [PATCH] Further clean up array usage --- .../disk/indexed/IndexedDiskCache.java | 20 +++--- .../remote/server/RemoteCacheServer.java | 66 +++++++------------ .../AbstractIndexDiskCacheUnitTest.java | 10 +-- 3 files changed, 38 insertions(+), 58 deletions(-) diff --git a/commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java b/commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java index e70bdcf7e..4f16181d4 100644 --- a/commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java +++ b/commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java @@ -24,6 +24,7 @@ import java.io.Serializable; import java.nio.file.Files; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -398,7 +399,7 @@ else if (checkForDedOverlaps) * @param sortedDescriptors * @return false if there are overlaps. */ - protected boolean checkForDedOverlaps(final IndexedDiskElementDescriptor[] sortedDescriptors) + protected boolean checkForDedOverlaps(final Collection sortedDescriptors) { final ElapsedTimer timer = new ElapsedTimer(); boolean isOk = true; @@ -1124,7 +1125,7 @@ protected void optimizeFile() log.info("{0}: Beginning Optimization #{1}", logCacheName, timesOptimized); // CREATE SNAPSHOT - IndexedDiskElementDescriptor[] defragList = null; + Collection defragList = null; storageLock.writeLock().lock(); @@ -1153,10 +1154,8 @@ protected void optimizeFile() { if (!queuedPutList.isEmpty()) { - defragList = queuedPutList.toArray(new IndexedDiskElementDescriptor[0]); - // pack them at the end - expectedNextPos = defragFile(defragList, expectedNextPos); + expectedNextPos = defragFile(queuedPutList, expectedNextPos); } // TRUNCATE THE FILE dataFile.truncate(expectedNextPos); @@ -1197,7 +1196,7 @@ protected void optimizeFile() * the start position in the file * @return this is the potential new file end */ - private long defragFile(final IndexedDiskElementDescriptor[] defragList, final long startingPos) + private long defragFile(final Collection defragList, final long startingPos) { final ElapsedTimer timer = new ElapsedTimer(); long preFileSize = 0; @@ -1208,7 +1207,8 @@ private long defragFile(final IndexedDiskElementDescriptor[] defragList, final l preFileSize = this.dataFile.length(); // find the first gap in the disk and start defragging. expectedNextPos = startingPos; - for (final IndexedDiskElementDescriptor element : defragList) { + for (final IndexedDiskElementDescriptor element : defragList) + { storageLock.writeLock().lock(); try { @@ -1247,14 +1247,14 @@ private long defragFile(final IndexedDiskElementDescriptor[] defragList, final l * sorted by position in the dataFile. *

* - * @return IndexedDiskElementDescriptor[] + * @return Collection */ - private IndexedDiskElementDescriptor[] createPositionSortedDescriptorList() + private Collection createPositionSortedDescriptorList() { final List defragList = new ArrayList<>(keyHash.values()); Collections.sort(defragList, (ded1, ded2) -> Long.compare(ded1.pos, ded2.pos)); - return defragList.toArray(new IndexedDiskElementDescriptor[0]); + return defragList; } /** diff --git a/commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheServer.java b/commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheServer.java index 527839d27..97f13375d 100644 --- a/commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheServer.java +++ b/commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/server/RemoteCacheServer.java @@ -26,7 +26,9 @@ import java.rmi.server.RMISocketFactory; import java.rmi.server.UnicastRemoteObject; import java.rmi.server.Unreferenced; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -314,9 +316,10 @@ private void processUpdate( final ICacheElement item, final long requester // IF LOCAL CLUSTER CONSISTENCY IS CONFIGURED if (!fromCluster || fromCluster && remoteCacheServerAttributes.isLocalClusterConsistency()) { - final ICacheEventQueue[] qlist = getEventQList( cacheDesc, requesterId ); - log.debug( "qlist.length = {0}", qlist.length ); - for (final ICacheEventQueue element : qlist) { + final List> qlist = getEventQList( cacheDesc, requesterId ); + log.debug("qlist.size() = {0}", qlist.size()); + for (final ICacheEventQueue element : qlist) + { element.addPutEvent( item ); } } @@ -837,9 +840,10 @@ private void processRemove( final String cacheName, final K key, final long requ // IF LOCAL CLUSTER CONSISTENCY IS CONFIGURED if (!fromCluster || fromCluster && remoteCacheServerAttributes.isLocalClusterConsistency()) { - final ICacheEventQueue[] qlist = getEventQList( cacheDesc, requesterId ); + final List> qlist = getEventQList( cacheDesc, requesterId ); - for (final ICacheEventQueue element : qlist) { + for (final ICacheEventQueue element : qlist) + { element.addRemoveEvent( key ); } } @@ -921,7 +925,7 @@ private void processRemoveAll( final String cacheName, final long requesterId ) // update registered listeners if (!fromCluster || fromCluster && remoteCacheServerAttributes.isLocalClusterConsistency()) { - final ICacheEventQueue[] qlist = getEventQList( cacheDesc, requesterId ); + final List> qlist = getEventQList( cacheDesc, requesterId ); for (final ICacheEventQueue q : qlist) { @@ -995,9 +999,10 @@ private void processDispose( final String cacheName, final long requesterId ) // best attempt to achieve ordered free-cache-op and notification. synchronized ( cacheDesc ) { - final ICacheEventQueue[] qlist = getEventQList( cacheDesc, requesterId ); + final List> qlist = getEventQList( cacheDesc, requesterId ); - for (final ICacheEventQueue element : qlist) { + for (final ICacheEventQueue element : qlist) + { element.addDisposeEvent(); } cacheManager.freeCache( cacheName ); @@ -1016,9 +1021,10 @@ public void release() { for (final CacheListeners cacheDesc : cacheListenersMap.values()) { - final ICacheEventQueue[] qlist = getEventQList( cacheDesc, 0 ); + final List> qlist = getEventQList( cacheDesc, 0 ); - for (final ICacheEventQueue element : qlist) { + for (final ICacheEventQueue element : qlist) + { element.addDisposeEvent(); } } @@ -1034,7 +1040,6 @@ public void release() */ protected CacheListeners getCacheListeners( final String cacheName ) { - return cacheListenersMap.computeIfAbsent(cacheName, key -> { final CompositeCache cache = cacheManager.getCache(key); return new CacheListeners<>( cache ); @@ -1050,7 +1055,6 @@ protected CacheListeners getCacheListeners( final String cacheName ) */ protected CacheListeners getClusterListeners( final String cacheName ) { - return clusterListenersMap.computeIfAbsent(cacheName, key -> { final CompositeCache cache = cacheManager.getCache( cacheName ); return new CacheListeners<>( cache ); @@ -1069,40 +1073,14 @@ protected CacheListeners getClusterListeners( final String cacheName ) * @param requesterId * @return The eventQList value */ - @SuppressWarnings("unchecked") // No generic arrays in java - private ICacheEventQueue[] getEventQList( final CacheListeners cacheListeners, final long requesterId ) + private List> getEventQList( final CacheListeners cacheListeners, final long requesterId ) { - final ICacheEventQueue[] list = cacheListeners.eventQMap.values().toArray( new ICacheEventQueue[0] ); - int count = 0; - // Set those not qualified to null; Count those qualified. - for ( int i = 0; i < list.length; i++ ) - { - final ICacheEventQueue q = list[i]; - if ( q.isWorking() && q.getListenerId() != requesterId ) - { - count++; - } - else - { - list[i] = null; - } - } - if ( count == list.length ) - { - // All qualified. - return list; - } + final List> list = new ArrayList<>(cacheListeners.eventQMap.values()); - // Returns only the qualified. - final ICacheEventQueue[] qq = new ICacheEventQueue[count]; - count = 0; - for (final ICacheEventQueue element : list) { - if ( element != null ) - { - qq[count++] = element; - } - } - return qq; + // Only return qualified event queues + list.removeIf(q -> (!q.isWorking() || q.getListenerId() == requesterId)); + + return list; } /** diff --git a/commons-jcs3-core/src/test/java/org/apache/commons/jcs3/auxiliary/disk/indexed/AbstractIndexDiskCacheUnitTest.java b/commons-jcs3-core/src/test/java/org/apache/commons/jcs3/auxiliary/disk/indexed/AbstractIndexDiskCacheUnitTest.java index 4729a15eb..15f149afd 100644 --- a/commons-jcs3-core/src/test/java/org/apache/commons/jcs3/auxiliary/disk/indexed/AbstractIndexDiskCacheUnitTest.java +++ b/commons-jcs3-core/src/test/java/org/apache/commons/jcs3/auxiliary/disk/indexed/AbstractIndexDiskCacheUnitTest.java @@ -21,7 +21,9 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -147,12 +149,12 @@ public void testCheckForDedOverlaps_noOverlap() final int numDescriptors = 5; int pos = 0; - final IndexedDiskElementDescriptor[] sortedDescriptors = new IndexedDiskElementDescriptor[numDescriptors]; + final List sortedDescriptors = new ArrayList<>(); for (int i = 0; i < numDescriptors; i++) { final IndexedDiskElementDescriptor descriptor = new IndexedDiskElementDescriptor(pos, i * 2); pos = pos + (i * 2) + IndexedDisk.HEADER_SIZE_BYTES; - sortedDescriptors[i] = descriptor; + sortedDescriptors.add(descriptor); } // DO WORK @@ -175,13 +177,13 @@ public void testCheckForDedOverlaps_overlaps() final int numDescriptors = 5; int pos = 0; - final IndexedDiskElementDescriptor[] sortedDescriptors = new IndexedDiskElementDescriptor[numDescriptors]; + final List sortedDescriptors = new ArrayList<>(); for (int i = 0; i < numDescriptors; i++) { final IndexedDiskElementDescriptor descriptor = new IndexedDiskElementDescriptor(pos, i * 2); // don't add the header + IndexedDisk.RECORD_HEADER; pos = pos + (i * 2); - sortedDescriptors[i] = descriptor; + sortedDescriptors.add(descriptor); } // DO WORK