Search in sources :

Example 1 with DBIDView

use of de.lmu.ifi.dbs.elki.database.relation.DBIDView in project elki by elki-project.

the class StaticArrayDatabase method initialize.

/**
 * Initialize the database by getting the initial data from the database
 * connection.
 */
@Override
public void initialize() {
    if (databaseConnection != null) {
        if (LOG.isDebugging()) {
            LOG.debugFine("Loading data from database connection.");
        }
        MultipleObjectsBundle bundle = databaseConnection.loadData();
        // Run at most once.
        databaseConnection = null;
        // Find DBIDs for bundle
        {
            DBIDs bids = bundle.getDBIDs();
            if (bids instanceof ArrayStaticDBIDs) {
                this.ids = (ArrayStaticDBIDs) bids;
            } else if (bids == null) {
                this.ids = DBIDUtil.generateStaticDBIDRange(bundle.dataLength());
            } else {
                this.ids = (ArrayStaticDBIDs) DBIDUtil.makeUnmodifiable(bids);
            }
        }
        // Replace id representation (it would be nicer if we would not need
        // DBIDView at all)
        this.idrep = new DBIDView(this.ids);
        relations.add(this.idrep);
        getHierarchy().add(this, idrep);
        DBIDArrayIter it = this.ids.iter();
        int numrel = bundle.metaLength();
        for (int i = 0; i < numrel; i++) {
            SimpleTypeInformation<?> meta = bundle.meta(i);
            @SuppressWarnings("unchecked") SimpleTypeInformation<Object> ometa = (SimpleTypeInformation<Object>) meta;
            WritableDataStore<Object> store = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_DB, ometa.getRestrictionClass());
            for (it.seek(0); it.valid(); it.advance()) {
                store.put(it, bundle.data(it.getOffset(), i));
            }
            Relation<?> relation = new MaterializedRelation<>(ometa, ids, null, store);
            relations.add(relation);
            getHierarchy().add(this, relation);
            // Try to add indexes where appropriate
            for (IndexFactory<?, ?> factory : indexFactories) {
                if (factory.getInputTypeRestriction().isAssignableFromType(ometa)) {
                    @SuppressWarnings("unchecked") final IndexFactory<Object, ?> ofact = (IndexFactory<Object, ?>) factory;
                    @SuppressWarnings("unchecked") final Relation<Object> orep = (Relation<Object>) relation;
                    final Index index = ofact.instantiate(orep);
                    Duration duration = LOG.isStatistics() ? LOG.newDuration(index.getClass().getName() + ".construction").begin() : null;
                    index.initialize();
                    if (duration != null) {
                        LOG.statistics(duration.end());
                    }
                    getHierarchy().add(relation, index);
                }
            }
        }
        // fire insertion event
        eventManager.fireObjectsInserted(ids);
    }
}
Also used : DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) ArrayStaticDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayStaticDBIDs) MultipleObjectsBundle(de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle) SimpleTypeInformation(de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Index(de.lmu.ifi.dbs.elki.index.Index) Duration(de.lmu.ifi.dbs.elki.logging.statistics.Duration) ArrayStaticDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayStaticDBIDs) MaterializedRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) MaterializedRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation) IndexFactory(de.lmu.ifi.dbs.elki.index.IndexFactory) DBIDView(de.lmu.ifi.dbs.elki.database.relation.DBIDView)

Example 2 with DBIDView

use of de.lmu.ifi.dbs.elki.database.relation.DBIDView in project elki by elki-project.

the class LuceneDatabase method initialize.

@Override
public void initialize() {
    try {
        reader = IndexReader.open(directory);
        ids = DBIDUtil.generateStaticDBIDRange(reader.maxDoc());
        // ID relation:
        idrep = new DBIDView(ids);
        relations.add(idrep);
        getHierarchy().add(this, idrep);
        // Documents relation:
        docrep = new LuceneDocumentRelation(ids, reader);
        relations.add(docrep);
        getHierarchy().add(this, docrep);
        eventManager.fireObjectsInserted(ids);
    } catch (CorruptIndexException e) {
        throw new AbortException("Index is corrupt.", e);
    } catch (IOException e) {
        throw new AbortException("I/O error reading index.", e);
    }
}
Also used : CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException) DBIDView(de.lmu.ifi.dbs.elki.database.relation.DBIDView) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Aggregations

DBIDView (de.lmu.ifi.dbs.elki.database.relation.DBIDView)2 SimpleTypeInformation (de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation)1 ArrayStaticDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayStaticDBIDs)1 DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)1 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)1 MaterializedRelation (de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation)1 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)1 MultipleObjectsBundle (de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle)1 Index (de.lmu.ifi.dbs.elki.index.Index)1 IndexFactory (de.lmu.ifi.dbs.elki.index.IndexFactory)1 Duration (de.lmu.ifi.dbs.elki.logging.statistics.Duration)1 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)1 IOException (java.io.IOException)1 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)1