<commit_before>package org.ohdsi.webapi.cohortimportexport;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * Imports exports cohorts using something like http://localhost:8080/WebAPI/import/cohort/7
 *
 */
@Path("/cohort/")
@Component
public class CohortImportExportService {

	@Autowired
	public CohortRepository cohortRepository;

	@GET
	@Path("import/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<CohortEntity> getCohortListById(@PathParam("id") final long id) {

		List<CohortEntity> d = this.cohortRepository.getAllCohortsForId(id);
		

		return d;
	}
	
	@POST
	@Path("export")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String saveCohortListToCDM(List<CohortEntity> cohort) {

		this.cohortRepository.save(cohort);
		//System.out.println(cohort);

		return "ok";
	}

}
<commit_msg>Added flushing of the inserts to the save cohort to help speed up the insert<commit_after>package org.ohdsi.webapi.cohortimportexport;

import java.util.List;

import javax.persistence.EntityManager;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
/**
 * Imports exports cohorts using something like http://localhost:8080/WebAPI/import/cohort/7
 *
 */
@Path("/cohort/")
@Component
public class CohortImportExportService {

	@Autowired
	public CohortRepository cohortRepository;
	
	@Autowired
    private TransactionTemplate transactionTemplate;
	
	@Autowired
    private EntityManager em;

	@GET
	@Path("import/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<CohortEntity> getCohortListById(@PathParam("id") final long id) {

		List<CohortEntity> d = this.cohortRepository.getAllCohortsForId(id);
		

		return d;
	}
	
	@POST
	@Path("export")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String saveCohortListToCDM(final List<CohortEntity> cohort) {

		this.transactionTemplate.execute(new TransactionCallback<Void>() {
            @Override
            public Void doInTransaction(TransactionStatus status) {
                int i = 0;
                for (CohortEntity cohortEntity : cohort) {
                	em.persist(cohortEntity);
                    if (i % 5 == 0) { //5, same as the JDBC batch size
                        //flush a batch of inserts and release memory:
                        em.flush();
                        em.clear();
                    }
                    i++;
                }
                return null;
            }
        });
        
		//System.out.println(cohort);

		return "ok";
	}

}

{'fraction_non_alphanumeric': 0.10896847260483625, 'fraction_numerical': 0.0042852770125497396, 'mean_word_length': 3.489010989010989, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2280328', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1006, 'n_words': 197}