CollectionIterator.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include "eyedb_p.h"
00026 
00027 #define TRY_GETELEMS_GC
00028 
00029 namespace eyedb {
00030 
00031   CollectionIterator::CollectionIterator(const Collection *_coll, Bool indexed)
00032   {
00033     init(_coll, indexed);
00034   }
00035 
00036   CollectionIterator::CollectionIterator(const Collection &_coll, Bool indexed)
00037   {
00038     init(&_coll, indexed);
00039   }
00040 
00041   CollectionIterator::CollectionIterator(const CollectionPtr &coll_ptr, Bool indexed)
00042   {
00043     init(coll_ptr.getCollection(), indexed);
00044   }
00045 
00046 
00047   void CollectionIterator::init(const Collection *_coll, Bool indexed)
00048   {
00049     coll = _coll;
00050     if (coll->isPartiallyStored()) {
00051       q = 0;
00052       cur = 0;
00053       // 1/11/06
00054       // val_arr may contains objects: these objets must *not* be released
00055       // by the client
00056       status = coll->getElements(val_arr);
00057       return;
00058     }
00059 
00060     q = new Iterator(coll, indexed);
00061     status = q->getStatus();
00062     if (status) {
00063       delete q;
00064       q = 0;
00065     }
00066   }
00067 
00068   Bool CollectionIterator::next(Oid &oid)
00069   {
00070     if (status)
00071       throw *status;
00072 
00073     if (!q) {
00074       for (int n = cur; cur < val_arr.getCount(); cur++) {
00075         const Value &v = val_arr[cur];
00076         if (v.type == Value::tObject) {
00077           if (v.o)
00078             oid = v.o->getOid();
00079           else
00080             oid.invalidate();
00081           cur++;
00082           return True;
00083         }
00084 
00085         if (v.type == Value::tObjectPtr) {
00086           if (v.o_ptr->getObject())
00087             oid = v.o_ptr->getObject()->getOid();
00088           else
00089             oid.invalidate();
00090           cur++;
00091           return True;
00092         }
00093 
00094         if (v.type == Value::tOid) {
00095           oid = *v.oid;
00096           return True;
00097         }
00098       }
00099 
00100       return False;
00101     }
00102 
00103     Bool found;
00104     status = q->scanNext(found, oid);
00105     if (status)
00106       throw *status;
00107     return found;
00108   }
00109 
00110   Bool CollectionIterator::next(ObjectPtr &o_ptr, const RecMode *rcm)
00111   {
00112     Object *o = 0;
00113     Bool b = next(o, rcm);
00114     o_ptr = o;
00115     return b;
00116   }
00117 
00118   Bool CollectionIterator::next(Object *&o, const RecMode *rcm)
00119   {
00120     if (status)
00121       throw *status;
00122 
00123     if (!q) {
00124       for (int n = cur; cur < val_arr.getCount(); cur++) {
00125         const Value &v = val_arr[cur];
00126         if (v.type == Value::tObject) {
00127           o = v.o;
00128           if (o)
00129             o->setMustRelease(false);
00130           cur++;
00131           return True;
00132         }
00133 
00134         if (v.type == Value::tObjectPtr) {
00135           o = v.o_ptr->getObject();
00136           cur++;
00137           return True;
00138         }
00139 
00140         if (v.type == Value::tOid && coll->getDatabase()) {
00141           status = const_cast<Database *>(coll->getDatabase())->loadObject(*v.oid, o, rcm);
00142           if (status)
00143             throw *status;
00144 #ifdef TRY_GETELEMS_GC
00145           if (o) {
00146             objv.push_back(o);
00147             o->setMustRelease(false);
00148           }
00149 #endif
00150           cur++;
00151           return True;
00152         }
00153       }
00154 
00155       return False;
00156     }
00157 
00158     Bool found;
00159     o = 0;
00160     status = q->scanNext(found, o, rcm);
00161     if (status) 
00162       throw *status;
00163 
00164 #ifdef TRY_GETELEMS_GC
00165     if (o) {
00166       objv.push_back(o);
00167       o->setMustRelease(false);
00168     }
00169 #endif
00170     return found;
00171   }
00172 
00173   Bool CollectionIterator::next(Value &v)
00174   {
00175     if (status)
00176       throw *status;
00177 
00178     if (!q) {
00179       if (cur < val_arr.getCount()) {
00180         v = val_arr[cur++];
00181         return True;
00182       }
00183       return False;
00184     }
00185 
00186     Bool found;
00187     status = q->scanNext(found, v);
00188     if (status)
00189       throw *status;
00190 
00191 #ifdef TRY_GETELEMS_GC
00192     if (v.type == Value::tObject && v.o)
00193       objv.push_back(v.o);
00194 #endif
00195 
00196     const_cast<Collection *>(coll)->makeValue(v);
00197     return found;
00198   }
00199 
00200   CollectionIterator::~CollectionIterator()
00201   {
00202 #ifdef TRY_GETELEMS_GC
00203     std::vector<Object *>::iterator begin = objv.begin();
00204     std::vector<Object *>::iterator end = objv.end();
00205     while (begin != end) {
00206       (*begin)->release();
00207       ++begin;
00208     }
00209 #else
00210     if (!q) {
00211       for (int n = cur; cur < val_arr.getCount(); cur++) {
00212         const Value &v = val_arr[cur];
00213         if (v.type == Value::tObject && v.o)
00214           v.o->release();
00215       }
00216     }
00217 #endif
00218     delete q;
00219   }
00220 }

Generated on Mon Dec 22 18:15:52 2008 for eyedb by  doxygen 1.5.3