Idx.h

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 #ifndef _EYEDBSM_IDX_H
00026 #define _EYEDBSM_IDX_H
00027 
00028 #include <string>
00029 
00030 namespace eyedbsm {
00031 
00032   class BIdx;
00033   class HIdx;
00034 
00035   class Idx {
00036 
00037   public:
00038 
00039     enum Type
00040       {
00041         tChar,
00042         tUnsignedChar,
00043         tSignedChar,
00044         tInt16,
00045         tUnsignedInt16,
00046         tInt32,
00047         tUnsignedInt32,
00048         tInt64,
00049         tUnsignedInt64,
00050         tFloat32,
00051         tFloat64,
00052         tString,
00053         tOid
00054       };
00055 
00056     enum IdxType {
00057       HashType  = 0x2311,
00058       BTreeType = 0xa765
00059     };
00060 
00061     struct KeyType
00062     {
00063       Type type;
00064       unsigned int count;
00065       unsigned int offset;
00066     };
00067 
00068     typedef eyedbsm::Status (*hash_key_t)(const void *, unsigned int, void *, unsigned int &);
00069   
00070     class Key {
00071 
00072     public:
00073       Key(int sz = 0);
00074       void *getKey() {return key;}
00075       void setKey(void *key, int sz, const KeyType &);
00076       unsigned int getSize() const {return size;}
00077       ~Key();
00078 
00079     private:
00080       void *key;
00081       unsigned int size;
00082     };
00083 
00089     Idx(eyedbsm::Boolean opened,
00090         eyedbsm::Boolean (*precmp)(void const * p, void const * q,
00091                                    KeyType const * type, int & r) = 0);
00092 
00093 
00094     // "virtual" constructor
00102     static eyedbsm::Status make(eyedbsm::DbHandle *dbh, const eyedbsm::Oid &oid,
00103                                 Idx *&idx);
00104 
00109     eyedbsm::Boolean isOpened() const {return opened;}
00110 
00111     virtual eyedbsm::Status insert(const void *key, const void *data) = 0;
00112 
00113     virtual eyedbsm::Status insert(const void *key, const void *data, unsigned int datasz) = 0;
00114 
00115     virtual eyedbsm::Status remove(const void *key, const void *data, eyedbsm::Boolean *found = 0) = 0;
00116 
00117     virtual eyedbsm::Status remove(const void *key, const void *data, unsigned int datasz, eyedbsm::Boolean *found = 0) = 0;
00118 
00119     virtual eyedbsm::Status search(const void *key, unsigned int *found_cnt) = 0;
00120 
00121     virtual eyedbsm::Status searchAny(const void *key, eyedbsm::Boolean *found, void * xdata = 0) = 0;
00122 
00127     virtual eyedbsm::Status destroy() = 0;
00128 
00133     virtual eyedbsm::Oid const &oid() const = 0;
00134 
00139     virtual eyedbsm::Status status() const = 0;
00140 
00145     virtual BIdx *asBIdx() {return NULL;}
00146 
00151     virtual HIdx *asHIdx() {return NULL;}
00152 
00166     virtual eyedbsm::Status reimplementToHash(eyedbsm::Oid &newoid, int key_count,
00167                                               int mag_order = 0,
00168                                               short dspid = eyedbsm::DefaultDspid,
00169                                               const int *impl_hints = 0,
00170                                               unsigned int impl_hints_cnt = 0,
00171                                               hash_key_t hash_key = 0,
00172                                               void *hash_data = 0,
00173                                               KeyType *ktype = 0) = 0;
00174 
00182     virtual eyedbsm::Status reimplementToBTree(eyedbsm::Oid &newoid, int degree = 0,
00183                                                short dspid = eyedbsm::DefaultDspid) = 0;
00184 
00189     virtual unsigned int computeCount();
00190 
00195     virtual unsigned int getCount() const = 0;
00196 
00201     virtual short getDefaultDspid() const = 0;
00202 
00207     virtual void setDefaultDspid(short dspid) = 0;
00208 
00215     virtual eyedbsm::Status getObjects(eyedbsm::Oid *&oids, unsigned int &cnt) const = 0;
00216 
00217     virtual ~Idx() {}
00218 
00219   public: // implementation access
00220     int compare(void const * p, void const * q, KeyType const * type,
00221                 unsigned char bswap) const;
00222     static size_t typeSize(Type type);
00223     static const char *typeString(Type type);
00224     eyedbsm::Boolean (*precmp)(void const * p, void const * q,
00225                                KeyType const * type, int & r);
00226 
00227   protected:
00228     eyedbsm::Boolean opened;
00229     eyedbsm::Status checkOpened() const;
00230 
00231   public:
00232     static void h2x(void *xkey, const void *hkey, const KeyType &keyType);
00233     static void x2h(void *hkey, const void *xkey, const KeyType &keyType,
00234                     unsigned int size);
00235   };
00236 
00237   class DataBuffer {
00238 
00239   public:
00240     DataBuffer() : data(0), datasz(0), allocsz(0) { }
00241 
00242     DataBuffer(const DataBuffer &buffer) : data(0), datasz(0), allocsz(0) {
00243       *this = buffer;
00244     }
00245 
00246     DataBuffer &operator=(const DataBuffer &buffer) {
00247       garbage();
00248       setData(buffer.data, buffer.datasz);
00249       return *this;
00250     }
00251 
00252     void *getData(unsigned int &_datasz) const {
00253       _datasz = datasz;
00254       return (void *)data;
00255     }
00256 
00257     void setData(void *data, unsigned int datasz);
00258 
00259     ~DataBuffer() {
00260       garbage();
00261     }
00262 
00263   private:
00264     unsigned char *data;
00265     unsigned int datasz;
00266     unsigned int allocsz;
00267 
00268     void garbage() {
00269       delete [] data;
00270       data = 0;
00271     }
00272   };
00273 
00274   class AllocBuffer {
00275 
00276   public:
00277     AllocBuffer() : allocsz(0), data(0) { }
00278 
00279     char *alloc(unsigned int size) {
00280       if (size > allocsz) {
00281         allocsz = 2 * size;
00282         delete [] data;
00283         data = new char[allocsz];
00284       }
00285       return data;
00286     }
00287     
00288     ~AllocBuffer() {
00289       delete [] data;
00290       data = 0;
00291     }
00292 
00293   private:
00294     unsigned int allocsz;
00295     char *data;
00296   };
00297 
00298   class IdxCursor {
00299 
00300   public:
00304     IdxCursor() {}
00305 
00313     virtual Status next(eyedbsm::Boolean *found, void *data = 0, Idx::Key *key = 0) = 0;
00314 
00322     virtual Status next(eyedbsm::Boolean *found, DataBuffer &data, Idx::Key *key = 0) = 0;
00323 
00330     virtual Status next(unsigned int *found_cnt, Idx::Key *key = 0) = 0;
00331 
00332     virtual ~IdxCursor() {}
00333   };
00334 
00335   enum {
00336     OPS_NOSWAP = 0,
00337     OP1_SWAP = 0x1,
00338     OP2_SWAP = 0x2,
00339     OPS_SWAP = (OP1_SWAP|OP2_SWAP)
00340   };
00341 
00342   const size_t Idx_max_type_size = 256; // must be greater or equal than
00343   // any type size (i.e. sizeof(eyedbsm::Oid), sizeof(eyedblib::float64) ...) +
00344   // [+ sizeof(index overhead)]
00345 
00346 };
00347 
00348 #endif

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