lib/kolab_sync_backend_state.php

Aleksander Machniak machniak at kolabsys.com
Thu Apr 11 09:22:14 CEST 2013


 lib/kolab_sync_backend_state.php |   20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

New commits:
commit 41a0a5da3ffc83303ce71d316fd947b260a62f49
Author: Aleksander Machniak <alec at alec.pl>
Date:   Thu Apr 11 09:21:26 2013 +0200

    Improve validate() performance by skipping one DELETE query for most calls

diff --git a/lib/kolab_sync_backend_state.php b/lib/kolab_sync_backend_state.php
index 87711da..f65ea6c 100644
--- a/lib/kolab_sync_backend_state.php
+++ b/lib/kolab_sync_backend_state.php
@@ -150,15 +150,13 @@ class kolab_sync_backend_state extends kolab_sync_backend_common implements Sync
     {
         $device_id = $deviceid instanceof Syncroton_Model_IDevice ? $deviceid->id : $deviceid;
         $folder_id = $folderid instanceof Syncroton_Model_IFolder ? $folderid->id : $folderid;
+        $states    = array();
 
-        // get sync data for current request and check if it's the last one
-        // by fetching synckey+1
-        $states = array();
-        $keys   = array($sync_key, $sync_key + 1);
-
+        // get sync data
+        // we'll get all records, thanks to this we'll be able to
+        // skip _deleteOtherStates() call below (one DELETE query less)
         $where['device_id'] = $this->db->quote_identifier('device_id') . ' = ' . $this->db->quote($device_id);
         $where['type']      = $this->db->quote_identifier('type')      . ' = ' . $this->db->quote($folder_id);
-        $where['counter']   = $this->db->quote_identifier('counter')   . ' IN (' . $this->db->array2list($keys, 'int') . ')';
 
         $select = $this->db->query('SELECT * FROM ' . $this->table_name .' WHERE ' . implode(' AND ', $where));
 
@@ -166,19 +164,21 @@ class kolab_sync_backend_state extends kolab_sync_backend_common implements Sync
             $states[$row['counter']] = $this->get_object($row);
         }
 
+        // last state not found
         if (empty($states) || empty($states[$sync_key])) {
             return false;
         }
 
         $state = $states[$sync_key];
+        $next  = max(array_keys($states));
 
         $where = array();
         $where['device_id']  = $this->db->quote_identifier('device_id')  . ' = ' . $this->db->quote($device_id);
         $where['folder_id']  = $this->db->quote_identifier('folder_id')  . ' = ' . $this->db->quote($folder_id);
         $where['is_deleted'] = $this->db->quote_identifier('is_deleted') . ' = 1';
 
-        // found more recent synckey => the last sync repsonse got not received by the client
-        if (!empty($states[$sync_key + 1])) {
+        // found more recent synckey => the last sync response got not received by the client
+        if ($next > $sync_key) {
             $where['synckey'] = $this->db->quote_identifier('creation_synckey') . ' = ' . $this->db->quote($state->counter);
             // undelete entries marked as deleted in syncroton_content table
             $this->db->query('UPDATE syncroton_content SET is_deleted = 0 WHERE ' . implode(' AND ', $where));
@@ -195,7 +195,9 @@ class kolab_sync_backend_state extends kolab_sync_backend_common implements Sync
         }
 
         // remove all other synckeys
-        $this->_deleteOtherStates($state);
+        if (count($states) > 1) {
+            $this->_deleteOtherStates($state);
+        }
 
         return $state;
     }





More information about the commits mailing list