<commit_before><?php
/**
 * Searchable via Solr template
 *
 * @package     tjSolrDoctrineBehaviorPlugin
 * @subpackage  Template
 * @author      <NAME> <<EMAIL>>
 **/
class Doctrine_Template_Solr extends Doctrine_Template
{
  protected $_options = array(
    'host' => 'localhost',
    'port' => '8983',
    'path' => '/solr',
    'key' => 'sf_unique_id',
    'fields' => array(),
    'fieldmap' => array(),
    'boost' => array()
  );

  protected static $solr;

  public function setTableDefinition()
  {
    $this->addListener(new Doctrine_Template_Listener_Solr($this->_options));
  }

  public function setUp()
  {
  }

  /**
    * Returns a solr connexion handler
    *
    * @return Apache_Solr_Service
   **/
  public function getSolrService()
  {
    static $solr;

    if(null === $solr)
    {
      $solr = new Apache_Solr_Service($this->_options['host'],
                                      $this->_options['port'],
                                      $this->_options['path']
      );
    }

    return $solr;
  }

  /**
    * Return true if the solr handler is available and connected
   **/
  public function isSearchAvailableTableProxy()
  {
    return $this->getSolrService()->ping();
  }

  /**
   * Get a unique solr document identifier
   **/
  public function getSolrId()
  {
    return sprintf('%s_%s', get_class($this->getInvoker()), $this->getInvoker()->getId());
  }

  /**
   * Index the invoker into Solr
   **/
  public function addToIndex()
  {
    $invoker = $this->getInvoker();
    $solr = $invoker->getSolrService();

    $solr->addDocument($invoker->getSolrDocument());
    $solr->commit();
  }

  /**
   * Delete the invoker from index
   **/
  public function deleteFromIndex()
  {
    $invoker = $this->getInvoker();
    $solr = $invoker->getSolrService();

    $solr->deleteById($invoker->getSolrId());
    $solr->commit();
  }

  /**
    * Build a Document for Solr indexing
    *
    * @return Apache_Solr_Document
   **/
  public function getSolrDocument()
  {
    $document = new Apache_Solr_Document();
    $invoker = $this->getInvoker();

    // Set document key
    $document->addField($this->_options['key'], $this->getSolrId());

    // set meta data
    $document->addField('sf_meta_class', get_class($invoker));
    $document->addField('sf_meta_id', $invoker->getId());

    // Set others fields
    $fields = $this->_options['fields'];
    $map = $this->_options['fieldmap'];
    $boost = $this->_options['boost'];
    foreach($fields as $field)
    {
      $fieldName = $map[$field] ? $map[$field] : $field;
      $boost = $boost[$field] ? $boost[$field] : 1;

      $value = $invoker->get($field);

      // Solr_Apache_Document always expect an array
      if(!is_array($value))
        $value = array($value);

      $document->setField($fieldName, $value, $boost);
    }

    return $document;
  }

  /**
    * Remove every indexed documents
    *
    * handle with care
   **/
  public function deleteIndexTableProxy()
  {
    $solr = $this->getSolrService();
    $solr->deleteByQuery('*:*');
    $solr->commit();
  }

  /**
   * Performs a research through Solr
   *
   * @return array The solr response as a php array
   **/
  public function searchTableProxy($search, $offset = 0, $limit = 30)
  {
    $solr = $this->getSolrService();

    // We filter the results types
    $params = array(
      'fq' => 'sf_meta_class:'.get_class($this->getInvoker())
    );

    $results = $solr->search($search, $offset, $limit, $params);
    $response = json_decode($results->getRawResponse());

    return $response->response;
  }

  /**
   * Generate a doctrine query based on a Solr search
   *
   * @return Doctrine_Query
   **/
  public function createSearchQueryTableProxy($search, $limit = 256)
  {
    $response = $this->getTable()->search($search, 0, $limit);

    $pks = array();
    foreach($response->docs as $doc)
    {
      $pks[] = $doc->sf_meta_id;
    }

    $q = $this->getTable()->createQuery();
    $alias = $q->getRootAlias();

    if($pks)
    {
      $q->whereIn($alias.'.id', $pks);
      // preserve score order
      $q->addSelect(sprintf('FIELD(%s.id,%s) as field', $alias, implode(',', $pks)));
      $q->orderBy('field');
    }
    else
    {
      $q->whereIn($alias.'.id', -1);
    }

    return $q;
  }
}<commit_msg>manage transactions<commit_after><?php
/**
 * Searchable via Solr template
 *
 * @package     tjSolrDoctrineBehaviorPlugin
 * @subpackage  Template
 * @author      <NAME> <<EMAIL>>
 **/
class Doctrine_Template_Solr extends Doctrine_Template
{
  protected $_options = array(
    'host' => 'localhost',
    'port' => '8983',
    'path' => '/solr',
    'key' => 'sf_unique_id',
    'fields' => array(),
    'fieldmap' => array(),
    'boost' => array()
  );

  protected static $solr;
  protected $_inTransaction;

  public function setTableDefinition()
  {
    $this->addListener(new Doctrine_Template_Listener_Solr($this->_options));
  }

  public function setUp()
  {
    $this->_inTransaction = 0;
  }

  /**
    * Returns a solr connexion handler
    *
    * @return Apache_Solr_Service
   **/
  public function getSolrService()
  {
    static $solr;

    if(null === $solr)
    {
      $solr = new Apache_Solr_Service($this->_options['host'],
                                      $this->_options['port'],
                                      $this->_options['path']
      );
    }

    return $solr;
  }

  /**
    * Return true if the solr handler is available and connected
   **/
  public function isSearchAvailableTableProxy()
  {
    return $this->getSolrService()->ping();
  }

  /**
   * Get a unique solr document identifier
   **/
  public function getSolrId()
  {
    return sprintf('%s_%s', get_class($this->getInvoker()), $this->getInvoker()->getId());
  }

  /**
   * Index the invoker into Solr
   **/
  public function addToIndex()
  {
    $invoker = $this->getInvoker();
    $solr = $invoker->getSolrService();

    $solr->addDocument($invoker->getSolrDocument());

    if($this->_inTransaction == 0)
      $solr->commit();
  }

  /**
   * Delete the invoker from index
   **/
  public function deleteFromIndex()
  {
    $invoker = $this->getInvoker();
    $solr = $invoker->getSolrService();

    $solr->deleteById($invoker->getSolrId());

    if($this->_inTransaction == 0)
      $solr->commit();
  }

  /**
    * Build a Document for Solr indexing
    *
    * @return Apache_Solr_Document
   **/
  public function getSolrDocument()
  {
    $document = new Apache_Solr_Document();
    $invoker = $this->getInvoker();

    // Set document key
    $document->addField($this->_options['key'], $this->getSolrId());

    // set meta data
    $document->addField('sf_meta_class', get_class($invoker));
    $document->addField('sf_meta_id', $invoker->getId());

    // Set others fields
    $fields = $this->_options['fields'];
    $map = $this->_options['fieldmap'];
    $boost = $this->_options['boost'];
    foreach($fields as $field)
    {
      $fieldName = $map[$field] ? $map[$field] : $field;
      $boost = $boost[$field] ? $boost[$field] : 1;

      $value = $invoker->get($field);

      // Solr_Apache_Document always expect an array
      if(!is_array($value))
        $value = array($value);

      $document->setField($fieldName, $value, $boost);
    }

    return $document;
  }

  /**
    * Remove every indexed documents
    *
    * handle with care
   **/
  public function deleteIndexTableProxy()
  {
    $solr = $this->getSolrService();
    $solr->deleteByQuery('*:*');

    if($this->_inTransaction == 0)
      $solr->commit();
  }

  /**
   * Performs a research through Solr
   *
   * @return array The solr response as a php array
   **/
  public function searchTableProxy($search, $offset = 0, $limit = 30)
  {
    $solr = $this->getSolrService();

    // We filter the results types
    $params = array(
      'fq' => 'sf_meta_class:'.get_class($this->getInvoker())
    );

    $results = $solr->search($search, $offset, $limit, $params);
    $response = json_decode($results->getRawResponse());

    return $response->response;
  }

  /**
   * Generate a doctrine query based on a Solr search
   *
   * @return Doctrine_Query
   **/
  public function createSearchQueryTableProxy($search, $limit = 256)
  {
    $response = $this->getTable()->search($search, 0, $limit);

    $pks = array();
    foreach($response->docs as $doc)
    {
      $pks[] = $doc->sf_meta_id;
    }

    $q = $this->getTable()->createQuery();
    $alias = $q->getRootAlias();

    if($pks)
    {
      $q->whereIn($alias.'.id', $pks);
      // preserve score order
      $q->addSelect(sprintf('FIELD(%s.id,%s) as field', $alias, implode(',', $pks)));
      $q->orderBy('field');
    }
    else
    {
      $q->whereIn($alias.'.id', -1);
    }

    return $q;
  }

  /**
    * Starts a transaction for indexing
    *
    * When using a transaction, the amount of processing that solr does
    * decreases, increasing indexing performance. Without this, we
    * sends a commit after every document that is indexed. Transactions can be
    * nested, when commit() is called the same number of times as
    * beginTransaction(), we send a commit.
   **/
  public function beginTransactionTableProxy()
  {
    $this->_inTransaction++;
  }

  /**
    * Ends a transaction, and sends a commit message to Solr
    *
    * As transactions can be nested, we only send a real commit
    * when all transactions are closed
   **/
  public function commitTableProxy()
  {
    if($this->_inTransaction < 1)
      throw new sfException('Cannot commit when not in transaction');

    $this->_inTransaction--;

    if($this->_inTransaction == 0)
    {
      $this->getSolrService()->commit();
    }
  }
}
{'fraction_non_alphanumeric': 0.16474345765505874, 'fraction_numerical': 0.00329692973418504, 'mean_word_length': 2.726295585412668, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 137, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '793021', 'n_tokens_mistral': 3259, 'n_tokens_neox': 3120, 'n_words': 685}