def test_add_artifacts(self): from allura.lib.search import find_shortlinks with mock.patch('allura.lib.search.find_shortlinks') as find_slinks: find_slinks.side_effect = lambda s: find_shortlinks(s) old_shortlinks = M.Shortlink.query.find().count() old_solr_size = len(g.solr.db) artifacts = [_TestArtifact() for x in range(5)] for i, a in enumerate(artifacts): a._shorthand_id = 't%d' % i a.text = 'This is a reference to [t3]' arefs = [M.ArtifactReference.from_artifact(a) for a in artifacts] ref_ids = [r._id for r in arefs] M.artifact_orm_session.flush() index_tasks.add_artifacts(ref_ids) new_shortlinks = M.Shortlink.query.find().count() new_solr_size = len(g.solr.db) assert old_shortlinks + \ 5 == new_shortlinks, 'Shortlinks not created' assert old_solr_size + \ 5 == new_solr_size, "Solr additions didn't happen" M.main_orm_session.flush() M.main_orm_session.clear() t3 = _TestArtifact.query.get(_shorthand_id='t3') assert len(t3.backrefs) == 5, t3.backrefs assert_equal(find_slinks.call_args_list, [mock.call(a.index().get('text')) for a in artifacts])
def test_del_artifacts(self, solr): old_shortlinks = M.Shortlink.query.find().count() artifacts = [ _TestArtifact(_shorthand_id='ta_%s' % x) for x in range(5) ] M.artifact_orm_session.flush() arefs = [M.ArtifactReference.from_artifact(a) for a in artifacts] ref_ids = [r._id for r in arefs] M.artifact_orm_session.flush() index_tasks.add_artifacts(ref_ids) M.main_orm_session.flush() M.main_orm_session.clear() new_shortlinks = M.Shortlink.query.find().count() assert old_shortlinks + 5 == new_shortlinks, 'Shortlinks not created' assert solr.add.call_count == 1 sort_key = operator.itemgetter('id') assert_equal( sorted(solr.add.call_args[0][0], key=sort_key), sorted([ref.artifact.solarize() for ref in arefs], key=sort_key)) index_tasks.del_artifacts(ref_ids) M.main_orm_session.flush() M.main_orm_session.clear() new_shortlinks = M.Shortlink.query.find().count() assert old_shortlinks == new_shortlinks, 'Shortlinks not deleted' solr_query = 'id:({0})'.format(' || '.join(ref_ids)) solr.delete.assert_called_once_with(q=solr_query)
def test_del_artifacts(self, solr): old_shortlinks = M.Shortlink.query.find().count() artifacts = [_TestArtifact(_shorthand_id='ta_%s' % x) for x in range(5)] M.artifact_orm_session.flush() arefs = [M.ArtifactReference.from_artifact(a) for a in artifacts] ref_ids = [r._id for r in arefs] M.artifact_orm_session.flush() index_tasks.add_artifacts(ref_ids) M.main_orm_session.flush() M.main_orm_session.clear() new_shortlinks = M.Shortlink.query.find().count() assert old_shortlinks + 5 == new_shortlinks, 'Shortlinks not created' assert solr.add.call_count == 1 sort_key = operator.itemgetter('id') assert_equal( sorted(solr.add.call_args[0][0], key=sort_key), sorted([ref.artifact.solarize() for ref in arefs], key=sort_key)) index_tasks.del_artifacts(ref_ids) M.main_orm_session.flush() M.main_orm_session.clear() new_shortlinks = M.Shortlink.query.find().count() assert old_shortlinks == new_shortlinks, 'Shortlinks not deleted' solr_query = 'id:({0})'.format(' || '.join(ref_ids)) solr.delete.assert_called_once_with(q=solr_query)
def _chunked_add_artifacts(self, ref_ids): # ref_ids contains solr index ids which can easily be over # 100 bytes. Here we allow for 160 bytes avg, plus # room for other document overhead. for chunk in utils.chunked_list(ref_ids, self.options.max_chunk): if self.options.tasks: self._post_add_artifacts(chunk) else: add_artifacts( chunk, update_solr=self.options.solr, update_refs=self.options.refs, **self.add_artifact_kwargs )
def _chunked_add_artifacts(self, ref_ids): # ref_ids contains solr index ids which can easily be over # 100 bytes. Here we allow for 160 bytes avg, plus # room for other document overhead. for chunk in utils.chunked_list(ref_ids, self.options.max_chunk): if self.options.tasks: self._post_add_artifacts(chunk) else: add_artifacts(chunk, update_solr=self.options.solr, update_refs=self.options.refs, **self.add_artifact_kwargs)
def test_add_artifacts(self): old_shortlinks = M.Shortlink.query.find().count() old_solr_size = len(g.solr.db) artifacts = [ _TestArtifact() for x in range(5) ] for i, a in enumerate(artifacts): a._shorthand_id = 't%d' % i a.text = 'This is a reference to [t3]' arefs = [ M.ArtifactReference.from_artifact(a) for a in artifacts ] ref_ids = [ r._id for r in arefs ] M.artifact_orm_session.flush() index_tasks.add_artifacts(ref_ids) new_shortlinks = M.Shortlink.query.find().count() new_solr_size = len(g.solr.db) assert old_shortlinks + 5 == new_shortlinks, 'Shortlinks not created' assert old_solr_size + 5 == new_solr_size, "Solr additions didn't happen" M.main_orm_session.flush() M.main_orm_session.clear() a = _TestArtifact.query.get(_shorthand_id='t3') assert len(a.backrefs) == 5, a.backrefs
def test_add_artifacts(self): old_shortlinks = M.Shortlink.query.find().count() old_solr_size = len(g.solr.db) artifacts = [_TestArtifact() for x in range(5)] for i, a in enumerate(artifacts): a._shorthand_id = 't%d' % i a.text = 'This is a reference to [t3]' arefs = [M.ArtifactReference.from_artifact(a) for a in artifacts] ref_ids = [r._id for r in arefs] M.artifact_orm_session.flush() index_tasks.add_artifacts(ref_ids) new_shortlinks = M.Shortlink.query.find().count() new_solr_size = len(g.solr.db) assert old_shortlinks + 5 == new_shortlinks, 'Shortlinks not created' assert old_solr_size + 5 == new_solr_size, "Solr additions didn't happen" M.main_orm_session.flush() M.main_orm_session.clear() a = _TestArtifact.query.get(_shorthand_id='t3') assert len(a.backrefs) == 5, a.backrefs
def test_del_artifacts(self): old_shortlinks = M.Shortlink.query.find().count() old_solr_size = len(g.solr.db) artifacts = [ _TestArtifact(_shorthand_id='ta_%s' % x) for x in range(5) ] M.artifact_orm_session.flush() arefs = [M.ArtifactReference.from_artifact(a) for a in artifacts] ref_ids = [r._id for r in arefs] M.artifact_orm_session.flush() index_tasks.add_artifacts(ref_ids) M.main_orm_session.flush() M.main_orm_session.clear() new_shortlinks = M.Shortlink.query.find().count() new_solr_size = len(g.solr.db) assert old_shortlinks + 5 == new_shortlinks, 'Shortlinks not created' assert old_solr_size + 5 == new_solr_size, "Solr additions didn't happen" index_tasks.del_artifacts(ref_ids) M.main_orm_session.flush() M.main_orm_session.clear() new_shortlinks = M.Shortlink.query.find().count() new_solr_size = len(g.solr.db) assert old_shortlinks == new_shortlinks, 'Shortlinks not deleted' assert old_solr_size == new_solr_size, "Solr deletions didn't happen"
def command(self): from allura import model as M self.basic_setup() graph = build_model_inheritance_graph() if self.options.project: q_project = dict(shortname=self.options.project) elif self.options.project_regex: q_project = dict(shortname={'$regex': self.options.project_regex}) elif self.options.neighborhood: neighborhood_id = M.Neighborhood.query.get( url_prefix='/%s/' % self.options.neighborhood)._id q_project = dict(neighborhood_id=neighborhood_id) else: q_project = {} # if none specified, do all if not self.options.solr and not self.options.refs: self.options.solr = self.options.refs = True for projects in utils.chunked_find(M.Project, q_project): for p in projects: c.project = p base.log.info('Reindex project %s', p.shortname) # Clear index for this project if self.options.solr and not self.options.skip_solr_delete: g.solr.delete(q='project_id_s:%s' % p._id) if self.options.refs: M.ArtifactReference.query.remove({'artifact_reference.project_id':p._id}) M.Shortlink.query.remove({'project_id':p._id}) app_config_ids = [ ac._id for ac in p.app_configs ] # Traverse the inheritance graph, finding all artifacts that # belong to this project for _, a_cls in dfs(M.Artifact, graph): base.log.info(' %s', a_cls) ref_ids = [] # Create artifact references and shortlinks for a in a_cls.query.find(dict(app_config_id={'$in': app_config_ids})): if self.options.verbose: base.log.info(' %s', a.shorthand_id()) if self.options.refs: try: M.ArtifactReference.from_artifact(a) M.Shortlink.from_artifact(a) except: base.log.exception('Making ArtifactReference/Shortlink from %s', a) continue ref_ids.append(a.index_id()) M.main_orm_session.flush() M.artifact_orm_session.clear() try: if self.options.tasks: self._chunked_add_artifacts(ref_ids) else: add_artifacts(ref_ids, update_solr=self.options.solr, update_refs=self.options.refs) except CompoundError, err: base.log.exception('Error indexing artifacts:\n%r', err) base.log.error('%s', err.format_error()) M.main_orm_session.flush() M.main_orm_session.clear()