def test_update_cache_version_for_map(self): waypoint = Waypoint(waypoint_type='summit') waypoint_unrelated = Waypoint(waypoint_type='summit') topo_map = TopoMap() self.session.add_all([waypoint, waypoint_unrelated, topo_map]) self.session.flush() self.session.add( TopoMapAssociation(document_id=waypoint.document_id, topo_map_id=topo_map.document_id)) self.session.flush() update_cache_version_for_map(topo_map) cache_version_waypoint = self.session.query(CacheVersion).get( waypoint.document_id) cache_version_untouched = self.session.query(CacheVersion).get( waypoint_unrelated.document_id) cache_version_map = self.session.query(CacheVersion).get( topo_map.document_id) self.assertEqual(cache_version_waypoint.version, 2) self.assertEqual(cache_version_untouched.version, 1) # the cache key of the map is also not updated! self.assertEqual(cache_version_map.version, 1)
def test_update_cache_version_for_map(self): waypoint = Waypoint(waypoint_type='summit') waypoint_unrelated = Waypoint(waypoint_type='summit') topo_map = TopoMap() self.session.add_all([waypoint, waypoint_unrelated, topo_map]) self.session.flush() self.session.add(TopoMapAssociation( document_id=waypoint.document_id, topo_map_id=topo_map.document_id)) self.session.flush() update_cache_version_for_map(topo_map) cache_version_waypoint = self.session.query(CacheVersion).get( waypoint.document_id) cache_version_untouched = self.session.query(CacheVersion).get( waypoint_unrelated.document_id) cache_version_map = self.session.query(CacheVersion).get( topo_map.document_id) self.assertEqual(cache_version_waypoint.version, 2) self.assertEqual(cache_version_untouched.version, 1) # the cache key of the map is also not updated! self.assertEqual(cache_version_map.version, 1)
def update_associations(topo_map, update_types, user_id): """Update the links between this mapq and documents when the geometry has changed. """ if update_types: # update cache key for currently associated docs update_cache_version_for_map(topo_map) if UpdateType.GEOM in update_types: update_map(topo_map, reset=True)
def update_map(topo_map, reset=False): """Create associations for the given map with all intersecting documents. If `reset` is True, all possible existing associations to this map are dropped before creating new associations. """ if reset: DBSession.execute( TopoMapAssociation.__table__.delete().where( TopoMapAssociation.topo_map_id == topo_map.document_id) ) if topo_map.redirects_to: # ignore forwarded maps return map_geom = select([DocumentGeometry.geom_detail]). \ where(DocumentGeometry.document_id == topo_map.document_id) intersecting_documents = DBSession. \ query( DocumentGeometry.document_id, # id of a document literal_column(str(topo_map.document_id))). \ join( Document, and_( Document.document_id == DocumentGeometry.document_id, Document.type != MAP_TYPE)). \ filter(Document.redirects_to.is_(None)). \ filter( or_( DocumentGeometry.geom.ST_Intersects( map_geom.label('t1')), DocumentGeometry.geom_detail.ST_Intersects( map_geom.label('t2')) )) DBSession.execute( TopoMapAssociation.__table__.insert().from_select( [TopoMapAssociation.document_id, TopoMapAssociation.topo_map_id], intersecting_documents)) # update cache key for now associated docs update_cache_version_for_map(topo_map)