Skip to content

Commit

Permalink
Further clean up array usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tvand committed Aug 22, 2023
1 parent 65a2041 commit 21ca693
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IndexedDiskElementDescriptor> sortedDescriptors)
{
final ElapsedTimer timer = new ElapsedTimer();
boolean isOk = true;
Expand Down Expand Up @@ -1124,7 +1125,7 @@ protected void optimizeFile()
log.info("{0}: Beginning Optimization #{1}", logCacheName, timesOptimized);

// CREATE SNAPSHOT
IndexedDiskElementDescriptor[] defragList = null;
Collection<IndexedDiskElementDescriptor> defragList = null;

storageLock.writeLock().lock();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<IndexedDiskElementDescriptor> defragList, final long startingPos)
{
final ElapsedTimer timer = new ElapsedTimer();
long preFileSize = 0;
Expand All @@ -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
{
Expand Down Expand Up @@ -1247,14 +1247,14 @@ private long defragFile(final IndexedDiskElementDescriptor[] defragList, final l
* sorted by position in the dataFile.
* <p>
*
* @return IndexedDiskElementDescriptor[]
* @return Collection<IndexedDiskElementDescriptor>
*/
private IndexedDiskElementDescriptor[] createPositionSortedDescriptorList()
private Collection<IndexedDiskElementDescriptor> createPositionSortedDescriptorList()
{
final List<IndexedDiskElementDescriptor> defragList = new ArrayList<>(keyHash.values());
Collections.sort(defragList, (ded1, ded2) -> Long.compare(ded1.pos, ded2.pos));

return defragList.toArray(new IndexedDiskElementDescriptor[0]);
return defragList;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -314,9 +316,10 @@ private void processUpdate( final ICacheElement<K, V> item, final long requester
// IF LOCAL CLUSTER CONSISTENCY IS CONFIGURED
if (!fromCluster || fromCluster && remoteCacheServerAttributes.isLocalClusterConsistency())
{
final ICacheEventQueue<K, V>[] qlist = getEventQList( cacheDesc, requesterId );
log.debug( "qlist.length = {0}", qlist.length );
for (final ICacheEventQueue<K, V> element : qlist) {
final List<ICacheEventQueue<K,V>> qlist = getEventQList( cacheDesc, requesterId );
log.debug("qlist.size() = {0}", qlist.size());
for (final ICacheEventQueue<K, V> element : qlist)
{
element.addPutEvent( item );
}
}
Expand Down Expand Up @@ -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<K, V>[] qlist = getEventQList( cacheDesc, requesterId );
final List<ICacheEventQueue<K,V>> qlist = getEventQList( cacheDesc, requesterId );

for (final ICacheEventQueue<K, V> element : qlist) {
for (final ICacheEventQueue<K, V> element : qlist)
{
element.addRemoveEvent( key );
}
}
Expand Down Expand Up @@ -921,7 +925,7 @@ private void processRemoveAll( final String cacheName, final long requesterId )
// update registered listeners
if (!fromCluster || fromCluster && remoteCacheServerAttributes.isLocalClusterConsistency())
{
final ICacheEventQueue<K, V>[] qlist = getEventQList( cacheDesc, requesterId );
final List<ICacheEventQueue<K,V>> qlist = getEventQList( cacheDesc, requesterId );

for (final ICacheEventQueue<K, V> q : qlist)
{
Expand Down Expand Up @@ -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<K, V>[] qlist = getEventQList( cacheDesc, requesterId );
final List<ICacheEventQueue<K,V>> qlist = getEventQList( cacheDesc, requesterId );

for (final ICacheEventQueue<K, V> element : qlist) {
for (final ICacheEventQueue<K, V> element : qlist)
{
element.addDisposeEvent();
}
cacheManager.freeCache( cacheName );
Expand All @@ -1016,9 +1021,10 @@ public void release()
{
for (final CacheListeners<K, V> cacheDesc : cacheListenersMap.values())
{
final ICacheEventQueue<K, V>[] qlist = getEventQList( cacheDesc, 0 );
final List<ICacheEventQueue<K,V>> qlist = getEventQList( cacheDesc, 0 );

for (final ICacheEventQueue<K, V> element : qlist) {
for (final ICacheEventQueue<K, V> element : qlist)
{
element.addDisposeEvent();
}
}
Expand All @@ -1034,7 +1040,6 @@ public void release()
*/
protected CacheListeners<K, V> getCacheListeners( final String cacheName )
{

return cacheListenersMap.computeIfAbsent(cacheName, key -> {
final CompositeCache<K, V> cache = cacheManager.getCache(key);
return new CacheListeners<>( cache );
Expand All @@ -1050,7 +1055,6 @@ protected CacheListeners<K, V> getCacheListeners( final String cacheName )
*/
protected CacheListeners<K, V> getClusterListeners( final String cacheName )
{

return clusterListenersMap.computeIfAbsent(cacheName, key -> {
final CompositeCache<K, V> cache = cacheManager.getCache( cacheName );
return new CacheListeners<>( cache );
Expand All @@ -1069,40 +1073,14 @@ protected CacheListeners<K, V> getClusterListeners( final String cacheName )
* @param requesterId
* @return The eventQList value
*/
@SuppressWarnings("unchecked") // No generic arrays in java
private ICacheEventQueue<K, V>[] getEventQList( final CacheListeners<K, V> cacheListeners, final long requesterId )
private List<ICacheEventQueue<K, V>> getEventQList( final CacheListeners<K, V> cacheListeners, final long requesterId )
{
final ICacheEventQueue<K, V>[] 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<K, V> q = list[i];
if ( q.isWorking() && q.getListenerId() != requesterId )
{
count++;
}
else
{
list[i] = null;
}
}
if ( count == list.length )
{
// All qualified.
return list;
}
final List<ICacheEventQueue<K, V>> list = new ArrayList<>(cacheListeners.eventQMap.values());

// Returns only the qualified.
final ICacheEventQueue<K, V>[] qq = new ICacheEventQueue[count];
count = 0;
for (final ICacheEventQueue<K, V> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -147,12 +149,12 @@ public void testCheckForDedOverlaps_noOverlap()

final int numDescriptors = 5;
int pos = 0;
final IndexedDiskElementDescriptor[] sortedDescriptors = new IndexedDiskElementDescriptor[numDescriptors];
final List<IndexedDiskElementDescriptor> 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
Expand All @@ -175,13 +177,13 @@ public void testCheckForDedOverlaps_overlaps()

final int numDescriptors = 5;
int pos = 0;
final IndexedDiskElementDescriptor[] sortedDescriptors = new IndexedDiskElementDescriptor[numDescriptors];
final List<IndexedDiskElementDescriptor> 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
Expand Down

0 comments on commit 21ca693

Please sign in to comment.