<commit_before>/*
 * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution for a full
 * listing of individual contributors.
 * 
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.zanata.dao;

import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.search.FullTextSession;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.transaction.Transaction;
import org.zanata.exception.EntityMissingException;
import org.zanata.model.tm.TransMemory;
import org.zanata.model.tm.TransMemoryUnit;
import com.google.common.base.Optional;

/**
 * Data Access Object for Translation Memory and related entities.
 *
 * @author <NAME> <a href="mailto:<EMAIL>"><EMAIL></a>
 */
@Name("transMemoryDAO")
@Scope(ScopeType.STATELESS)
@AutoCreate
public class TransMemoryDAO extends AbstractDAOImpl<TransMemory, Long>
{

   @In
   private FullTextSession session;

   public TransMemoryDAO()
   {
      super(TransMemory.class);
   }

   public TransMemoryDAO(Session session)
   {
      super(TransMemory.class, session);
   }

   public Optional<TransMemory> getBySlug(@Nonnull String slug)
   {
      if(!StringUtils.isEmpty(slug))
      {
         TransMemory tm = (TransMemory) getSession().byNaturalId(TransMemory.class).using("slug", slug).load();
         return Optional.fromNullable(tm);
      }
      return Optional.absent();
   }

   /**
    * Deletes the contents for a single translation memory.
    * Because TMs could potentially contain very large numbers of translation units,
    * this process is broken into multiple transactions and could take a long time.
    *
    * @param slug Translation memory identifier to clear.
    */
   public void deleteTransMemoryContents(@Nonnull String slug)
   {
      Optional<TransMemory> tm = getBySlug(slug);
      if (!tm.isPresent())
      {
         throw new EntityMissingException("Translation memory " + slug + " was not found.");
      }

      final int batchSize = 1000;
      int deleted;
      do
      {
         try
         {
            Transaction.instance().begin();
         }
         catch (Exception e)
         {
            throw new RuntimeException(e);
         }

         List<TransMemoryUnit> toRemove =
            session.createQuery("from TransMemoryUnit tu where tu.translationMemory = :tm")
                  .setParameter("tm", tm.get())
                  .setFirstResult(0)
                  .setFetchSize(batchSize)
                  .list();
<commit_msg>rhbz995964 - Fix issue with the clearing of large TMs.<commit_after>/*
 * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution for a full
 * listing of individual contributors.
 * 
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.zanata.dao;

import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.search.FullTextSession;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.transaction.Transaction;
import org.zanata.exception.EntityMissingException;
import org.zanata.model.tm.TransMemory;
import org.zanata.model.tm.TransMemoryUnit;
import com.google.common.base.Optional;

/**
 * Data Access Object for Translation Memory and related entities.
 *
 * @author <NAME> <a href="mailto:<EMAIL>"><EMAIL></a>
 */
@Name("transMemoryDAO")
@Scope(ScopeType.STATELESS)
@AutoCreate
public class TransMemoryDAO extends AbstractDAOImpl<TransMemory, Long>
{

   @In
   private FullTextSession session;

   public TransMemoryDAO()
   {
      super(TransMemory.class);
   }

   public TransMemoryDAO(Session session)
   {
      super(TransMemory.class, session);
   }

   public Optional<TransMemory> getBySlug(@Nonnull String slug)
   {
      if(!StringUtils.isEmpty(slug))
      {
         TransMemory tm = (TransMemory) getSession().byNaturalId(TransMemory.class).using("slug", slug).load();
         return Optional.fromNullable(tm);
      }
      return Optional.absent();
   }

   /**
    * Deletes the contents for a single translation memory.
    * Because TMs could potentially contain very large numbers of translation units,
    * this process is broken into multiple transactions and could take a long time.
    *
    * @param slug Translation memory identifier to clear.
    */
   public void deleteTransMemoryContents(@Nonnull String slug)
   {
      Optional<TransMemory> tm = getBySlug(slug);
      if (!tm.isPresent())
      {
         throw new EntityMissingException("Translation memory " + slug + " was not found.");
      }

      final int batchSize = 100;
      int deleted;
      do
      {
         try
         {
            Transaction.instance().begin();
         }
         catch (Exception e)
         {
            throw new RuntimeException(e);
         }

         List<TransMemoryUnit> toRemove =
            session.createQuery("from TransMemoryUnit tu where tu.translationMemory = :tm")
                  .setParameter("tm", tm.get())
                  .setFirstResult(0)
                  .setMaxResults(batchSize)
                  .list();

{'fraction_non_alphanumeric': 0.09148132605958875, 'fraction_numerical': 0.006854105469296405, 'mean_word_length': 3.1186635944700463, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4553297', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1922, 'n_words': 644}