[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [bridgedb/master] 5482 - Do not update database inside query loop.



commit 64230a4ecc6bc97932d70cddd0871d3d2108bab6
Author: aagbsn <aagbsn@xxxxxxxx>
Date:   Mon Nov 5 13:37:30 2012 -0800

    5482 - Do not update database inside query loop.
    
    Fixes a bug where updated BridgeHistory objects are not stored in the
    db.
---
 lib/bridgedb/Stability.py |   21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/lib/bridgedb/Stability.py b/lib/bridgedb/Stability.py
index bdc4a4a..0f217a7 100644
--- a/lib/bridgedb/Stability.py
+++ b/lib/bridgedb/Stability.py
@@ -174,9 +174,12 @@ def addOrUpdateBridgeHistory(bridge, timestamp):
     
     # Increment total weighted time for all bridges by seconds since the last
     # status was published. Note, capped at 1 hour
+    toupdate = []
+
     for bh in db.getAllBridgeHistory():
         bh.weightedTime += secondsSinceLastStatusPublication
-        db.updateIntoBridgeHistory(bh)
+        toupdate.append(bh)
+    [ db.updateIntoBridgeHistory(bh) for bh in toupdate ]
 
     # For Running Bridges only:
     # compare the stored history against the descriptor and see if the
@@ -207,23 +210,25 @@ def addOrUpdateBridgeHistory(bridge, timestamp):
 def discountAndPruneBridgeHistories(discountUntilMillis):
     db = bridgedb.Storage.getDB()
     bhToRemove = []
+    bhToUpdate = []
     # Just check the first item to see if we're anywhere close yet
     sample = None
     for y in db.getAllBridgeHistory():
         sample = y
         break
-    if not sample: return 0
+    if not sample: return
 
-    if discountUntilMillis - sample.lastDiscountedHistoryValues < discountIntervalMillis:
-        return 0
+    if discountUntilMillis - sample.lastDiscountedHistoryValues \
+            < discountIntervalMillis: return
     for bh in db.getAllBridgeHistory():
         # discount previous values by factor of 0.95 every 12 hours
         bh.discountWeightedFractionalUptimeAndWeightedTime(discountUntilMillis)
 
-        if (bh.weightedTime != 0 and (bh.weightedUptime * 10000 / bh.weightedTime) < 1):
-            bhToRemove.append(k)
+        if (bh.weightedTime != 0 and bh.weightedUptime != 0 and (bh.weightedUptime * 10000 / bh.weightedTime) < 1):
+            logging.debug("Removing bridge from history: %s" % bh.fingerprint)
+            bhToRemove.append(bh.fingerprint)
         else:
-            db.updateIntoBridgeHistory(bh)
-
+            bhToUpdate.append(bh)
+    for k in bhToUpdate: db.updateIntoBridgeHistory(k)
     # prune items
     for k in bhToRemove: db.delBridgeHistory(k)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits