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
示例#2
0
    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 createRelation(self, relation):
		lock = DB_ENV.lock_get(DB_ENV.lock_id(), "next_id_increment", bdb.DB_LOCK_WRITE)
		next_id = long(self.db.get("next_id"))
		self.db.delete("next_id")
		self.db.put("next_id", "%d"%(next_id+1))
		DB_ENV.lock_put(lock)

		relation.id = next_id
		relation.version = 1

		relation_id_str = "%d"%relation.id

		data = thrift_wrapper.to_string(relation)
		cursor = self.db.cursor()
		cursor.put(relation_id_str, data, bdb.DB_KEYFIRST)

		# Update indexes
		for member in relation.members:
			if member.node != None:
				self.reverse_node_index.put("%d"%member.node, relation_id_str)
			elif member.way != None:
				self.reverse_way_index.put("%d"%member.way, relation_id_str)
			elif member.relation != None:
				self.reverse_relation_index.put("%d"%member.relation, relation_id_str)

		return relation.id
示例#4
0
    def createWays(self, ways):
        cursor = self.db.cursor()
        for way in ways:
            way.version = 1

            way_id_str = "%d" % way.id

            data = thrift_wrapper.to_string(way)
            cursor.put(way_id_str, data, bdb.DB_KEYFIRST)

            # Update indexes
            for node_id in way.nodes:
                self.reverse_node_index.put("%d" % node_id, way_id_str)

        cursor.close()
示例#5
0
	def createNode(self, node):
		node.version = 1
		node.visible = True

		data = thrift_wrapper.to_string(node)
		# Note to self: The bulk import process was 100's times faster not creating a new cursor
		# Maybe unnecessary locking issues?
		cursor = self.db.cursor()
		cursor.put("%d"%node.id, data, bdb.DB_KEYFIRST)
		cursor.close()

		if self.spatial_index:
			self.spatial_index.insert(node)

		return node.id
示例#6
0
    def createWay(self, way):
        lock = DB_ENV.lock_get(DB_ENV.lock_id(), "next_id_increment", bdb.DB_LOCK_WRITE)
        next_id = long(self.db.get("next_id"))
        self.db.delete("next_id")
        self.db.put("next_id", "%d" % (next_id + 1))
        DB_ENV.lock_put(lock)

        way.id = next_id
        way.version = 1

        way_id_str = "%d" % way.id

        data = thrift_wrapper.to_string(way)
        cursor = self.db.cursor()
        cursor.put(way_id_str, data, bdb.DB_KEYFIRST)
        cursor.close()

        # Update indexes
        for node_id in way.nodes:
            self.reverse_node_index.put("%d" % node_id, way_id_str)

        return way.id
示例#7
0
	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()
示例#8
0
	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()