Search in sources :

Example 6 with Request

use of org.datanucleus.store.rdbms.request.Request in project datanucleus-rdbms by datanucleus.

the class RDBMSPersistenceHandler method fetchObject.

// ------------------------------ Fetch ----------------------------------
/**
 * Fetches (fields of) a persistent object from the database.
 * This does a single SELECT on the candidate of the class in question. Will join to inherited
 * tables as appropriate to get values persisted into other tables. Can also join to the tables of
 * related objects (1-1, N-1) as neccessary to retrieve those objects.
 * @param op Object Provider of the object to be fetched.
 * @param memberNumbers The numbers of the members to be fetched.
 * @throws NucleusObjectNotFoundException if the object doesn't exist
 * @throws NucleusDataStoreException when an error occurs in the datastore communication
 */
public void fetchObject(ObjectProvider op, int[] memberNumbers) {
    ExecutionContext ec = op.getExecutionContext();
    ClassLoaderResolver clr = ec.getClassLoaderResolver();
    // Extract metadata of members to process
    AbstractMemberMetaData[] mmds = null;
    if (memberNumbers != null && memberNumbers.length > 0) {
        int[] memberNumbersToProcess = memberNumbers;
        AbstractClassMetaData cmd = op.getClassMetaData();
        if (storeMgr.getBooleanProperty(RDBMSPropertyNames.PROPERTY_RDBMS_FETCH_UNLOADED_AUTO)) {
            // Here we simply load up any unloaded non-relation or 1-1/N-1 members
            if (!op.getLifecycleState().isDeleted()) {
                // Check if this will actually do a SELECT (because we don't want to impose that if not otherwise)
                boolean fetchPerformsSelect = false;
                for (int i = 0; i < memberNumbers.length; i++) {
                    AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(memberNumbers[i]);
                    RelationType relationType = mmd.getRelationType(clr);
                    if (relationType != RelationType.ONE_TO_MANY_UNI && relationType != RelationType.ONE_TO_MANY_BI && relationType != RelationType.MANY_TO_MANY_BI) {
                        fetchPerformsSelect = true;
                        break;
                    }
                }
                if (fetchPerformsSelect) {
                    // Definitely does a SELECT, so try to identify any additional non-relation or 1-1, N-1
                    // members that aren't loaded that could be fetched right now in this call.
                    List<Integer> memberNumberList = new ArrayList<>();
                    for (int i = 0; i < memberNumbers.length; i++) {
                        memberNumberList.add(memberNumbers[i]);
                    }
                    // Check if we could retrieve any other unloaded fields in this call
                    boolean[] loadedFlags = op.getLoadedFields();
                    for (int i = 0; i < loadedFlags.length; i++) {
                        boolean requested = false;
                        for (int j = 0; j < memberNumbers.length; j++) {
                            if (memberNumbers[j] == i) {
                                requested = true;
                                break;
                            }
                        }
                        if (!requested && !loadedFlags[i]) {
                            AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(i);
                            RelationType relType = mmd.getRelationType(clr);
                            if (relType == RelationType.NONE || relType == RelationType.ONE_TO_ONE_BI || relType == RelationType.ONE_TO_ONE_UNI) {
                                memberNumberList.add(i);
                            }
                        }
                    }
                    memberNumbersToProcess = new int[memberNumberList.size()];
                    int i = 0;
                    Iterator<Integer> fieldNumberIter = memberNumberList.iterator();
                    while (fieldNumberIter.hasNext()) {
                        memberNumbersToProcess[i++] = fieldNumberIter.next();
                    }
                }
            }
        }
        // Convert the field numbers for this class into their metadata for the class
        mmds = new AbstractMemberMetaData[memberNumbersToProcess.length];
        for (int i = 0; i < mmds.length; i++) {
            mmds[i] = cmd.getMetaDataForManagedMemberAtAbsolutePosition(memberNumbersToProcess[i]);
        }
    }
    if (op.isEmbedded()) {
        StringBuilder str = new StringBuilder();
        if (mmds != null) {
            for (int i = 0; i < mmds.length; i++) {
                if (i > 0) {
                    str.append(',');
                }
                str.append(mmds[i].getName());
            }
        }
        NucleusLogger.PERSISTENCE.info("Request to load fields \"" + str.toString() + "\" of class " + op.getClassMetaData().getFullClassName() + " but object is embedded, so ignored");
    } else {
        if (ec.getStatistics() != null) {
            ec.getStatistics().incrementFetchCount();
        }
        DatastoreClass table = getDatastoreClass(op.getClassMetaData().getFullClassName(), clr);
        Request req = getFetchRequest(table, mmds, op.getClassMetaData(), clr);
        req.execute(op);
    }
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ArrayList(java.util.ArrayList) LocateRequest(org.datanucleus.store.rdbms.request.LocateRequest) Request(org.datanucleus.store.rdbms.request.Request) FetchRequest(org.datanucleus.store.rdbms.request.FetchRequest) DeleteRequest(org.datanucleus.store.rdbms.request.DeleteRequest) InsertRequest(org.datanucleus.store.rdbms.request.InsertRequest) LocateBulkRequest(org.datanucleus.store.rdbms.request.LocateBulkRequest) UpdateRequest(org.datanucleus.store.rdbms.request.UpdateRequest) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ExecutionContext(org.datanucleus.ExecutionContext) RelationType(org.datanucleus.metadata.RelationType) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

DeleteRequest (org.datanucleus.store.rdbms.request.DeleteRequest)6 FetchRequest (org.datanucleus.store.rdbms.request.FetchRequest)6 InsertRequest (org.datanucleus.store.rdbms.request.InsertRequest)6 LocateBulkRequest (org.datanucleus.store.rdbms.request.LocateBulkRequest)6 LocateRequest (org.datanucleus.store.rdbms.request.LocateRequest)6 Request (org.datanucleus.store.rdbms.request.Request)6 UpdateRequest (org.datanucleus.store.rdbms.request.UpdateRequest)6 RequestIdentifier (org.datanucleus.store.rdbms.request.RequestIdentifier)5 ArrayList (java.util.ArrayList)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 ExecutionContext (org.datanucleus.ExecutionContext)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 RelationType (org.datanucleus.metadata.RelationType)1 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)1 SecondaryDatastoreClass (org.datanucleus.store.rdbms.table.SecondaryDatastoreClass)1