[Zope-Checkins] SVN: Zope/trunk/src/Products/ZCatalog/plan.py Encapsulate priority map

Hanno Schlichting hannosch at hannosch.eu
Sun Aug 1 18:34:13 EDT 2010


Log message for revision 115361:
  Encapsulate priority map
  

Changed:
  U   Zope/trunk/src/Products/ZCatalog/plan.py

-=-
Modified: Zope/trunk/src/Products/ZCatalog/plan.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/plan.py	2010-08-01 22:16:11 UTC (rev 115360)
+++ Zope/trunk/src/Products/ZCatalog/plan.py	2010-08-01 22:34:13 UTC (rev 115361)
@@ -25,10 +25,24 @@
 REPORTS_LOCK = allocate_lock()
 REPORTS = {}
 
-PRIORITYMAP_LOCK = allocate_lock()
-PRIORITYMAP = {}
+class ThreadDict(object):
 
+    @classmethod
+    def get_entry(cls, key):
+        return cls.value.get(key, None)
 
+    @classmethod
+    def set_entry(cls, key, value):
+        with cls.lock:
+            cls.value[key] = value
+
+
+class PriorityMap(ThreadDict):
+
+    lock = allocate_lock()
+    value = {}
+
+
 class ValueIndexes(object):
 
     lock = allocate_lock()
@@ -128,6 +142,7 @@
         self.catalog = catalog
         self.query = query
         self.key = make_key(catalog, query)
+        self.benchmark = {}
         self.threshold = threshold
         self.cid = self.get_id()
         self.init_timer()
@@ -148,16 +163,8 @@
         self.stop_time = None
         self.duration = None
 
-    def benchmark(self):
-        # holds the benchmark of each index
-        bm = PRIORITYMAP.get(self.key, None)
-        if bm is None:
-            with PRIORITYMAP_LOCK:
-                PRIORITYMAP[self.key] = {}
-        return bm
-
     def plan(self):
-        benchmark = self.benchmark()
+        benchmark = PriorityMap.get_entry(self.key)
         if not benchmark:
             return None
 
@@ -187,10 +194,9 @@
             name=name, duration=current - start_time, num=length))
 
         # remember index's hits, search time and calls
-        benchmark = self.benchmark()
+        benchmark = self.benchmark
         if name not in benchmark:
-            with PRIORITYMAP_LOCK:
-                benchmark[name] = Benchmark(num=length, duration=dt, hits=1)
+            benchmark[name] = Benchmark(num=length, duration=dt, hits=1)
         else:
             num, duration, hits = benchmark[name]
             num = int(((num * hits) + length) / float(hits + 1))
@@ -199,19 +205,12 @@
             if hits % REFRESH_RATE == 0:
                 hits = 0
             hits += 1
-            with PRIORITYMAP_LOCK:
-                benchmark[name] = Benchmark(num, duration, hits)
+            benchmark[name] = Benchmark(num, duration, hits)
 
     def stop(self):
         self.end_time = time.time()
         self.duration = self.end_time - self.start_time
-
-        key = self.key
-        benchmark = self.benchmark()
-
-        with PRIORITYMAP_LOCK:
-            PRIORITYMAP[key] = benchmark
-
+        PriorityMap.set_entry(self.key, self.benchmark)
         self.log()
 
     def log(self):



More information about the Zope-Checkins mailing list