Search in sources :

Example 6 with StoreManager

use of org.datanucleus.store.StoreManager in project datanucleus-core by datanucleus.

the class FederatedPersistenceHandler method fetchObject.

/* (non-Javadoc)
     * @see org.datanucleus.store.StorePersistenceHandler#fetchObject(org.datanucleus.store.ObjectProvider, int[])
     */
public void fetchObject(ObjectProvider op, int[] fieldNumbers) {
    StoreManager classStoreMgr = storeMgr.getStoreManagerForClass(op.getClassMetaData());
    classStoreMgr.getPersistenceHandler().fetchObject(op, fieldNumbers);
}
Also used : StoreManager(org.datanucleus.store.StoreManager)

Example 7 with StoreManager

use of org.datanucleus.store.StoreManager in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManagerFactory method supportedOptions.

/**
 * The application can determine from the results of this method which optional features, and which query languages are supported by the JDO implementation.
 * See section 11.6 of the JDO specification.
 * @return A Collection of String representing the supported options.
 */
public Collection<String> supportedOptions() {
    // Generate the list of supported options, taking the general options we support and removing
    // any that the particular StoreManager doesn't support
    Set<String> options = new HashSet<>(Arrays.asList(OPTION_ARRAY));
    StoreManager storeMgr = nucleusContext.getStoreManager();
    if (storeMgr != null) {
        Collection<String> storeMgrOptions = storeMgr.getSupportedOptions();
        if (!storeMgrOptions.contains(StoreManager.OPTION_NONDURABLE_ID)) {
            options.remove("javax.jdo.option.NonDurableIdentity");
        }
        if (!storeMgrOptions.contains(StoreManager.OPTION_DATASTORE_ID)) {
            options.remove("javax.jdo.option.DatastoreIdentity");
        }
        if (!storeMgrOptions.contains(StoreManager.OPTION_APPLICATION_ID)) {
            options.remove("javax.jdo.option.ApplicationIdentity");
        }
        if (!storeMgr.supportsQueryLanguage(Query.LANGUAGE_JDOQL)) {
            options.remove("javax.jdo.query.JDOQL");
        }
        if (!storeMgr.supportsQueryLanguage(Query.LANGUAGE_SQL)) {
            options.remove("javax.jdo.query.SQL");
        }
        if (storeMgrOptions.contains(StoreManager.OPTION_TXN_ISOLATION_READ_COMMITTED)) {
            options.add("javax.jdo.option.TransactionIsolationLevel.read-committed");
        }
        if (storeMgrOptions.contains(StoreManager.OPTION_TXN_ISOLATION_READ_UNCOMMITTED)) {
            options.add("javax.jdo.option.TransactionIsolationLevel.read-uncommitted");
        }
        if (storeMgrOptions.contains(StoreManager.OPTION_TXN_ISOLATION_REPEATABLE_READ)) {
            options.add("javax.jdo.option.TransactionIsolationLevel.repeatable-read");
        }
        if (storeMgrOptions.contains(StoreManager.OPTION_TXN_ISOLATION_SERIALIZABLE)) {
            options.add("javax.jdo.option.TransactionIsolationLevel.serializable");
        }
        if (storeMgrOptions.contains("TransactionIsolationLevel.snapshot")) {
            options.add("javax.jdo.option.TransactionIsolationLevel.snapshot");
        }
        if (storeMgrOptions.contains(StoreManager.OPTION_QUERY_CANCEL)) {
            options.add("javax.jdo.option.QueryCancel");
        }
        if (storeMgrOptions.contains(StoreManager.OPTION_DATASTORE_TIMEOUT)) {
            options.add("javax.jdo.option.DatastoreTimeout");
        }
        if (storeMgrOptions.contains(StoreManager.OPTION_QUERY_JDOQL_BITWISE_OPS)) {
            options.add("javax.jdo.query.JDOQL.BitwiseOperations");
        }
    }
    return Collections.unmodifiableSet(options);
}
Also used : HashSet(java.util.HashSet) StoreManager(org.datanucleus.store.StoreManager)

Example 8 with StoreManager

use of org.datanucleus.store.StoreManager in project tests by datanucleus.

the class DynamicEnhanceSchemaToolTest method schemaCreate.

public void schemaCreate(DynamicEnhanceSchemaToolClassLoader runtimeCL) throws Exception {
    Map props = getPropertiesForDatastore(runtimeCL);
    JDOPersistenceManagerFactory pmf = (JDOPersistenceManagerFactory) JDOHelper.getPersistenceManagerFactory(props);
    try {
        JDOMetadata filemd = pmf.newMetadata();
        createMetadata(filemd);
        pmf.registerMetadata(filemd);
        Set<String> classNames = new HashSet<>();
        classNames.add("test.Client");
        PersistenceNucleusContext nucCtx = pmf.getNucleusContext();
        StoreManager storeMgr = nucCtx.getStoreManager();
        if (!(storeMgr instanceof SchemaAwareStoreManager)) {
            // Can't create schema with this datastore
            return;
        }
        try {
            SchemaTool schematool = new SchemaTool();
            schematool.setDdlFile("target/schema.ddl");
            schematool.setCompleteDdl(true);
            SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) nucCtx.getStoreManager();
            schematool.createSchemaForClasses(schemaStoreMgr, classNames);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } finally {
        pmf.close();
    }
}
Also used : JDOMetadata(javax.jdo.metadata.JDOMetadata) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) SchemaTool(org.datanucleus.store.schema.SchemaTool) HashMap(java.util.HashMap) Map(java.util.Map) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) HashSet(java.util.HashSet) StoreManager(org.datanucleus.store.StoreManager) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager)

Example 9 with StoreManager

use of org.datanucleus.store.StoreManager in project datanucleus-core by datanucleus.

the class PersistenceNucleusContextImpl method initialiseSchema.

/**
 * Method to handle generation (create, drop, drop+create) of a schema at initialisation.
 * Will generate the schema for all classes that have had their metadata loaded at this point, which
 * typically means the persistence-unit.
 * @param generateModeStr Generate "mode"
 * @param generateScripts Whether to generate
 */
protected void initialiseSchema(String generateModeStr, boolean generateScripts) {
    Mode mode = null;
    if (generateModeStr.equalsIgnoreCase("create")) {
        mode = Mode.CREATE;
    } else if (generateModeStr.equalsIgnoreCase("drop")) {
        mode = Mode.DELETE;
    } else if (generateModeStr.equalsIgnoreCase("drop-and-create")) {
        mode = Mode.DELETE_CREATE;
    }
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        if (mode == Mode.CREATE) {
            NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014000"));
        } else if (mode == Mode.DELETE) {
            NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014001"));
        } else if (mode == Mode.DELETE_CREATE) {
            NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014045"));
        }
    }
    // Extract the classes that have metadata loaded (e.g persistence-unit)
    Set<String> schemaClassNames = null;
    MetaDataManager metaDataMgr = getMetaDataManager();
    FileMetaData[] filemds = metaDataMgr.getFileMetaData();
    schemaClassNames = new TreeSet<String>();
    if (filemds == null) {
        throw new NucleusUserException("No classes to process in generateSchema");
    }
    for (int i = 0; i < filemds.length; i++) {
        for (int j = 0; j < filemds[i].getNoOfPackages(); j++) {
            for (int k = 0; k < filemds[i].getPackage(j).getNoOfClasses(); k++) {
                String className = filemds[i].getPackage(j).getClass(k).getFullClassName();
                if (!schemaClassNames.contains(className)) {
                    schemaClassNames.add(className);
                }
            }
        }
    }
    StoreManager storeMgr = getStoreManager();
    if (storeMgr instanceof SchemaAwareStoreManager) {
        SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) storeMgr;
        SchemaTool schemaTool = new SchemaTool();
        if (mode == Mode.CREATE) {
            if (generateScripts) {
                // Generate the required script
                schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_TARGET));
                schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
            } else {
                // Process the required metadata/script
                String createOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_CREATE_ORDER);
                String createScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_SOURCE);
                if (StringUtils.isWhitespace(createScript)) {
                    createScript = null;
                }
                if (StringUtils.isWhitespace(createOrder)) {
                    createOrder = (createScript != null) ? "script" : "metadata";
                } else {
                    if (createOrder.equals("script") || createOrder.equals("metadata-then-script") || createOrder.equals("script-the-metadata")) {
                        if (createScript == null) {
                            NucleusLogger.DATASTORE_SCHEMA.warn("create order set to " + createOrder + " but no script defined, so using metadata instead");
                            createOrder = "metadata";
                        }
                    }
                }
                if (createOrder.equals("script")) {
                    processDatastoreScript(createScript);
                } else if (createOrder.equals("script-then-metadata")) {
                    processDatastoreScript(createScript);
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                } else if (createOrder.equals("metadata-then-script")) {
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                    processDatastoreScript(createScript);
                } else {
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                }
            }
        } else if (mode == Mode.DELETE) {
            if (generateScripts) {
                // Generate the required script
                schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_TARGET));
                schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
            } else {
                // Process the required metadata/script
                String dropOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DROP_ORDER);
                String dropScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_SOURCE);
                if (StringUtils.isWhitespace(dropScript)) {
                    dropScript = null;
                }
                if (StringUtils.isWhitespace(dropOrder)) {
                    dropOrder = (dropScript != null) ? "script" : "metadata";
                } else {
                    if (dropOrder.equals("script") || dropOrder.equals("metadata-then-script") || dropOrder.equals("script-the-metadata")) {
                        if (dropScript == null) {
                            NucleusLogger.DATASTORE_SCHEMA.warn("drop order set to " + dropOrder + " but no script defined, so using metadata instead");
                            dropOrder = "metadata";
                        }
                    }
                }
                if (dropOrder.equals("script")) {
                    processDatastoreScript(dropScript);
                } else if (dropOrder.equals("script-then-metadata")) {
                    processDatastoreScript(dropScript);
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                } else if (dropOrder.equals("metadata-then-script")) {
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                    processDatastoreScript(dropScript);
                } else {
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                }
            }
        } else if (mode == Mode.DELETE_CREATE) {
            if (generateScripts) {
                // Generate the required scripts
                schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_TARGET));
                schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_TARGET));
                schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
            } else {
                // Process the required metadata/scripts
                String dropOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DROP_ORDER);
                String dropScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_SOURCE);
                if (StringUtils.isWhitespace(dropScript)) {
                    dropScript = null;
                }
                if (StringUtils.isWhitespace(dropOrder)) {
                    dropOrder = (dropScript != null) ? "script" : "metadata";
                } else {
                    if (dropOrder.equals("script") || dropOrder.equals("metadata-then-script") || dropOrder.equals("script-the-metadata")) {
                        if (dropScript == null) {
                            NucleusLogger.DATASTORE_SCHEMA.warn("drop order set to " + dropOrder + " but no script defined, so using metadata instead");
                            dropOrder = "metadata";
                        }
                    }
                }
                if (dropOrder.equals("script")) {
                    processDatastoreScript(dropScript);
                } else if (dropOrder.equals("script-then-metadata")) {
                    processDatastoreScript(dropScript);
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                } else if (dropOrder.equals("metadata-then-script")) {
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                    processDatastoreScript(dropScript);
                } else {
                    schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
                }
                String createOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_CREATE_ORDER);
                String createScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_SOURCE);
                if (StringUtils.isWhitespace(createScript)) {
                    createScript = null;
                }
                if (StringUtils.isWhitespace(createOrder)) {
                    createOrder = (createScript != null) ? "script" : "metadata";
                } else {
                    if (createOrder.equals("script") || createOrder.equals("metadata-then-script") || createOrder.equals("script-the-metadata")) {
                        if (createScript == null) {
                            NucleusLogger.DATASTORE_SCHEMA.warn("create order set to " + createOrder + " but no script defined, so using metadata instead");
                            createOrder = "metadata";
                        }
                    }
                }
                if (createOrder.equals("script")) {
                    processDatastoreScript(createScript);
                } else if (createOrder.equals("script-then-metadata")) {
                    processDatastoreScript(createScript);
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                } else if (createOrder.equals("metadata-then-script")) {
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                    processDatastoreScript(createScript);
                } else {
                    schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
                }
            }
        }
        String loadScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_LOAD_SOURCE);
        if (!StringUtils.isWhitespace(loadScript)) {
            String scriptContent = getDatastoreScriptForResourceName(loadScript);
            if (storeMgr instanceof SchemaScriptAwareStoreManager && !StringUtils.isWhitespace(scriptContent)) {
                ((SchemaScriptAwareStoreManager) storeMgr).executeScript(scriptContent);
            }
        }
    } else {
        if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
            NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("008016", StringUtils.toJVMIDString(storeMgr)));
        }
    }
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014043"));
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Mode(org.datanucleus.store.schema.SchemaTool.Mode) MetaDataManager(org.datanucleus.metadata.MetaDataManager) SchemaTool(org.datanucleus.store.schema.SchemaTool) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) FederatedStoreManager(org.datanucleus.store.federation.FederatedStoreManager) StoreManager(org.datanucleus.store.StoreManager) SchemaScriptAwareStoreManager(org.datanucleus.store.schema.SchemaScriptAwareStoreManager) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) SchemaScriptAwareStoreManager(org.datanucleus.store.schema.SchemaScriptAwareStoreManager) FileMetaData(org.datanucleus.metadata.FileMetaData)

Example 10 with StoreManager

use of org.datanucleus.store.StoreManager in project datanucleus-core by datanucleus.

the class TypeManagerImpl method createSCOInstance.

/**
 * Method to create a new SCO wrapper for member type.
 * Will find a wrapper suitable for the instantiated type (if provided), otherwise suitable for the member metadata type.
 * @param ownerOP ObjectProvider for the owning object
 * @param mmd The MetaData for the related member.
 * @param instantiatedType Type to instantiate the member as (if known), otherwise falls back to the type from metadata
 * @return The wrapper object of the required type
 * @throws NucleusUserException if an error occurred when creating the SCO instance
 */
private SCO createSCOInstance(ObjectProvider ownerOP, AbstractMemberMetaData mmd, Class instantiatedType) {
    String typeName = (instantiatedType != null) ? instantiatedType.getName() : mmd.getTypeName();
    // Find the SCO wrapper type most suitable
    StoreManager storeMgr = ownerOP.getExecutionContext().getStoreManager();
    boolean backedWrapper = storeMgr.useBackedSCOWrapperForMember(mmd, ownerOP.getExecutionContext());
    Class wrapperType = null;
    if (mmd.isSerialized()) {
        // If we have all elements serialised into a column then cannot have backing stores
        backedWrapper = false;
    }
    if (backedWrapper) {
        wrapperType = getBackedWrapperTypeForType(mmd.getType(), instantiatedType, typeName);
    } else {
        wrapperType = getSimpleWrapperTypeForType(mmd.getType(), instantiatedType, typeName);
    }
    if (wrapperType == null) {
        throw new NucleusUserException(Localiser.msg("023011", mmd.getTypeName(), typeName, mmd.getFullFieldName()));
    }
    // Create the SCO wrapper
    try {
        return (SCO) ClassUtils.newInstance(wrapperType, new Class[] { ObjectProvider.class, AbstractMemberMetaData.class }, new Object[] { ownerOP, mmd });
    } catch (UnsupportedOperationException uoe) {
        // Can't create backing store? so try simple wrapper
        if (backedWrapper) {
            NucleusLogger.PERSISTENCE.warn("Creation of backed wrapper for " + mmd.getFullFieldName() + " unsupported, so trying simple wrapper");
            wrapperType = getSimpleWrapperTypeForType(mmd.getType(), instantiatedType, typeName);
            return (SCO) ClassUtils.newInstance(wrapperType, new Class[] { ObjectProvider.class, AbstractMemberMetaData.class }, new Object[] { ownerOP, mmd });
        }
        throw uoe;
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ObjectProvider(org.datanucleus.state.ObjectProvider) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) StoreManager(org.datanucleus.store.StoreManager)

Aggregations

StoreManager (org.datanucleus.store.StoreManager)13 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)4 NucleusException (org.datanucleus.exceptions.NucleusException)3 SchemaAwareStoreManager (org.datanucleus.store.schema.SchemaAwareStoreManager)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 MetaDataManager (org.datanucleus.metadata.MetaDataManager)2 SchemaTool (org.datanucleus.store.schema.SchemaTool)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 StringTokenizer (java.util.StringTokenizer)1 JDOMetadata (javax.jdo.metadata.JDOMetadata)1 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)1 StoreNucleusContext (org.datanucleus.StoreNucleusContext)1 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)1 TransactionIsolationNotSupportedException (org.datanucleus.exceptions.TransactionIsolationNotSupportedException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1