def __init__(self, config, options, db): "Initialize an operations structure." self.db = db self.verbose = options.verbose self.geotable = GeoGroupTable(config, options, db)
class DBOps: """This class implements the semantics of adding OSM elements and changesets to the backend.""" def __init__(self, config, options, db): "Initialize an operations structure." self.db = db self.verbose = options.verbose self.geotable = GeoGroupTable(config, options, db) def add_element(self, elem): "Add an element to the datastore." self.db.store(elem) # If the element is a node, add it to the appropriate geodoc. ns = elem.namespace backreference = make_backreference(ns, elem.id) if self.verbose: increment_stats(ns) # Do element-specific processing. if ns == C.NODE: # Add the element to the appropriate geodoc. self.geotable.add(elem) elif ns == C.WAY: # Backlink referenced nodes to the current way. for (rstatus, node_or_key) in self.db.fetch_keys(C.NODE, map(str, elem[C.NODES])): if rstatus: node = node_or_key else: node = new_osm_element(C.NODE, node_or_key) node[C.REFERENCES].add(backreference) self.db.store(node) elif ns == C.RELATION: # If the element is a relation, backlink referenced ways & # relations. def _retrieve(selector, members): return [str(mref) for (mref, mrole, mtype) in members if mtype == selector] members = elem[C.MEMBERS] elements = [] for ns in [C.NODE, C.WAY, C.RELATIONS]: elements.append((ns, _retrieve(ns, members))) for (ns, refs) in elements: if len(refs) == 0: continue for (rstatus, node_or_key) in self.db.fetch_keys(ns, refs): # Retrieve all elements referenced by the relation. if rstatus: elem = node_or_key else: elem = new_osm_element(ns, node_or_key) # Add a backreference to the element being # referenced by this relation. elem[C.REFERENCES].add(backreference) self.db.store(elem) def add_changeset(self, changeset): "Add a changeset to the database." raise NotImplementedError def finish(self): """Signal the end of DB operations.""" # Push out all pending geodoc changes. self.geotable.flush() # Request the underlying database to wind up operation. self.db.finalize()
class DBOps: """This class implements the semantics of adding OSM elements and changesets to the backend.""" def __init__(self, config, options, db): "Initialize an operations structure." self.db = db self.verbose = options.verbose self.geotable = GeoGroupTable(config, options, db) def add_element(self, elem): "Add an element to the datastore." self.db.store(elem) # If the element is a node, add it to the appropriate geodoc. ns = elem.namespace backreference = make_backreference(ns, elem.id) if self.verbose: increment_stats(ns) # Do element-specific processing. if ns == C.NODE: # Add the element to the appropriate geodoc. self.geotable.add(elem) elif ns == C.WAY: # Backlink referenced nodes to the current way. for (rstatus, node_or_key) in \ self.db.fetch_keys(C.NODE, map(str, elem[C.NODES])): if rstatus: node = node_or_key else: node = new_osm_element(C.NODE, node_or_key) node[C.REFERENCES].add(backreference) self.db.store(node) elif ns == C.RELATION: # If the element is a relation, backlink referenced ways & # relations. def _retrieve(selector, members): return [ str(mref) for (mref, mrole, mtype) in members if mtype == selector ] members = elem[C.MEMBERS] elements = [] for ns in [C.NODE, C.WAY, C.RELATIONS]: elements.append((ns, _retrieve(ns, members))) for (ns, refs) in elements: if len(refs) == 0: continue for (rstatus, node_or_key) in self.db.fetch_keys(ns, refs): # Retrieve all elements referenced by the relation. if rstatus: elem = node_or_key else: elem = new_osm_element(ns, node_or_key) # Add a backreference to the element being # referenced by this relation. elem[C.REFERENCES].add(backreference) self.db.store(elem) def add_changeset(self, changeset): "Add a changeset to the database." raise NotImplementedError def finish(self): """Signal the end of DB operations.""" # Push out all pending geodoc changes. self.geotable.flush() # Request the underlying database to wind up operation. self.db.finalize()