[CalendarServer-changes] [6311] CalendarServer/trunk/txdav/caldav/datastore

source_changes at macosforge.org source_changes at macosforge.org
Thu Sep 16 20:26:06 PDT 2010


Revision: 6311
          http://trac.macosforge.org/projects/calendarserver/changeset/6311
Author:   cdaboo at apple.com
Date:     2010-09-16 20:26:05 -0700 (Thu, 16 Sep 2010)
Log Message:
-----------
Hash the attachment directory paths.

Modified Paths:
--------------
    CalendarServer/trunk/txdav/caldav/datastore/sql.py
    CalendarServer/trunk/txdav/caldav/datastore/test/test_sql.py

Modified: CalendarServer/trunk/txdav/caldav/datastore/sql.py
===================================================================
--- CalendarServer/trunk/txdav/caldav/datastore/sql.py	2010-09-17 00:46:28 UTC (rev 6310)
+++ CalendarServer/trunk/txdav/caldav/datastore/sql.py	2010-09-17 03:26:05 UTC (rev 6311)
@@ -209,7 +209,7 @@
 }
 
 def _pathToName(path):
-    return path.rsplit(".", 1)[0].split("-", 3)[-1]
+    return path.rsplit(".", 1)[0]
 
 class CalendarObject(CommonObjectResource):
     implements(ICalendarObject)
@@ -442,30 +442,38 @@
         return self.component().getOrganizer()
 
     def createAttachmentWithName(self, name, contentType):
-        path = self._attachmentPath(name)
-        attachment = Attachment(self, path)
+
+        try:
+            self._attachmentPathRoot().makedirs()
+        except:
+            pass
+
+        attachment = Attachment(self, name)
         self._txn.execSQL("""
             insert into ATTACHMENT (CALENDAR_OBJECT_RESOURCE_ID, CONTENT_TYPE,
             SIZE, MD5, PATH)
             values (%s, %s, %s, %s, %s)
             """,
             [
-                self._resourceID, generateContentType(contentType), 0, "",
-                attachment._pathValue()
+                self._resourceID,
+                generateContentType(contentType),
+                0,
+                "",
+                name,
             ]
         )
         return attachment.store(contentType)
 
     def removeAttachmentWithName(self, name):
-        attachment = Attachment(self, self._attachmentPath(name))
+        attachment = Attachment(self, name)
         self._txn.postCommit(attachment._path.remove)
         self._txn.execSQL("""
         delete from ATTACHMENT where CALENDAR_OBJECT_RESOURCE_ID = %s AND
         PATH = %s
-        """, [self._resourceID, attachment._pathValue()])
+        """, [self._resourceID, name])
 
     def attachmentWithName(self, name):
-        attachment = Attachment(self, self._attachmentPath(name))
+        attachment = Attachment(self, name)
         if attachment._populate():
             return attachment
         else:
@@ -477,26 +485,19 @@
     def dropboxID(self):
         return dropboxIDFromCalendarObject(self)
 
-    def _attachmentPath(self, name):
+    def _attachmentPathRoot(self):
         attachmentRoot = self._txn._store.attachmentsPath
-        try:
-            attachmentRoot.createDirectory()
-        except:
-            pass
-        return attachmentRoot.child(
-            "%s-%s-%s-%s.attachment" % (
-                self._calendar._home.uid(), self._calendar.name(),
-                self.name(), name
-            )
-        )
-
+        
+        # Use directory hashing scheme based on owner user id
+        homeName = self._calendar.ownerHome().name()
+        return attachmentRoot.child(homeName[0:2]).child(homeName[2:4]).child(homeName).child(self.uid())
+        
     def attachments(self):
         rows = self._txn.execSQL("""
         select PATH from ATTACHMENT where CALENDAR_OBJECT_RESOURCE_ID = %s 
         """, [self._resourceID])
         for row in rows:
-            demangledName = _pathToName(row[0])
-            yield self.attachmentWithName(demangledName)
+            yield self.attachmentWithName(row[0])
 
     def initPropertyStore(self, props):
         # Setup peruser special properties
@@ -545,21 +546,20 @@
 
     def loseConnection(self):
         self.attachment._path.setContent(self.buf)
-        pathValue = self.attachment._pathValue()
         contentTypeString = generateContentType(self.contentType)
         self._txn.execSQL(
             "update ATTACHMENT set CONTENT_TYPE = %s, SIZE = %s, MD5 = %s, MODIFIED = timezone('UTC', CURRENT_TIMESTAMP) "
             "WHERE PATH = %s",
-            [contentTypeString, len(self.buf), self.hash.hexdigest(), pathValue]
+            [contentTypeString, len(self.buf), self.hash.hexdigest(), self.attachment.name()]
         )
 
 class Attachment(object):
 
     implements(IAttachment)
 
-    def __init__(self, calendarObject, path):
+    def __init__(self, calendarObject, name):
         self._calendarObject = calendarObject
-        self._path = path
+        self._name = name
 
 
     @property
@@ -576,7 +576,7 @@
         rows = self._txn.execSQL(
             """
             select CONTENT_TYPE, SIZE, MD5, CREATED, MODIFIED from ATTACHMENT where PATH = %s
-            """, [self._pathValue()])
+            """, [self._name])
         if not rows:
             return False
         self._contentType = MimeType.fromString(rows[0][0])
@@ -588,17 +588,13 @@
 
 
     def name(self):
-        return _pathToName(self._pathValue())
+        return self._name
 
+    @property
+    def _path(self):
+        attachmentPath = self._calendarObject._attachmentPathRoot()
+        return attachmentPath.child(self.name())
 
-    def _pathValue(self):
-        """
-        Compute the value which should go into the 'path' column for this
-        attachment.
-        """
-        root = self._txn._store.attachmentsPath
-        return '/'.join(self._path.segmentsFrom(root))
-
     def properties(self):
         pass # stub
 

Modified: CalendarServer/trunk/txdav/caldav/datastore/test/test_sql.py
===================================================================
--- CalendarServer/trunk/txdav/caldav/datastore/test/test_sql.py	2010-09-17 00:46:28 UTC (rev 6310)
+++ CalendarServer/trunk/txdav/caldav/datastore/test/test_sql.py	2010-09-17 03:26:05 UTC (rev 6311)
@@ -103,6 +103,18 @@
         self.addCleanup(txn.commit)
         return txn
 
+    @inlineCallbacks
+    def test_attachmentPath(self):
+        """
+        L{ICalendarObject.createAttachmentWithName} will store an
+        L{IAttachment} object that can be retrieved by
+        L{ICalendarObject.attachmentWithName}.
+        """
+        yield self.createAttachmentTest(lambda x: x)
+        attachmentRoot = self.calendarObjectUnderTest()._txn._store.attachmentsPath
+        attachmentPath = attachmentRoot.child("ho").child("me").child("home1")
+        attachmentPath = attachmentPath.child(self.calendarObjectUnderTest().uid()).child("new.attachment")
+        self.assertTrue(attachmentPath.isfile())
 
     def test_migrateCalendarFromFile(self):
         """
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100916/59549b98/attachment-0001.html>


More information about the calendarserver-changes mailing list