IteratorBEEngine.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/eyedb.h>
00026 #include "CollectionBE.h"
00027 #include <eyedb/eyedb_p.h>
00028 #include "IteratorBEEngine.h"
00029 #include "kernel.h"
00030 #include <assert.h>
00031 #include <eyedblib/m_malloc.h>
00032 
00033 #define SUPPORT_BASIC_COLL
00034 
00035 // This variable (SKIP_INVALID_OID) has been introduced and
00036 // disconnected on the 08/01/99: because
00037 // there are no reasons to skip invalid oids in a query:
00038 // this could lead to inconsistant behaviours!
00039 // #define SKIP_INVALID_OID
00040 
00041 namespace eyedb {
00042 
00043   Status IteratorBEEngine::getStatus() const
00044   {
00045     return status;
00046   }
00047 
00048   IteratorBEEngine::~IteratorBEEngine()
00049   {
00050   }
00051 
00052   // iterator attribute
00053 
00054   IteratorBEEngineAttribute::IteratorBEEngineAttribute
00055   (Database *_db,
00056    const Attribute *_agr, int _ind,
00057    Data start, Data end, Bool sexcl, Bool eexcl, int x_size)
00058   {
00059     eyedbsm::Idx *idx;
00060 
00061     state = False;
00062 
00063     db = _db;
00064     dbh = NULL;
00065     agr = _agr;
00066     ind = _ind;
00067   
00068     int maxind;
00069 
00070     assert(0);
00071     //status = ((Attribute *)agr)->getIdx(ind, maxind, size, idx);
00072 
00073     if (status)
00074       return;
00075 
00076     if (!idx) {
00077       // in fact, this is not an error: must use them class object
00078       // collection!
00079       status = Exception::make(IDB_ITERATOR_ATTRIBUTE_NO_IDX);
00080       curs = 0;
00081       return;
00082     }
00083 
00084     if (x_size != size) {
00085       status = Exception::make(IDB_ITERATOR_ATTRIBUTE_INVALID_SIZE, "size `%d' expected, got `%d'", size, x_size);
00086       curs = 0;
00087       return;
00088     }
00089 
00090     if (ind != Attribute::composedMode && (ind < 0 || ind > maxind)) {
00091       status = Exception::make(IDB_ITERATOR_ATTRIBUTE_INVALID_INDICE, "indice in [0, %d] expected, got `%d'", maxind, ind);
00092       curs = 0;
00093       return;
00094     }
00095 
00096     if (ind == Attribute::composedMode)
00097       curs = new eyedbsm::HIdxCursor(idx->asHIdx(),
00098                                      (void const *)start, (void const *)end,
00099                                      (eyedbsm::Boolean)sexcl, (eyedbsm::Boolean)eexcl);
00100     else {
00101       unsigned char *es = (unsigned char *)malloc(sizeof(eyedblib::int32) + x_size);
00102       memcpy(es, &ind, sizeof(eyedblib::int32));
00103       memcpy(es+sizeof(eyedblib::int32), start, x_size);
00104 
00105       unsigned char *ee = (unsigned char *)malloc(sizeof(eyedblib::int32) + x_size);
00106       memcpy(ee, &ind, sizeof(eyedblib::int32));
00107       memcpy(ee+sizeof(eyedblib::int32), end, x_size);
00108 
00109       curs = new eyedbsm::BIdxCursor(idx->asBIdx(), (void const *)es,
00110                                      (void const *)ee,
00111                                      (eyedbsm::Boolean)sexcl, (eyedbsm::Boolean)eexcl);
00112       free(ee);
00113       free(es);
00114     }
00115 
00116     state = True;
00117     status = Success;
00118   }
00119 
00120   Status IteratorBEEngineAttribute::scanNext(int wanted, int *found, IteratorAtom *atom_array)
00121   {
00122     int n = 0;
00123 
00124     if (state && curs) {
00125       eyedbsm::Boolean sefound = eyedbsm::False;
00126       for (; n < wanted; n++) {
00127         Oid oid;
00128         eyedbsm::Status se_status = curs->next(&sefound, &oid);
00129 
00130         if (se_status)
00131           return Exception::make(se_status->err, se_status->err_msg);
00132 
00133         if (!sefound) {
00134           state = False;
00135           break;
00136         }
00137 
00138         std::cerr << "IteratorBEEngineAttribute::scanNext: oid must be swapped"
00139                   << std::endl;
00140 
00141         atom_array[n].type = IteratorAtom_OID;
00142         atom_array[n].oid = *oid.getOid();
00143       }
00144     }
00145 
00146     *found = n;
00147     return Success;
00148   }
00149 
00150   IteratorBEEngineAttribute::~IteratorBEEngineAttribute()
00151   {
00152     delete curs;
00153   }
00154 
00155   // iterator collection
00156 
00157   IteratorBEEngineCollection::IteratorBEEngineCollection(CollectionBE *_collbe, Bool _index)
00158   {
00159     eyedbsm::Idx *idx;
00160 
00161     collbe = _collbe;
00162     state = False;
00163 
00164     index = _index;
00165     db = collbe->getDatabase();
00166     dbh = collbe->getDbHandle();
00167 
00168     eyedblib::int16 item_size = collbe->getItemSize();
00169     //dim = 0;
00170     eyedbsm::Idx *idx1, *idx2;
00171     collbe->getIdx(&idx1, &idx2);
00172 
00173     if (idx2) {
00174       idx = idx2;
00175       data = new unsigned char[item_size];
00176       buff = new eyedbsm::Idx::Key(sizeof(int));
00177     }
00178     else {
00179       idx = idx1;
00180       data = 0;
00181       buff = new eyedbsm::Idx::Key(item_size);
00182     }
00183 
00184     if (!idx)
00185       {
00186         status = Exception::make(IDB_ERROR, "no index found in collection BE");
00187         curs = 0;
00188 
00189         return;
00190       }
00191 
00192     //if (collbe->isBIdx())
00193     if (idx->asBIdx()) {
00194       assert(idx->asBIdx());
00195       curs = new eyedbsm::BIdxCursor(idx->asBIdx(), 0, 0, eyedbsm::False, eyedbsm::False);
00196     }
00197     else {
00198       assert(idx->asHIdx());
00199       curs = new eyedbsm::HIdxCursor(idx->asHIdx(), 0, 0, eyedbsm::False, eyedbsm::False);
00200     }
00201 
00202     state = True;
00203     status = Success;
00204   }
00205 
00206   Status IteratorBEEngineCollection::scanNext(int wanted, int *found, IteratorAtom *atom_array)
00207   {
00208     if (status)
00209       return status;
00210 
00211     int n = 0;
00212 
00213     if (state && curs) {
00214       eyedbsm::Boolean sefound = eyedbsm::False;
00215       IteratorAtom *atom = atom_array;
00216       for (; n < wanted; ) {
00217         unsigned int ind;
00218         eyedbsm::Status se_status;
00219 
00220         if (data) {
00221           se_status = curs->next(&sefound, data, buff);
00222           memcpy(&ind, buff->getKey(), sizeof(int));
00223         }
00224         else {
00225           se_status = curs->next(&sefound, &ind, buff);
00226         }
00227 
00228         if (se_status)
00229           return Exception::make(se_status->err, se_status->err_msg);
00230 
00231         if (!sefound) {
00232           state = False;
00233           break;
00234         }
00235 
00236         if (index) {
00237           atom->type = IteratorAtom_INT32;
00238           atom->i32 = ind;
00239           atom++;
00240           n++;
00241           if (n == wanted)
00242             break;
00243         }
00244 
00245         void *k = (data ? data : buff->getKey());
00246 
00247         collbe->decode(k, *atom);
00248             
00249         n++;
00250         atom++;
00251       }
00252     }
00253 
00254     *found = n;
00255     return Success;
00256   }
00257 
00258   IteratorBEEngineCollection::~IteratorBEEngineCollection()
00259   {
00260     delete curs;
00261     delete buff;
00262     delete [] data;
00263   }
00264 }

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