Change to blogs.gnome.org



I'm committing attached patch to blogs-web. Currently blogs.gnome.org
(NewsBruiser) reads the configuration files of every blog. This is done
for *all* requests. The patch avoids that.
This makes attachment serving twice as fast (although my goal is to
serve the attachments statically). I put some background info here:
http://blogs.gnome.org/portal/ovitters

I've tested this on my machine and I'll test it again after I updated
the server. However, please revert if there are problems.

-- 
Regards,
Olav
Index: NBConfig.py
===================================================================
RCS file: /cvs/gnome/blogs-web/nb/NBConfig.py,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 NBConfig.py
--- NBConfig.py	9 Jun 2005 17:53:49 -0000	1.1.1.1
+++ NBConfig.py	26 Dec 2005 00:09:50 -0000
@@ -241,39 +241,57 @@ class NBConfig: 
         "Returns a list of all notebooks, sorted by ordinal."
         if not hasattr(self, 'notebookList') or self.notebookList == None:
             self.notebookList = []
-            for notebook in self.notebooks.values():
-                #Put the notebook in its designated place in the list
-                #of notebooks. If there's already a notebook with that
-                #ordinal, increment the ordinal until there's space.
-                myOrdinal = notebook.getOrdinal()     
-                while len(self.notebookList) <= myOrdinal:
-                    self.notebookList.append(None)
-                while len(self.notebookList) > myOrdinal and self.notebookList[myOrdinal] != None:
-                    myOrdinal = myOrdinal + 1
-                    if len(self.notebookList) <= myOrdinal:
+            do_slow = True
+            if os.path.exists(os.path.join(self.notebookDir, '.ordinals')):
+                notebook_order = file(os.path.join(self.notebookDir, '.ordinals'), "r").read().split("\n")
+                notebook_order_dict = dict(zip(notebook_order, range(0, len(notebook_order))))
+                notebookList = self.notebooks.values()
+                do_slow = False
+                try:
+                    notebookList.sort(lambda x,y: cmp(notebook_order_dict[x.name], notebook_order_dict[y.name]))
+                except KeyError:
+                    do_slow = True
+
+                if not do_slow:
+                    self.notebookList = notebookList
+                
+            if do_slow:
+                for notebook in self.notebooks.values():
+                    #Put the notebook in its designated place in the list
+                    #of notebooks. If there's already a notebook with that
+                    #ordinal, increment the ordinal until there's space.
+                    myOrdinal = notebook.getOrdinal()     
+                    while len(self.notebookList) <= myOrdinal:
                         self.notebookList.append(None)
-                if myOrdinal != notebook.getOrdinal():
-                    notebook.setOrdinal(myOrdinal)
-                self.notebookList[myOrdinal] = notebook        
-
-            #The following things should never happen unless someone
-            #has been messing with the config file or there was a
-            #problem with the ordinal change CGI.
-
-            #Make sure there are no gaps in the list of notebooks.
-            c = 1
-            while c:
-                try:                    
-                    self.notebookList.remove(None)
-                except ValueError:
-                    c = 0
-
-            #Make sure the skip in ordinals is never more than one.
-            ordinalShouldBe = -1
-            for i in self.notebookList:
-                ordinalShouldBe = ordinalShouldBe + 1
-                if int(i.getOrdinal()) > ordinalShouldBe:
-                    i.setOrdinal(ordinalShouldBe)
+                    while len(self.notebookList) > myOrdinal and self.notebookList[myOrdinal] != None:
+                        myOrdinal = myOrdinal + 1
+                        if len(self.notebookList) <= myOrdinal:
+                            self.notebookList.append(None)
+                    if myOrdinal != notebook.getOrdinal():
+                        notebook.setOrdinal(myOrdinal)
+                    self.notebookList[myOrdinal] = notebook        
+
+                #The following things should never happen unless someone
+                #has been messing with the config file or there was a
+                #problem with the ordinal change CGI.
+
+                #Make sure there are no gaps in the list of notebooks.
+                c = 1
+                while c:
+                    try:                    
+                        self.notebookList.remove(None)
+                    except ValueError:
+                        c = 0
+
+                #Make sure the skip in ordinals is never more than one.
+                ordinalShouldBe = -1
+                for i in self.notebookList:
+                    ordinalShouldBe = ordinalShouldBe + 1
+                    if int(i.getOrdinal()) > ordinalShouldBe:
+                        i.setOrdinal(ordinalShouldBe)
+                
+                # Save ordinals cache
+                file(os.path.join(self.notebookDir, '.ordinals'), "w").write("\n".join([x.name for x in self.notebookList]))
 
         return self.notebookList
 
@@ -338,6 +356,9 @@ class NBConfig: 
         for i in range(0, len(notebooks)):
             if notebooks[i].getOrdinal() != i:
                 notebooks[i].setOrdinal(i)
+
+        # Save ordinals cache
+        file(os.path.join(self.notebookDir, '.ordinals'), "w").write("\n".join([x.name for x in self.notebookList]))
 
     def selfTest(self):
         "Self test for NBConfig class."
Index: Notebook.py
===================================================================
RCS file: /cvs/gnome/blogs-web/nb/Notebook.py,v
retrieving revision 1.4
diff -u -p -r1.4 Notebook.py
--- Notebook.py	16 Sep 2005 07:44:06 -0000	1.4
+++ Notebook.py	26 Dec 2005 00:09:50 -0000
@@ -89,15 +89,32 @@ class Notebook(HtmlGenerator, Context, T
         the options defined in the .configuration file of the notebook,
         and the theme (if any) set for the notebook."""
         self.nbconfig = nbconfig
-        self.conf = { const.NAME_CONFIG_KEY: name,
-                      const.WORKING_DIR_CONFIG_KEY: nbconfig.notebookDir }
-        self.theme = {}
-        self.templates = {}
         self.name = name
+        self.did_init = 0
         self.optionConfig = Options.cache.get(self.nbconfig)
         if not self.optionConfig:
             self.optionConfig = NBOptionConfig(self.nbconfig)
             Options.cache[self.nbconfig] = self.optionConfig
+
+    def __getattr__(self, name):
+        """Helper routine. Checks if init2 initialization was done and if not
+        runs init2. Used to delay reading the configuration file until it is 
+        needed. Together with the ordinal cache it provides a hefty speedup."""
+        if not self.did_init and name[0] != "_":
+            self.did_init = 1
+            self.init2()
+            return getattr(self, name)
+        else:
+            raise AttributeError
+
+    def init2(self):
+        name = self.name
+        nbconfig = self.nbconfig
+        
+        self.conf = { const.NAME_CONFIG_KEY: name,
+                      const.WORKING_DIR_CONFIG_KEY: nbconfig.notebookDir }
+        self.theme = {}
+        self.templates = {}
 
         self.readConfiguration()
 


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