All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.data.SplitQueriesDataSetStrategy Maven / Gradle / Ivy

Go to download

The eobjects.dk MetaModel is a common domain model, query-engine and optimizer for different kinds of datastores.

The newest version!
/*
 * Copyright 2008 eobjects.dk
 *
 * 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 dk.eobjects.metamodel.data;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dk.eobjects.metamodel.DataContext;
import dk.eobjects.metamodel.MetaModelException;
import dk.eobjects.metamodel.query.Query;
import dk.eobjects.metamodel.query.SelectItem;

/**
 * DataSet strategy for split queries. Queries will be executed as needed, not
 * at once.
 * 
 * @see QuerySplitter
 */
public class SplitQueriesDataSetStrategy implements IDataSetStrategy {

	private static final Log _log = LogFactory
			.getLog(SplitQueriesDataSetStrategy.class);
	private DataContext _dataContext;
	private Iterator _queryIterator;
	private DataSet _currentDataSet;
	private SelectItem[] _selectItems;
	private int _queryIndex = 0;

	public SplitQueriesDataSetStrategy(DataContext dataContext,
			List splitQueries) {
		if (dataContext == null || splitQueries == null) {
			throw new IllegalArgumentException("Arguments cannot be null");
		}
		_dataContext = dataContext;
		_queryIterator = splitQueries.iterator();
		if (splitQueries.size() == 0) {
			_selectItems = new SelectItem[0];
		} else {
			_selectItems = splitQueries.get(0).getSelectClause().getItems()
					.toArray(new SelectItem[0]);
		}
	}

	public void close() {
		if (_currentDataSet != null) {
			_log.debug("currentDataSet.close() and System.gc()");
			_currentDataSet.close();
			System.gc();
		}
		_currentDataSet = null;
		_dataContext = null;
		_queryIterator = null;
	}

	public Row getRow() throws MetaModelException {
		if (_currentDataSet != null) {
			return _currentDataSet.getRow();
		}
		throw new IllegalStateException(
				"No rows available. Either DataSet is closed or next() hasn't been called");
	}

	public SelectItem[] getSelectItems() {
		return _selectItems;
	}

	public boolean next() {
		boolean result;
		if (_currentDataSet == null) {
			result = false;
		} else {
			result = _currentDataSet.next();
		}
		if (!result && _queryIterator.hasNext()) {
			if (_currentDataSet != null) {
				_log.debug("currentDataSet.close() and System.gc()");
				_currentDataSet.close();
				System.gc();
			}
			Query q = _queryIterator.next();
			_currentDataSet = _dataContext.executeQuery(q);
			if (_log.isDebugEnabled()) {
				_queryIndex++;
				_log.debug("Executing query #" + _queryIndex);
			}
			result = next();
		}
		return result;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy