def editRelation(self, relation): relation_id_str = "%d"%relation.id # Get the last version cursor = self.db.cursor() data_pair = cursor.get(relation_id_str, bdb.DB_SET) if data_pair: old_relation = Relation() thrift_wrapper.from_string(old_relation, data_pair[1]) # Indexes to add to for node_id in set(relation.nodes) - set(old_relation.nodes): self.reverse_node_index.put("%d"%node_id, relation_id_str) # Indexes to remove reverse_node_cursor = self.reverse_node_index.cursor() for node_id in set(old_relation.nodes) - set(relation.nodes): print "Going to remove node %d from the index" % node_id relation_id_search_str_pair = reverse_node_cursor.get("%d"%node_id, bdb.DB_SET) while relation_id_search_str_pair: if relation_id_str == relation_id_search_str_pair[1]: print "...calling delete for %s -> %s" % relation_id_search_str_pair reverse_node_cursor.delete() break relation_id_search_str_pair = reverse_node_cursor.get("%d"%node_id, bdb.DB_NEXT_DUP) relation.version = old_relation.version + 1 # This is bound to have concurrency issues data = thrift_wrapper.to_string(relation) cursor.put(relation_id_str, data, bdb.DB_KEYFIRST) return relation.id else: # There was no previous version! pass
def editWay(self, way): way_id_str = "%d" % way.id # Get the last version cursor = self.db.cursor() data_pair = cursor.get(way_id_str, bdb.DB_SET) if data_pair: old_way = Way() thrift_wrapper.from_string(old_way, data_pair[1]) # Indexes to add to for node_id in set(way.nodes) - set(old_way.nodes): self.reverse_node_index.put("%d" % node_id, way_id_str) # Indexes to remove reverse_node_cursor = self.reverse_node_index.cursor() for node_id in set(old_way.nodes) - set(way.nodes): way_id_search_str_pair = reverse_node_cursor.get("%d" % node_id, bdb.DB_SET) while way_id_search_str_pair: if way_id_str == way_id_search_str_pair[1]: reverse_node_cursor.delete() break way_id_search_str_pair = reverse_node_cursor.get("%d" % node_id, bdb.DB_NEXT_DUP) way.version = old_way.version + 1 data = thrift_wrapper.to_string(way) cursor.put(way_id_str, data, bdb.DB_KEYFIRST) return way.id else: # There was no previous version! pass
def getRelation(self, id): relation = Relation() data = self.db.get("%d"%id) if data: thrift_wrapper.from_string(relation, data) return relation else: return None
def getNode(self, id): node = Node() data = self.db.get("%d"%id) if data: thrift_wrapper.from_string(node, data) return node else: print "No node found, %d" % id return None
def getWay(self, id): print "getWay(%d)" % id way = Way() data = self.db.get("%d" % id) if data: thrift_wrapper.from_string(way, data) return way else: return None
def debug_print_db(self): way = Way() print "Way db:" cursor = self.db.cursor() data_pair = cursor.get(None, bdb.DB_FIRST) while data_pair: thrift_wrapper.from_string(way, data_pair[1]) print "%s -> %s" % (data_pair[0], way) data_pair = cursor.get(None, bdb.DB_NEXT)
def debug_print_db(self): relation = Relation() print "Relation db:" cursor = self.db.cursor() data_pair = cursor.get(None, bdb.DB_FIRST) while data_pair: thrift_wrapper.from_string(relation, data_pair[1]) print "%s -> %s" % (data_pair[0],relation) data_pair = cursor.get(None, bdb.DB_NEXT) cursor.close()
def getRelationHistory(self, id): relations = [] cursor = self.db.cursor() data_pair = cursor.get("%d"%id, bdb.DB_SET) while data_pair: relation = Relation() thrift_wrapper.from_string(relation, data_pair[1]) relations.append(relation) data_pair = cursor.get("%d"%id, bdb.DB_NEXT_DUP) return relations
def getWayHistory(self, id): ways = [] cursor = self.db.cursor() data_pair = cursor.get("%d" % id, bdb.DB_SET) while data_pair: way = Way() thrift_wrapper.from_string(way, data_pair[1]) ways.append(way) data_pair = cursor.get("%d" % id, bdb.DB_NEXT_DUP) return ways
def getRelationVersion(self, id, version): relation = Relation() cursor = self.db.cursor() data_pair = cursor.get("%d"%id, bdb.DB_SET) while data_pair: thrift_wrapper.from_string(relation, data_pair[1]) if relation.version == version: return relation data_pair = cursor.get("%d"%id, bdb.DB_NEXT_DUP) return None
def getWayVersion(self, id, version): way = Way() cursor = self.db.cursor() data_pair = cursor.get("%d" % id, bdb.DB_SET) while data_pair: thrift_wrapper.from_string(way, data_pair[1]) if way.version == version: return way data_pair = cursor.get("%d" % id, bdb.DB_NEXT_DUP) return None
def getNodeVersion(self, id, version): node = Node() cursor = self.db.cursor() data_pair = cursor.get("%d"%id, bdb.DB_SET) while data_pair: thrift_wrapper.from_string(node, data_pair[1]) if node.version == version: return node data_pair = cursor.get("%d"%id, bdb.DB_NEXT_DUP) cursor.close() return None
def getRelationsFromRelation(self, relation_id): cursor = self.reverse_relation_index.cursor() relation_ids = set() relation_id_str_pair = cursor.get("%d"%relation_id, bdb.DB_SET) while relation_id_str_pair: relation_ids.add(relation_id_str_pair[1]) relation_id_str_pair = cursor.get("%d"%relation_id, bdb.DB_NEXT_DUP) relations = [] for relation_id_str in relation_ids: data = self.db.get(relation_id_str) if data: relation = Relation() thrift_wrapper.from_string(relation, data) relations.append(relation) return relations
def getWaysFromNode(self, node_id): cursor = self.reverse_node_index.cursor() way_ids = set() way_id_str_pair = cursor.get("%d" % node_id, bdb.DB_SET) while way_id_str_pair: way_ids.add(way_id_str_pair[1]) way_id_str_pair = cursor.get("%d" % node_id, bdb.DB_NEXT_DUP) ways = [] for way_id_str in way_ids: data = self.db.get(way_id_str) if data: way = Way() thrift_wrapper.from_string(way, data) ways.append(way) return ways
def getNodeHistory(self, id): nodes = [] cursor = self.db.cursor() data_pair = cursor.get("%d"%id, bdb.DB_SET) while data_pair: node = Node() thrift_wrapper.from_string(node, data_pair[1]) nodes.append(node) data_pair = cursor.get("%d"%id, bdb.DB_NEXT_DUP) cursor.close() if len(nodes) > 0: return nodes else: return None
def getNodesInBounds(self, bounds): def contains(bounds, node): return (node.lat > bounds.min_lat and node.lat < bounds.max_lat) and (node.lon > bounds.min_lon and node.lon < bounds.max_lon) if self.spatial_index: node_ids = self.spatial_index.likely_intersection(bounds) # We'll get some nodes that are outside the requested bounds, so filter them out return [node for node in self.getNodes(node_ids) if contains(bounds,node)] else: # Do the stupid search nodes = [] cursor = self.db.cursor() data_pair = cursor.get(None, bdb.DB_FIRST) while data_pair: node = Node() thrift_wrapper.from_string(node, data_pair[1]) if contains(bounds, node): nodes.append(node) data_pair = cursor.get(None, bdb.DB_NEXT_NODUP) return nodes
def deleteNode(self, node_id): node_id_str = "%d"%node_id # Get the last version cursor = self.db.cursor() data_pair = cursor.get(node_id_str, bdb.DB_SET) if data_pair: old_node = Node() thrift_wrapper.from_string(old_node, data_pair[1]) old_node.visible = False old_node.version += 1 data = thrift_wrapper.to_string(old_node) cursor.put(node_id_str, data, bdb.DB_KEYFIRST) cursor.close() self.spatial_index.delete(old_node) return old_node.version else: # There was no previous version! cursor.close()
def editNode(self, node): node_id_str = "%d"%node.id # Get the last version cursor = self.db.cursor() data_pair = cursor.get(node_id_str, bdb.DB_SET) if data_pair: old_node = Node() thrift_wrapper.from_string(old_node, data_pair[1]) if node.lat != old_node.lat or node.lon != old_node.lon: self.spatial_index.delete(old_node) self.spatial_index.insert(node) node.version = old_node.version + 1 # This is bound to have concurrency issues data = thrift_wrapper.to_string(node) cursor.put(node_id_str, data, bdb.DB_KEYFIRST) cursor.close() return node.version else: # There was no previous version! cursor.close()