Package conduit :: Module MappingDB
[hide private]

Source Code for Module conduit.MappingDB

  1  import os 
  2  import os.path 
  3  import logging 
  4  log = logging.getLogger("MappingDB") 
  5   
  6   
  7  import conduit 
  8  import conduit.datatypes 
  9  import conduit.utils as Utils 
 10  import conduit.Database as Database 
 11   
 12  DB_FIELDS = ("sourceUID","sourceDataLUID","sourceDataMtime","sourceDataHash","sinkUID","sinkDataLUID","sinkDataMtime","sinkDataHash") 
 13  DB_TYPES =  ("TEXT",     "TEXT",          "timestamp",      "TEXT",          "TEXT",   "TEXT",        "timestamp",    "TEXT") 
14 -class Mapping(object):
15 """ 16 Manages a mapping of source -> sink 17 """
18 - def __init__(self, oid, sourceUID, sourceRid, sinkUID, sinkRid):
19 self.oid = oid 20 self.sourceUID = sourceUID 21 self.sourceRid = sourceRid 22 self.sinkUID = sinkUID 23 self.sinkRid = sinkRid
24
25 - def __str__(self):
26 return "%s) [%s] <-> [%s] (%s <-> %s)" % (self.oid,self.sourceRid,self.sinkRid, self.sourceUID, self.sinkUID)
27
28 - def get_source_rid(self):
29 return self.sourceRid
30
31 - def set_source_rid(self, rid):
32 self.sourceRid = rid
33
34 - def get_sink_rid(self):
35 return self.sinkRid
36
37 - def set_sink_rid(self, rid):
38 self.sinkRid = rid
39
40 - def values(self):
41 return (self.sourceUID,self.sourceRid.get_UID(),self.sourceRid.get_mtime(),self.sourceRid.get_hash(), 42 self.sinkUID,self.sinkRid.get_UID(),self.sinkRid.get_mtime(),self.sinkRid.get_hash())
43
44 -class MappingDB:
45 """ 46 Manages mappings of RID <-> RID on a per dataprovider basis. 47 Table with 5 fields - 48 1. Source Wrapper UID 49 2. Source Data LUID 50 3. Sink Wrapper UID 51 4. Sink Data LUID 52 5. Modification Time 53 """
54 - def __init__(self, filename):
55 self._open_db(filename)
56
57 - def _get_mapping_oid(self, sourceUID, dataLUID, sinkUID):
58 sql = "SELECT oid FROM mappings WHERE sourceUID = ? AND sinkUID = ? AND sourceDataLUID = ? " \ 59 "UNION " \ 60 "SELECT oid FROM mappings WHERE sourceUID = ? AND sinkUID = ? AND sourceDataLUID = ? " \ 61 "UNION " \ 62 "SELECT oid FROM mappings WHERE sourceUID = ? AND sinkUID = ? AND sinkDataLUID = ? " \ 63 "UNION " \ 64 "SELECT oid FROM mappings WHERE sourceUID = ? AND sinkUID = ? AND sinkDataLUID = ? " 65 params = ( sourceUID,sinkUID,dataLUID, 66 sinkUID,sourceUID,dataLUID, 67 sourceUID,sinkUID,dataLUID, 68 sinkUID,sourceUID,dataLUID 69 ) 70 71 oid = self._db.select_one(sql, params) 72 if oid == None: 73 return None 74 else: 75 return oid[0]
76
77 - def _open_db_and_check_structure(self, filename):
78 self._db = Database.ThreadSafeGenericDB(filename,detect_types=True) 79 if "mappings" not in self._db.get_tables(): 80 self._db.create( 81 table="mappings", 82 fields=DB_FIELDS, 83 fieldtypes=DB_TYPES 84 )
85
86 - def _open_db(self, f):
87 """ 88 Opens the mapping DB at the location @ filename 89 """ 90 filename = os.path.abspath(f) 91 try: 92 self._open_db_and_check_structure(filename) 93 except: 94 os.unlink(filename) 95 self._open_db_and_check_structure(filename)
96
97 - def get_mapping(self, sourceUID, dataLUID, sinkUID):
98 """ 99 pass 100 """ 101 oid = self._get_mapping_oid(sourceUID, dataLUID, sinkUID) 102 if oid == None: 103 m = Mapping( 104 None, 105 sourceUID=sourceUID, 106 sourceRid=conduit.datatypes.Rid(uid=dataLUID), 107 sinkUID=sinkUID, 108 sinkRid=conduit.datatypes.Rid() 109 ) 110 else: 111 sql = "SELECT * FROM mappings WHERE oid = ?" 112 res = self._db.select_one(sql, (oid,)) 113 #a mapping is always returned relative to the source -> sink 114 #order in which it was called. 115 if (res[1] == sourceUID): 116 m = Mapping( 117 res[0], 118 sourceUID=res[1], 119 sourceRid=conduit.datatypes.Rid(res[2],res[3],res[4]), 120 sinkUID=res[5], 121 sinkRid=conduit.datatypes.Rid(res[6],res[7],res[8]) 122 ) 123 else: 124 m = Mapping( 125 res[0], 126 sourceUID=res[5], 127 sourceRid=conduit.datatypes.Rid(res[6],res[7],res[8]), 128 sinkUID=res[1], 129 sinkRid=conduit.datatypes.Rid(res[2],res[3],res[4]) 130 ) 131 #FIXME: Remove these... 132 #assert(m.sourceUID == sourceUID) 133 #assert(m.sinkUID == sinkUID) 134 return m
135
136 - def get_mappings_for_dataproviders(self, sourceUID, sinkUID):
137 """ 138 Gets all the data mappings for the dataprovider pair 139 sourceUID --> sinkUID 140 """ 141 mappings = [] 142 sql = "SELECT * FROM mappings WHERE sourceUID = ? AND sinkUID = ?" 143 for res in self._db.select(sql, (sourceUID, sinkUID)): 144 m = Mapping( 145 res[0], 146 sourceUID=res[1], 147 sourceRid=conduit.datatypes.Rid(res[2],res[3],res[4]), 148 sinkUID=res[5], 149 sinkRid=conduit.datatypes.Rid(res[6],res[7],res[8]) 150 ) 151 mappings.append(m) 152 153 return mappings
154
155 - def save_mapping(self, mapping):
156 """ 157 Saves a mapping between the dataproviders 158 """ 159 if mapping.oid == None: 160 #log.debug("New Mapping: %s" % mapping) 161 self._db.insert( 162 table="mappings", 163 values=mapping.values() 164 ) 165 else: 166 #log.debug("Update Mapping: %s" % mapping) 167 self._db.update( 168 table="mappings", 169 oid=mapping.oid, 170 values=mapping.values() 171 )
172
173 - def get_matching_UID(self, sourceUID, dataLUID, sinkUID):
174 """ 175 For a given source and sink pair and a dataLUID from the pair 176 find the other matching dataLUID. 177 178 @returns: dataLUID 179 """ 180 oid = self._get_mapping_oid(sourceUID, dataLUID, sinkUID) 181 if oid != None: 182 sourceDataLUID, sinkDataLUID = self._db.select_one("SELECT sourceDataLUID,sinkDataLUID FROM mappings WHERE oid = ?",(oid,)) 183 #return the other LUID 184 if dataLUID == sourceDataLUID: 185 return sinkDataLUID 186 elif dataLUID == sinkDataLUID: 187 return sourceDataLUID 188 else: 189 log.warn("Mapping Error") 190 return None 191 else: 192 log.debug("No mapping found for LUID: %s (source: %s, sink %s)" % (dataLUID, sourceUID, sinkUID)) 193 return None
194
195 - def delete_mapping(self, mapping):
196 """ 197 Deletes mapping between the dataproviders sourceUID and sinkUID 198 that involve dataLUID 199 """ 200 if mapping.oid == None: 201 log.warn("Could not delete mapping ") 202 self._db.delete(table="mappings",oid=mapping.oid)
203
204 - def save(self):
205 self._db.save()
206
207 - def delete(self):
208 self._db.execute("DELETE FROM mappings")
209
210 - def debug(self):
211 self._db.debug()
212
213 - def close(self):
214 self._db.close()
215