[CalendarServer-changes] [6714] CalendarServer/trunk/twistedcaldav

source_changes at macosforge.org source_changes at macosforge.org
Mon Jan 3 22:17:53 PST 2011


Revision: 6714
          http://trac.macosforge.org/projects/calendarserver/changeset/6714
Author:   sagen at apple.com
Date:     2011-01-03 22:17:47 -0800 (Mon, 03 Jan 2011)
Log Message:
-----------
Like the XML directory implementation, the XML augments implementation now occasionally stats the file(s) and only re-parses them when a change is detected.

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/directory/augment.py
    CalendarServer/trunk/twistedcaldav/directory/test/test_augment.py
    CalendarServer/trunk/twistedcaldav/directory/xmlfile.py
    CalendarServer/trunk/twistedcaldav/stdconfig.py

Modified: CalendarServer/trunk/twistedcaldav/directory/augment.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/augment.py	2011-01-03 20:14:59 UTC (rev 6713)
+++ CalendarServer/trunk/twistedcaldav/directory/augment.py	2011-01-04 06:17:47 UTC (rev 6714)
@@ -208,11 +208,15 @@
     XMLFile based augment database implementation.
     """
 
-    def __init__(self, xmlFiles, cacheTimeout=30):
+    def __init__(self, xmlFiles, statSeconds=15):
 
         super(AugmentXMLDB, self).__init__()
         self.xmlFiles = [fullServerPath(config.DataRoot, path) for path in xmlFiles]
-        self.cacheTimeout = cacheTimeout * 60 # Value is mins we want secs
+        self.xmlFileStats = { }
+        for path in self.xmlFiles:
+            self.xmlFileStats[path] = (0, 0) # mtime, size
+
+        self.statSeconds = statSeconds # Don't stat more often than this value
         self.lastCached = 0
         self.db = {}
 
@@ -246,7 +250,7 @@
         """
         
         # May need to re-cache
-        if self.lastCached + self.cacheTimeout <= time.time():
+        if time.time() - self.lastCached > self.statSeconds:
             self.refresh()
             
         return succeed(self.db.get(uid))
@@ -433,6 +437,18 @@
         self.removeAugmentRecords(self.db.keys())
         return succeed(None)
 
+    def _shouldReparse(self, xmlFile):
+        """
+        Check to see whether the given file has been modified since we last
+        parsed it.
+        """
+        oldModTime, oldSize = self.xmlFileStats.get(xmlFile, (0, 0))
+        newModTime = os.path.getmtime(xmlFile)
+        newSize = os.path.getsize(xmlFile)
+        if (oldModTime != newModTime) or (oldSize != newSize):
+            return True
+        return False
+
     def _parseXML(self):
         """
         Parse self.xmlFiles into AugmentRecords.
@@ -446,8 +462,14 @@
         allMissing = True
         for xmlFile in self.xmlFiles:
             if os.path.exists(xmlFile):
-                # Creating a parser does the parse
-                XMLAugmentsParser(xmlFile, results)
+                # Compare previously seen modification time and size of each
+                # xml file.  If unchanged, skip.
+                if self._shouldReparse(xmlFile):
+                    # Creating a parser does the parse
+                    XMLAugmentsParser(xmlFile, results)
+                    newModTime = os.path.getmtime(xmlFile)
+                    newSize = os.path.getsize(xmlFile)
+                    self.xmlFileStats[xmlFile] = (newModTime, newSize)
                 allMissing = False
 
         if allMissing:
@@ -457,7 +479,7 @@
                 enabledForCalendaring=True,
                 enabledForAddressBooks=True,
             )
-        
+
         return results
 
 class AugmentADAPI(AugmentDB, AbstractADBAPIDatabase):

Modified: CalendarServer/trunk/twistedcaldav/directory/test/test_augment.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/test/test_augment.py	2011-01-03 20:14:59 UTC (rev 6713)
+++ CalendarServer/trunk/twistedcaldav/directory/test/test_augment.py	2011-01-04 06:17:47 UTC (rev 6714)
@@ -274,6 +274,17 @@
             yield self._checkRecord(newdb, item)
         yield self._checkRecord(newdb, testModifyRecords[0])
 
+    def test_shouldReparse(self):
+        """
+        Verify that a change to the file will get noticed
+        """
+        newxmlfile = FilePath(self.mktemp())
+        FilePath(xmlFile).copyTo(newxmlfile)
+        db = AugmentXMLDB((newxmlfile.path,))
+        self.assertFalse(db._shouldReparse(newxmlfile.path)) # No need to parse
+        newxmlfile.setContent("") # Change the file
+        self.assertTrue(db._shouldReparse(newxmlfile.path)) # Need to parse
+
 class AugmentSqliteTests(AugmentTests, AugmentTestsMixin):
 
     def _db(self, dbpath=None):

Modified: CalendarServer/trunk/twistedcaldav/directory/xmlfile.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/xmlfile.py	2011-01-03 20:14:59 UTC (rev 6713)
+++ CalendarServer/trunk/twistedcaldav/directory/xmlfile.py	2011-01-04 06:17:47 UTC (rev 6714)
@@ -69,12 +69,14 @@
                 self.recordType_resources,
             ),
             'realmName' : '/Search',
+            'statSeconds' : 15,
         }
         ignored = None
         params = self.getParams(params, defaults, ignored)
 
         self._recordTypes = params['recordTypes']
         self.realmName = params['realmName']
+        self.statSeconds = params['statSeconds']
 
         super(XMLDirectoryService, self).__init__()
 
@@ -138,15 +140,15 @@
         XMLAccountRecords as returned by XMLAccountsParser, returns a list
         of XMLAccountRecords.
 
-        The XML file is only stat'ed at most every 60 seconds, and is only
-        reparsed if it's been modified.
+        The XML file is only stat'ed at most every self.statSeconds, and is
+        only reparsed if it's been modified.
 
         FIXME: don't return XMLAccountRecords, and have any code in this module
         which currently does work with XMLAccountRecords, modify such code to
         use XMLDirectoryRecords instead.
         """
         currentTime = time()
-        if self._alwaysStat or currentTime - self._lastCheck > 60:
+        if self._alwaysStat or currentTime - self._lastCheck > self.statSeconds:
             self.xmlFile.restat()
             self._lastCheck = currentTime
             fileInfo = (self.xmlFile.getmtime(), self.xmlFile.getsize())

Modified: CalendarServer/trunk/twistedcaldav/stdconfig.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/stdconfig.py	2011-01-03 20:14:59 UTC (rev 6713)
+++ CalendarServer/trunk/twistedcaldav/stdconfig.py	2011-01-04 06:17:47 UTC (rev 6714)
@@ -43,6 +43,7 @@
     "twistedcaldav.directory.xmlfile.XMLDirectoryService": {
         "xmlFile": "accounts.xml",
         "recordTypes": ("users", "groups"),
+        "statSeconds" : 15,
     },
     "twistedcaldav.directory.appleopendirectory.OpenDirectoryService": {
         "node": "/Search",
@@ -63,6 +64,7 @@
 DEFAULT_AUGMENT_PARAMS = {
     "twistedcaldav.directory.augment.AugmentXMLDB": {
         "xmlFiles": ["augments.xml",],
+        "statSeconds" : 15,
     },
     "twistedcaldav.directory.augment.AugmentSqliteDB": {
         "dbpath": "augments.sqlite",
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110103/d1b748f7/attachment.html>


More information about the calendarserver-changes mailing list