Search in sources :

Example 6 with DatasetDefinition

use of io.cdap.cdap.api.dataset.DatasetDefinition in project cdap by caskdata.

the class InMemoryDatasetFramework method deleteAllInstances.

@Override
public void deleteAllInstances(NamespaceId namespaceId) throws DatasetManagementException, IOException {
    writeLock.lock();
    try {
        for (DatasetSpecification spec : instances.row(namespaceId).values()) {
            DatasetDefinition def = getDefinitionForType(namespaceId, spec.getType());
            if (def == null) {
                throw new DatasetManagementException(String.format("Dataset type '%s' is neither registered in the '%s' namespace nor in the system namespace", spec.getType(), namespaceId));
            }
            def.getAdmin(DatasetContext.from(namespaceId.getEntityName()), spec, null).drop();
            publishAudit(namespaceId.dataset(spec.getName()), AuditType.DELETE);
        }
        instances.row(namespaceId).clear();
    } finally {
        writeLock.unlock();
    }
}
Also used : DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetDefinition(io.cdap.cdap.api.dataset.DatasetDefinition) AbstractDatasetDefinition(io.cdap.cdap.api.dataset.lib.AbstractDatasetDefinition)

Example 7 with DatasetDefinition

use of io.cdap.cdap.api.dataset.DatasetDefinition in project cdap by caskdata.

the class InMemoryDatasetFramework method getAdmin.

@Nullable
@Override
public <T extends DatasetAdmin> T getAdmin(DatasetId datasetInstanceId, @Nullable ClassLoader classLoader, DatasetClassLoaderProvider classLoaderProvider) throws IOException {
    readLock.lock();
    try {
        DatasetSpecification spec = instances.get(datasetInstanceId.getParent(), datasetInstanceId);
        if (spec == null) {
            return null;
        }
        LinkedHashSet<String> availableModuleClasses = getAvailableModuleClasses(datasetInstanceId.getParent());
        DatasetDefinition impl = createRegistry(availableModuleClasses, classLoader).get(spec.getType());
        return (T) impl.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec, classLoader);
    } finally {
        readLock.unlock();
    }
}
Also used : DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetDefinition(io.cdap.cdap.api.dataset.DatasetDefinition) AbstractDatasetDefinition(io.cdap.cdap.api.dataset.lib.AbstractDatasetDefinition) Nullable(javax.annotation.Nullable)

Example 8 with DatasetDefinition

use of io.cdap.cdap.api.dataset.DatasetDefinition in project cdap by caskdata.

the class SingleTypeModule method register.

@Override
public void register(DatasetDefinitionRegistry registry) {
    final Constructor ctor = findSuitableCtorOrFail(dataSetClass);
    DatasetType typeAnn = dataSetClass.getAnnotation(DatasetType.class);
    // default type name to dataset class name
    String typeName = typeAnn != null ? typeAnn.value() : dataSetClass.getName();
    // The ordering is important. It is the same order as the parameters
    final Map<String, DatasetDefinition> embeddedDefinitions = Maps.newLinkedHashMap();
    final Class<?>[] paramTypes = ctor.getParameterTypes();
    Annotation[][] paramAnns = ctor.getParameterAnnotations();
    // Gather all dataset name and type information for the @EmbeddedDataset parameters
    for (int i = 1; i < paramTypes.length; i++) {
        // Must have the EmbeddedDataset as it's the contract of the findSuitableCtorOrFail method
        EmbeddedDataset anno = Iterables.filter(Arrays.asList(paramAnns[i]), EmbeddedDataset.class).iterator().next();
        String type = anno.type();
        // default to dataset class name if dataset type name is not specified through the annotation
        if (EmbeddedDataset.DEFAULT_TYPE_NAME.equals(type)) {
            type = paramTypes[i].getName();
        }
        DatasetDefinition embeddedDefinition = registry.get(type);
        if (embeddedDefinition == null) {
            throw new IllegalStateException(String.format("Unknown Dataset type '%s', specified by parameter number %d of the %s Dataset", type, i, dataSetClass.getName()));
        }
        embeddedDefinitions.put(anno.value(), embeddedDefinition);
    }
    registry.add(new CompositeDatasetDefinition<Dataset>(typeName, embeddedDefinitions) {

        @Override
        public Dataset getDataset(DatasetContext datasetContext, DatasetSpecification spec, Map<String, String> arguments, ClassLoader classLoader) throws IOException {
            List<Object> params = new ArrayList<>();
            params.add(spec);
            for (Map.Entry<String, DatasetDefinition> entry : embeddedDefinitions.entrySet()) {
                params.add(entry.getValue().getDataset(datasetContext, spec.getSpecification(entry.getKey()), arguments, classLoader));
            }
            try {
                return (Dataset) ctor.newInstance(params.toArray());
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    });
}
Also used : EmbeddedDataset(io.cdap.cdap.api.dataset.module.EmbeddedDataset) Constructor(java.lang.reflect.Constructor) EmbeddedDataset(io.cdap.cdap.api.dataset.module.EmbeddedDataset) Dataset(io.cdap.cdap.api.dataset.Dataset) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetType(io.cdap.cdap.api.dataset.module.DatasetType) IOException(java.io.IOException) CompositeDatasetDefinition(io.cdap.cdap.api.dataset.lib.CompositeDatasetDefinition) DatasetDefinition(io.cdap.cdap.api.dataset.DatasetDefinition) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List) DatasetContext(io.cdap.cdap.api.dataset.DatasetContext)

Example 9 with DatasetDefinition

use of io.cdap.cdap.api.dataset.DatasetDefinition in project cdap by cdapio.

the class InMemoryDatasetFramework method deleteInstance.

@Override
public void deleteInstance(DatasetId instanceId) throws DatasetManagementException, IOException {
    writeLock.lock();
    try {
        DatasetSpecification spec = instances.remove(instanceId.getParent(), instanceId);
        if (spec == null) {
            throw new InstanceNotFoundException(instanceId.getEntityName());
        }
        DatasetDefinition def = getDefinitionForType(instanceId.getParent(), spec.getType());
        if (def == null) {
            throw new DatasetManagementException(String.format("Dataset type '%s' is neither registered in the '%s' namespace nor in the system namespace", spec.getType(), instanceId.getParent()));
        }
        def.getAdmin(DatasetContext.from(instanceId.getNamespace()), spec, null).drop();
        publishAudit(instanceId, AuditType.DELETE);
    } finally {
        writeLock.unlock();
    }
}
Also used : DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) InstanceNotFoundException(io.cdap.cdap.api.dataset.InstanceNotFoundException) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetDefinition(io.cdap.cdap.api.dataset.DatasetDefinition) AbstractDatasetDefinition(io.cdap.cdap.api.dataset.lib.AbstractDatasetDefinition)

Example 10 with DatasetDefinition

use of io.cdap.cdap.api.dataset.DatasetDefinition in project cdap by cdapio.

the class InMemoryDatasetFramework method updateInstance.

@Override
public void updateInstance(DatasetId datasetInstanceId, DatasetProperties props) throws DatasetManagementException, IOException {
    writeLock.lock();
    try {
        DatasetSpecification oldSpec = instances.get(datasetInstanceId.getParent(), datasetInstanceId);
        if (oldSpec == null) {
            throw new InstanceNotFoundException(datasetInstanceId.getEntityName());
        }
        DatasetDefinition def = getDefinitionForType(datasetInstanceId.getParent(), oldSpec.getType());
        if (def == null) {
            throw new DatasetManagementException(String.format("Dataset type '%s' is neither registered in the '%s' namespace nor in the system namespace", oldSpec.getType(), datasetInstanceId.getParent()));
        }
        DatasetSpecification spec = AbstractDatasetDefinition.reconfigure(def, datasetInstanceId.getEntityName(), props, oldSpec).setOriginalProperties(props);
        if (props.getDescription() != null) {
            spec = spec.setDescription(props.getDescription());
        }
        instances.put(datasetInstanceId.getParent(), datasetInstanceId, spec);
        DatasetAdmin admin = def.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec, null);
        if (admin instanceof Updatable) {
            ((Updatable) admin).update(oldSpec);
        } else {
            admin.upgrade();
        }
        publishAudit(datasetInstanceId, AuditType.UPDATE);
    } catch (IncompatibleUpdateException e) {
        throw new InstanceConflictException("Update failed for dataset instance " + datasetInstanceId, e);
    } finally {
        writeLock.unlock();
    }
}
Also used : DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) InstanceConflictException(io.cdap.cdap.api.dataset.InstanceConflictException) InstanceNotFoundException(io.cdap.cdap.api.dataset.InstanceNotFoundException) Updatable(io.cdap.cdap.api.dataset.Updatable) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) DatasetDefinition(io.cdap.cdap.api.dataset.DatasetDefinition) AbstractDatasetDefinition(io.cdap.cdap.api.dataset.lib.AbstractDatasetDefinition) IncompatibleUpdateException(io.cdap.cdap.api.dataset.IncompatibleUpdateException)

Aggregations

DatasetDefinition (io.cdap.cdap.api.dataset.DatasetDefinition)28 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)22 AbstractDatasetDefinition (io.cdap.cdap.api.dataset.lib.AbstractDatasetDefinition)14 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)10 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)6 IncompatibleUpdateException (io.cdap.cdap.api.dataset.IncompatibleUpdateException)6 InstanceNotFoundException (io.cdap.cdap.api.dataset.InstanceNotFoundException)6 DatasetContext (io.cdap.cdap.api.dataset.DatasetContext)4 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)4 InstanceConflictException (io.cdap.cdap.api.dataset.InstanceConflictException)4 Reconfigurable (io.cdap.cdap.api.dataset.Reconfigurable)4 Nullable (javax.annotation.Nullable)4 Test (org.junit.Test)4 Dataset (io.cdap.cdap.api.dataset.Dataset)2 Updatable (io.cdap.cdap.api.dataset.Updatable)2 CompositeDatasetDefinition (io.cdap.cdap.api.dataset.lib.CompositeDatasetDefinition)2 IndexedTableDefinition (io.cdap.cdap.api.dataset.lib.IndexedTableDefinition)2 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)2 DatasetType (io.cdap.cdap.api.dataset.module.DatasetType)2 EmbeddedDataset (io.cdap.cdap.api.dataset.module.EmbeddedDataset)2