]> code.delx.au - offlineimap/blobdiff - src/Data/Syncable.hs
Much progress on syncable; important tests are passing!
[offlineimap] / src / Data / Syncable.hs
index d872905ea21fcf987f583e47f5eb3e5508a1527e..f583466c7bc372e54e919f8c7a69f3e1cbb18f2d 100644 (file)
@@ -118,38 +118,33 @@ syncBiDir masterstate childstate lastchildstate =
                           findAdded masterstate childstate $ lastchildstate)
                          ++ (map (pairToFunc ModifyContent) . Map.toList $ childPayloadChanges)
           masterPayloadChanges = 
-                 (Map.filterWithKey filterKV
-                     (Map.intersection 
-                         (Map.union (findModified childstate lastchildstate)
-                                    (findModified childstate masterstate)
-                         )
-                         masterstate
-                     )
-                 )
-              where filterKV k v = -- Eliminate changes that would set the value to what it already is
-                        case Map.lookup k masterstate of
-                          (Just m) -> m /= v 
-                          Nothing -> False
+              Map.union 
+                 (findModified masterstate childstate childstate lastchildstate)
+                 (findModified masterstate childstate reducedChildState masterstate)
+              where reducedChildState = 
+                        Map.difference childstate lastchildstate
                  
           -- The child's payload takes precedence, so we are going to
           -- calculate the changes made on the master to apply to the client,
           -- then subtract out any items in the master changes that have the
           -- same key.
           childPayloadChanges = 
-              Map.intersection
-                 (Map.difference (findModified masterstate lastchildstate)
-                  (findModified childstate lastchildstate))
-                 childstate
+              Map.difference (findModified childstate masterstate masterstate lastchildstate)
+                 (findModified masterstate childstate childstate lastchildstate)
 
 {- | Compares two SyncCollections, and returns the commands that, when
 applied to the first collection, would yield the second. -}
-diffCollection :: (Ord k, Show k, Show v) => 
+diffCollection :: (Ord k, Show k, Eq v, Show v) => 
                   SyncCollection k v
                -> SyncCollection k v
                -> [SyncCommand k v]
 diffCollection coll1 coll2 = 
     (map DeleteItem . findDeleted coll2 coll1 $ coll1) ++
     (map (pairToFunc CopyItem) . findAdded coll2 coll1 $ coll1)
+    {-
+    (map (pairToFunc ModifyContent) . Map.toList .
+         findModified coll1 coll2 $ coll1)
+     -}
 
 {- | Returns a list of keys that exist in state2 and lastchildstate
 but not in state1 -}
@@ -167,15 +162,34 @@ findAdded :: (Ord k, Eq k) =>
 findAdded state1 state2 lastchildstate =
     Map.toList . Map.difference state1 . Map.union state2 $ lastchildstate
 
+
 {- Finds all items that exist in both state1 and lastchildstate in which the payload
 is different in state1 than it was in lastchildstate.  Returns the key and new
 payload for each such item found. -}
 findModified :: (Ord k, Eq v) =>
-                SyncCollection k v -> SyncCollection k v -> SyncCollection k v
-findModified state1 lastchildstate =
-    Map.mapMaybe id .  
-    Map.intersectionWith (\v1 v2 -> if v1 /= v2 then Just v1 else Nothing) 
-       state1 $ lastchildstate
+                SyncCollection k v 
+             -> SyncCollection k v
+             -> SyncCollection k v
+             -> SyncCollection k v
+             -> SyncCollection k v
+findModified basestate authoritativestate comparisonstate laststate =
+    Map.mapMaybe id $
+       Map.intersectionWithKey compareFunc comparisonstate laststate
+    where compareFunc k compv lastv =
+              if lastv == compv
+                 then Nothing
+                 else case (Map.lookup k basestate, Map.lookup k authoritativestate) of
+                        (Nothing, _) -> Nothing
+                        (Just basev, Nothing) ->
+                            if compv /= basev
+                               then Just compv
+                               else Nothing
+                        (Just basev, Just authv) ->
+                            if (authv /= lastv) && (authv /= basev)
+                               then Just authv
+                               else if compv /= basev && (authv /= basev)
+                                    then Just compv
+                                    else Nothing
 
 {- | Apply the specified changes to the given SyncCollection.  Returns
 a new SyncCollection with the changes applied.  If changes are specified