00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00036
00037
00038
00039
00040
00041 namespace eyedb {
00042
00043 Status IteratorBEEngine::getStatus() const
00044 {
00045 return status;
00046 }
00047
00048 IteratorBEEngine::~IteratorBEEngine()
00049 {
00050 }
00051
00052
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
00072
00073 if (status)
00074 return;
00075
00076 if (!idx) {
00077
00078
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
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
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
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 }