lux/storage/curia_hash.h

00001 #ifndef LUX_STORAGE_CURIAHASH_H
00002 #define LUX_STORAGE_CURIAHASH_H
00003 
00004 #include "lux/lux.h"
00005 #include "storage_engine.h"
00006 #include "data_unit.h"
00007 #include <curia.h>
00008 #include <cassert>
00009 #include <iostream>
00010 
00011 namespace Lux {
00012 
00013     class CuriaHash {
00014         
00015     public:
00016         CuriaHash()
00017         {}
00018 
00019         CuriaHash(const char *filename, db_flags_t open_flags)
00020         {
00021             this->open(filename, open_flags);
00022         }
00023 
00024         CuriaHash(std::string filename, db_flags_t open_flags)
00025         {
00026             this->open(filename.c_str(), open_flags);
00027         } 
00028 
00029         bool open(const char *filename, db_flags_t open_flags)
00030         {
00031             assert(filename);
00032             int oflags = 0;
00033             if (open_flags & DB_RDONLY) {
00034                 oflags = CR_OREADER;
00035             }
00036             if (open_flags & DB_RDWR) {
00037                 oflags = CR_OWRITER;
00038             }
00039             if (open_flags & DB_CREAT) {
00040                 oflags = CR_OWRITER | CR_OCREAT;
00041                 mkdir(filename);
00042             }
00043             if (open_flags & DB_TRUNC) {
00044                 oflags |= CR_OTRUNC;
00045             }
00046             if (!(curia_ = cropen(filename, oflags, -1, -1))) {
00047                 return false;
00048             }
00049             return true;
00050         }
00051 
00052         bool close()
00053         { 
00054             if (!crclose(curia_)) {
00055                 return false;
00056             }
00057             curia_ = NULL;
00058             return true;
00059         }
00060 
00061         bool put(LuxDataUnit &key, LuxDataUnit &val)
00062         {
00063             return __put(key, val, CR_DOVER);
00064         }
00065 
00066         bool put(LuxDataUnit *key, LuxDataUnit *val)
00067         {
00068             return put(*key, *val);
00069         }
00070 
00071         bool append(LuxDataUnit &key, LuxDataUnit &val)
00072         {
00073             return __put(key, val, CR_DCAT);
00074         }
00075 
00076         bool append(LuxDataUnit *key, LuxDataUnit *val)
00077         {
00078             return append(*key, *val);
00079         }
00080 
00081         bool get(LuxDataUnit &key, LuxDataUnit &val)
00082         {
00083             assert(key.get_data());
00084             int val_size;
00085             void *val_data;
00086 
00087             val_data = crget(curia_, static_cast<const char *>(key.get_data()),
00088                              key.get_size(), 0, -1, &(val_size));
00089 
00090             if (val_data == NULL) {
00091                 return false;
00092             }
00093 
00094             val.set_cleanup_needed(true);
00095             val.set_data(val_data);
00096             val.set_size(static_cast<unsigned int>(val_size));
00097 
00098             return true;
00099         }
00100 
00101         bool get(LuxDataUnit *key, LuxDataUnit *val)
00102         {
00103             return get(*key, *val);
00104         }
00105 
00106         bool del(LuxDataUnit &key)
00107         {
00108             assert(key.get_data());
00109             if (crout(curia_, static_cast<const char *>(key.get_data()),
00110                 key.get_size())) {
00111                 return true;
00112             }
00113             return false;
00114         }
00115         bool del(LuxDataUnit *key)
00116         {
00117             return del(*key);
00118         }
00119 
00120     protected:
00121         ~CuriaHash()
00122         {
00123             if (curia_ != NULL) {
00124                 if (crclose(curia_)) {
00125                     curia_ = NULL;
00126                 }
00127             }
00128         }
00129 
00130     private:
00131         CURIA *curia_;
00132 
00133         bool __put(LuxDataUnit &key, LuxDataUnit &val, int mode)
00134         {
00135             assert(key.get_data() && val.get_data());
00136             if (crput(curia_,
00137                       static_cast<const char *>(key.get_data()),
00138                       key.get_size(),
00139                       static_cast<const char *>(val.get_data()),
00140                       val.get_size(),
00141                       mode)
00142                 ) {
00143                 return true;
00144             }
00145             return false;
00146         }
00147     };
00148 }
00149 
00150 #endif

Generated on Fri Feb 5 15:50:30 2010 for Lux by  doxygen 1.4.7