Package conduit :: Module DeltaProvider
[hide private]

Source Code for Module conduit.DeltaProvider

 1  """ 
 2  Shared API for comparing the previous state of a dp to the current  
 3  state. Returns only changes to core synch mechanism. 
 4   
 5  This class is a proxy for the TwoWay dataprovider. If the dataprovider 
 6  cannot implement get_changes() using backend dependant means this class  
 7  uses the mapping DB to implement get_changes() 
 8   
 9  This class will always be slower than if the backend implements the function 
10  iteself. 
11   
12  Copyright: John Stowers, 2006 
13  License: GPLv2 
14  """ 
15  import logging 
16  log = logging.getLogger("DeltaProvider") 
17   
18  import conduit 
19   
20 -class DeltaProvider:
21 - def __init__(self, dpw, otherdpw):
22 self.me = dpw 23 self.other = otherdpw 24 25 log.info("Delta: Source (%s) does not implement get_changes(). Proxying..." % self.me.get_UID())
26
27 - def get_changes(self):
28 """ 29 @returns: added, modified, deleted 30 """ 31 allItems = [] 32 for i in self.me.module.get_all(): 33 #Make sure the are in unicode to assure a 34 #good comparison with mapping UID's 35 if type(i) != unicode: 36 i = unicode(i,errors='replace') 37 allItems.append(i) 38 39 log.debug("Delta: Got %s items\n%s" % (len(allItems), allItems)) 40 41 #In order to detect deletions we need to fetch all the existing relationships. 42 #we also get the rids because we need those to detect if something has changed 43 rids = {} 44 for m in conduit.GLOBALS.mappingDB.get_mappings_for_dataproviders(self.me.get_UID(), self.other.get_UID()): 45 rids[ m.get_source_rid().get_UID() ] = m.get_source_rid() 46 for m in conduit.GLOBALS.mappingDB.get_mappings_for_dataproviders(self.other.get_UID(), self.me.get_UID()): 47 rids[ m.get_sink_rid().get_UID() ] = m.get_sink_rid() 48 49 log.debug("Delta: Expecting %s items" % len(rids)) 50 for uid,rid in rids.items(): 51 log.debug("%s) -- %s" % (uid,rid)) 52 53 #now classify all my items relative to the expected data from the previous 54 #sync with the supplied other dataprovider. Copy (slice) the list because we 55 #modify it in place 56 modified = [] 57 for i in allItems[:]: 58 if i in rids: 59 data = self.me.module.get(i) 60 if data.get_rid() != rids[i]: 61 log.debug("Modified: Actual:%s v DB:%s" % (data.get_rid(), rids[i])) 62 modified.append(i) 63 del(rids[i]) 64 allItems.remove(i) 65 66 #now all that remains in rids is data which has been deleted, 67 #and all that remains in allItems is new data 68 return allItems, modified, rids.keys()
69