package de.lmu.ifi.dbs.elki.database;
/*
This file is part of ELKI:
Environment for Developing KDD-Applications Supported by Index-Structures
Copyright (C) 2012
Ludwig-Maximilians-Universität München
Lehr- und Forschungseinheit für Datenbanksysteme
ELKI Development Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
import java.util.BitSet;
import java.util.Collection;
import de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation;
import de.lmu.ifi.dbs.elki.data.type.TypeUtil;
import de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs;
import de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs;
import de.lmu.ifi.dbs.elki.database.ids.DBID;
import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil;
import de.lmu.ifi.dbs.elki.database.relation.DBIDView;
import de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation;
import de.lmu.ifi.dbs.elki.database.relation.Relation;
import de.lmu.ifi.dbs.elki.datasource.DatabaseConnection;
import de.lmu.ifi.dbs.elki.datasource.FileBasedDatabaseConnection;
import de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle;
import de.lmu.ifi.dbs.elki.datasource.bundle.ObjectBundle;
import de.lmu.ifi.dbs.elki.index.Index;
import de.lmu.ifi.dbs.elki.index.IndexFactory;
import de.lmu.ifi.dbs.elki.logging.Logging;
import de.lmu.ifi.dbs.elki.utilities.documentation.Description;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.Parameterizable;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ObjectListParameter;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ObjectParameter;
/**
* This database class uses array-based storage and thus does not allow for
* dynamic insert, delete and update operations. However, array access is
* expected to be faster and use less memory.
*
* @author Arthur Zimek
* @author Erich Schubert
*
* @apiviz.landmark
* @apiviz.composedOf ArrayDBIDs
* @apiviz.uses DatabaseConnection
*/
@Description("Database using an in-memory hashtable and at least providing linear scans.")
public class StaticArrayDatabase extends AbstractDatabase implements Parameterizable {
/**
* Our logger
*/
private static final Logging logger = Logging.getLogger(StaticArrayDatabase.class);
/**
* IDs of this database
*/
private ArrayDBIDs ids;
/**
* The DBID representation we use
*/
private DBIDView idrep;
/**
* The data source we get the initial data from.
*/
protected DatabaseConnection databaseConnection;
/**
* Constructor.
*
* @param databaseConnection Database connection to get the initial data from.
* @param indexFactories Indexes to add
*/
public StaticArrayDatabase(DatabaseConnection databaseConnection, Collection> indexFactories) {
super();
this.databaseConnection = databaseConnection;
this.ids = null;
this.idrep = null;
// Add indexes.
if(indexFactories != null) {
this.indexFactories.addAll(indexFactories);
}
}
/**
* Constructor with no indexes.
*/
public StaticArrayDatabase() {
this(null, null);
}
/**
* Initialize the database by getting the initial data from the database
* connection.
*/
@Override
public void initialize() {
if(databaseConnection != null) {
if(logger.isDebugging()) {
logger.debugFine("Loading data from database connection.");
}
MultipleObjectsBundle objpackages = databaseConnection.loadData();
// Run at most once.
databaseConnection = null;
// Find DBID column
int idrepnr = findDBIDColumn(objpackages);
// Build DBID array
if(idrepnr == -1) {
this.ids = DBIDUtil.generateStaticDBIDRange(objpackages.dataLength());
}
else {
final ArrayModifiableDBIDs newids = DBIDUtil.newArray(objpackages.dataLength());
for(int j = 0; j < objpackages.dataLength(); j++) {
DBID newid = (DBID) objpackages.data(j, idrepnr);
newids.add(newid);
}
this.ids = newids;
}
// Replace id representation.
// TODO: this is an ugly hack
this.idrep = new DBIDView(this, this.ids);
relations.add(this.idrep);
getHierarchy().add(this, idrep);
// insert into db - note: DBIDs should have been prepared before this!
Relation>[] targets = alignColumns(objpackages);
for(int j = 0; j < objpackages.dataLength(); j++) {
// insert object
final DBID newid = ids.get(j);
for(int i = 0; i < targets.length; i++) {
// DBIDs were handled above.
if(i == idrepnr) {
continue;
}
@SuppressWarnings("unchecked")
final Relation