now we store also the synckey when a contentstate got created
authorLars Kneschke <l.kneschke@metaways.de>
Wed, 22 Feb 2012 08:20:59 +0000 (09:20 +0100)
committerLars Kneschke <l.kneschke@metaways.de>
Wed, 22 Feb 2012 08:20:59 +0000 (09:20 +0100)
this makes it easier to delete/update the correct content_states when
validating the synckey

lib/Syncope/Backend/Content.php
lib/Syncope/Backend/SyncState.php
lib/Syncope/Command/Sync.php
lib/Syncope/Model/IContent.php
tests/Syncope/Backend/ContentTests.php
tests/bootstrap.php

index 3790ddc..a974086 100644 (file)
@@ -47,12 +47,13 @@ class Syncope_Backend_Content implements Syncope_Backend_IContent
         $folderId = $_state->folder_id instanceof Syncope_Model_IFolder ? $_state->folder_id->id : $_state->folder_id;
         
         $this->_db->insert($this->_tablePrefix . 'content', array(
-            'id'            => $id, 
-            'device_id'     => $deviceId,
-            'folder_id'     => $folderId,
-            'contentid'     => $_state->contentid,
-            'creation_time' => $_state->creation_time->format('Y-m-d H:i:s'),
-            'is_deleted'    => isset($_state->is_deleted) ? (int)!!$_state->is_deleted : 0
+            'id'               => $id, 
+            'device_id'        => $deviceId,
+            'folder_id'        => $folderId,
+            'contentid'        => $_state->contentid,
+            'creation_time'    => $_state->creation_time->format('Y-m-d H:i:s'),
+            'creation_synckey' => $_state->creation_synckey,
+            'is_deleted'       => isset($_state->is_deleted) ? (int)!!$_state->is_deleted : 0
         ));
         
         return $this->get($id);
index 0ee12bd..290550e 100644 (file)
@@ -223,16 +223,17 @@ class Syncope_Backend_SyncState implements Syncope_Backend_ISyncState
             $this->_db->update($this->_tablePrefix . 'content', array(
                 'is_deleted'  => 0,
             ), array(
-                'device_id = ?'  => $deviceId,
-                'folder_id = ?'  => $folderId,
-                'is_deleted = ?' => 1
+                'device_id = ?'        => $deviceId,
+                'folder_id = ?'        => $folderId,
+                'creation_synckey = ?' => $state->counter,
+                'is_deleted = ?'       => 1
             ));
             
             // remove entries added during latest sync in syncope_content table
             $this->_db->delete($this->_tablePrefix . 'content', array(
-                'device_id = ?'     => $deviceId,
-                'folder_id = ?'     => $folderId,
-                'creation_time > ?' => $state->lastsync->format('Y-m-d H:i:s'),
+                'device_id = ?'        => $deviceId,
+                'folder_id = ?'        => $folderId,
+                'creation_synckey > ?' => $state->counter,
             ));
             
         } else {
index 25b73d2..7a95245 100644 (file)
@@ -246,11 +246,11 @@ class Syncope_Command_Sync extends Syncope_Command_Wbxml
                             'serverId'     => $serverId,
                             'status'       => self::STATUS_SUCCESS,
                             'contentState' => $this->_contentStateBackend->create(new Syncope_Model_Content(array(
-                                'device_id'     => $this->_device,
-                                'folder_id'     => $collectionData['folder'],
-                                'contentid'     => $serverId,
-                                'creation_time' => $this->_syncTimeStamp
-                            
+                                'device_id'        => $this->_device,
+                                'folder_id'        => $collectionData['folder'],
+                                'contentid'        => $serverId,
+                                'creation_time'    => $this->_syncTimeStamp,
+                                'creation_synckey' => $collectionData['syncKey'] + 1
                             )))
                         );
                         
@@ -557,25 +557,25 @@ class Syncope_Command_Sync extends Syncope_Command_Wbxml
                         break;
                     }
                     
-                    /**
-                     * somewhere is a problem in the logic for handling moreAvailable
-                     * 
-                     * it can happen, that we have a contentstate (which means we sent the entry to the client
-                     * and that this entry is yet in $collectionData['syncState']->pendingdata['serverAdds']
-                     * I have no idea how this can happen, but the next lines of code work around this problem
-                     */
-                    try {
-                        $this->_contentStateBackend->getContentState($this->_device, $collectionData['folder'], $serverId);
-                    
-                        if ($this->_logger instanceof Zend_Log) 
-                            $this->_logger->info(__METHOD__ . '::' . __LINE__ . " skipped an entry($serverId) which is already on the client");
-                        
-                        unset($serverAdds[$id]);
-                        continue;
-                        
-                    } catch (Syncope_Exception_NotFound $senf) {
-                        // do nothing => content state should not exist yet
-                    }
+                    #/**
+                    # * somewhere is a problem in the logic for handling moreAvailable
+                    # * 
+                    # * it can happen, that we have a contentstate (which means we sent the entry to the client
+                    # * and that this entry is yet in $collectionData['syncState']->pendingdata['serverAdds']
+                    # * I have no idea how this can happen, but the next lines of code work around this problem
+                    # */
+                    #try {
+                    #    $this->_contentStateBackend->getContentState($this->_device, $collectionData['folder'], $serverId);
+                    # 
+                    #    if ($this->_logger instanceof Zend_Log) 
+                    #        $this->_logger->info(__METHOD__ . '::' . __LINE__ . " skipped an entry($serverId) which is already on the client");
+                    #    
+                    #    unset($serverAdds[$id]);
+                    #    continue;
+                    #    
+                    #} catch (Syncope_Exception_NotFound $senf) {
+                    #    // do nothing => content state should not exist yet
+                    #}
                     
                     try {
                         $add = $this->_outputDom->createElementNS('uri:AirSync', 'Add');
@@ -594,10 +594,11 @@ class Syncope_Command_Sync extends Syncope_Command_Wbxml
                     
                     // mark as send to the client, even the conversion to xml might have failed 
                     $newContentStates[] = new Syncope_Model_Content(array(
-                        'device_id'     => $this->_device,
-                        'folder_id'     => $collectionData['folder'],
-                        'contentid'     => $serverId,
-                        'creation_time' => $this->_syncTimeStamp
+                        'device_id'        => $this->_device,
+                        'folder_id'        => $collectionData['folder'],
+                        'contentid'        => $serverId,
+                        'creation_time'    => $this->_syncTimeStamp,
+                        'creation_synckey' => $collectionData['syncState']->counter
                     ));
                     unset($serverAdds[$id]);    
                 }
index 7970945..a8233f4 100644 (file)
@@ -18,6 +18,7 @@
  * @property    string    folder_id
  * @property    string    contentid
  * @property    DateTime  creation_time
+ * @property    string    creation_synckey
  * @property    string    is_deleted
  */
 
index af428cb..bc8fc74 100644 (file)
@@ -162,10 +162,11 @@ class Syncope_Backend_ContentTests extends PHPUnit_Framework_TestCase
     public static function getTestContent(Syncope_Model_IDevice $_device, Syncope_Model_IFolder $_folder)
     {
         return new Syncope_Model_Content(array(
-            'device_id'     => $_device,
-            'folder_id'     => $_folder,
-            'contentid'     => 'abc1234',
-            'creation_time' => new DateTime(null, new DateTimeZone('utc'))
+            'device_id'        => $_device,
+            'folder_id'        => $_folder,
+            'contentid'        => 'abc1234',
+            'creation_time'    => new DateTime(null, new DateTimeZone('utc')),
+            'creation_synckey' => 1
         ));
     }
 }
index 39bc9cc..98c1b82 100644 (file)
@@ -74,6 +74,7 @@ function getTestDatabase()
         `folder_id` varchar(40) NOT NULL,
         `contentid` varchar(64) NOT NULL,
         `creation_time` datetime NOT NULL,
+        `creation_synckey` int(11) NOT NULL,
         `is_deleted` int(11) DEFAULT '0',
         PRIMARY KEY (`id`),
         UNIQUE (`device_id`,`folder_id`,`contentid`)