<commit_before>
// $Id: RDBMSTopicMapReference.java,v 1.42 2007/08/30 09:12:31 geir.gronmo Exp $

package net.ontopia.topicmaps.impl.rdbms;

import java.util.Map;

import net.ontopia.infoset.core.LocatorIF;
import net.ontopia.persistence.proxy.StorageIF;
import net.ontopia.topicmaps.core.StoreDeletedException;
import net.ontopia.topicmaps.core.TopicMapStoreFactoryIF;
import net.ontopia.topicmaps.core.TopicMapStoreIF;
import net.ontopia.topicmaps.entry.AbstractTopicMapReference;
import net.ontopia.topicmaps.impl.utils.AbstractTopicMapStore;
import net.ontopia.topicmaps.impl.utils.StorePoolableObjectFactory;
import net.ontopia.utils.OntopiaRuntimeException;
import net.ontopia.utils.PropertyUtils;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * INTERNAL: RDBMS database topic map reference.
 */

public class RDBMSTopicMapReference extends AbstractTopicMapReference {

  // Define a logging category.
  static Logger log = LoggerFactory.getLogger(RDBMSTopicMapReference.class.getName());

  protected StorageIF storage;

  protected long topicmap_id;

  protected LocatorIF base_address;

  // store pool
  protected StorePoolableObjectFactory ofactory;
  protected GenericObjectPool pool;
  protected RDBMSTopicMapStore rostore;

  public RDBMSTopicMapReference(String _id, String _title, StorageIF _storage,
      long _topicmap_id, LocatorIF _base_address) {
    super(_id, _title);
    this.storage = _storage;
    this.topicmap_id = _topicmap_id;
    this.base_address = _base_address;
  }

  protected RDBMSTopicMapStore _createStore(boolean readonly) {
    RDBMSTopicMapStore store = new RDBMSTopicMapStore(storage, topicmap_id);
    store.setReadOnly(readonly);
    store.setReference(RDBMSTopicMapReference.this);
    if (base_address != null)
      store.setBaseAddressOverride(base_address);
    return store;    
  }

  protected void init() {
    // store factory
    TopicMapStoreFactoryIF sfactory = new TopicMapStoreFactoryIF() {
      public TopicMapStoreIF createStore() {
       return _createStore(false);
      }
    };

    // initialize pool
    this.ofactory = new StorePoolableObjectFactory(sfactory);
    this.pool = new GenericObjectPool(ofactory);
    this.pool.setTestOnBorrow(true);

    Map properties = storage.getProperties();
    if (properties != null) {
      // Set minimum pool size (default: 0)
      String _minsize = PropertyUtils.getProperty(properties,
          "net.ontopia.topicmaps.impl.rdbms.StorePool.MinimumSize", false);
      int minsize = (_minsize == null ? 0 : Integer.parseInt(_minsize));
      log.debug("Setting StorePool.MinimumSize '" + minsize + "'");
      pool.setMinIdle(minsize); // 0 = no limit

      // Set maximum pool size (default: Integer.MAX_VALUE)
      String _maxsize = PropertyUtils.getProperty(properties,
          "net.ontopia.topicmaps.impl.rdbms.StorePool.MaximumSize", false);
      int maxsize = (_maxsize == null ? 8 : Integer.parseInt(_maxsize));
      log.debug("Setting StorePool.MaximumSize '" + maxsize + "'");
      pool.setMaxActive(maxsize); // 0 = no limit

      // Set soft maximum - emergency objects (default: false)
      boolean softmax = PropertyUtils.isTrue(properties,
          "net.ontopia.topicmaps.impl.rdbms.StorePool.SoftMaximum", false);
      log.debug("Setting StorePool.SoftMaximum '" + softmax + "'");
      if (softmax)
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
      else
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    }
  }

  public synchronized void open() {
    // ignore if already open
    if (isOpen())
      return;
    if (isDeleted())<commit_msg>Added WhenExhaustedAction pool option to StorePool<commit_after>
// $Id: RDBMSTopicMapReference.java,v 1.42 2007/08/30 09:12:31 geir.gronmo Exp $

package net.ontopia.topicmaps.impl.rdbms;

import java.util.Map;

import net.ontopia.infoset.core.LocatorIF;
import net.ontopia.persistence.proxy.StorageIF;
import net.ontopia.topicmaps.core.StoreDeletedException;
import net.ontopia.topicmaps.core.TopicMapStoreFactoryIF;
import net.ontopia.topicmaps.core.TopicMapStoreIF;
import net.ontopia.topicmaps.entry.AbstractTopicMapReference;
import net.ontopia.topicmaps.impl.utils.AbstractTopicMapStore;
import net.ontopia.topicmaps.impl.utils.StorePoolableObjectFactory;
import net.ontopia.utils.OntopiaRuntimeException;
import net.ontopia.utils.PropertyUtils;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * INTERNAL: RDBMS database topic map reference.
 */

public class RDBMSTopicMapReference extends AbstractTopicMapReference {

  public static final String EXHAUSED_BLOCK = "block";
  public static final String EXHAUSED_GROW = "grow";
  public static final String EXHAUSED_FAIL = "fail";

  // Define a logging category.
  static Logger log = LoggerFactory.getLogger(RDBMSTopicMapReference.class.getName());

  protected StorageIF storage;

  protected long topicmap_id;

  protected LocatorIF base_address;

  // store pool
  protected StorePoolableObjectFactory ofactory;
  protected GenericObjectPool pool;
  protected RDBMSTopicMapStore rostore;

  public RDBMSTopicMapReference(String _id, String _title, StorageIF _storage,
      long _topicmap_id, LocatorIF _base_address) {
    super(_id, _title);
    this.storage = _storage;
    this.topicmap_id = _topicmap_id;
    this.base_address = _base_address;
  }

  protected RDBMSTopicMapStore _createStore(boolean readonly) {
    RDBMSTopicMapStore store = new RDBMSTopicMapStore(storage, topicmap_id);
    store.setReadOnly(readonly);
    store.setReference(RDBMSTopicMapReference.this);
    if (base_address != null)
      store.setBaseAddressOverride(base_address);
    return store;    
  }

  protected void init() {
    // store factory
    TopicMapStoreFactoryIF sfactory = new TopicMapStoreFactoryIF() {
      public TopicMapStoreIF createStore() {
       return _createStore(false);
      }
    };

    // initialize pool
    this.ofactory = new StorePoolableObjectFactory(sfactory);
    this.pool = new GenericObjectPool(ofactory);
    this.pool.setTestOnBorrow(true);

    Map properties = storage.getProperties();
    if (properties != null) {
      // Set minimum pool size (default: 0)
      String _minsize = PropertyUtils.getProperty(properties,
          "net.ontopia.topicmaps.impl.rdbms.StorePool.MinimumSize", false);
      int minsize = (_minsize == null ? 0 : Integer.parseInt(_minsize));
      log.debug("Setting StorePool.MinimumSize '" + minsize + "'");
      pool.setMinIdle(minsize); // 0 = no limit

      // Set maximum pool size (default: Integer.MAX_VALUE)
      String _maxsize = PropertyUtils.getProperty(properties,
          "net.ontopia.topicmaps.impl.rdbms.StorePool.MaximumSize", false);
      int maxsize = (_maxsize == null ? 8 : Integer.parseInt(_maxsize));
      log.debug("Setting StorePool.MaximumSize '" + maxsize + "'");
      pool.setMaxActive(maxsize); // 0 = no limit

      // Set soft maximum - emergency objects (default: false)
      boolean softmax = PropertyUtils.isTrue(properties,
          "net.ontopia.topicmaps.impl.rdbms.StorePool.SoftMaximum", false);
      log.debug("Setting StorePool.SoftMaximum '" + softmax + "'");
      if (softmax)
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
      else
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    }

    // allow the user to fully overwrite exhausted options
    String _whenExhaustedAction = PropertyUtils.getProperty(properties, "net.ontopia.topicmaps.impl.rdbms.StorePool.WhenExhaustedAction", false);
    if (EXHAUSED_BLOCK.equals(_whenExhaustedAction))
      pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    if (EXHAUSED_GROW.equals(_whenExhaustedAction))
      pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    if (EXHAUSED_FAIL.equals(_whenExhaustedAction))
      pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);

    if (pool.getWhenExhaustedAction() == GenericObjectPool.WHEN_EXHAUSTED_BLOCK)
      log.debug("Pool is set to block on exhaused");
    if (pool.getWhenExhaustedAction() == GenericObjectPool.WHEN_EXHAUSTED_GROW)
      log.debug("Pool is set to grow on exhaused");
    if (pool.getWhenExhaustedAction() == GenericObjectPool.WHEN_EXHAUSTED_FAIL)
      log.debug("Pool is set to fail on exhaused");

  }

  public synchronized void open() {
    // ignore if already open
    if (isOpen())
      return;
    if (isDeleted())
{'fraction_non_alphanumeric': 0.09820289855072464, 'fraction_numerical': 0.005565217391304348, 'mean_word_length': 4.14370900417412, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5014902', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2616, 'n_words': 520}