[CalendarServer-changes] [5113] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sat Feb 13 13:12:11 PST 2010


Revision: 5113
          http://trac.macosforge.org/projects/calendarserver/changeset/5113
Author:   cdaboo at apple.com
Date:     2010-02-13 13:12:10 -0800 (Sat, 13 Feb 2010)
Log Message:
-----------
Change augment add api so there is no need to differentiate a new record vs an update.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/loadaugmentdb.py
    CalendarServer/trunk/calendarserver/tools/manageaugments.py
    CalendarServer/trunk/twistedcaldav/directory/augment.py
    CalendarServer/trunk/twistedcaldav/directory/test/test_augment.py
    CalendarServer/trunk/twistedcaldav/directory/xmlaugmentsparser.py

Modified: CalendarServer/trunk/calendarserver/tools/loadaugmentdb.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/loadaugmentdb.py	2010-02-13 19:24:59 UTC (rev 5112)
+++ CalendarServer/trunk/calendarserver/tools/loadaugmentdb.py	2010-02-13 21:12:10 UTC (rev 5113)
@@ -165,6 +165,7 @@
         updated = 0
         removed = 0
         if dbxml:
+            yield augment.AugmentService.addAugmentRecords(dbxml.db.values(), )
             add_records = list()
             modify_records = list()
             for record in dbxml.db.values():
@@ -172,18 +173,12 @@
                     modify_records.append(record)
                 else:
                     add_records.append(record)
-            yield augment.AugmentService.addAugmentRecords(add_records, False)
             added = len(add_records)
-
-            yield augment.AugmentService.addAugmentRecords(modify_records, True)
             updated = len(modify_records)
 
             remove_uids = uids.difference(dbxml.db.keys())
             yield augment.AugmentService.removeAugmentRecords(remove_uids)
             removed = len(remove_uids)
-        else:
-            yield augment.AugmentService.clean()
-            removed = len(uids)
             
         print "Changes:"
         print "  Added: %d" % (added,)

Modified: CalendarServer/trunk/calendarserver/tools/manageaugments.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/manageaugments.py	2010-02-13 19:24:59 UTC (rev 5112)
+++ CalendarServer/trunk/calendarserver/tools/manageaugments.py	2010-02-13 21:12:10 UTC (rev 5113)
@@ -127,11 +127,11 @@
         if args[0] == "add":
             if not options.node:
                 parser.error("Partition node must be specified when adding")
-            yield augment.AugmentService.addAugmentRecords([makeRecord(uid, options) for uid in uids], False)
+            yield augment.AugmentService.addAugmentRecords([makeRecord(uid, options) for uid in uids])
             for uid in uids:
                 print "Added uid '%s' to augment database" % (uid,)
         elif args[0] == "modify":
-            yield augment.AugmentService.addAugmentRecords([makeRecord(uid, options) for uid in uids], True)
+            yield augment.AugmentService.addAugmentRecords([makeRecord(uid, options) for uid in uids])
             for uid in uids:
                 print "Modified uid '%s' in augment database" % (uid,)
         elif args[0] == "remove":

Modified: CalendarServer/trunk/twistedcaldav/directory/augment.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/augment.py	2010-02-13 19:24:59 UTC (rev 5112)
+++ CalendarServer/trunk/twistedcaldav/directory/augment.py	2010-02-13 21:12:10 UTC (rev 5113)
@@ -104,14 +104,12 @@
         
         raise NotImplementedError("Child class must define this.")
 
-    def addAugmentRecords(self, records, update=False):
+    def addAugmentRecords(self, records):
         """
         Add an AugmentRecord to the DB.
 
         @param record: augment records to add
         @type record: C{list} of L{AugmentRecord}
-        @param update: C{True} if changing an existing record
-        @type update: C{bool}
         
         @return: L{Deferred}
         """
@@ -133,9 +131,21 @@
     def refresh(self):
         """
         Refresh any cached data.
+        
+        @return: L{Deferred}
         """
-        pass
+
+        return succeed(None)
+    
+    def clean(self):
+        """
+        Remove all records.
         
+        @return: L{Deferred}
+        """
+
+        raise NotImplementedError("Child class must define this.")
+        
 AugmentService = AugmentDB()   # Global augment service
 
 
@@ -208,7 +218,7 @@
             
         return succeed(self.db.get(uid))
 
-    def addAugmentRecords(self, records, update=False):
+    def addAugmentRecords(self, records):
         """
         Add an AugmentRecord to the DB.
 
@@ -220,14 +230,23 @@
         @return: L{Deferred}
         """
 
-        if update:
+        # Look at each record and determine whether it is new or a modify
+        new_records = list()
+        existing_records = list() 
+        for record in records:
+            (existing_records if record.uid in self.db else new_records).append(record)
+
+        if existing_records:
             # Now look at each file and modify the UIDs
             for xmlFile in self.xmlFiles:
-                self._doModifyInFile(xmlFile, records)
-        else:
+                self._doModifyInFile(xmlFile, existing_records)
+
+        if new_records:
             # Add to first file in list
-            self._doAddToFile(self.xmlFiles[0], records)
+            self._doAddToFile(self.xmlFiles[0], new_records)
 
+        return succeed(None)
+
     def _doAddToFile(self, xmlfile, records):
     
         _ignore_etree, augments_node = readXML(xmlfile)
@@ -327,6 +346,16 @@
             log.error("Failed to parse XML augments file during cache refresh - ignoring")
         self.lastCached = time.time()
 
+        return succeed(None)
+
+    def clean(self):
+        """
+        Remove all records.
+        """
+
+        self.removeAugmentRecords(self.db.keys())
+        return succeed(None)
+        
     def _parseXML(self):
         
         # Do each file
@@ -395,40 +424,17 @@
             returnValue(record)
 
     @inlineCallbacks
-    def addAugmentRecords(self, records, update=False):
+    def addAugmentRecords(self, records):
 
         for record in records:
-            partitionid = (yield self._getPartitionID(record.hostedAt))
             
+            results = (yield self.query("select UID from AUGMENTS where UID = :1", (record.uid,)))
+            update = len(results) > 0
+
             if update:
-                yield self.execute(
-                    """update AUGMENTS set
-                    (UID, ENABLED, PARTITIONID, CALENDARING, ADDRESSBOOKS, AUTOSCHEDULE) =
-                    (:1, :2, :3, :4, :5, :6) where UID = :7""",
-                    (
-                        record.uid,
-                        "T" if record.enabled else "F",
-                        partitionid,
-                        "T" if record.enabledForCalendaring else "F",
-                        "T" if record.enabledForAddressBooks else "F",
-                        "T" if record.autoSchedule else "F",
-                        record.uid,
-                    )
-                )
+                yield self._modifyRecord(record)
             else:
-                yield self.execute(
-                    """insert into AUGMENTS
-                    (UID, ENABLED, PARTITIONID, CALENDARING, ADDRESSBOOKS, AUTOSCHEDULE)
-                    values (:1, :2, :3, :4, :5, :6)""",
-                    (
-                        record.uid,
-                        "T" if record.enabled else "F",
-                        partitionid,
-                        "T" if record.enabledForCalendaring else "F",
-                        "T" if record.enabledForAddressBooks else "F",
-                        "T" if record.autoSchedule else "F",
-                    )
-                )
+                yield self._addRecord(record)
 
     @inlineCallbacks
     def removeAugmentRecords(self, uids):
@@ -436,6 +442,13 @@
         for uid in uids:
             yield self.execute("delete from AUGMENTS where UID = :1", (uid,))
 
+    def clean(self):
+        """
+        Remove all records.
+        """
+
+        return self.execute("delete from AUGMENTS", ())
+        
     @inlineCallbacks
     def _getPartitionID(self, hostedat, createIfMissing=True):
         
@@ -523,6 +536,26 @@
         ADBAPISqliteMixin.__init__(self)
         AugmentADAPI.__init__(self, "Augments", "sqlite3", (fullServerPath(config.DataRoot, dbpath),))
 
+    @inlineCallbacks
+    def _addRecord(self, record):
+        partitionid = (yield self._getPartitionID(record.hostedAt))
+        yield self.execute(
+            """insert or replace into AUGMENTS
+            (UID, ENABLED, PARTITIONID, CALENDARING, ADDRESSBOOKS, AUTOSCHEDULE)
+            values (:1, :2, :3, :4, :5, :6)""",
+            (
+                record.uid,
+                "T" if record.enabled else "F",
+                partitionid,
+                "T" if record.enabledForCalendaring else "F",
+                "T" if record.enabledForAddressBooks else "F",
+                "T" if record.autoSchedule else "F",
+            )
+        )
+
+    def _modifyRecord(self, record):
+        return self._addRecord(record)
+
 class AugmentPostgreSQLDB(ADBAPIPostgreSQLMixin, AugmentADAPI):
     """
     PostgreSQL based augment database implementation.
@@ -533,3 +566,37 @@
         ADBAPIPostgreSQLMixin.__init__(self)
         AugmentADAPI.__init__(self, "Augments", "pgdb", (), host=host, database=database, user=user, password=password,)
 
+    @inlineCallbacks
+    def _addRecord(self, record):
+        partitionid = (yield self._getPartitionID(record.hostedAt))
+        yield self.execute(
+            """insert into AUGMENTS
+            (UID, ENABLED, PARTITIONID, CALENDARING, ADDRESSBOOKS, AUTOSCHEDULE)
+            values (:1, :2, :3, :4, :5, :6)""",
+            (
+                record.uid,
+                "T" if record.enabled else "F",
+                partitionid,
+                "T" if record.enabledForCalendaring else "F",
+                "T" if record.enabledForAddressBooks else "F",
+                "T" if record.autoSchedule else "F",
+            )
+        )
+
+    @inlineCallbacks
+    def _modifyRecord(self, record):
+        partitionid = (yield self._getPartitionID(record.hostedAt))
+        yield self.execute(
+            """update AUGMENTS set
+            (UID, ENABLED, PARTITIONID, CALENDARING, ADDRESSBOOKS, AUTOSCHEDULE) =
+            (:1, :2, :3, :4, :5, :6) where UID = :7""",
+            (
+                record.uid,
+                "T" if record.enabled else "F",
+                partitionid,
+                "T" if record.enabledForCalendaring else "F",
+                "T" if record.enabledForAddressBooks else "F",
+                "T" if record.autoSchedule else "F",
+                record.uid,
+            )
+        )

Modified: CalendarServer/trunk/twistedcaldav/directory/test/test_augment.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/test/test_augment.py	2010-02-13 19:24:59 UTC (rev 5112)
+++ CalendarServer/trunk/twistedcaldav/directory/test/test_augment.py	2010-02-13 21:12:10 UTC (rev 5113)
@@ -16,11 +16,12 @@
 
 from twistedcaldav.test.util import TestCase
 from twistedcaldav.directory.augment import AugmentXMLDB, AugmentSqliteDB,\
-    AugmentPostgreSQLDB
+    AugmentPostgreSQLDB, AugmentRecord
 from twisted.internet.defer import inlineCallbacks
 from twistedcaldav.directory.xmlaugmentsparser import XMLAugmentsParser
 import cStringIO
 import os
+from twisted.python.filepath import FilePath
 
 xmlFile = os.path.join(os.path.dirname(__file__), "augments-test.xml")
 xmlFileDefault = os.path.join(os.path.dirname(__file__), "augments-test-default.xml")
@@ -37,6 +38,15 @@
 
 testRecordDefault = {"uid":"A4318887-F2C7-4A70-9056-B88CC8DB26F1", "enabled":True,  "hostedAt":"00001", "enabledForCalendaring":True, "enabledForAddressBooks":True, "autoSchedule":False}
 
+testAddRecords = (
+    {"uid":"D11F03A0-97EA-48AF-9A6C-FAC7F3975767", "enabled":True,  "hostedAt":"", "enabledForCalendaring":False, "enabledForAddressBooks":False, "autoSchedule":False},
+)
+
+testModifyRecords = (
+    {"uid":"D11F03A0-97EA-48AF-9A6C-FAC7F3975767", "enabled":True,  "hostedAt":"", "enabledForCalendaring":True, "enabledForAddressBooks":True, "autoSchedule":False},
+)
+
+
 class AugmentTests(TestCase):
 
     @inlineCallbacks
@@ -105,6 +115,40 @@
   </record>
 """), db)
 
+    @inlineCallbacks
+    def test_add_modify(self):
+        
+        # Duplicate file as we will change it
+        newxmlfile = FilePath(self.mktemp())
+        FilePath(xmlFile).copyTo(newxmlfile)
+        
+        db = AugmentXMLDB((newxmlfile.path,))
+
+        for item in testRecords:
+            yield self._checkRecord(db, item)
+
+        newrecord = AugmentRecord(
+            **testAddRecords[0]
+        )
+        yield db.addAugmentRecords((newrecord,))
+
+        newdb = AugmentXMLDB((newxmlfile.path,))
+
+        for item in testRecords:
+            yield self._checkRecord(newdb, item)
+        yield self._checkRecord(newdb, testAddRecords[0])
+
+        newrecord = AugmentRecord(
+            **testModifyRecords[0]
+        )
+        yield db.addAugmentRecords((newrecord,))
+
+        newdb = AugmentXMLDB((newxmlfile.path,))
+
+        for item in testRecords:
+            yield self._checkRecord(newdb, item)
+        yield self._checkRecord(newdb, testModifyRecords[0])
+
 class AugmentSqliteTests(AugmentTests):
 
     @inlineCallbacks
@@ -133,6 +177,42 @@
 
         yield self._checkRecord(db, testRecordDefault)
 
+    @inlineCallbacks
+    def test_add_modify(self):
+        
+        dbpath = os.path.abspath(self.mktemp())
+        db = AugmentSqliteDB(dbpath)
+
+        dbxml = AugmentXMLDB((xmlFile,))
+        yield db.addAugmentRecords(dbxml.db.values())
+
+        for item in testRecords:
+            yield self._checkRecord(db, item)
+
+        yield self._checkNoRecord(db, "D11F03A0-97EA-48AF-9A6C-FAC7F3975767")
+
+        newrecord = AugmentRecord(
+            **testAddRecords[0]
+        )
+        yield db.addAugmentRecords((newrecord,))
+
+        newdb = AugmentSqliteDB(dbpath)
+
+        for item in testRecords:
+            yield self._checkRecord(newdb, item)
+        yield self._checkRecord(newdb, testAddRecords[0])
+
+        newrecord = AugmentRecord(
+            **testModifyRecords[0]
+        )
+        yield db.addAugmentRecords((newrecord,))
+
+        newdb = AugmentSqliteDB(dbpath)
+
+        for item in testRecords:
+            yield self._checkRecord(newdb, item)
+        yield self._checkRecord(newdb, testModifyRecords[0])
+
 class AugmentPostgreSQLTests(AugmentTests):
 
     @inlineCallbacks
@@ -163,6 +243,42 @@
 
         yield self._checkRecord(db, testRecordDefault)
 
+    @inlineCallbacks
+    def test_add_modify(self):
+        
+        db = AugmentPostgreSQLDB("localhost", "augments")
+        yield db.clean()
+
+        dbxml = AugmentXMLDB((xmlFile,))
+        yield db.addAugmentRecords(dbxml.db.values())
+
+        for item in testRecords:
+            yield self._checkRecord(db, item)
+
+        yield self._checkNoRecord(db, "D11F03A0-97EA-48AF-9A6C-FAC7F3975767")
+
+        newrecord = AugmentRecord(
+            **testAddRecords[0]
+        )
+        yield db.addAugmentRecords((newrecord,))
+
+        newdb = AugmentPostgreSQLDB("localhost", "augments")
+
+        for item in testRecords:
+            yield self._checkRecord(newdb, item)
+        yield self._checkRecord(newdb, testAddRecords[0])
+
+        newrecord = AugmentRecord(
+            **testModifyRecords[0]
+        )
+        yield db.addAugmentRecords((newrecord,))
+
+        newdb = AugmentPostgreSQLDB("localhost", "augments")
+
+        for item in testRecords:
+            yield self._checkRecord(newdb, item)
+        yield self._checkRecord(newdb, testModifyRecords[0])
+
 try:
     import pgdb
 except ImportError:

Modified: CalendarServer/trunk/twistedcaldav/directory/xmlaugmentsparser.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/xmlaugmentsparser.py	2010-02-13 19:24:59 UTC (rev 5112)
+++ CalendarServer/trunk/twistedcaldav/directory/xmlaugmentsparser.py	2010-02-13 21:12:10 UTC (rev 5113)
@@ -98,7 +98,7 @@
                     ELEMENT_UID,
                     ELEMENT_HOSTEDAT,
                 ):
-                    fields[node.tag] = node.text
+                    fields[node.tag] = node.text if node.text else ""
                 elif node.tag in (
                     ELEMENT_ENABLE,
                     ELEMENT_ENABLECALENDAR,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100213/f77141c2/attachment-0001.html>


More information about the calendarserver-changes mailing list