<commit_before>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.core.db;

import java.util.HashMap;
import java.util.Map;

import com.orientechnologies.common.concur.lock.OLockException;
import com.orientechnologies.common.concur.resource.OResourcePool;
import com.orientechnologies.common.concur.resource.OResourcePoolListener;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;

public abstract class ODatabasePool<DB extends ODatabase> implements OResourcePoolListener<String, DB> {
	private static final int															DEF_WAIT_TIMEOUT	= 5000;
	private final Map<String, OResourcePool<String, DB>>	pools							= new HashMap<String, OResourcePool<String, DB>>();
	private int																						maxSize;
	private int																						timeout						= DEF_WAIT_TIMEOUT;

	public ODatabasePool(final int iMaxSize) {
		maxSize = iMaxSize;
	}

	public ODatabasePool(final int iMaxSize, final int iTimeout) {
		maxSize = iMaxSize;
		timeout = iTimeout;
	}

	public DB acquireDatabase(final String iURL) throws OLockException, InterruptedException {
		final String url = iURL;

		OResourcePool<String, DB> pool = pools.get(url);
		if (pool == null) {
			synchronized (pools) {
				if (pool == null) {
					pool = new OResourcePool<String, DB>(maxSize, this);
					pools.put(url, pool);
				}
			}
		}

		return pool.getResource(iURL, timeout);
	}

	public void releaseDatabase(final String iURL, final DB iDatabase) {
		if (iDatabase instanceof ODatabaseRecord<?>)
			((ODatabaseRecord<?>) iDatabase).rollback();

		OResourcePool<String, DB> pool = pools.get(iURL);
		if (pool == null)
			throw new OLockException("Can't release a database URL not acquired before. URL: " + iURL);

		pool.returnResource(iDatabase);
	}

	public Map<String, OResourcePool<String, DB>> getPools() {<commit_msg>Fixed problem with release of connections after last changes.<commit_after>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.core.db;

import java.util.HashMap;
import java.util.Map;

import com.orientechnologies.common.concur.lock.OLockException;
import com.orientechnologies.common.concur.resource.OResourcePool;
import com.orientechnologies.common.concur.resource.OResourcePoolListener;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;

public abstract class ODatabasePool<DB extends ODatabase> implements OResourcePoolListener<String, DB> {
	private static final int															DEF_WAIT_TIMEOUT	= 5000;
	private final Map<String, OResourcePool<String, DB>>	pools							= new HashMap<String, OResourcePool<String, DB>>();
	private int																						maxSize;
	private int																						timeout						= DEF_WAIT_TIMEOUT;

	public ODatabasePool(final int iMaxSize) {
		maxSize = iMaxSize;
	}

	public ODatabasePool(final int iMaxSize, final int iTimeout) {
		maxSize = iMaxSize;
		timeout = iTimeout;
	}

	public DB acquireDatabase(final String iURL) throws OLockException, InterruptedException {
		final String url = iURL.indexOf(":") > -1 ? iURL.substring(0, iURL.lastIndexOf(":")) : iURL;

		OResourcePool<String, DB> pool = pools.get(url);
		if (pool == null) {
			synchronized (pools) {
				if (pool == null) {
					pool = new OResourcePool<String, DB>(maxSize, this);
					pools.put(url, pool);
				}
			}
		}

		return pool.getResource(iURL, timeout);
	}

	public void releaseDatabase(final String iURL, final DB iDatabase) {
		if (iDatabase instanceof ODatabaseRecord<?>)
			((ODatabaseRecord<?>) iDatabase).rollback();

		OResourcePool<String, DB> pool = pools.get(iURL);
		if (pool == null)
			throw new OLockException("Can't release a database URL not acquired before. URL: " + iURL);

		pool.returnResource(iDatabase);
	}

	public Map<String, OResourcePool<String, DB>> getPools() {
{'fraction_non_alphanumeric': 0.09931781701444622, 'fraction_numerical': 0.006821829855537721, 'mean_word_length': 4.040444893832154, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 32, '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': '5453751', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1468, 'n_words': 450}