All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.db4o.DB4OExtent Maven / Gradle / Ivy

/**********************************************************************
 Copyright (c) 2006 Andy Jefferson and others. All rights reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 Contributors:
 ...
 **********************************************************************/
package org.datanucleus.store.db4o;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.datanucleus.FetchPlan;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.store.AbstractExtent;
import org.datanucleus.store.ExecutionContext;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Candidate;
import com.db4o.query.Evaluation;
import com.db4o.query.Query;

/**
 * Extent for use with DB4O datastores.
 * The Extent is generated in two ways :-
 * 
    *
  • with subclasses - Use a simple candidate query in DB4O
  • *
  • without subclasses - Use a SODA query to eliminate subclasses in DB4O
  • *
*/ public class DB4OExtent extends AbstractExtent { /** Localiser for messages. */ protected static final Localiser LOCALISER_DB4O = Localiser.getInstance("org.datanucleus.store.db4o.Localisation", DB4OStoreManager.class.getClassLoader()); /** Set of iterators created by this Extent. */ private Set iterators = new HashSet(); /** FetchPlan for use with this Extent. */ private FetchPlan fetchPlan = null; /** * Constructor. * @param ec execution context * @param cls candidate class * @param subclasses Whether to include subclasses * @param cmd MetaData for the candidate class */ public DB4OExtent(ExecutionContext ec, Class cls, boolean subclasses, AbstractClassMetaData cmd) { super(ec, cls, subclasses, cmd); this.fetchPlan = ec.getFetchPlan().getCopy(); } /** * Method to close the extent. * @param iter an iterator obtained by the method iterator() on this Extent instance. */ public void close(Iterator iter) { iterators.remove(iter); } /** * Close all Iterators associated with this Extent instance. Iterators closed by this method will return false to * hasNext() and will throw NoSuchElementException on next(). The Extent instance can still be used as a parameter * of Query.setCandidates, and to get an Iterator. */ public void closeAll() { iterators.clear(); } /** * Returns an iterator over all the instances in the Extent. * @return an iterator over all the instances in the Extent. */ public Iterator iterator() { // Retrieve the possible instances ManagedConnection mconn = storeMgr.getConnection(ec); ObjectContainer cont = (ObjectContainer) mconn.getConnection(); try { ObjectSet set = null; long startTime = System.currentTimeMillis(); if (NucleusLogger.DATASTORE_RETRIEVE.isDebugEnabled()) { NucleusLogger.DATASTORE_RETRIEVE.debug(LOCALISER_DB4O.msg("DB4O.Extent.Execute", candidateClass, "" + subclasses)); } if (subclasses) { // Use the simple candidate query since we want subclasses too set = cont.query(candidateClass); } else { // Use SODA query to eliminate subclasses Query q = cont.query(); q.constrain(candidateClass); q.constrain(new Evaluation() { public void evaluate(Candidate c) { c.include(c.getObject().getClass() == candidateClass); } }); set = q.execute(); } if (NucleusLogger.DATASTORE_RETRIEVE.isDebugEnabled()) { NucleusLogger.DATASTORE_RETRIEVE.debug(LOCALISER_DB4O.msg("DB4O.ExecutionTime", (System.currentTimeMillis() - startTime))); } DB4OExtentIterator iter = new DB4OExtentIterator(set); iterators.add(iter); return iter; } finally { mconn.release(); } } /** * This method retrieves the fetch plan associated with the Extent. It always returns the identical instance for the * same Extent instance. Any change made to the fetch plan affects subsequent instance retrievals via next(). Only * instances not already in memory are affected by the fetch plan. Fetch plan is described in Section 12.7. * @return the FetchPlan */ public FetchPlan getFetchPlan() { return fetchPlan; } /** * Iterator for use with DB4O Extents. */ public class DB4OExtentIterator implements Iterator { /** The DB4O ObjectSet for the Extent. */ ObjectSet objectSet = null; /** * Constructor. * @param objects The DB4O ObjectSet */ public DB4OExtentIterator(ObjectSet objects) { this.objectSet = objects; } /** * Method to return if there is another object in the iterator. * @return Whether there is another object */ public boolean hasNext() { return objectSet.hasNext(); } /** * Method to return the next object in the iterator. * @return The next object */ public Object next() { Object obj = objectSet.next(); ManagedConnection mconn = storeMgr.getConnection(ec); ObjectContainer container = (ObjectContainer) mconn.getConnection(); try { DB4OUtils.prepareDB4OObjectForUse(obj, ec, container, cmd, (DB4OStoreManager)ec.getStoreManager()); } finally { mconn.release(); } return obj; } /** * Method to remove an object. */ public void remove() { throw new UnsupportedOperationException(LOCALISER_DB4O.msg("DB4O.Extent.IteratorRemoveNotSupported")); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy