Author: j16sdiz
Date: 2008-06-04 08:53:46 +0000 (Wed, 04 Jun 2008)
New Revision: 20192

Modified:
   
branches/saltedhashstore/freenet/src/freenet/store/SaltedHashFreenetStore.java
Log:
use java.util.concurrent.atomic.*


Modified: 
branches/saltedhashstore/freenet/src/freenet/store/SaltedHashFreenetStore.java
===================================================================
--- 
branches/saltedhashstore/freenet/src/freenet/store/SaltedHashFreenetStore.java  
    2008-06-04 08:37:33 UTC (rev 20191)
+++ 
branches/saltedhashstore/freenet/src/freenet/store/SaltedHashFreenetStore.java  
    2008-06-04 08:53:46 UTC (rev 20192)
@@ -16,6 +16,7 @@
 import java.util.Map;
 import java.util.Random;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
@@ -118,7 +119,7 @@
                        Entry entry = probeEntry(routingKey);

                        if (entry == null) {
-                               incMisses();
+                               misses.incrementAndGet();
                                return null;
                        }

@@ -126,13 +127,13 @@

                        try {
                                StorableBlock block = 
entry.getStorableBlock(routingKey, fullKey);
-                               incHits();
+                               hits.incrementAndGet();
                                if (updateBloom && !checkBloom)
                                        
bloomFilter.updateFilter(getDigestedRoutingKey(routingKey), false);
                                return block;
                        } catch (KeyVerifyException e) {
                                Logger.minor(this, "key verification 
exception", e);
-                               incMisses();
+                               misses.incrementAndGet();
                                return null;
                        }
                } finally {
@@ -158,7 +159,7 @@
                if (entry == null && prevStoreSize != 0)
                        entry = probeEntry0(routingKey, prevStoreSize);
                if (checkBloom && entry == null)
-                       incBloomFalsePos();
+               bloomFalsePos.incrementAndGet();

                return entry;
        }
@@ -222,7 +223,7 @@
                                                
bloomFilter.updateFilter(getDigestedRoutingKey(routingKey), syncBloom);
                                        Entry entry = new Entry(routingKey, 
header, data);
                                        writeEntry(entry, oldOffset);
-                                       incWrites();
+                                       writes.incrementAndGet();
                                        return;
                                } finally {
                                        unlockEntry(oldOffset);
@@ -245,8 +246,8 @@
                                                if (updateBloom)
                                                        
bloomFilter.updateFilter(getDigestedRoutingKey(routingKey), syncBloom);
                                                writeEntry(entry, offset[i]);
-                                               incWrites();
-                                               incKeyCount();
+                                               writes.incrementAndGet();
+                                               keyCount.incrementAndGet();
                                                return;
                                        }
                                } finally {
@@ -265,7 +266,7 @@
                                if (updateBloom)
                                        
bloomFilter.updateFilter(getDigestedRoutingKey(routingKey), syncBloom);
                                writeEntry(entry, offset[0]);
-                               incWrites();
+                               writes.incrementAndGet();
                        } finally {
                                unlockEntry(offset[0]);
                        }
@@ -750,8 +751,6 @@
                        salt = new byte[0x10];
                        random.nextBytes(salt);

-                       keyCount = 0;
-
                        writeConfigFile();
                } else {
                        // try to load
@@ -761,7 +760,7 @@

                        storeSize = raf.readLong();
                        prevStoreSize = raf.readLong();
-                       keyCount = raf.readLong();
+                       keyCount.set(raf.readLong());
                        raf.readLong();

                        raf.close();
@@ -782,7 +781,7 @@

                        raf.writeLong(storeSize);
                        raf.writeLong(prevStoreSize);
-                       raf.writeLong(keyCount);
+                       raf.writeLong(keyCount.get());
                        raf.writeLong(0);

                        raf.close();
@@ -1012,7 +1011,7 @@
                                                        if 
(Arrays.equals(digestedRoutingKey, digestedRoutingKey2)) {
                                                                // assume same 
routing key, drop this as duplicate
                                                                
freeOffset(offset);
-                                                               decKeyCount();
+                                                               
keyCount.decrementAndGet();
                                                                
droppedEntries++;

                                                                if (logDEBUG)
@@ -1029,7 +1028,7 @@
                                                                        + " 
queued");
                                                        
writeOldItem(oldItemsFC, entry);
                                                        freeOffset(offset);
-                                                       decKeyCount();
+                                                       
keyCount.decrementAndGet();
                                                }
                                        } finally {
                                                // unlock all entries
@@ -1086,7 +1085,7 @@
                                                                        
.debug(this, "Put back old item: "
                                                                                
+ HexUtil.bytesToHex(entry.digestedRoutingKey));
                                                        writeEntry(entry, 
newOffset[i]);
-                                                       incKeyCount();
+                                                       
keyCount.incrementAndGet();
                                                        resolvedEntries++;
                                                        continue LOOP_ITEMS;
                                                }
@@ -1307,78 +1306,35 @@
        }

        // ------------- Statistics (a.k.a. lies)
-       private final Object statLock = new Object();
-       private long hits;
-       private long misses;
-       private long writes;
-       private long keyCount;
-       private long bloomFalsePos;
+       private AtomicLong hits = new AtomicLong();
+       private AtomicLong misses = new AtomicLong();
+       private AtomicLong writes = new AtomicLong();
+       private AtomicLong keyCount = new AtomicLong();
+       private AtomicLong bloomFalsePos = new AtomicLong();

        public long hits() {
-               synchronized (statLock) {
-                       return hits;
-               }
+               return hits.get();
        }

-       private void incHits() {
-               Logger.debug(this, "hit");
-               synchronized (statLock) {
-                       hits++;
-               }
-       }
-
        public long misses() {
-               synchronized (statLock) {
-                       return misses;
-               }
+               return misses.get();
        }

-       private void incMisses() {
-               Logger.debug(this, "miss");
-               synchronized (statLock) {
-                       misses++;
-               }
-       }
-
        public long writes() {
-               synchronized (statLock) {
-                       return writes;
-               }
+               return writes.get();
        }

-       private void incWrites() {
-               Logger.debug(this, "write");
-               synchronized (statLock) {
-                       writes++;
-               }
-       }
-
-       private void incKeyCount() {
-               synchronized (statLock) {
-                       keyCount++;
-               }
-       }
-
-       private void decKeyCount() {
-               synchronized (statLock) {
-                       keyCount--;
-               }
-       }
-
        public long keyCount() {
-               return keyCount;
+               return keyCount.get();
        }

        public long getMaxKeys() {
-               return storeSize;
+               configLock.readLock().lock();
+               long _storeSize = storeSize;
+               configLock.readLock().unlock();
+               return _storeSize;      
        }

-       private void incBloomFalsePos() {
-               synchronized (statLock) {
-                       bloomFalsePos++;
-               }
-       }
-
        // ------------- Migration
        public void migrationFrom(File storeFile, File keyFile) {
                setBloomSync(false); // don't sync the bloom filter


Reply via email to