Search in sources :

Example 1 with MetadataDeleter

use of org.locationtech.geowave.core.store.operations.MetadataDeleter in project geowave by locationtech.

the class RecalculateStatsCommand method performStatsCommand.

@Override
protected boolean performStatsCommand(final DataStorePluginOptions storeOptions, final StatsCommandLineOptions statsOptions, final Console console) throws IOException {
    final DataStore dataStore = storeOptions.createDataStore();
    final DataStatisticsStore statStore = storeOptions.createDataStatisticsStore();
    final IndexStore indexStore = storeOptions.createIndexStore();
    if (all) {
        // check for legacy stats table and if it exists, delete it and add all default stats
        final DataStoreOperations ops = storeOptions.createDataStoreOperations();
        final MetadataReader reader = ops.createMetadataReader(MetadataType.LEGACY_STATISTICS);
        boolean legacyStatsExists;
        // implementation to check for at least one row
        try (CloseableIterator<GeoWaveMetadata> it = reader.query(new MetadataQuery(null, null))) {
            legacyStatsExists = it.hasNext();
        }
        if (legacyStatsExists) {
            console.println("Found legacy stats prior to v1.3. Deleting and recalculating all default stats as a migration to v" + VersionUtils.getVersion() + ".");
            // first let's do the add just to make sure things are in working order prior to deleting
            // legacy stats
            console.println("Adding default statistics...");
            final List<Statistic<?>> defaultStatistics = new ArrayList<>();
            for (final Index index : dataStore.getIndices()) {
                if (index instanceof DefaultStatisticsProvider) {
                    defaultStatistics.addAll(((DefaultStatisticsProvider) index).getDefaultStatistics());
                }
            }
            for (final DataTypeAdapter<?> adapter : dataStore.getTypes()) {
                final DefaultStatisticsProvider defaultStatProvider = BaseDataStoreUtils.getDefaultStatisticsProvider(adapter);
                if (defaultStatProvider != null) {
                    defaultStatistics.addAll(defaultStatProvider.getDefaultStatistics());
                }
            }
            dataStore.addEmptyStatistic(defaultStatistics.toArray(new Statistic[defaultStatistics.size()]));
            console.println("Deleting legacy statistics...");
            try (MetadataDeleter deleter = ops.createMetadataDeleter(MetadataType.LEGACY_STATISTICS)) {
                deleter.delete(new MetadataQuery(null, null));
            } catch (final Exception e) {
                LOGGER.warn("Error deleting legacy statistics", e);
            }
            // Clear out all options so that all stats get recalculated.
            statsOptions.setIndexName(null);
            statsOptions.setTypeName(null);
            statsOptions.setFieldName(null);
            statsOptions.setStatType(null);
            statsOptions.setTag(null);
        }
    }
    final List<Statistic<? extends StatisticValue<?>>> toRecalculate = statsOptions.resolveMatchingStatistics(dataStore, statStore, indexStore);
    if (toRecalculate.isEmpty()) {
        throw new ParameterException("A matching statistic could not be found");
    } else if ((toRecalculate.size() > 1) && !all) {
        throw new ParameterException("Multiple statistics matched the given parameters.  If this is intentional, " + "supply the --all option, otherwise provide additional parameters to " + "specify which statistic to recalculate.");
    }
    final Statistic<?>[] toRecalcArray = toRecalculate.toArray(new Statistic<?>[toRecalculate.size()]);
    dataStore.recalcStatistic(toRecalcArray);
    console.println(toRecalculate.size() + " statistic" + (toRecalculate.size() == 1 ? " was" : "s were") + " successfully recalculated.");
    return true;
}
Also used : DataStoreOperations(org.locationtech.geowave.core.store.operations.DataStoreOperations) StatisticValue(org.locationtech.geowave.core.store.api.StatisticValue) ArrayList(java.util.ArrayList) MetadataReader(org.locationtech.geowave.core.store.operations.MetadataReader) DefaultStatisticsProvider(org.locationtech.geowave.core.store.statistics.DefaultStatisticsProvider) GeoWaveMetadata(org.locationtech.geowave.core.store.entities.GeoWaveMetadata) Index(org.locationtech.geowave.core.store.api.Index) ParameterException(com.beust.jcommander.ParameterException) IOException(java.io.IOException) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) Statistic(org.locationtech.geowave.core.store.api.Statistic) MetadataDeleter(org.locationtech.geowave.core.store.operations.MetadataDeleter) DataStore(org.locationtech.geowave.core.store.api.DataStore) ParameterException(com.beust.jcommander.ParameterException) IndexStore(org.locationtech.geowave.core.store.index.IndexStore) MetadataQuery(org.locationtech.geowave.core.store.operations.MetadataQuery)

Example 2 with MetadataDeleter

use of org.locationtech.geowave.core.store.operations.MetadataDeleter in project geowave by locationtech.

the class AbstractGeoWavePersistence method deleteObjects.

protected static <T extends Persistable> boolean deleteObjects(final ByteArray primaryId, final ByteArray secondaryId, final boolean primaryIdPrefix, final DataStoreOperations operations, final MetadataType type, final AbstractGeoWavePersistence<T> cacheDeleter, final String... authorizations) {
    try {
        if (!operations.metadataExists(type)) {
            return false;
        }
    } catch (final IOException e1) {
        LOGGER.error("Unable to check for existence of metadata to delete objects", e1);
        return false;
    }
    try (final MetadataDeleter deleter = operations.createMetadataDeleter(type)) {
        if ((primaryId == null) && (secondaryId == null)) {
            // merely a performance optimization)
            if (cacheDeleter != null) {
                cacheDeleter.cache.invalidateAll();
            }
            return deleter.delete(new MetadataQuery((byte[]) null, (byte[]) null, authorizations));
        }
        boolean retVal = false;
        final MetadataReader reader = operations.createMetadataReader(type);
        try (final CloseableIterator<GeoWaveMetadata> it = reader.query(new MetadataQuery(primaryId != null ? primaryId.getBytes() : null, secondaryId != null ? secondaryId.getBytes() : null, primaryIdPrefix, authorizations))) {
            while (it.hasNext()) {
                retVal = true;
                final GeoWaveMetadata entry = it.next();
                if (cacheDeleter != null) {
                    cacheDeleter.deleteObjectFromCache(new ByteArray(entry.getPrimaryId()), new ByteArray(entry.getSecondaryId()));
                }
                deleter.delete(new MetadataQuery(entry.getPrimaryId(), entry.getSecondaryId(), authorizations));
            }
        }
        return retVal;
    } catch (final Exception e) {
        LOGGER.error("Unable to delete objects", e);
    }
    return false;
}
Also used : MetadataDeleter(org.locationtech.geowave.core.store.operations.MetadataDeleter) MetadataReader(org.locationtech.geowave.core.store.operations.MetadataReader) ByteArray(org.locationtech.geowave.core.index.ByteArray) GeoWaveMetadata(org.locationtech.geowave.core.store.entities.GeoWaveMetadata) IOException(java.io.IOException) IOException(java.io.IOException) MetadataQuery(org.locationtech.geowave.core.store.operations.MetadataQuery)

Example 3 with MetadataDeleter

use of org.locationtech.geowave.core.store.operations.MetadataDeleter in project geowave by locationtech.

the class MigrationCommand method migrate0to1.

public void migrate0to1(final DataStorePluginOptions options, final DataStoreOperations operations, final Console console) {
    console.println("Migration 1.x -> 2.x");
    final DataStore dataStore = options.createDataStore();
    console.println("  Migrating data type adapters...");
    final PersistentAdapterStore adapterStore = options.createAdapterStore();
    final List<Short> adapterIDs = Lists.newArrayList();
    int migratedAdapters = 0;
    final InternalDataAdapter<?>[] adapters = adapterStore.getAdapters();
    for (final InternalDataAdapter<?> adapter : adapters) {
        adapterIDs.add(adapter.getAdapterId());
        if (adapter instanceof LegacyInternalDataAdapterWrapper) {
            adapterStore.removeAdapter(adapter.getAdapterId());
            // Write updated adapter
            adapterStore.addAdapter(((LegacyInternalDataAdapterWrapper<?>) adapter).getUpdatedAdapter());
            migratedAdapters++;
        } else if (adapter.getAdapter() instanceof LegacyFeatureDataAdapter) {
            final FeatureDataAdapter updatedAdapter = ((LegacyFeatureDataAdapter) adapter.getAdapter()).getUpdatedAdapter();
            final VisibilityHandler visibilityHandler = ((LegacyFeatureDataAdapter) adapter.getAdapter()).getVisibilityHandler();
            // Write updated adapter
            adapterStore.removeAdapter(adapter.getAdapterId());
            adapterStore.addAdapter(updatedAdapter.asInternalAdapter(adapter.getAdapterId(), visibilityHandler));
            migratedAdapters++;
        }
    }
    if (migratedAdapters > 0) {
        console.println("    Migrated " + migratedAdapters + " data type adapters.");
    } else {
        console.println("    No data type adapters needed to be migrated.");
    }
    console.println("  Migrating indices...");
    final IndexStore indexStore = options.createIndexStore();
    int migratedIndices = 0;
    try (CloseableIterator<Index> indices = indexStore.getIndices()) {
        while (indices.hasNext()) {
            final Index index = indices.next();
            final CommonIndexModel indexModel = index.getIndexModel();
            // if the index model uses any spatial fields, update and re-write
            if ((indexModel != null) && (indexModel instanceof BasicIndexModel)) {
                final NumericDimensionField<?>[] oldFields = indexModel.getDimensions();
                final NumericDimensionField<?>[] updatedFields = new NumericDimensionField<?>[oldFields.length];
                boolean updated = false;
                for (int i = 0; i < oldFields.length; i++) {
                    if (oldFields[i] instanceof LegacySpatialField) {
                        updatedFields[i] = ((LegacySpatialField<?>) oldFields[i]).getUpdatedField(index);
                        updated = true;
                    } else {
                        updatedFields[i] = oldFields[i];
                    }
                }
                if (updated) {
                    ((BasicIndexModel) indexModel).init(updatedFields);
                    indexStore.removeIndex(index.getName());
                    indexStore.addIndex(index);
                    migratedIndices++;
                }
            }
        }
    }
    if (migratedIndices > 0) {
        console.println("    Migrated " + migratedIndices + " indices.");
    } else {
        console.println("    No indices needed to be migrated.");
    }
    console.println("  Migrating index mappings...");
    // Rewrite adapter to index mappings
    final LegacyAdapterIndexMappingStore legacyIndexMappings = new LegacyAdapterIndexMappingStore(operations, options.getFactoryOptions().getStoreOptions());
    final AdapterIndexMappingStore indexMappings = options.createAdapterIndexMappingStore();
    console.println("    Writing new mappings...");
    int indexMappingCount = 0;
    for (final Short adapterId : adapterIDs) {
        final LegacyAdapterToIndexMapping mapping = legacyIndexMappings.getIndicesForAdapter(adapterId);
        final InternalDataAdapter<?> adapter = adapterStore.getAdapter(adapterId);
        for (final String indexName : mapping.getIndexNames()) {
            indexMappings.addAdapterIndexMapping(BaseDataStoreUtils.mapAdapterToIndex(adapter, indexStore.getIndex(indexName)));
            indexMappingCount++;
        }
    }
    if (indexMappingCount > 0) {
        console.println("    Migrated " + indexMappingCount + " index mappings.");
        console.println("    Deleting legacy index mappings...");
        try (MetadataDeleter deleter = operations.createMetadataDeleter(MetadataType.LEGACY_INDEX_MAPPINGS)) {
            deleter.delete(new MetadataQuery(null));
        } catch (final Exception e) {
            LOGGER.warn("Error deleting legacy index mappings", e);
        }
    } else {
        console.println("    No index mappings to migrate.");
    }
    // Update statistics
    console.println("  Migrating statistics...");
    final List<Statistic<?>> defaultStatistics = new ArrayList<>();
    for (final Index index : dataStore.getIndices()) {
        if (index instanceof DefaultStatisticsProvider) {
            defaultStatistics.addAll(((DefaultStatisticsProvider) index).getDefaultStatistics());
        }
    }
    for (final DataTypeAdapter<?> adapter : dataStore.getTypes()) {
        final DefaultStatisticsProvider defaultStatProvider = BaseDataStoreUtils.getDefaultStatisticsProvider(adapter);
        if (defaultStatProvider != null) {
            defaultStatistics.addAll(defaultStatProvider.getDefaultStatistics());
        }
    }
    console.println("    Calculating updated statistics...");
    dataStore.addStatistic(defaultStatistics.toArray(new Statistic[defaultStatistics.size()]));
    console.println("    Deleting legacy statistics...");
    try (MetadataDeleter deleter = operations.createMetadataDeleter(MetadataType.LEGACY_STATISTICS)) {
        deleter.delete(new MetadataQuery(null));
    } catch (final Exception e) {
        LOGGER.warn("Error deleting legacy statistics", e);
    }
}
Also used : NumericDimensionField(org.locationtech.geowave.core.store.dimension.NumericDimensionField) ArrayList(java.util.ArrayList) DefaultStatisticsProvider(org.locationtech.geowave.core.store.statistics.DefaultStatisticsProvider) Index(org.locationtech.geowave.core.store.api.Index) LegacyAdapterIndexMappingStore(org.locationtech.geowave.migration.legacy.core.store.LegacyAdapterIndexMappingStore) CommonIndexModel(org.locationtech.geowave.core.store.index.CommonIndexModel) Statistic(org.locationtech.geowave.core.store.api.Statistic) MetadataDeleter(org.locationtech.geowave.core.store.operations.MetadataDeleter) DataStore(org.locationtech.geowave.core.store.api.DataStore) InternalDataAdapter(org.locationtech.geowave.core.store.adapter.InternalDataAdapter) LegacySpatialField(org.locationtech.geowave.migration.legacy.core.geotime.LegacySpatialField) LegacyFeatureDataAdapter(org.locationtech.geowave.migration.legacy.adapter.vector.LegacyFeatureDataAdapter) LegacyInternalDataAdapterWrapper(org.locationtech.geowave.migration.legacy.adapter.LegacyInternalDataAdapterWrapper) AdapterIndexMappingStore(org.locationtech.geowave.core.store.adapter.AdapterIndexMappingStore) LegacyAdapterIndexMappingStore(org.locationtech.geowave.migration.legacy.core.store.LegacyAdapterIndexMappingStore) ParameterException(com.beust.jcommander.ParameterException) IOException(java.io.IOException) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) VisibilityHandler(org.locationtech.geowave.core.store.api.VisibilityHandler) BasicIndexModel(org.locationtech.geowave.core.store.index.BasicIndexModel) LegacyAdapterToIndexMapping(org.locationtech.geowave.migration.legacy.core.store.LegacyAdapterToIndexMapping) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter) LegacyFeatureDataAdapter(org.locationtech.geowave.migration.legacy.adapter.vector.LegacyFeatureDataAdapter) IndexStore(org.locationtech.geowave.core.store.index.IndexStore) MetadataQuery(org.locationtech.geowave.core.store.operations.MetadataQuery)

Aggregations

IOException (java.io.IOException)3 MetadataDeleter (org.locationtech.geowave.core.store.operations.MetadataDeleter)3 MetadataQuery (org.locationtech.geowave.core.store.operations.MetadataQuery)3 ParameterException (com.beust.jcommander.ParameterException)2 ArrayList (java.util.ArrayList)2 DataStore (org.locationtech.geowave.core.store.api.DataStore)2 Index (org.locationtech.geowave.core.store.api.Index)2 Statistic (org.locationtech.geowave.core.store.api.Statistic)2 GeoWaveMetadata (org.locationtech.geowave.core.store.entities.GeoWaveMetadata)2 IndexStore (org.locationtech.geowave.core.store.index.IndexStore)2 MetadataReader (org.locationtech.geowave.core.store.operations.MetadataReader)2 DefaultStatisticsProvider (org.locationtech.geowave.core.store.statistics.DefaultStatisticsProvider)2 FeatureDataAdapter (org.locationtech.geowave.adapter.vector.FeatureDataAdapter)1 ByteArray (org.locationtech.geowave.core.index.ByteArray)1 AdapterIndexMappingStore (org.locationtech.geowave.core.store.adapter.AdapterIndexMappingStore)1 InternalDataAdapter (org.locationtech.geowave.core.store.adapter.InternalDataAdapter)1 PersistentAdapterStore (org.locationtech.geowave.core.store.adapter.PersistentAdapterStore)1 StatisticValue (org.locationtech.geowave.core.store.api.StatisticValue)1 VisibilityHandler (org.locationtech.geowave.core.store.api.VisibilityHandler)1 NumericDimensionField (org.locationtech.geowave.core.store.dimension.NumericDimensionField)1