<commit_before>package org.waag.ah.bigdata;

import javax.ejb.Singleton;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationConverter;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.repository.sail.SailRepositoryConnection;
import org.openrdf.sail.Sail;
import org.openrdf.sail.SailException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.waag.ah.PlatformConfigHelper;
import org.waag.ah.RepositoryConnectionFactory;
import org.waag.ah.exception.ConnectionException;
import org.waag.ah.useekm.PostgisIndexerSettingsGenerator;

import com.bigdata.journal.Options;
import com.bigdata.rdf.sail.BigdataSailRepository;
import com.useekm.bigdata.BigdataSail;
import com.useekm.indexing.IndexingSail;
import com.useekm.indexing.exception.IndexException;
import com.useekm.indexing.postgis.PostgisIndexerSettings;

@Singleton
public class BigdataConnectionService implements RepositoryConnectionFactory {
	final static Logger logger = LoggerFactory.getLogger(BigdataConnectionService.class);

	private BigdataSailRepository bigdataRepo;
	private PropertiesConfiguration properties;
	private Sail sail;
	private SailRepository repo; 

	private synchronized void connect() throws ConnectionException {
		
		try {
			if (bigdataRepo == null) {
				bigdataRepo = createRepository(loadProperties());
				bigdataRepo.initialize();
			}
			if (repo == null) {
				sail = getIndexingSail();
				repo = new SailRepository(sail);
			}
		} catch (Exception e) {
			throw new ConnectionException(e.getMessage(), e);
		}
	}
	
	/**
	 * Get BigData journal file location from platform config and merge it with
	 * the repository connection properties.
	 *
	 * @author <NAME> <<EMAIL>>
	 * @throws ConfigurationException 
	 * @since Mar 8, 2012
	 */
	private Configuration loadProperties() throws ConfigurationException {
		PropertiesConfiguration config = PlatformConfigHelper.getConfig(); 
		properties = new PropertiesConfiguration("bigdata.properties");
		properties.setProperty(Options.FILE, config.getProperty("bigdata.journal"));
		return properties;
	}
	
	@Override
	public Sail getSail() throws ConnectionException {
		connect();
		return new BigdataSail(bigdataRepo);
	}
	
	@Override
	public Sail getIndexingSail() throws ConnectionException {
		Sail sail = getSail();		
		PostgisIndexerSettings settings = PostgisIndexerSettingsGenerator.generateSettings();
		IndexingSail idxSail = new IndexingSail(sail, settings);
		try {
			idxSail.getConnection().reindex();
		} catch (IndexException e) {
			logger.error(e.getMessage(), e);
		} catch (SailException e) {
			throw new ConnectionException(e.getMessage(), e);
		}
		return idxSail;		
	}
	
	/**
	 * Return read/write connection to the BigData repository.
	 *
	 * @author <NAME> <<EMAIL>>
	 * @throws ConfigurationException 
	 * @since Mar 8, 2012
	 */
	public SailRepositoryConnection getConnection() throws ConnectionException { 
//		BigdataSailRepositoryConnection conn = repo.getReadWriteConnection();
//		SailRepository repo = new SailRepository(sail);<commit_msg>Removed bug: endless recursion in connect function<commit_after>package org.waag.ah.bigdata;

import javax.ejb.Singleton;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationConverter;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.repository.sail.SailRepositoryConnection;
import org.openrdf.sail.Sail;
import org.openrdf.sail.SailException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.waag.ah.PlatformConfigHelper;
import org.waag.ah.RepositoryConnectionFactory;
import org.waag.ah.exception.ConnectionException;
import org.waag.ah.useekm.PostgisIndexerSettingsGenerator;

import com.bigdata.journal.Options;
import com.bigdata.rdf.sail.BigdataSailRepository;
import com.useekm.bigdata.BigdataSail;
import com.useekm.indexing.IndexingSail;
import com.useekm.indexing.IndexingSailConnection;
import com.useekm.indexing.exception.IndexException;
import com.useekm.indexing.postgis.PostgisIndexerSettings;

@Singleton
public class BigdataConnectionService implements RepositoryConnectionFactory {
	final static Logger logger = LoggerFactory.getLogger(BigdataConnectionService.class);

	private BigdataSailRepository bigdataRepo;
	private PropertiesConfiguration properties;
	private Sail sail;
	private SailRepository repo; 

	private synchronized void connect() throws ConnectionException {
		
		try {
			if (bigdataRepo == null) {
				bigdataRepo = createRepository(loadProperties());
				bigdataRepo.initialize();
			}
			if (repo == null) {
				sail = getIndexingSail();
				repo = new SailRepository(sail);
			}
		} catch (Exception e) {
			throw new ConnectionException(e.getMessage(), e);
		}
	}
	
	/**
	 * Get BigData journal file location from platform config and merge it with
	 * the repository connection properties.
	 *
	 * @author <NAME> <<EMAIL>>
	 * @throws ConfigurationException 
	 * @since Mar 8, 2012
	 */
	private Configuration loadProperties() throws ConfigurationException {
		PropertiesConfiguration config = PlatformConfigHelper.getConfig(); 
		properties = new PropertiesConfiguration("bigdata.properties");
		properties.setProperty(Options.FILE, config.getProperty("bigdata.journal"));
		return properties;
	}
	
	@Override
	public Sail getSail() throws ConnectionException {
		connect();
		return new BigdataSail(bigdataRepo);
	}
	
	@Override
	public Sail getIndexingSail() throws ConnectionException {
		Sail sail = new BigdataSail(bigdataRepo);		
		PostgisIndexerSettings settings = PostgisIndexerSettingsGenerator.generateSettings();
		IndexingSail idxSail = new IndexingSail(sail, settings);
		try {
			IndexingSailConnection idxConn = idxSail.getConnection();
			idxConn.reindex();
			idxConn.commit();
			idxConn.close();
		} catch (IndexException e) {
			logger.error(e.getMessage(), e);
		} catch (SailException e) {
			throw new ConnectionException(e.getMessage(), e);
		}
		return idxSail;		
	}
	
	/**
	 * Return read/write connection to the BigData repository.
	 *
	 * @author <NAME> <<EMAIL>>
	 * @throws ConfigurationException 
	 * @since Mar 8, 2012
	 */
	public SailRepositoryConnection getConnection() throws ConnectionException { 
//		BigdataSailRepositoryConnection conn = repo.getReadWriteConnection();
//		SailRepository repo = new SailRepository(sail);
{'fraction_non_alphanumeric': 0.09493577439834637, 'fraction_numerical': 0.0035434814705448104, 'mean_word_length': 5.426944971537002, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2374763', 'n_tokens_mistral': 2096, 'n_tokens_neox': 1933, 'n_words': 429}