from BTrees.IIBTree import weightedIntersection
import warnings
+_VOLATILE_SOLR_NAME = '_v_solrConnection'
+
class SolrTransactionHook :
''' commit solr couplé sur le commit de la ZODB '''
- def __init__(self, connection) :
- self.connection = connection
+ def __init__(self, context, con) :
+ self.context = context
+ self.con = con
def __call__(self, status) :
if status :
- self.connection.commit()
- self.connection.close()
+ self.con.commit()
+ self.con.close()
else :
- self.connection.close()
+ self.con.close()
+ try :
+ delattr(self.context, _VOLATILE_SOLR_NAME)
+ except AttributeError :
+ pass
class CatalogTool(BaseCatalogTool) :
meta_type = 'Plinn Catalog'
self.solr_url = 'http://localhost:8983/solr'
self.delegatedIndexes = ('Title', 'Description', 'SearchableText')
+ def _getSolrConnection(self) :
+ if not hasattr(self, _VOLATILE_SOLR_NAME) :
+ con = SolrConnection(self.solr_url)
+ setattr(self, _VOLATILE_SOLR_NAME, con)
+ txn = transaction.get()
+ txn.addAfterCommitHook(SolrTransactionHook(self, con))
+ return getattr(self, _VOLATILE_SOLR_NAME)
+
security.declarePrivate('solrAdd')
def solrAdd(self, object, idxs=[], uid=None) :
if IIndexableObject.providedBy(object):
for name in idxs :
attr = getattr(w, name, '')
data[name] = attr() if callable(attr) else attr
- c = SolrConnection(self.solr_url)
+ c = self._getSolrConnection()
c.add(**data)
- txn = transaction.get()
- txn.addAfterCommitHook(SolrTransactionHook(c))
# PortalCatalog api overloads
"""Remove from catalog.
"""
super(CatalogTool, self).unindexObject(object)
- c = SolrConnection(self.solr_url)
+ c = self._getSolrConnection()
url = self.__url(object)
c.delete(id=url)
- txn = transaction.get()
- txn.addAfterCommitHook(SolrTransactionHook(c))
InitializeClass(CatalogTool)